Skip to content

HTTPS clone URL

Subversion checkout URL

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