Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 242 lines (202 sloc) 8.495 kb
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
1 require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2 require 'thor/runner'
3
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
4 describe Thor::Runner do
a62fb61 @cldwalker fix help of an unknown command when in a setup without thorfiles
cldwalker authored
5 def when_no_thorfiles_exist
6 old_dir = Dir.pwd
7 Dir.chdir '..'
8 delete = Thor::Base.subclasses.select {|e| e.namespace == 'default' }
9 delete.each {|e| Thor::Base.subclasses.delete e }
10 yield
11 Thor::Base.subclasses.concat delete
12 Dir.chdir old_dir
13 end
14
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
15 describe "#help" do
16 it "shows information about Thor::Runner itself" do
35b14a9 @sferik Add rcov to bundle
sferik authored
17 capture(:stdout){ Thor::Runner.start(["help"]) }.should =~ /List the available thor tasks/
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
18 end
19
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
20 it "shows information about an specific Thor::Runner task" do
21 content = capture(:stdout){ Thor::Runner.start(["help", "list"]) }
35b14a9 @sferik Add rcov to bundle
sferik authored
22 content.should =~ /List the available thor tasks/
23 content.should_not =~ /help \[TASK\]/
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
24 end
25
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
26 it "shows information about a specific Thor class" do
27 content = capture(:stdout){ Thor::Runner.start(["help", "my_script"]) }
35b14a9 @sferik Add rcov to bundle
sferik authored
28 content.should =~ /zoo\s+# zoo around/m
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
29 end
e2d3d09 @josevalim Thor runner accepts class calls. This allows default tasks to be invoked...
josevalim authored
30
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
31 it "shows information about an specific task from an specific Thor class" do
32 content = capture(:stdout){ Thor::Runner.start(["help", "my_script:zoo"]) }
35b14a9 @sferik Add rcov to bundle
sferik authored
33 content.should =~ /zoo around/
34 content.should_not =~ /help \[TASK\]/
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
35 end
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
36
50a6f71 @josevalim Thor::Generator is now Thor::Group.
josevalim authored
37 it "shows information about a specific Thor group class" do
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
38 content = capture(:stdout){ Thor::Runner.start(["help", "my_counter"]) }
35b14a9 @sferik Add rcov to bundle
sferik authored
39 content.should =~ /my_counter N/
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
40 end
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
41
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
42 it "raises error if a class/task cannot be found" do
43 content = capture(:stderr){ Thor::Runner.start(["help", "unknown"]) }
35b14a9 @sferik Add rcov to bundle
sferik authored
44 content.strip.should == 'Could not find task "unknown" in "default" namespace.'
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
45 end
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
46
a62fb61 @cldwalker fix help of an unknown command when in a setup without thorfiles
cldwalker authored
47 it "raises error if a class/task cannot be found for a setup without thorfiles" do
48 when_no_thorfiles_exist do
49 Thor::Runner.should_receive :exit
50 content = capture(:stderr){ Thor::Runner.start(["help", "unknown"]) }
51 content.strip.should == 'Could not find task "unknown".'
52 end
b18a6cf @cldwalker fix unknown command handling when in a setup without thorfiles
cldwalker authored
53 end
a62fb61 @cldwalker fix help of an unknown command when in a setup without thorfiles
cldwalker authored
54 end
b18a6cf @cldwalker fix unknown command handling when in a setup without thorfiles
cldwalker authored
55
a62fb61 @cldwalker fix help of an unknown command when in a setup without thorfiles
cldwalker authored
56 describe "#start" do
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
57 it "invokes a task from Thor::Runner" do
58 ARGV.replace ["list"]
35b14a9 @sferik Add rcov to bundle
sferik authored
59 capture(:stdout){ Thor::Runner.start }.should =~ /my_counter N/
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
60 end
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
61
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
62 it "invokes a task from a specific Thor class" do
63 ARGV.replace ["my_script:zoo"]
35b14a9 @sferik Add rcov to bundle
sferik authored
64 Thor::Runner.start.should be_true
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
65 end
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
66
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
67 it "invokes the default task from a specific Thor class if none is specified" do
68 ARGV.replace ["my_script"]
35b14a9 @sferik Add rcov to bundle
sferik authored
69 Thor::Runner.start.should == "default task"
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
70 end
71
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
72 it "forwads arguments to the invoked task" do
6bc478e @josevalim Make pending specs pass.
josevalim authored
73 ARGV.replace ["my_script:animal", "horse"]
35b14a9 @sferik Add rcov to bundle
sferik authored
74 Thor::Runner.start.should == ["horse"]
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
75 end
76
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
77 it "invokes tasks through shortcuts" do
6bc478e @josevalim Make pending specs pass.
josevalim authored
78 ARGV.replace ["my_script", "-T", "horse"]
35b14a9 @sferik Add rcov to bundle
sferik authored
79 Thor::Runner.start.should == ["horse"]
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
80 end
81
50a6f71 @josevalim Thor::Generator is now Thor::Group.
josevalim authored
82 it "invokes a Thor::Group" do
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
83 ARGV.replace ["my_counter", "1", "2", "--third", "3"]
35b14a9 @sferik Add rcov to bundle
sferik authored
84 Thor::Runner.start.should == [1, 2, 3]
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
85 end
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
86
578fa98 @josevalim Improving Thor::Runner specs.
josevalim authored
87 it "raises an error if class/task can't be found" do
6bc478e @josevalim Make pending specs pass.
josevalim authored
88 ARGV.replace ["unknown"]
ac419fa @josevalim Unify error messages.
josevalim authored
89 content = capture(:stderr){ Thor::Runner.start }
35b14a9 @sferik Add rcov to bundle
sferik authored
90 content.strip.should == 'Could not find task "unknown" in "default" namespace.'
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
91 end
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
92
b18a6cf @cldwalker fix unknown command handling when in a setup without thorfiles
cldwalker authored
93 it "raises an error if class/task can't be found in a setup without thorfiles" do
94 when_no_thorfiles_exist do
95 ARGV.replace ["unknown"]
96 Thor::Runner.should_receive :exit
97 content = capture(:stderr){ Thor::Runner.start }
a62fb61 @cldwalker fix help of an unknown command when in a setup without thorfiles
cldwalker authored
98 content.strip.should == 'Could not find task "unknown".'
b18a6cf @cldwalker fix unknown command handling when in a setup without thorfiles
cldwalker authored
99 end
100 end
101
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
102 it "does not swallow NoMethodErrors that occur inside the called method" do
103 ARGV.replace ["my_script:call_unexistent_method"]
35b14a9 @sferik Add rcov to bundle
sferik authored
104 lambda { Thor::Runner.start }.should raise_error(NoMethodError)
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
105 end
8b144c6 @josevalim Ensure Thor::Group ArgumentError passes through Thor::Runner.
josevalim authored
106
4289017 @josevalim Refactoring on how tasks are shown.
josevalim authored
107 it "does not swallow Thor::Group InvocationError" do
8b144c6 @josevalim Ensure Thor::Group ArgumentError passes through Thor::Runner.
josevalim authored
108 ARGV.replace ["whiny_generator"]
a24b669 @wycats More friendly error messages (ht: @tomdale)
wycats authored
109 lambda { Thor::Runner.start }.should raise_error(ArgumentError, /thor wrong_arity takes 1 argument, but it should not/)
8b144c6 @josevalim Ensure Thor::Group ArgumentError passes through Thor::Runner.
josevalim authored
110 end
111
4289017 @josevalim Refactoring on how tasks are shown.
josevalim authored
112 it "does not swallow Thor InvocationError" do
8b144c6 @josevalim Ensure Thor::Group ArgumentError passes through Thor::Runner.
josevalim authored
113 ARGV.replace ["my_script:animal"]
ac419fa @josevalim Unify error messages.
josevalim authored
114 content = capture(:stderr) { Thor::Runner.start }
a24b669 @wycats More friendly error messages (ht: @tomdale)
wycats authored
115 content.strip.should == 'thor animal requires at least 1 argument: "thor my_script:animal TYPE".'
8b144c6 @josevalim Ensure Thor::Group ArgumentError passes through Thor::Runner.
josevalim authored
116 end
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
117 end
118
119 describe "tasks" do
509b87b @sferik Replace before(:each) with before
sferik authored
120 before do
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
121 @location = "#{File.dirname(__FILE__)}/fixtures/task.thor"
122 @original_yaml = {
123 "random" => {
124 :location => @location,
125 :filename => "4a33b894ffce85d7b412fc1b36f88fe0",
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
126 :namespaces => ["amazing"]
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
127 }
128 }
129
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
130 root_file = File.join(Thor::Util.thor_root, "thor.yml")
131
c9a2259 @josevalim No more constants. Now everything is namespaces.
josevalim authored
132 # Stub load and save to avoid thor.yaml from being overwritten
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
133 YAML.stub!(:load_file).and_return(@original_yaml)
134 File.stub!(:exists?).with(root_file).and_return(true)
135 File.stub!(:open).with(root_file, "w")
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
136 end
137
138 describe "list" do
139 it "gives a list of the available tasks" do
140 ARGV.replace ["list"]
14bc921 @josevalim Per Mislav suggestion, do not show task usage and description in the sam...
josevalim authored
141 content = capture(:stdout) { Thor::Runner.start }
35b14a9 @sferik Add rcov to bundle
sferik authored
142 content.should =~ /amazing:describe NAME\s+# say that someone is amazing/m
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
143 end
144
50a6f71 @josevalim Thor::Generator is now Thor::Group.
josevalim authored
145 it "gives a list of the available Thor::Group classes" do
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
146 ARGV.replace ["list"]
35b14a9 @sferik Add rcov to bundle
sferik authored
147 capture(:stdout) { Thor::Runner.start }.should =~ /my_counter N/
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
148 end
149
150 it "can filter a list of the available tasks by --group" do
151 ARGV.replace ["list", "--group", "standard"]
35b14a9 @sferik Add rcov to bundle
sferik authored
152 capture(:stdout) { Thor::Runner.start }.should =~ /amazing:describe NAME/
6c0ce70 @josevalim Allow initializer config values to be overwriten on invocation.
josevalim authored
153 ARGV.replace []
35b14a9 @sferik Add rcov to bundle
sferik authored
154 capture(:stdout) { Thor::Runner.start }.should_not =~ /my_script:animal TYPE/
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
155 ARGV.replace ["list", "--group", "script"]
35b14a9 @sferik Add rcov to bundle
sferik authored
156 capture(:stdout) { Thor::Runner.start }.should =~ /my_script:animal TYPE/
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
157 end
158
159 it "can skip all filters to show all tasks using --all" do
160 ARGV.replace ["list", "--all"]
161 content = capture(:stdout) { Thor::Runner.start }
35b14a9 @sferik Add rcov to bundle
sferik authored
162 content.should =~ /amazing:describe NAME/
163 content.should =~ /my_script:animal TYPE/
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
164 end
165
166 it "doesn't list superclass tasks in the subclass" do
167 ARGV.replace ["list"]
35b14a9 @sferik Add rcov to bundle
sferik authored
168 capture(:stdout) { Thor::Runner.start }.should_not =~ /amazing:help/
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
169 end
170
171 it "presents tasks in the default namespace with an empty namespace" do
172 ARGV.replace ["list"]
35b14a9 @sferik Add rcov to bundle
sferik authored
173 capture(:stdout) { Thor::Runner.start }.should =~ /^thor :cow\s+# prints 'moo'/m
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
174 end
175
176 it "runs tasks with an empty namespace from the default namespace" do
8a4e9ca @indirect Allow default namespace tasks to be invoked without a colon
indirect authored
177 ARGV.replace [":task_conflict"]
35b14a9 @sferik Add rcov to bundle
sferik authored
178 capture(:stdout) { Thor::Runner.start }.should == "task\n"
8a4e9ca @indirect Allow default namespace tasks to be invoked without a colon
indirect authored
179 end
180
181 it "runs groups even when there is a task with the same name" do
182 ARGV.replace ["task_conflict"]
35b14a9 @sferik Add rcov to bundle
sferik authored
183 capture(:stdout) { Thor::Runner.start }.should == "group\n"
8a4e9ca @indirect Allow default namespace tasks to be invoked without a colon
indirect authored
184 end
185
186 it "runs tasks with no colon in the default namespace" do
187 ARGV.replace ["cow"]
35b14a9 @sferik Add rcov to bundle
sferik authored
188 capture(:stdout) { Thor::Runner.start }.should == "moo\n"
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
189 end
190 end
191
192 describe "uninstall" do
509b87b @sferik Replace before(:each) with before
sferik authored
193 before do
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
194 path = File.join(Thor::Util.thor_root, @original_yaml["random"][:filename])
195 FileUtils.should_receive(:rm_rf).with(path)
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
196 end
197
198 it "uninstalls existing thor modules" do
199 silence(:stdout) { Thor::Runner.start(["uninstall", "random"]) }
200 end
201 end
202
203 describe "installed" do
509b87b @sferik Replace before(:each) with before
sferik authored
204 before do
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
205 Dir.should_receive(:[]).and_return([])
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
206 end
207
208 it "displays the modules installed in a pretty way" do
209 stdout = capture(:stdout) { Thor::Runner.start(["installed"]) }
35b14a9 @sferik Add rcov to bundle
sferik authored
210 stdout.should =~ /random\s*amazing/
211 stdout.should =~ /amazing:describe NAME\s+# say that someone is amazing/m
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
212 end
213 end
214
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
215 describe "install/update" do
509b87b @sferik Replace before(:each) with before
sferik authored
216 before do
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
217 FileUtils.stub!(:mkdir_p)
218 FileUtils.stub!(:touch)
219 $stdin.stub!(:gets).and_return("Y")
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
220
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
221 path = File.join(Thor::Util.thor_root, Digest::MD5.hexdigest(@location + "random"))
222 File.should_receive(:open).with(path, "w")
223 end
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
224
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
225 it "updates existing thor files" do
226 path = File.join(Thor::Util.thor_root, @original_yaml["random"][:filename])
b5b6d61 @kaze testing if thor deletes bundle's directory
kaze authored
227 if File.directory? path
228 FileUtils.should_receive(:rm_rf).with(path)
229 else
230 File.should_receive(:delete).with(path)
231 end
7f892ff @josevalim Migration to rspec mock framework.
josevalim authored
232 silence(:stdout) { Thor::Runner.start(["update", "random"]) }
233 end
234
235 it "installs thor files" do
236 ARGV.replace ["install", @location]
d25dec5 @josevalim More specs for Thor::Runner.
josevalim authored
237 silence(:stdout) { Thor::Runner.start }
238 end
239 end
e144bdc @josevalim Bringing Thor::Runner back to life.
josevalim authored
240 end
241 end
Something went wrong with that request. Please try again.