-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_spec.cr
186 lines (147 loc) · 4.51 KB
/
command_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require "./spec_helper"
private class TestArgsCommand < Cling::Command
def setup : Nil
@name = "main"
add_argument "first", required: true
add_argument "second", multiple: true
add_option 's', "skip"
end
def pre_run(arguments : Cling::Arguments, options : Cling::Options) : Nil
exit_program 0 if options.has? "skip"
end
def run(arguments : Cling::Arguments, options : Cling::Options) : Nil
arguments.get "first"
arguments.get "second"
end
end
private class TestOptionsCommand < Cling::Command
def setup : Nil
@name = "main"
add_option "foo"
add_option "double-foo", required: true
add_option 'b', "bar", type: :single, required: true
add_option 'n', "num", type: :multiple, default: %w[]
end
def run(arguments : Cling::Arguments, options : Cling::Options) : Nil
options.get? "foo"
options.get "double-foo"
options.get('b').as_s
options.get('n').as_a
end
end
private class TestHooksCommand < Cling::Command
def setup : Nil
@name = "main"
add_argument "foo", required: true
add_option "double-foo", required: true
add_option 'b', "bar", type: :single
add_option 'n', "num", type: :multiple, default: %w[]
end
def run(arguments : Cling::Arguments, options : Cling::Options) : Nil
end
def on_missing_arguments(arguments : Array(String))
stderr.puts arguments.join ", "
end
def on_unknown_arguments(arguments : Array(String))
stderr.puts arguments.join ", "
end
def on_invalid_option(message : String)
stderr.puts message
end
def on_missing_options(options : Array(String))
stderr.puts options.join ", "
end
def on_unknown_options(options : Array(String))
stderr.puts options.join ", "
end
end
private class TestErrorsCommand < Cling::Command
def setup : Nil
@name = "main"
add_option "fail-fast"
end
def run(arguments : Cling::Arguments, options : Cling::Options) : Nil
if options.has? "fail-fast"
exit_program
else
raise "failed slowly"
end
end
end
arguments_command = TestArgsCommand.new
options_command = TestOptionsCommand.new
hooks_command = TestHooksCommand.new
errors_command = TestErrorsCommand.new
describe Cling::Command do
it "executes the pre_run only" do
arguments_command.execute("--skip").should eq 0
end
it "fails on missing arguments" do
expect_raises Cling::CommandError do
arguments_command.execute ""
end
end
it "executes without errors" do
arguments_command.execute("foo bar").should eq 0
arguments_command.execute("foo bar baz qux").should eq 0
end
it "fails on unknown values" do
expect_raises Cling::ValueNotFound do
arguments_command.execute "foo"
end
end
it "fails on missing options" do
expect_raises Cling::CommandError do
options_command.execute ""
end
end
it "executes without errors" do
options_command.execute("--double-foo --bar=true").should eq 0
end
it "fails on unknown options" do
expect_raises Cling::CommandError do
options_command.execute "--double-foo --double-bar"
end
end
it "fails on invalid options" do
expect_raises Cling::CommandError do
options_command.execute "--foo=true --double-foo"
end
expect_raises Cling::CommandError do
options_command.execute "--double-foo=true --bar baz"
end
end
it "catches missing required arguments" do
io = IO::Memory.new
hooks_command.stderr = io
hooks_command.execute("--double-foo").should eq 0
io.to_s.should eq "foo\n"
end
it "catches unknown arguments" do
io = IO::Memory.new
hooks_command.stderr = io
hooks_command.execute("foo --double-foo bar baz").should eq 0
io.to_s.should eq "bar, baz\n"
end
it "catches an invalid option" do
io = IO::Memory.new
hooks_command.stderr = io
hooks_command.execute("foo --double-foo=true\n").should eq 1
io.to_s.should eq "Option 'double-foo' takes no arguments\n"
end
it "catches missing required values for options" do
io = IO::Memory.new
hooks_command.stderr = io
hooks_command.execute("foo --double-foo --bar").should eq 1
io.to_s.should eq "Missing required argument for option 'bar'\n"
io.rewind
hooks_command.execute("foo --double-foo -n").should eq 1
io.to_s.should eq "Missing required arguments for option 'num'\n"
end
it "catches exceptions for program exit and other errors" do
errors_command.execute("--fail-fast").should eq 1
expect_raises(Exception, "failed slowly") do
errors_command.execute ""
end
end
end