thor 0.18.1 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +13 -7
- data/Thorfile +4 -5
- data/bin/thor +1 -1
- data/lib/thor.rb +78 -67
- data/lib/thor/actions.rb +57 -56
- data/lib/thor/actions/create_file.rb +33 -35
- data/lib/thor/actions/create_link.rb +2 -3
- data/lib/thor/actions/directory.rb +37 -38
- data/lib/thor/actions/empty_directory.rb +67 -69
- data/lib/thor/actions/file_manipulation.rb +17 -15
- data/lib/thor/actions/inject_into_file.rb +27 -29
- data/lib/thor/base.rb +193 -189
- data/lib/thor/command.rb +20 -23
- data/lib/thor/core_ext/hash_with_indifferent_access.rb +21 -24
- data/lib/thor/core_ext/io_binary_read.rb +2 -4
- data/lib/thor/core_ext/ordered_hash.rb +9 -11
- data/lib/thor/error.rb +5 -1
- data/lib/thor/group.rb +53 -54
- data/lib/thor/invocation.rb +44 -38
- data/lib/thor/line_editor.rb +17 -0
- data/lib/thor/line_editor/basic.rb +35 -0
- data/lib/thor/line_editor/readline.rb +88 -0
- data/lib/thor/parser.rb +4 -4
- data/lib/thor/parser/argument.rb +28 -29
- data/lib/thor/parser/arguments.rb +102 -98
- data/lib/thor/parser/option.rb +26 -22
- data/lib/thor/parser/options.rb +86 -86
- data/lib/thor/rake_compat.rb +9 -10
- data/lib/thor/runner.rb +141 -141
- data/lib/thor/shell.rb +27 -34
- data/lib/thor/shell/basic.rb +91 -63
- data/lib/thor/shell/color.rb +44 -43
- data/lib/thor/shell/html.rb +59 -60
- data/lib/thor/util.rb +24 -27
- data/lib/thor/version.rb +1 -1
- data/spec/actions/create_file_spec.rb +25 -27
- data/spec/actions/create_link_spec.rb +19 -18
- data/spec/actions/directory_spec.rb +31 -31
- data/spec/actions/empty_directory_spec.rb +18 -18
- data/spec/actions/file_manipulation_spec.rb +38 -28
- data/spec/actions/inject_into_file_spec.rb +13 -13
- data/spec/actions_spec.rb +43 -43
- data/spec/base_spec.rb +45 -38
- data/spec/command_spec.rb +13 -14
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +19 -19
- data/spec/core_ext/ordered_hash_spec.rb +6 -6
- data/spec/exit_condition_spec.rb +4 -4
- data/spec/fixtures/invoke.thor +19 -0
- data/spec/fixtures/script.thor +1 -1
- data/spec/group_spec.rb +30 -24
- data/spec/helper.rb +28 -15
- data/spec/invocation_spec.rb +39 -19
- data/spec/line_editor/basic_spec.rb +28 -0
- data/spec/line_editor/readline_spec.rb +69 -0
- data/spec/line_editor_spec.rb +43 -0
- data/spec/parser/argument_spec.rb +12 -12
- data/spec/parser/arguments_spec.rb +11 -11
- data/spec/parser/option_spec.rb +33 -25
- data/spec/parser/options_spec.rb +66 -52
- data/spec/quality_spec.rb +75 -0
- data/spec/rake_compat_spec.rb +10 -10
- data/spec/register_spec.rb +60 -30
- data/spec/runner_spec.rb +67 -62
- data/spec/sandbox/application.rb +2 -0
- data/spec/sandbox/app{1}/README +3 -0
- data/spec/sandbox/bundle/execute.rb +6 -0
- data/spec/sandbox/bundle/main.thor +1 -0
- data/spec/sandbox/command.thor +10 -0
- data/spec/sandbox/doc/%file_name%.rb.tt +1 -0
- data/spec/sandbox/doc/COMMENTER +11 -0
- data/spec/sandbox/doc/README +3 -0
- data/spec/sandbox/doc/block_helper.rb +3 -0
- data/spec/sandbox/doc/config.rb +1 -0
- data/spec/sandbox/doc/config.yaml.tt +1 -0
- data/spec/sandbox/doc/excluding/%file_name%.rb.tt +1 -0
- data/spec/sandbox/enum.thor +10 -0
- data/spec/sandbox/group.thor +128 -0
- data/spec/sandbox/invoke.thor +131 -0
- data/spec/sandbox/path with spaces b/data/spec/sandbox/path with → spaces +0 -0
- data/spec/sandbox/preserve/script.sh +3 -0
- data/spec/sandbox/script.thor +220 -0
- data/spec/sandbox/subcommand.thor +17 -0
- data/spec/shell/basic_spec.rb +107 -86
- data/spec/shell/color_spec.rb +32 -8
- data/spec/shell/html_spec.rb +3 -4
- data/spec/shell_spec.rb +7 -7
- data/spec/subcommand_spec.rb +20 -2
- data/spec/thor_spec.rb +111 -97
- data/spec/util_spec.rb +30 -30
- data/thor.gemspec +14 -14
- metadata +69 -25
data/spec/command_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require "helper"
|
2
2
|
|
3
3
|
describe Thor::Command do
|
4
|
-
def command(options={})
|
4
|
+
def command(options = {})
|
5
5
|
options.each do |key, value|
|
6
6
|
options[key] = Thor::Option.parse(key, value)
|
7
7
|
end
|
@@ -34,16 +34,16 @@ describe Thor::Command do
|
|
34
34
|
|
35
35
|
describe "#dynamic" do
|
36
36
|
it "creates a dynamic command with the given name" do
|
37
|
-
expect(Thor::DynamicCommand.new(
|
38
|
-
expect(Thor::DynamicCommand.new(
|
39
|
-
expect(Thor::DynamicCommand.new(
|
40
|
-
expect(Thor::DynamicCommand.new(
|
37
|
+
expect(Thor::DynamicCommand.new("command").name).to eq("command")
|
38
|
+
expect(Thor::DynamicCommand.new("command").description).to eq("A dynamically-generated command")
|
39
|
+
expect(Thor::DynamicCommand.new("command").usage).to eq("command")
|
40
|
+
expect(Thor::DynamicCommand.new("command").options).to eq({})
|
41
41
|
end
|
42
42
|
|
43
43
|
it "does not invoke an existing method" do
|
44
|
-
|
45
|
-
|
46
|
-
Thor::DynamicCommand.new(
|
44
|
+
dub = double
|
45
|
+
expect(dub.class).to receive(:handle_no_command_error).with("to_s")
|
46
|
+
Thor::DynamicCommand.new("to_s").run(dub)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -57,9 +57,9 @@ describe Thor::Command do
|
|
57
57
|
|
58
58
|
describe "#run" do
|
59
59
|
it "runs a command by calling a method in the given instance" do
|
60
|
-
|
61
|
-
|
62
|
-
expect(command.run(
|
60
|
+
dub = double
|
61
|
+
expect(dub).to receive(:can_has).and_return { |*args| args }
|
62
|
+
expect(command.run(dub, [1, 2, 3])).to eq([1, 2, 3])
|
63
63
|
end
|
64
64
|
|
65
65
|
it "raises an error if the method to be invoked is private" do
|
@@ -67,11 +67,10 @@ describe Thor::Command do
|
|
67
67
|
def self.handle_no_command_error(name)
|
68
68
|
name
|
69
69
|
end
|
70
|
-
|
71
|
-
private
|
72
70
|
def can_has
|
73
71
|
"fail"
|
74
72
|
end
|
73
|
+
private :can_has
|
75
74
|
end
|
76
75
|
|
77
76
|
expect(command.run(klass.new)).to eq("can_has")
|
@@ -1,48 +1,48 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "helper"
|
2
|
+
require "thor/core_ext/hash_with_indifferent_access"
|
3
3
|
|
4
4
|
describe Thor::CoreExt::HashWithIndifferentAccess do
|
5
5
|
before do
|
6
|
-
@hash = Thor::CoreExt::HashWithIndifferentAccess.new :foo =>
|
6
|
+
@hash = Thor::CoreExt::HashWithIndifferentAccess.new :foo => "bar", "baz" => "bee", :force => true
|
7
7
|
end
|
8
8
|
|
9
9
|
it "has values accessible by either strings or symbols" do
|
10
|
-
expect(@hash[
|
11
|
-
expect(@hash[:foo]).to eq(
|
10
|
+
expect(@hash["foo"]).to eq("bar")
|
11
|
+
expect(@hash[:foo]).to eq("bar")
|
12
12
|
|
13
|
-
expect(@hash.values_at(:foo, :baz)).to eq([
|
14
|
-
expect(@hash.delete(:foo)).to eq(
|
13
|
+
expect(@hash.values_at(:foo, :baz)).to eq(%w[bar bee])
|
14
|
+
expect(@hash.delete(:foo)).to eq("bar")
|
15
15
|
end
|
16
16
|
|
17
17
|
it "handles magic boolean predicates" do
|
18
|
-
expect(@hash.force?).to
|
19
|
-
expect(@hash.foo?).to
|
20
|
-
expect(@hash.nothing?).to
|
18
|
+
expect(@hash.force?).to be true
|
19
|
+
expect(@hash.foo?).to be true
|
20
|
+
expect(@hash.nothing?).to be false
|
21
21
|
end
|
22
22
|
|
23
|
-
it "handles magic
|
24
|
-
expect(@hash.foo?(
|
25
|
-
expect(@hash.foo?(
|
23
|
+
it "handles magic comparisons" do
|
24
|
+
expect(@hash.foo?("bar")).to be true
|
25
|
+
expect(@hash.foo?("bee")).to be false
|
26
26
|
end
|
27
27
|
|
28
28
|
it "maps methods to keys" do
|
29
|
-
expect(@hash.foo).to eq(@hash[
|
29
|
+
expect(@hash.foo).to eq(@hash["foo"])
|
30
30
|
end
|
31
31
|
|
32
32
|
it "merges keys independent if they are symbols or strings" do
|
33
|
-
@hash.merge!(
|
33
|
+
@hash.merge!("force" => false, :baz => "boom")
|
34
34
|
expect(@hash[:force]).to eq(false)
|
35
|
-
expect(@hash[
|
35
|
+
expect(@hash["baz"]).to eq("boom")
|
36
36
|
end
|
37
37
|
|
38
38
|
it "creates a new hash by merging keys independent if they are symbols or strings" do
|
39
|
-
other = @hash.merge(
|
39
|
+
other = @hash.merge("force" => false, :baz => "boom")
|
40
40
|
expect(other[:force]).to eq(false)
|
41
|
-
expect(other[
|
41
|
+
expect(other["baz"]).to eq("boom")
|
42
42
|
end
|
43
43
|
|
44
44
|
it "converts to a traditional hash" do
|
45
45
|
expect(@hash.to_hash.class).to eq(Hash)
|
46
|
-
expect(@hash).to eq(
|
46
|
+
expect(@hash).to eq("foo" => "bar", "baz" => "bee", "force" => true)
|
47
47
|
end
|
48
48
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "helper"
|
2
|
+
require "thor/core_ext/ordered_hash"
|
3
3
|
|
4
4
|
describe Thor::CoreExt::OrderedHash do
|
5
5
|
before do
|
@@ -8,7 +8,7 @@ describe Thor::CoreExt::OrderedHash do
|
|
8
8
|
|
9
9
|
describe "without any items" do
|
10
10
|
it "returns nil for an undefined key" do
|
11
|
-
expect(@hash["foo"]).to
|
11
|
+
expect(@hash["foo"]).to be nil
|
12
12
|
end
|
13
13
|
|
14
14
|
it "doesn't iterate through any items" do
|
@@ -35,7 +35,7 @@ describe Thor::CoreExt::OrderedHash do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
it "returns nil for an undefined key" do
|
38
|
-
expect(@hash[:boom]).to
|
38
|
+
expect(@hash[:boom]).to be nil
|
39
39
|
end
|
40
40
|
|
41
41
|
it "returns the value for each key" do
|
@@ -52,7 +52,7 @@ describe Thor::CoreExt::OrderedHash do
|
|
52
52
|
arr << [key, value]
|
53
53
|
end
|
54
54
|
expect(arr).to eq([[:foo, "Foo!"], [:bar, "Bar!"], [:baz, "Baz!"],
|
55
|
-
|
55
|
+
[:bop, "Bop!"], [:bat, "Bat!"]])
|
56
56
|
end
|
57
57
|
|
58
58
|
it "returns the keys in order of insertion" do
|
@@ -109,7 +109,7 @@ describe Thor::CoreExt::OrderedHash do
|
|
109
109
|
end
|
110
110
|
|
111
111
|
it "returns nil if the value to be deleted can't be found" do
|
112
|
-
expect(@hash.delete(:nothing)).to
|
112
|
+
expect(@hash.delete(:nothing)).to be nil
|
113
113
|
end
|
114
114
|
end
|
115
115
|
end
|
data/spec/exit_condition_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "helper"
|
2
|
+
require "thor/base"
|
3
3
|
|
4
4
|
describe "Exit conditions" do
|
5
5
|
it "exits 0, not bubble up EPIPE, if EPIPE is raised" do
|
@@ -9,11 +9,11 @@ describe "Exit conditions" do
|
|
9
9
|
desc "my_action", "testing EPIPE"
|
10
10
|
define_method :my_action do
|
11
11
|
epiped = true
|
12
|
-
|
12
|
+
fail Errno::EPIPE
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
expect{ command.start(["my_action"]) }.to raise_error(SystemExit)
|
16
|
+
expect { command.start(["my_action"]) }.to raise_error(SystemExit)
|
17
17
|
expect(epiped).to eq(true)
|
18
18
|
end
|
19
19
|
end
|
data/spec/fixtures/invoke.thor
CHANGED
@@ -53,6 +53,12 @@ class B < Thor
|
|
53
53
|
def three
|
54
54
|
self
|
55
55
|
end
|
56
|
+
|
57
|
+
desc "four", "invoke four"
|
58
|
+
option :defaulted_value, :type => :string, :default => 'default'
|
59
|
+
def four
|
60
|
+
options.defaulted_value
|
61
|
+
end
|
56
62
|
end
|
57
63
|
|
58
64
|
class C < Thor::Group
|
@@ -110,3 +116,16 @@ class H < Thor::Group
|
|
110
116
|
class_option :defined, :type => :boolean, :default => true
|
111
117
|
invoke_from_option :defined
|
112
118
|
end
|
119
|
+
|
120
|
+
class I < Thor
|
121
|
+
desc "two", "Two"
|
122
|
+
def two
|
123
|
+
current_command_chain
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
class J < Thor
|
128
|
+
desc "i", "I"
|
129
|
+
subcommand :one, I
|
130
|
+
end
|
131
|
+
|
data/spec/fixtures/script.thor
CHANGED
@@ -88,7 +88,7 @@ END
|
|
88
88
|
method_option :lazy_hash, :type => :hash, :lazy_default => {'swedish' => 'meatballs'}
|
89
89
|
desc "with_optional NAME", "invoke with optional name"
|
90
90
|
def with_optional(name=nil, *args)
|
91
|
-
[
|
91
|
+
[name, options, args]
|
92
92
|
end
|
93
93
|
|
94
94
|
class AnotherScript < Thor
|
data/spec/group_spec.rb
CHANGED
@@ -1,43 +1,43 @@
|
|
1
|
-
require
|
1
|
+
require "helper"
|
2
2
|
|
3
3
|
describe Thor::Group do
|
4
4
|
describe "command" do
|
5
5
|
it "allows to use private methods from parent class as commands" do
|
6
|
-
expect(ChildGroup.start).to eq([
|
6
|
+
expect(ChildGroup.start).to eq(%w[bar foo baz])
|
7
7
|
expect(ChildGroup.new.baz("bar")).to eq("bar")
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "#start" do
|
12
12
|
it "invokes all the commands under the Thor group" do
|
13
|
-
expect(MyCounter.start([
|
13
|
+
expect(MyCounter.start(%w[1 2 --third 3])).to eq([1, 2, 3, nil, nil, nil])
|
14
14
|
end
|
15
15
|
|
16
|
-
it "uses argument default value" do
|
17
|
-
expect(MyCounter.start([
|
16
|
+
it "uses argument's default value" do
|
17
|
+
expect(MyCounter.start(%w[1 --third 3])).to eq([1, 2, 3, nil, nil, nil])
|
18
18
|
end
|
19
19
|
|
20
20
|
it "invokes all the commands in the Thor group and its parents" do
|
21
|
-
expect(BrokenCounter.start([
|
21
|
+
expect(BrokenCounter.start(%w[1 2 --third 3])).to eq([nil, 2, 3, false, 5, nil])
|
22
22
|
end
|
23
23
|
|
24
24
|
it "raises an error if a required argument is added after a non-required" do
|
25
|
-
expect
|
25
|
+
expect do
|
26
26
|
MyCounter.argument(:foo, :type => :string)
|
27
|
-
|
27
|
+
end.to raise_error(ArgumentError, 'You cannot have "foo" as required argument after the non-required argument "second".')
|
28
28
|
end
|
29
29
|
|
30
30
|
it "raises when an exception happens within the command call" do
|
31
|
-
expect{ BrokenCounter.start([
|
31
|
+
expect { BrokenCounter.start(%w[1 2 --fail]) }.to raise_error
|
32
32
|
end
|
33
33
|
|
34
34
|
it "raises an error when a Thor group command expects arguments" do
|
35
|
-
expect{ WhinyGenerator.start }.to raise_error(ArgumentError, /thor wrong_arity takes 1 argument, but it should not/)
|
35
|
+
expect { WhinyGenerator.start }.to raise_error(ArgumentError, /thor wrong_arity takes 1 argument, but it should not/)
|
36
36
|
end
|
37
37
|
|
38
|
-
it "invokes help message if any of the shortcuts
|
39
|
-
MyCounter.
|
40
|
-
MyCounter.start([
|
38
|
+
it "invokes help message if any of the shortcuts are given" do
|
39
|
+
expect(MyCounter).to receive(:help)
|
40
|
+
MyCounter.start(%w[-h])
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -51,7 +51,7 @@ describe Thor::Group do
|
|
51
51
|
end
|
52
52
|
|
53
53
|
it "can be nil" do
|
54
|
-
expect(WhinyGenerator.desc).to
|
54
|
+
expect(WhinyGenerator.desc).to be nil
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -117,17 +117,23 @@ describe Thor::Group do
|
|
117
117
|
end
|
118
118
|
|
119
119
|
it "does not invoke if the option is nil" do
|
120
|
-
expect(capture(:stdout) { G.start([
|
120
|
+
expect(capture(:stdout) { G.start(%w[--skip-invoked]) }).not_to match(/invoke/)
|
121
121
|
end
|
122
122
|
|
123
123
|
it "prints a message if invocation cannot be found" do
|
124
|
-
content = capture(:stdout) { G.start([
|
124
|
+
content = capture(:stdout) { G.start(%w[--invoked unknown]) }
|
125
125
|
expect(content).to match(/error unknown \[not found\]/)
|
126
126
|
end
|
127
127
|
|
128
128
|
it "allows to invoke a class from the class binding by the given option" do
|
129
|
-
|
129
|
+
error = nil
|
130
|
+
content = capture(:stdout) do
|
131
|
+
error = capture(:stderr) do
|
132
|
+
G.start(%w[--invoked e])
|
133
|
+
end
|
134
|
+
end
|
130
135
|
expect(content).to match(/invoke e/)
|
136
|
+
expect(error).to match(/ERROR: "thor two" was called with arguments/)
|
131
137
|
end
|
132
138
|
|
133
139
|
it "shows invocation information to the user" do
|
@@ -156,7 +162,7 @@ describe Thor::Group do
|
|
156
162
|
end
|
157
163
|
|
158
164
|
it "does not invoke if the option is false" do
|
159
|
-
expect(capture(:stdout) { H.start([
|
165
|
+
expect(capture(:stdout) { H.start(%w[--no-defined]) }).not_to match(/invoke/)
|
160
166
|
end
|
161
167
|
|
162
168
|
it "shows invocation information to the user" do
|
@@ -189,9 +195,9 @@ describe Thor::Group do
|
|
189
195
|
end
|
190
196
|
end
|
191
197
|
|
192
|
-
expect(klass.start([
|
193
|
-
expect(klass.start([
|
194
|
-
expect(klass.start([
|
198
|
+
expect(klass.start(%w[jose])).to eq(["Hi jose"])
|
199
|
+
expect(klass.start(%w[jose --loud])).to eq(["Hi JOSE"])
|
200
|
+
expect(klass.start(%w[--loud jose])).to eq(["Hi JOSE"])
|
195
201
|
end
|
196
202
|
|
197
203
|
it "provides extra args as `args`" do
|
@@ -208,9 +214,9 @@ describe Thor::Group do
|
|
208
214
|
end
|
209
215
|
end
|
210
216
|
|
211
|
-
expect(klass.start([
|
212
|
-
expect(klass.start([
|
213
|
-
expect(klass.start([
|
217
|
+
expect(klass.start(%w[jose])).to eq(["Hi jose"])
|
218
|
+
expect(klass.start(%w[jose --loud])).to eq(["Hi JOSE"])
|
219
|
+
expect(klass.start(%w[--loud jose])).to eq(["Hi JOSE"])
|
214
220
|
end
|
215
221
|
end
|
216
222
|
end
|
data/spec/helper.rb
CHANGED
@@ -1,23 +1,27 @@
|
|
1
|
-
$TESTING=true
|
1
|
+
$TESTING = true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "simplecov"
|
4
|
+
require "coveralls"
|
5
5
|
|
6
6
|
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
7
7
|
SimpleCov::Formatter::HTMLFormatter,
|
8
8
|
Coveralls::SimpleCov::Formatter
|
9
9
|
]
|
10
|
-
SimpleCov.start
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
SimpleCov.start do
|
12
|
+
add_filter "/spec/"
|
13
|
+
minimum_coverage(92.21)
|
14
|
+
end
|
15
|
+
|
16
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
17
|
+
require "thor"
|
18
|
+
require "thor/group"
|
19
|
+
require "stringio"
|
16
20
|
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
+
require "rdoc"
|
22
|
+
require "rspec"
|
23
|
+
require "diff/lcs" # You need diff/lcs installed to run specs (but not to run Thor).
|
24
|
+
require "fakeweb" # You need fakeweb installed to run specs (but not to run Thor).
|
21
25
|
|
22
26
|
# Set shell to basic
|
23
27
|
$0 = "thor"
|
@@ -56,12 +60,21 @@ RSpec.configure do |config|
|
|
56
60
|
end
|
57
61
|
|
58
62
|
def source_root
|
59
|
-
File.join(File.dirname(__FILE__),
|
63
|
+
File.join(File.dirname(__FILE__), "fixtures")
|
60
64
|
end
|
61
65
|
|
62
66
|
def destination_root
|
63
|
-
File.join(File.dirname(__FILE__),
|
67
|
+
File.join(File.dirname(__FILE__), "sandbox")
|
68
|
+
end
|
69
|
+
|
70
|
+
# This code was adapted from Ruby on Rails, available under MIT-LICENSE
|
71
|
+
# Copyright (c) 2004-2013 David Heinemeier Hansson
|
72
|
+
def silence_warnings
|
73
|
+
old_verbose, $VERBOSE = $VERBOSE, nil
|
74
|
+
yield
|
75
|
+
ensure
|
76
|
+
$VERBOSE = old_verbose
|
64
77
|
end
|
65
78
|
|
66
|
-
alias
|
79
|
+
alias silence capture
|
67
80
|
end
|
data/spec/invocation_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "helper"
|
2
|
+
require "thor/base"
|
3
3
|
|
4
4
|
describe Thor::Invocation do
|
5
5
|
describe "#invoke" do
|
@@ -16,8 +16,8 @@ describe Thor::Invocation do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
it "invokes a command with arguments" do
|
19
|
-
expect(A.new.invoke(:five, [5])).to
|
20
|
-
expect(A.new.invoke(:five, [7])).to
|
19
|
+
expect(A.new.invoke(:five, [5])).to be true
|
20
|
+
expect(A.new.invoke(:five, [7])).to be false
|
21
21
|
end
|
22
22
|
|
23
23
|
it "invokes the default command if none is given to a Thor class" do
|
@@ -34,20 +34,40 @@ describe Thor::Invocation do
|
|
34
34
|
|
35
35
|
it "accepts a class as argument with a command to invoke" do
|
36
36
|
base = A.new([], :last_name => "Valim")
|
37
|
-
expect(base.invoke(B, :one, [
|
37
|
+
expect(base.invoke(B, :one, %w[Jose])).to eq("Valim, Jose")
|
38
38
|
end
|
39
39
|
|
40
40
|
it "allows customized options to be given" do
|
41
41
|
base = A.new([], :last_name => "Wrong")
|
42
|
-
expect(base.invoke(B, :one, [
|
42
|
+
expect(base.invoke(B, :one, %w[Jose], :last_name => "Valim")).to eq("Valim, Jose")
|
43
43
|
end
|
44
44
|
|
45
45
|
it "reparses options in the new class" do
|
46
|
-
expect(A.start([
|
46
|
+
expect(A.start(%w[invoker --last-name Valim])).to eq("Valim, Jose")
|
47
47
|
end
|
48
48
|
|
49
49
|
it "shares initialize options with invoked class" do
|
50
|
-
expect(A.new([], :foo => :bar).invoke("b:two")).to eq(
|
50
|
+
expect(A.new([], :foo => :bar).invoke("b:two")).to eq("foo" => :bar)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "uses default options from invoked class if no matching arguments are given" do
|
54
|
+
expect(A.new([]).invoke("b:four")).to eq("default")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "overrides default options if options are passed to the invoker" do
|
58
|
+
expect(A.new([], :defaulted_value => "not default").invoke("b:four")).to eq("not default")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns the command chain" do
|
62
|
+
expect(I.new.invoke("two")).to eq([:two])
|
63
|
+
|
64
|
+
if RUBY_VERSION < "1.9.3"
|
65
|
+
result = J.start(["one", "two" ])
|
66
|
+
expect(result).to include(:one)
|
67
|
+
expect(result).to include(:two)
|
68
|
+
else
|
69
|
+
expect(J.start(["one", "two" ])).to eq([:one, :two])
|
70
|
+
end
|
51
71
|
end
|
52
72
|
|
53
73
|
it "dump configuration values to be used in the invoked class" do
|
@@ -66,35 +86,35 @@ describe Thor::Invocation do
|
|
66
86
|
|
67
87
|
it "does not invoke a Thor::Group twice" do
|
68
88
|
base = A.new
|
69
|
-
silence(:stdout){ base.invoke(:c) }
|
89
|
+
silence(:stdout) { base.invoke(:c) }
|
70
90
|
expect(capture(:stdout) { base.invoke(:c) }).to be_empty
|
71
91
|
end
|
72
92
|
|
73
93
|
it "does not invoke any of Thor::Group commands twice" do
|
74
94
|
base = A.new
|
75
|
-
silence(:stdout){ base.invoke(:c) }
|
95
|
+
silence(:stdout) { base.invoke(:c) }
|
76
96
|
expect(capture(:stdout) { base.invoke("c:one") }).to be_empty
|
77
97
|
end
|
78
98
|
|
79
|
-
it "raises Thor::
|
80
|
-
expect
|
99
|
+
it "raises Thor::UndefinedCommandError if the command can't be found" do
|
100
|
+
expect do
|
81
101
|
A.new.invoke("foo:bar")
|
82
|
-
|
102
|
+
end.to raise_error(Thor::UndefinedCommandError)
|
83
103
|
end
|
84
104
|
|
85
|
-
it "raises Thor::
|
105
|
+
it "raises Thor::UndefinedCommandError if the command can't be found even if all commands were already executed" do
|
86
106
|
base = C.new
|
87
|
-
silence(:stdout){ base.invoke_all }
|
107
|
+
silence(:stdout) { base.invoke_all }
|
88
108
|
|
89
|
-
expect
|
109
|
+
expect do
|
90
110
|
base.invoke("foo:bar")
|
91
|
-
|
111
|
+
end.to raise_error(Thor::UndefinedCommandError)
|
92
112
|
end
|
93
113
|
|
94
114
|
it "raises an error if a non Thor class is given" do
|
95
|
-
expect
|
115
|
+
expect do
|
96
116
|
A.new.invoke(Object)
|
97
|
-
|
117
|
+
end.to raise_error(RuntimeError, "Expected Thor class, got Object")
|
98
118
|
end
|
99
119
|
end
|
100
120
|
end
|