From eba93e4598d789e3fb40207375c1a2de8655d085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Thu, 13 Feb 2020 22:37:54 +0100 Subject: [PATCH] Add shards info command --- Makefile | 2 +- man/shards.1 | 22 ++++++++++++++ src/cli.cr | 5 +++- src/commands/info.cr | 56 +++++++++++++++++++++++++++++++++++ src/commands/version.cr | 32 -------------------- test/commands/info_test.cr | 24 +++++++++++++++ test/integration/info_test.cr | 34 +++++++++++++++++++++ test/support/cli.cr | 2 ++ test/support/factories.cr | 2 +- 9 files changed, 144 insertions(+), 35 deletions(-) create mode 100644 src/commands/info.cr delete mode 100644 src/commands/version.cr create mode 100644 test/commands/info_test.cr create mode 100644 test/integration/info_test.cr diff --git a/Makefile b/Makefile index 9c033dcd..ce0a02a3 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ uninstall: phony test: test_unit test_integration test_unit: phony - $(CRYSTAL) run test/*_test.cr + $(CRYSTAL) run test/*_test.cr test/commands/*_test.cr test_integration: bin/shards phony $(CRYSTAL) run test/integration/*_test.cr diff --git a/man/shards.1 b/man/shards.1 index 68ae844f..b6181e2c 100644 --- a/man/shards.1 +++ b/man/shards.1 @@ -39,6 +39,28 @@ dependencies are satisfied, dependencies aren't satisfied. .RE .PP +\fBinfo []\fR +.RS 4 +Displays information about a shard. +.SS +.RS 4 +Commands: +.PP +.TP 3 +\fB--name\fR +Print the name of the shard. +.TP 3 +\fB--version\fR +Print the version in `spec.yml`. +.TP 3 +\fB-h, --help\fR +Print usage synopsis. +.RE +.PP +.RS 4 +If no command is given, a summary including name and version is printed. +.RE +.PP \fBinit\fR .RS 4 Initializes a default \fIshard.yml\fR in the current folder. diff --git a/src/cli.cr b/src/cli.cr index 41aab407..ed2563e2 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -9,6 +9,7 @@ module Shards Commands: build [] [] - Build the specified in `bin` path. check - Verify all dependencies are installed. + info [...] - Show information about a shard. Pass `--help` for details. init - Initialize a `shard.yml` file. install - Install dependencies, creating or using the `shard.lock` file. list [--tree] - List installed dependencies. @@ -41,6 +42,8 @@ module Shards build(path, args) when "check" Commands::Check.run(path) + when "info" + Commands::Info.run(path, args) when "init" Commands::Init.run(path) when "install" @@ -64,7 +67,7 @@ module Shards args.reject(&.starts_with?("--")) ) when "version" - Commands::Version.run(args[1]? || path) + Commands::Info.run(args.shift? || path, ["--version"]) when "--version" puts self.version_string when "-h", "--help" diff --git a/src/commands/info.cr b/src/commands/info.cr new file mode 100644 index 00000000..2f4bf290 --- /dev/null +++ b/src/commands/info.cr @@ -0,0 +1,56 @@ +require "./command" + +module Shards + module Commands + class Info < Command + def initialize(path) + super lookup_path(path) + end + + def display_help + puts <<-HELP + shards info [] + + Displays information about a shard. + + Commands: + --name - Print the name of the shard. + --version - Print the version in `spec.yml`. + -h, --help - Print usage synopsis. + + If no command is given, a summary including name and version is printed. + HELP + end + + def run(args, *, stdout = STDOUT) + case args.shift? + when "--name" + stdout.puts spec.name + when "--version" + stdout.puts spec.version + when "--help", "-h" + display_help + else + stdout.puts " name: #{spec.name}" + stdout.puts "version: #{spec.version}" + end + end + + # look up for `SPEC_FILENAME` in *path* or up + private def lookup_path(path) + previous = nil + current = File.expand_path(path) + + until !File.directory?(current) || current == previous + shard_file = File.join(current, SPEC_FILENAME) + break if File.exists?(shard_file) + + previous = current + current = File.dirname(current) + end + + current + end + end + end +end diff --git a/src/commands/version.cr b/src/commands/version.cr deleted file mode 100644 index 804580b0..00000000 --- a/src/commands/version.cr +++ /dev/null @@ -1,32 +0,0 @@ -require "./command" - -module Shards - module Commands - class Version < Command - def self.run(path) - path = lookup_path(path) - new(path).run - end - - def run - puts spec.version - end - - # look up for `SPEC_FILENAME` in *path* or up - private def self.lookup_path(path) - previous = nil - current = File.expand_path(path) - - until !File.directory?(current) || current == previous - shard_file = File.join(current, SPEC_FILENAME) - break if File.exists?(shard_file) - - previous = current - current = File.dirname(current) - end - - current - end - end - end -end diff --git a/test/commands/info_test.cr b/test/commands/info_test.cr new file mode 100644 index 00000000..58b96feb --- /dev/null +++ b/test/commands/info_test.cr @@ -0,0 +1,24 @@ +require "../../src/commands/info" +require "../test_helper" +require "../support/cli" + +private def capture(command, *args) + String.build do |io| + command.run(args.to_a, stdout: io) + end.chomp +end + +class Shards::Commands::InfoTest < Minitest::Test + def test_reports_name + with_shard({name: "foo", version: "1.2.3"}) do + info = Shards::Commands::Info.new(application_path) + + assert_equal "foo", capture(info, "--name") + assert_equal "1.2.3", capture(info, "--version") + assert_equal <<-OUT, capture(info, "") + name: foo + version: 1.2.3 + OUT + end + end +end diff --git a/test/integration/info_test.cr b/test/integration/info_test.cr new file mode 100644 index 00000000..66aa667f --- /dev/null +++ b/test/integration/info_test.cr @@ -0,0 +1,34 @@ +require "../integration_helper" + +class InfoCommandTest < Minitest::Test + def test_reports_name + Dir.cd(application_path) do + with_shard({name: "foo"}) do + output = run "shards info --name", capture: true + assert_match "foo", output + end + end + end + + def test_reports_version + Dir.cd(application_path) do + with_shard({version: "1.2.3"}) do + output = run "shards info --version", capture: true + assert_match "1.2.3", output + end + end + end + + def test_reports_info + Dir.cd(application_path) do + with_shard({name: "foo", version: "1.2.3"}) do + output = run "shards info", capture: true + assert_match <<-OUT, output + name: foo + version: 1.2.3 + + OUT + end + end + end +end diff --git a/test/support/cli.cr b/test/support/cli.cr index f63ad6f3..7fe17e5f 100644 --- a/test/support/cli.cr +++ b/test/support/cli.cr @@ -1,3 +1,5 @@ +require "./factories" + module Shards module CliHelper def before_setup diff --git a/test/support/factories.cr b/test/support/factories.cr index a2971191..24e34361 100644 --- a/test/support/factories.cr +++ b/test/support/factories.cr @@ -3,7 +3,7 @@ class FailedCommand < Exception getter stderr : String def initialize(message, @stdout, @stderr) - super message + super "#{message}: #{stderr}" end end