Skip to content

Commit ded8464

Browse files
Add shards info command
1 parent 7f60825 commit ded8464

File tree

9 files changed

+144
-35
lines changed

9 files changed

+144
-35
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ uninstall: phony
3434
test: test_unit test_integration
3535

3636
test_unit: phony
37-
$(CRYSTAL) run test/*_test.cr
37+
$(CRYSTAL) run test/*_test.cr test/commands/*_test.cr
3838

3939
test_integration: bin/shards phony
4040
$(CRYSTAL) run test/integration/*_test.cr

man/shards.1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ dependencies are satisfied,
3939
dependencies aren't satisfied.
4040
.RE
4141
.PP
42+
\fBinfo [<command>]\fR
43+
.RS 4
44+
Displays information about a shard.
45+
.SS
46+
.RS 4
47+
Commands:
48+
.PP
49+
.TP 3
50+
\fB--name\fR
51+
Print the name of the shard.
52+
.TP 3
53+
\fB--version\fR
54+
Print the version in `spec.yml`.
55+
.TP 3
56+
\fB-h, --help\fR
57+
Print usage synopsis.
58+
.RE
59+
.PP
60+
.RS 4
61+
If no command is given, a summary including name and version is printed.
62+
.RE
63+
.PP
4264
\fBinit\fR
4365
.RS 4
4466
Initializes a default \fIshard.yml\fR in the current folder.

src/cli.cr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Shards
99
Commands:
1010
build [<targets>] [<options>] - Build the specified <targets> in `bin` path.
1111
check - Verify all dependencies are installed.
12+
info [--name | --version] - Show information about a shard.
1213
init - Initialize a `shard.yml` file.
1314
install - Install dependencies, creating or using the `shard.lock` file.
1415
list [--tree] - List installed dependencies.
@@ -41,6 +42,8 @@ module Shards
4142
build(path, args)
4243
when "check"
4344
Commands::Check.run(path)
45+
when "info"
46+
Commands::Info.run(path, args)
4447
when "init"
4548
Commands::Init.run(path)
4649
when "install"
@@ -64,7 +67,7 @@ module Shards
6467
args.reject(&.starts_with?("--"))
6568
)
6669
when "version"
67-
Commands::Version.run(args[1]? || path)
70+
Commands::Info.run(args.shift? || path, ["--version"])
6871
when "--version"
6972
puts self.version_string
7073
when "-h", "--help"

src/commands/info.cr

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require "./command"
2+
3+
module Shards
4+
module Commands
5+
class Info < Command
6+
def initialize(path)
7+
super lookup_path(path)
8+
end
9+
10+
def display_help
11+
puts <<-HELP
12+
shards info [<command>]
13+
14+
Displays information about a shard.
15+
16+
Commands:
17+
--name - Print the name of the shard.
18+
--version - Print the version in `spec.yml`.
19+
-h, --help - Print usage synopsis.
20+
21+
If no command is given, a summary including name and version is printed.
22+
HELP
23+
end
24+
25+
def run(args, *, stdout = STDOUT)
26+
case args.shift?
27+
when "--name"
28+
stdout.puts spec.name
29+
when "--version"
30+
stdout.puts spec.version
31+
when "--help", "-h"
32+
display_help
33+
else
34+
stdout.puts " name: #{spec.name}"
35+
stdout.puts "version: #{spec.version}"
36+
end
37+
end
38+
39+
# look up for `SPEC_FILENAME` in *path* or up
40+
private def lookup_path(path)
41+
previous = nil
42+
current = File.expand_path(path)
43+
44+
until !File.directory?(current) || current == previous
45+
shard_file = File.join(current, SPEC_FILENAME)
46+
break if File.exists?(shard_file)
47+
48+
previous = current
49+
current = File.dirname(current)
50+
end
51+
52+
current
53+
end
54+
end
55+
end
56+
end

src/commands/version.cr

Lines changed: 0 additions & 32 deletions
This file was deleted.

test/commands/info_test.cr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "../../src/commands/info"
2+
require "../test_helper"
3+
require "../support/cli"
4+
5+
private def capture(command, *args)
6+
String.build do |io|
7+
command.run(args.to_a, stdout: io)
8+
end.chomp
9+
end
10+
11+
class Shards::Commands::InfoTest < Minitest::Test
12+
def test_reports_name
13+
with_shard({name: "foo", version: "1.2.3"}) do
14+
info = Shards::Commands::Info.new(application_path)
15+
16+
assert_equal "foo", capture(info, "--name")
17+
assert_equal "1.2.3", capture(info, "--version")
18+
assert_equal <<-OUT, capture(info, "")
19+
name: foo
20+
version: 1.2.3
21+
OUT
22+
end
23+
end
24+
end

test/integration/info_test.cr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "../integration_helper"
2+
3+
class InfoCommandTest < Minitest::Test
4+
def test_reports_name
5+
Dir.cd(application_path) do
6+
with_shard({name: "foo"}) do
7+
output = run "shards info --name", capture: true
8+
assert_match "foo", output
9+
end
10+
end
11+
end
12+
13+
def test_reports_version
14+
Dir.cd(application_path) do
15+
with_shard({version: "1.2.3"}) do
16+
output = run "shards info --version", capture: true
17+
assert_match "1.2.3", output
18+
end
19+
end
20+
end
21+
22+
def test_reports_info
23+
Dir.cd(application_path) do
24+
with_shard({name: "foo", version: "1.2.3"}) do
25+
output = run "shards info", capture: true
26+
assert_match <<-OUT, output
27+
name: foo
28+
version: 1.2.3
29+
30+
OUT
31+
end
32+
end
33+
end
34+
end

test/support/cli.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "./factories"
2+
13
module Shards
24
module CliHelper
35
def before_setup

test/support/factories.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class FailedCommand < Exception
33
getter stderr : String
44

55
def initialize(message, @stdout, @stderr)
6-
super message
6+
super "#{message}: #{stderr}"
77
end
88
end
99

0 commit comments

Comments
 (0)