Skip to content

Commit

Permalink
Add shards info command
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Feb 13, 2020
1 parent 7f60825 commit eba93e4
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions man/shards.1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ dependencies are satisfied,
dependencies aren't satisfied.
.RE
.PP
\fBinfo [<command>]\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.
Expand Down
5 changes: 4 additions & 1 deletion src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Shards
Commands:
build [<targets>] [<options>] - Build the specified <targets> in `bin` path.
check - Verify all dependencies are installed.
info [<options>...] - 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.
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
56 changes: 56 additions & 0 deletions src/commands/info.cr
Original file line number Diff line number Diff line change
@@ -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 [<command>]
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
32 changes: 0 additions & 32 deletions src/commands/version.cr

This file was deleted.

24 changes: 24 additions & 0 deletions test/commands/info_test.cr
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions test/integration/info_test.cr
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions test/support/cli.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "./factories"

module Shards
module CliHelper
def before_setup
Expand Down
2 changes: 1 addition & 1 deletion test/support/factories.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class FailedCommand < Exception
getter stderr : String

def initialize(message, @stdout, @stderr)
super message
super "#{message}: #{stderr}"
end
end

Expand Down

0 comments on commit eba93e4

Please sign in to comment.