wycats-thor 0.11.4 → 0.11.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -2
- data/Thorfile +57 -0
- data/VERSION +1 -0
- data/lib/thor.rb +13 -6
- data/lib/thor/base.rb +4 -1
- data/lib/thor/invocation.rb +1 -1
- data/lib/thor/rake_compat.rb +67 -0
- data/lib/thor/runner.rb +12 -8
- data/lib/thor/shell/basic.rb +8 -4
- data/lib/thor/task.rb +9 -1
- data/lib/thor/util.rb +28 -8
- data/spec/actions/create_file_spec.rb +170 -0
- data/spec/actions/directory_spec.rb +118 -0
- data/spec/actions/empty_directory_spec.rb +91 -0
- data/spec/actions/file_manipulation_spec.rb +242 -0
- data/spec/actions/inject_into_file_spec.rb +80 -0
- data/spec/actions_spec.rb +291 -0
- data/spec/base_spec.rb +235 -0
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
- data/spec/core_ext/ordered_hash_spec.rb +115 -0
- data/spec/fixtures/bundle/execute.rb +6 -0
- data/spec/fixtures/doc/config.rb +1 -0
- data/spec/group_spec.rb +177 -0
- data/spec/invocation_spec.rb +107 -0
- data/spec/parser/argument_spec.rb +47 -0
- data/spec/parser/arguments_spec.rb +64 -0
- data/spec/parser/option_spec.rb +212 -0
- data/spec/parser/options_spec.rb +255 -0
- data/spec/rake_compat_spec.rb +64 -0
- data/spec/runner_spec.rb +204 -0
- data/spec/shell/basic_spec.rb +206 -0
- data/spec/shell/color_spec.rb +41 -0
- data/spec/shell_spec.rb +25 -0
- data/spec/spec_helper.rb +52 -0
- data/spec/task_spec.rb +82 -0
- data/spec/thor_spec.rb +234 -0
- data/spec/util_spec.rb +192 -0
- metadata +56 -36
- data/Rakefile +0 -6
- data/lib/thor/tasks.rb +0 -4
- data/lib/thor/tasks/install.rb +0 -35
- data/lib/thor/tasks/package.rb +0 -31
- data/lib/thor/tasks/spec.rb +0 -70
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Thor::Shell::Color do
|
4
|
+
def shell
|
5
|
+
@shell ||= Thor::Shell::Color.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#say" do
|
9
|
+
it "set the color if specified" do
|
10
|
+
mock($stdout).puts("\e[32mWow! Now we have colors!\e[0m")
|
11
|
+
shell.say "Wow! Now we have colors!", :green
|
12
|
+
end
|
13
|
+
|
14
|
+
it "does not use a new line even with colors" do
|
15
|
+
mock($stdout).print("\e[32mWow! Now we have colors! \e[0m")
|
16
|
+
shell.say "Wow! Now we have colors! ", :green
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#say_status" do
|
21
|
+
it "uses color to say status" do
|
22
|
+
mock($stdout).puts("\e[1m\e[31m conflict\e[0m README")
|
23
|
+
shell.say_status :conflict, "README", :red
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#file_collision" do
|
28
|
+
describe "when a block is given" do
|
29
|
+
it "invokes the diff command" do
|
30
|
+
stub($stdout).print
|
31
|
+
mock($stdin).gets{ 'd' }
|
32
|
+
mock($stdin).gets{ 'n' }
|
33
|
+
|
34
|
+
output = capture(:stdout){ shell.file_collision('spec/fixtures/doc/README'){ "README\nEND\n" } }
|
35
|
+
output.must =~ /\e\[31m\- __start__\e\[0m/
|
36
|
+
output.must =~ /^ README/
|
37
|
+
output.must =~ /\e\[32m\+ END\e\[0m/
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/shell_spec.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Thor::Shell do
|
4
|
+
def shell
|
5
|
+
@shell ||= Thor::Base.shell.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#initialize" do
|
9
|
+
it "sets shell value" do
|
10
|
+
base = MyCounter.new [1, 2], { }, :shell => shell
|
11
|
+
base.shell.must == shell
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sets the base value on the shell if an accessor is available" do
|
15
|
+
base = MyCounter.new [1, 2], { }, :shell => shell
|
16
|
+
shell.base.must == base
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#shell" do
|
21
|
+
it "returns the shell in use" do
|
22
|
+
MyCounter.new([1,2]).shell.must be_kind_of(Thor::Base.shell)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
$TESTING=true
|
2
|
+
|
3
|
+
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
|
5
|
+
require 'thor'
|
6
|
+
require 'stringio'
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rr'
|
9
|
+
require 'diff/lcs' # You need diff/lcs installed to run specs (but not to run Thor).
|
10
|
+
|
11
|
+
# Load fixtures
|
12
|
+
load File.join(File.dirname(__FILE__), "fixtures", "task.thor")
|
13
|
+
load File.join(File.dirname(__FILE__), "fixtures", "group.thor")
|
14
|
+
load File.join(File.dirname(__FILE__), "fixtures", "script.thor")
|
15
|
+
load File.join(File.dirname(__FILE__), "fixtures", "invoke.thor")
|
16
|
+
|
17
|
+
# Set shell to basic
|
18
|
+
Thor::Base.shell = Thor::Shell::Basic
|
19
|
+
|
20
|
+
Kernel.module_eval do
|
21
|
+
alias_method :must, :should
|
22
|
+
alias_method :must_not, :should_not
|
23
|
+
undef_method :should
|
24
|
+
undef_method :should_not
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Runner.configure do |config|
|
28
|
+
config.mock_with :rr
|
29
|
+
|
30
|
+
def capture(stream)
|
31
|
+
begin
|
32
|
+
stream = stream.to_s
|
33
|
+
eval "$#{stream} = StringIO.new"
|
34
|
+
yield
|
35
|
+
result = eval("$#{stream}").string
|
36
|
+
ensure
|
37
|
+
eval("$#{stream} = #{stream.upcase}")
|
38
|
+
end
|
39
|
+
|
40
|
+
result
|
41
|
+
end
|
42
|
+
|
43
|
+
def source_root
|
44
|
+
File.join(File.dirname(__FILE__), 'fixtures')
|
45
|
+
end
|
46
|
+
|
47
|
+
def destination_root
|
48
|
+
File.join(File.dirname(__FILE__), 'sandbox')
|
49
|
+
end
|
50
|
+
|
51
|
+
alias silence capture
|
52
|
+
end
|
data/spec/task_spec.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Thor::Task do
|
4
|
+
def task(options={})
|
5
|
+
options.each do |key, value|
|
6
|
+
options[key] = Thor::Option.parse(key, value)
|
7
|
+
end
|
8
|
+
|
9
|
+
@task ||= Thor::Task.new(:can_has, "I can has cheezburger", "can_has", options)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#formatted_usage" do
|
13
|
+
it "shows usage with options" do
|
14
|
+
task('foo' => true, :bar => :required).formatted_usage.must == "can_has --bar=BAR [--foo]"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "includes namespace within usage" do
|
18
|
+
stub(Object).namespace{ "foo" }
|
19
|
+
stub(Object).arguments{ [] }
|
20
|
+
task(:bar => :required).formatted_usage(Object, true).must == "foo:can_has --bar=BAR"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "does not show options if required" do
|
24
|
+
stub(Object).namespace{ "foo" }
|
25
|
+
stub(Object).arguments{ [] }
|
26
|
+
task(:bar => :required).formatted_usage(Object, true, false).must == "foo:can_has"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "removes default from namespace" do
|
30
|
+
stub(Object).namespace{ "default:foo" }
|
31
|
+
stub(Object).arguments{ [] }
|
32
|
+
task(:bar => :required).formatted_usage(Object, true).must == ":foo:can_has --bar=BAR"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "injects arguments into usage" do
|
36
|
+
stub(Object).namespace{ "foo" }
|
37
|
+
stub(Object).arguments{ [ Thor::Argument.new(:bar, nil, true, :string) ] }
|
38
|
+
task(:foo => true).formatted_usage(Object).must == "can_has BAR [--foo]"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#dynamic" do
|
43
|
+
it "creates a dynamic task with the given name" do
|
44
|
+
Thor::Task.dynamic('task').name.must == 'task'
|
45
|
+
Thor::Task.dynamic('task').description.must == 'A dynamically-generated task'
|
46
|
+
Thor::Task.dynamic('task').usage.must == 'task'
|
47
|
+
Thor::Task.dynamic('task').options.must == {}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#dup" do
|
52
|
+
it "dup options hash" do
|
53
|
+
task = Thor::Task.new("can_has", nil, nil, :foo => true, :bar => :required)
|
54
|
+
task.dup.options.delete(:foo)
|
55
|
+
task.options[:foo].must_not be_nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#run" do
|
60
|
+
it "runs a task by calling a method in the given instance" do
|
61
|
+
mock = mock!.send("can_has", 1, 2, 3).subject
|
62
|
+
task.run(mock, [1, 2, 3])
|
63
|
+
end
|
64
|
+
|
65
|
+
it "raises an error if the method to be invoked is private" do
|
66
|
+
mock = mock!.private_methods{ [ 'can_has' ] }.subject
|
67
|
+
lambda {
|
68
|
+
task.run(mock)
|
69
|
+
}.must raise_error(Thor::UndefinedTaskError, "the 'can_has' task of Object is private")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#short_description" do
|
74
|
+
it "returns the first line of the description" do
|
75
|
+
Thor::Task.new(:task, "I can has\ncheezburger", "can_has").short_description == "I can has"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns the whole description if it's one line" do
|
79
|
+
Thor::Task.new(:task, "I can has cheezburger", "can_has").short_description == "I can has cheezburger"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/thor_spec.rb
ADDED
@@ -0,0 +1,234 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Thor do
|
4
|
+
describe "#method_option" do
|
5
|
+
it "sets options to the next method to be invoked" do
|
6
|
+
args = ["foo", "bar", "--force"]
|
7
|
+
arg, options = MyScript.start(args)
|
8
|
+
options.must == { "force" => true }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "when :for is supplied" do
|
12
|
+
it "updates an already defined task" do
|
13
|
+
args, options = MyChildScript.start(["animal", "horse", "--other=fish"])
|
14
|
+
options[:other].must == "fish"
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "and the target is on the parent class" do
|
18
|
+
it "updates an already defined task" do
|
19
|
+
args = ["example_default_task", "my_param", "--new-option=verified"]
|
20
|
+
options = Scripts::MyScript.start(args)
|
21
|
+
options[:new_option].must == "verified"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "adds a task to the tasks list if the updated task is on the parent class" do
|
25
|
+
Scripts::MyScript.tasks["example_default_task"].must_not be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it "clones the parent task" do
|
29
|
+
Scripts::MyScript.tasks["example_default_task"].must_not == MyChildScript.tasks["example_default_task"]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#default_task" do
|
36
|
+
it "sets a default task" do
|
37
|
+
MyScript.default_task.must == "example_default_task"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "invokes the default task if no command is specified" do
|
41
|
+
MyScript.start([]).must == "default task"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "inherits the default task from parent" do
|
45
|
+
MyChildScript.default_task.must == "example_default_task"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#map" do
|
50
|
+
it "calls the alias of a method if one is provided" do
|
51
|
+
MyScript.start(["-T", "fish"]).must == ["fish"]
|
52
|
+
end
|
53
|
+
|
54
|
+
it "calls the alias of a method if several are provided via .map" do
|
55
|
+
MyScript.start(["-f", "fish"]).must == ["fish", {}]
|
56
|
+
MyScript.start(["--foo", "fish"]).must == ["fish", {}]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "inherits all mappings from parent" do
|
60
|
+
MyChildScript.default_task.must == "example_default_task"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#desc" do
|
65
|
+
before(:all) do
|
66
|
+
@content = capture(:stdout) { MyScript.start(["help"]) }
|
67
|
+
end
|
68
|
+
|
69
|
+
it "provides description for a task" do
|
70
|
+
@content.must =~ /zoo\s+# zoo around/m
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "when :for is supplied" do
|
74
|
+
it "overwrites a previous defined task" do
|
75
|
+
capture(:stdout) { MyChildScript.start(["help"]) }.must =~ /animal KIND \[\-\-other=OTHER\]\s+# fish around/m
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#method_options" do
|
81
|
+
it "sets default options if called before an initializer" do
|
82
|
+
options = MyChildScript.class_options
|
83
|
+
options[:force].type.must == :boolean
|
84
|
+
options[:param].type.must == :numeric
|
85
|
+
end
|
86
|
+
|
87
|
+
it "overwrites default options if called on the method scope" do
|
88
|
+
args = ["zoo", "--force", "--param", "feathers"]
|
89
|
+
options = MyChildScript.start(args)
|
90
|
+
options.must == { "force" => true, "param" => "feathers" }
|
91
|
+
end
|
92
|
+
|
93
|
+
it "allows default options to be merged with method options" do
|
94
|
+
args = ["animal", "bird", "--force", "--param", "1.0", "--other", "tweets"]
|
95
|
+
arg, options = MyChildScript.start(args)
|
96
|
+
arg.must == 'bird'
|
97
|
+
options.must == { "force"=>true, "param"=>1.0, "other"=>"tweets" }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "#start" do
|
102
|
+
it "calls a no-param method when no params are passed" do
|
103
|
+
MyScript.start(["zoo"]).must == true
|
104
|
+
end
|
105
|
+
|
106
|
+
it "calls a single-param method when a single param is passed" do
|
107
|
+
MyScript.start(["animal", "fish"]).must == ["fish"]
|
108
|
+
end
|
109
|
+
|
110
|
+
it "does not set options in attributes" do
|
111
|
+
MyScript.start(["with_optional", "--all"]).must == [nil, { "all" => true }]
|
112
|
+
end
|
113
|
+
|
114
|
+
it "raises an error if a required param is not provided" do
|
115
|
+
capture(:stderr) { MyScript.start(["animal"]) }.must =~ /'animal' was called incorrectly\. Call as 'my_script:animal TYPE'/
|
116
|
+
end
|
117
|
+
|
118
|
+
it "raises an error if the invoked task does not exist" do
|
119
|
+
capture(:stderr) { Amazing.start(["animal"]) }.must =~ /The amazing namespace doesn't have a 'animal' task/
|
120
|
+
end
|
121
|
+
|
122
|
+
it "calls method_missing if an unknown method is passed in" do
|
123
|
+
MyScript.start(["unk", "hello"]).must == [:unk, ["hello"]]
|
124
|
+
end
|
125
|
+
|
126
|
+
it "does not call a private method no matter what" do
|
127
|
+
capture(:stderr) { MyScript.start(["what"]) }.must =~ /the 'what' task of MyScript is private/
|
128
|
+
end
|
129
|
+
|
130
|
+
it "uses task default options" do
|
131
|
+
options = MyChildScript.start(["animal", "fish"]).last
|
132
|
+
options.must == { "other" => "method default" }
|
133
|
+
end
|
134
|
+
|
135
|
+
it "raises when an exception happens within the task call" do
|
136
|
+
lambda { MyScript.start(["call_myself_with_wrong_arity"]) }.must raise_error
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "#help" do
|
141
|
+
def shell
|
142
|
+
@shell ||= Thor::Base.shell.new
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "on general" do
|
146
|
+
before(:each) do
|
147
|
+
@content = capture(:stdout){ MyScript.help(shell) }
|
148
|
+
end
|
149
|
+
|
150
|
+
it "provides useful help info for the help method itself" do
|
151
|
+
@content.must =~ /help \[TASK\]\s+# Describe available tasks/m
|
152
|
+
end
|
153
|
+
|
154
|
+
it "provides useful help info for a method with params" do
|
155
|
+
@content.must =~ /animal TYPE\s+# horse around/m
|
156
|
+
end
|
157
|
+
|
158
|
+
it "provides description for tasks from classes in the same namespace" do
|
159
|
+
@content.must =~ /baz\s+# do some bazing/m
|
160
|
+
end
|
161
|
+
|
162
|
+
it "shows superclass tasks" do
|
163
|
+
content = capture(:stdout){ MyChildScript.help(shell) }
|
164
|
+
content.must =~ /foo BAR \[\-\-force\]\s+# do some fooing/m
|
165
|
+
end
|
166
|
+
|
167
|
+
it "shows class options information" do
|
168
|
+
content = capture(:stdout){ MyChildScript.help(shell) }
|
169
|
+
content.must =~ /Class options\:/
|
170
|
+
content.must =~ /\[\-\-param=N\]/
|
171
|
+
end
|
172
|
+
|
173
|
+
it "injects class arguments into default usage" do
|
174
|
+
content = capture(:stdout){ Scripts::MyScript.help(shell) }
|
175
|
+
content.must =~ /zoo ACCESSOR \-\-param\=PARAM/
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "for a specific task" do
|
180
|
+
it "provides full help info when talking about a specific task" do
|
181
|
+
capture(:stdout) { MyScript.help(shell, "foo") }.must == <<END
|
182
|
+
Usage:
|
183
|
+
foo BAR
|
184
|
+
|
185
|
+
Method options:
|
186
|
+
[--force] # Force to do some fooing
|
187
|
+
|
188
|
+
do some fooing
|
189
|
+
This is more info!
|
190
|
+
Everyone likes more info!
|
191
|
+
END
|
192
|
+
end
|
193
|
+
|
194
|
+
it "raises an error if the task can't be found" do
|
195
|
+
lambda {
|
196
|
+
MyScript.help(shell, "unknown")
|
197
|
+
}.must raise_error(Thor::Error, "task 'unknown' could not be found in namespace 'my_script'")
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "options" do
|
202
|
+
it "shows the namespace if required" do
|
203
|
+
capture(:stdout){ MyScript.help(shell, nil, :namespace => true) }.must =~ /my_script:foo BAR/
|
204
|
+
end
|
205
|
+
|
206
|
+
it "does not show superclass tasks if required" do
|
207
|
+
capture(:stdout){ MyScript.help(shell, nil, :short => true) }.must_not =~ /help/
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "instance method" do
|
212
|
+
it "calls the class method" do
|
213
|
+
stub(MyScript).help(mock.instance_of(Thor::Base.shell), nil, :namespace => nil)
|
214
|
+
MyScript.start(["help"])
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe "when creating tasks" do
|
220
|
+
it "prints a warning if a public method is created without description or usage" do
|
221
|
+
capture(:stdout) {
|
222
|
+
klass = Class.new(Thor)
|
223
|
+
klass.class_eval "def hello_from_thor; end"
|
224
|
+
}.must =~ /\[WARNING\] Attempted to create task "hello_from_thor" without usage or description/
|
225
|
+
end
|
226
|
+
|
227
|
+
it "does not print if overwriting a previous task" do
|
228
|
+
capture(:stdout) {
|
229
|
+
klass = Class.new(Thor)
|
230
|
+
klass.class_eval "def help; end"
|
231
|
+
}.must be_empty
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
data/spec/util_spec.rb
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
module Thor::Util
|
4
|
+
def self.clear_user_home!
|
5
|
+
@@user_home = nil
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Thor::Util do
|
10
|
+
describe "#find_by_namespace" do
|
11
|
+
it "returns 'Default' if no namespace is given" do
|
12
|
+
Thor::Util.find_by_namespace('').must == Scripts::MyDefaults
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns nil if the namespace can't be found" do
|
16
|
+
Thor::Util.find_by_namespace('thor:core_ext:ordered_hash').must be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns a class if it matches the namespace" do
|
20
|
+
Thor::Util.find_by_namespace('app:broken:counter').must == BrokenCounter
|
21
|
+
end
|
22
|
+
|
23
|
+
it "matches classes default namespace" do
|
24
|
+
Thor::Util.find_by_namespace('scripts:my_script').must == Scripts::MyScript
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#namespace_from_thor_class" do
|
29
|
+
it "replaces constant nesting with task namespacing" do
|
30
|
+
Thor::Util.namespace_from_thor_class("Foo::Bar::Baz").must == "foo:bar:baz"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "snake-cases component strings" do
|
34
|
+
Thor::Util.namespace_from_thor_class("FooBar::BarBaz::BazBoom").must == "foo_bar:bar_baz:baz_boom"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "gets rid of an initial Default module" do
|
38
|
+
Thor::Util.namespace_from_thor_class("Default::Foo::Bar").must == ":foo:bar"
|
39
|
+
Thor::Util.namespace_from_thor_class("Default").must == ""
|
40
|
+
end
|
41
|
+
|
42
|
+
it "accepts class and module objects" do
|
43
|
+
Thor::Util.namespace_from_thor_class(Thor::CoreExt::OrderedHash).must == "thor:core_ext:ordered_hash"
|
44
|
+
Thor::Util.namespace_from_thor_class(Thor::Util).must == "thor:util"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "removes Thor::Sandbox namespace" do
|
48
|
+
Thor::Util.namespace_from_thor_class("Thor::Sandbox::Package").must == "package"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#namespaces_in_content" do
|
53
|
+
it "returns an array of names of constants defined in the string" do
|
54
|
+
list = Thor::Util.namespaces_in_content("class Foo; class Bar < Thor; end; end; class Baz; class Bat; end; end")
|
55
|
+
list.must include("foo:bar")
|
56
|
+
list.must_not include("bar:bat")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "doesn't put the newly-defined constants in the enclosing namespace" do
|
60
|
+
Thor::Util.namespaces_in_content("class Blat; end")
|
61
|
+
defined?(Blat).must_not be
|
62
|
+
defined?(Thor::Sandbox::Blat).must be
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#snake_case" do
|
67
|
+
it "preserves no-cap strings" do
|
68
|
+
Thor::Util.snake_case("foo").must == "foo"
|
69
|
+
Thor::Util.snake_case("foo_bar").must == "foo_bar"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "downcases all-caps strings" do
|
73
|
+
Thor::Util.snake_case("FOO").must == "foo"
|
74
|
+
Thor::Util.snake_case("FOO_BAR").must == "foo_bar"
|
75
|
+
end
|
76
|
+
|
77
|
+
it "downcases initial-cap strings" do
|
78
|
+
Thor::Util.snake_case("Foo").must == "foo"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "replaces camel-casing with underscores" do
|
82
|
+
Thor::Util.snake_case("FooBarBaz").must == "foo_bar_baz"
|
83
|
+
Thor::Util.snake_case("Foo_BarBaz").must == "foo_bar_baz"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "places underscores between multiple capitals" do
|
87
|
+
Thor::Util.snake_case("ABClass").must == "a_b_class"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#namespace_to_thor_class_and_task" do
|
92
|
+
it "returns a Thor::Group class if full namespace matches" do
|
93
|
+
Thor::Util.namespace_to_thor_class_and_task("my_counter").must == [MyCounter, nil]
|
94
|
+
end
|
95
|
+
|
96
|
+
it "returns a Thor class if full namespace matches" do
|
97
|
+
Thor::Util.namespace_to_thor_class_and_task("thor").must == [Thor, nil]
|
98
|
+
end
|
99
|
+
|
100
|
+
it "returns a Thor class and the task name" do
|
101
|
+
Thor::Util.namespace_to_thor_class_and_task("thor:help").must == [Thor, "help"]
|
102
|
+
end
|
103
|
+
|
104
|
+
it "fallbacks in the namespace:task look up even if a full namespace does not match" do
|
105
|
+
Thor.const_set(:Help, Module.new)
|
106
|
+
Thor::Util.namespace_to_thor_class_and_task("thor:help").must == [Thor, "help"]
|
107
|
+
Thor.send :remove_const, :Help
|
108
|
+
end
|
109
|
+
|
110
|
+
describe 'errors' do
|
111
|
+
it "raises an error if the Thor class or task can't be found" do
|
112
|
+
lambda {
|
113
|
+
Thor::Util.namespace_to_thor_class_and_task("foobar")
|
114
|
+
}.must raise_error(Thor::Error, "could not find Thor class or task 'foobar'")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "#thor_classes_in" do
|
120
|
+
it "returns thor classes inside the given class" do
|
121
|
+
Thor::Util.thor_classes_in(MyScript).must == [MyScript::AnotherScript]
|
122
|
+
Thor::Util.thor_classes_in(MyScript::AnotherScript).must be_empty
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "#user_home" do
|
127
|
+
before(:each) do
|
128
|
+
stub(ENV)[]
|
129
|
+
Thor::Util.clear_user_home!
|
130
|
+
end
|
131
|
+
|
132
|
+
it "returns the user path if none variable is set on the environment" do
|
133
|
+
Thor::Util.user_home.must == File.expand_path("~")
|
134
|
+
end
|
135
|
+
|
136
|
+
it "returns the *unix system path if file cannot be expanded and separator does not exist" do
|
137
|
+
stub(File).expand_path("~"){ raise }
|
138
|
+
previous_value = File::ALT_SEPARATOR
|
139
|
+
capture(:stderr){ File.const_set(:ALT_SEPARATOR, false) }
|
140
|
+
Thor::Util.user_home.must == "/"
|
141
|
+
capture(:stderr){ File.const_set(:ALT_SEPARATOR, previous_value) }
|
142
|
+
end
|
143
|
+
|
144
|
+
it "returns the windows system path if file cannot be expanded and a separator exists" do
|
145
|
+
stub(File).expand_path("~"){ raise }
|
146
|
+
previous_value = File::ALT_SEPARATOR
|
147
|
+
capture(:stderr){ File.const_set(:ALT_SEPARATOR, true) }
|
148
|
+
Thor::Util.user_home.must == "C:/"
|
149
|
+
capture(:stderr){ File.const_set(:ALT_SEPARATOR, previous_value) }
|
150
|
+
end
|
151
|
+
|
152
|
+
it "returns HOME/.thor if set" do
|
153
|
+
stub(ENV)["HOME"].returns{ "/home/user/" }
|
154
|
+
Thor::Util.user_home.must == "/home/user/"
|
155
|
+
end
|
156
|
+
|
157
|
+
it "returns path with HOMEDRIVE and HOMEPATH if set" do
|
158
|
+
stub(ENV)["HOMEDRIVE"].returns{ "D:/" }
|
159
|
+
stub(ENV)["HOMEPATH"].returns{ "Documents and Settings/James" }
|
160
|
+
Thor::Util.user_home.must == "D:/Documents and Settings/James"
|
161
|
+
end
|
162
|
+
|
163
|
+
it "returns APPDATA/.thor if set" do
|
164
|
+
stub(ENV)["APPDATA"].returns{ "/home/user/" }
|
165
|
+
Thor::Util.user_home.must == "/home/user/"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "#convert_constants_to_namespaces" do
|
170
|
+
before(:each) do
|
171
|
+
@hash = {
|
172
|
+
:git => {
|
173
|
+
:constants => [Object, "Thor::Sandbox::Package", Thor::CoreExt::OrderedHash]
|
174
|
+
}
|
175
|
+
}
|
176
|
+
end
|
177
|
+
|
178
|
+
it "converts constants in the hash to namespaces" do
|
179
|
+
Thor::Util.convert_constants_to_namespaces(@hash)
|
180
|
+
@hash[:git][:namespaces].must == [ "object", "package", "thor:core_ext:ordered_hash" ]
|
181
|
+
end
|
182
|
+
|
183
|
+
it "returns true if the hash changed" do
|
184
|
+
Thor::Util.convert_constants_to_namespaces(@hash).must be_true
|
185
|
+
end
|
186
|
+
|
187
|
+
it "does not add namespaces to the hash if namespaces were already added" do
|
188
|
+
Thor::Util.convert_constants_to_namespaces(@hash)
|
189
|
+
Thor::Util.convert_constants_to_namespaces(@hash).must be_false
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|