Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 293 lines (241 sloc) 9.156 kb
0fb761c @josevalim Added more behavior to copy file action and actions in general.
josevalim authored
1 require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
3 describe Thor::Actions do
40494cc @josevalim Remove default values from parser and add to class initialization.
josevalim authored
4 def runner(options={})
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
5 @runner ||= MyCounter.new([1], options, { :destination_root => destination_root })
79838a9 @josevalim Removed actions from Thor::Runner and added specs for inside.
josevalim authored
6 end
7
996fdd3 @josevalim Everyone loves code refactoring.
josevalim authored
8 def action(*args, &block)
9 capture(:stdout){ runner.send(*args, &block) }
10 end
11
79838a9 @josevalim Removed actions from Thor::Runner and added specs for inside.
josevalim authored
12 def file
13 File.join(destination_root, "foo")
14 end
15
f3cc428 @josevalim Options can be grouped. This allows a better output when several options...
josevalim authored
16 describe "on include" do
17 it "adds runtime options to the base class" do
2a2b77b @josevalim Does not inline default values in class options. Show them below the des...
josevalim authored
18 MyCounter.class_options.keys.must include(:pretend)
19 MyCounter.class_options.keys.must include(:force)
20 MyCounter.class_options.keys.must include(:quiet)
21 MyCounter.class_options.keys.must include(:skip)
f3cc428 @josevalim Options can be grouped. This allows a better output when several options...
josevalim authored
22 end
23 end
24
0fb761c @josevalim Added more behavior to copy file action and actions in general.
josevalim authored
25 describe "#initialize" do
26 it "has default behavior invoke" do
79838a9 @josevalim Removed actions from Thor::Runner and added specs for inside.
josevalim authored
27 runner.behavior.must == :invoke
0fb761c @josevalim Added more behavior to copy file action and actions in general.
josevalim authored
28 end
29
30 it "can have behavior revoke" do
5c87fd4 @josevalim More parser refactoring.
josevalim authored
31 MyCounter.new([1], {}, :behavior => :revoke).behavior.must == :revoke
0fb761c @josevalim Added more behavior to copy file action and actions in general.
josevalim authored
32 end
4a76b3a @josevalim Small changes for default values.
josevalim authored
33
34 it "when behavior is set to force, overwrite options" do
5c87fd4 @josevalim More parser refactoring.
josevalim authored
35 runner = MyCounter.new([1], { :force => false, :skip => true }, :behavior => :force)
4a76b3a @josevalim Small changes for default values.
josevalim authored
36 runner.behavior.must == :invoke
37 runner.options.force.must be_true
38 runner.options.skip.must_not be_true
39 end
40
41 it "when behavior is set to skip, overwrite options" do
57295cf @josevalim Ensure behavior is overwriten even if options is an array.
josevalim authored
42 runner = MyCounter.new([1], ["--force"], :behavior => :skip)
4a76b3a @josevalim Small changes for default values.
josevalim authored
43 runner.behavior.must == :invoke
44 runner.options.force.must_not be_true
45 runner.options.skip.must be_true
46 end
0fb761c @josevalim Added more behavior to copy file action and actions in general.
josevalim authored
47 end
48
b081fcf @josevalim Added chmod action.
josevalim authored
49 describe "accessors" do
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
50 describe "#destination_root=" do
b081fcf @josevalim Added chmod action.
josevalim authored
51 it "gets the current directory and expands the path to set the root" do
5c87fd4 @josevalim More parser refactoring.
josevalim authored
52 base = MyCounter.new([1])
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
53 base.destination_root = "here"
54 base.destination_root.must == File.expand_path(File.join(File.dirname(__FILE__), "..", "here"))
b081fcf @josevalim Added chmod action.
josevalim authored
55 end
7470eaf @josevalim Added specs for run and moved root logic to actions (it's no included by...
josevalim authored
56
b081fcf @josevalim Added chmod action.
josevalim authored
57 it "does not use the current directory if one is given" do
01fbc02 @luislavena Expand root to work on OS with drive letters
luislavena authored
58 root = File.expand_path("/")
5c87fd4 @josevalim More parser refactoring.
josevalim authored
59 base = MyCounter.new([1])
01fbc02 @luislavena Expand root to work on OS with drive letters
luislavena authored
60 base.destination_root = root
61 base.destination_root.must == root
b081fcf @josevalim Added chmod action.
josevalim authored
62 end
7470eaf @josevalim Added specs for run and moved root logic to actions (it's no included by...
josevalim authored
63
b081fcf @josevalim Added chmod action.
josevalim authored
64 it "uses the current directory if none is given" do
5c87fd4 @josevalim More parser refactoring.
josevalim authored
65 base = MyCounter.new([1])
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
66 base.destination_root.must == File.expand_path(File.join(File.dirname(__FILE__), ".."))
b081fcf @josevalim Added chmod action.
josevalim authored
67 end
7470eaf @josevalim Added specs for run and moved root logic to actions (it's no included by...
josevalim authored
68 end
69
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
70 describe "#relative_to_original_destination_root" do
b081fcf @josevalim Added chmod action.
josevalim authored
71 it "returns the path relative to the absolute root" do
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
72 runner.relative_to_original_destination_root(file).must == "foo"
b081fcf @josevalim Added chmod action.
josevalim authored
73 end
a87e6ac @josevalim More actions.
josevalim authored
74
b081fcf @josevalim Added chmod action.
josevalim authored
75 it "does not remove dot if required" do
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
76 runner.relative_to_original_destination_root(file, false).must == "./foo"
b081fcf @josevalim Added chmod action.
josevalim authored
77 end
a87e6ac @josevalim More actions.
josevalim authored
78
b081fcf @josevalim Added chmod action.
josevalim authored
79 it "always use the absolute root" do
80 runner.inside("foo") do
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
81 runner.relative_to_original_destination_root(file).must == "foo"
b081fcf @josevalim Added chmod action.
josevalim authored
82 end
a87e6ac @josevalim More actions.
josevalim authored
83 end
30eae32 @josevalim Solved source_paths loadind issues.
josevalim authored
84
f33776b @josevalim Simplifying source paths implementation.
josevalim authored
85 describe "#source_paths_for_search" do
86 it "add source_root to source_paths_for_search" do
87 MyCounter.source_paths_for_search.must include(File.expand_path("fixtures", File.dirname(__FILE__)))
30eae32 @josevalim Solved source_paths loadind issues.
josevalim authored
88 end
89
f33776b @josevalim Simplifying source paths implementation.
josevalim authored
90 it "keeps only current source root in source paths" do
91 ClearCounter.source_paths_for_search.must include(File.expand_path("fixtures/bundle", File.dirname(__FILE__)))
92 ClearCounter.source_paths_for_search.must_not include(File.expand_path("fixtures", File.dirname(__FILE__)))
30eae32 @josevalim Solved source_paths loadind issues.
josevalim authored
93 end
94
f33776b @josevalim Simplifying source paths implementation.
josevalim authored
95 it "customized source paths should be before source roots" do
96 ClearCounter.source_paths_for_search[0].must == File.expand_path("fixtures/doc", File.dirname(__FILE__))
97 ClearCounter.source_paths_for_search[1].must == File.expand_path("fixtures/bundle", File.dirname(__FILE__))
5894667 @josevalim Allow dynamic source roots to be cached on inheritance.
josevalim authored
98 end
99
f33776b @josevalim Simplifying source paths implementation.
josevalim authored
100 it "keeps inherited source paths at the end" do
101 ClearCounter.source_paths_for_search.last.must == File.expand_path("fixtures/broken", File.dirname(__FILE__))
30eae32 @josevalim Solved source_paths loadind issues.
josevalim authored
102 end
103 end
a87e6ac @josevalim More actions.
josevalim authored
104 end
1b475f5 @josevalim Added specs for source paths.
josevalim authored
105
106 describe "#find_in_source_paths" do
107 it "raises an error if source path is empty" do
108 lambda {
109 A.new.find_in_source_paths("foo")
110 }.must raise_error(Thor::Error, /You don't have any source path defined for class A/)
111 end
112
113 it "finds a template inside the source path" do
114 runner.find_in_source_paths("doc").must == File.expand_path("doc", source_root)
115 lambda { runner.find_in_source_paths("README") }.must raise_error
116
117 new_path = File.join(source_root, "doc")
c0bc615 @josevalim Allow source_paths to be manipulated in the instance level.
josevalim authored
118 runner.instance_variable_set(:@source_paths, nil)
119 runner.source_paths.unshift(new_path)
1b475f5 @josevalim Added specs for source paths.
josevalim authored
120 runner.find_in_source_paths("README").must == File.expand_path("README", new_path)
121 end
122 end
0fb761c @josevalim Added more behavior to copy file action and actions in general.
josevalim authored
123 end
79838a9 @josevalim Removed actions from Thor::Runner and added specs for inside.
josevalim authored
124
125 describe "#inside" do
126 it "executes the block inside the given folder" do
a87e6ac @josevalim More actions.
josevalim authored
127 runner.inside("foo") do
128 Dir.pwd.must == file
79838a9 @josevalim Removed actions from Thor::Runner and added specs for inside.
josevalim authored
129 end
130 end
131
87d11bf @josevalim When calling :inside, the root changes, so you can call templater method...
josevalim authored
132 it "changes the base root" do
996fdd3 @josevalim Everyone loves code refactoring.
josevalim authored
133 runner.inside("foo") do
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
134 runner.destination_root.must == file
87d11bf @josevalim When calling :inside, the root changes, so you can call templater method...
josevalim authored
135 end
136 end
137
79838a9 @josevalim Removed actions from Thor::Runner and added specs for inside.
josevalim authored
138 it "creates the directory if it does not exist" do
a87e6ac @josevalim More actions.
josevalim authored
139 runner.inside("foo") do
140 File.exists?(file).must be_true
79838a9 @josevalim Removed actions from Thor::Runner and added specs for inside.
josevalim authored
141 end
142 end
7470eaf @josevalim Added specs for run and moved root logic to actions (it's no included by...
josevalim authored
143
483c2ea @josevalim Another small actions refactoring.
josevalim authored
144 describe "when verbose" do
145 it "logs status" do
146 capture(:stdout) do
147 runner.inside("foo", :verbose => true) {}
148 end.must =~ /inside foo/
3880fe5 @josevalim Added invoke with pading action.
josevalim authored
149 end
150
483c2ea @josevalim Another small actions refactoring.
josevalim authored
151 it "uses padding in next status" do
152 capture(:stdout) do
153 runner.inside("foo", :verbose => true) do
154 runner.say_status :cool, :padding
155 end
156 end.must =~ /cool padding/
157 end
3880fe5 @josevalim Added invoke with pading action.
josevalim authored
158
483c2ea @josevalim Another small actions refactoring.
josevalim authored
159 it "removes padding after block" do
160 capture(:stdout) do
161 runner.inside("foo", :verbose => true) {}
162 runner.say_status :no, :padding
163 end.must =~ /no padding/
164 end
3880fe5 @josevalim Added invoke with pading action.
josevalim authored
165 end
166 end
167
7470eaf @josevalim Added specs for run and moved root logic to actions (it's no included by...
josevalim authored
168 describe "#in_root" do
169 it "executes the block in the root folder" do
1996cee @josevalim Small refactoring.
josevalim authored
170 runner.inside("foo") do
171 runner.in_root { Dir.pwd.must == destination_root }
7470eaf @josevalim Added specs for run and moved root logic to actions (it's no included by...
josevalim authored
172 end
173 end
87d11bf @josevalim When calling :inside, the root changes, so you can call templater method...
josevalim authored
174
175 it "changes the base root" do
1996cee @josevalim Small refactoring.
josevalim authored
176 runner.inside("foo") do
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
177 runner.in_root { runner.destination_root.must == destination_root }
87d11bf @josevalim When calling :inside, the root changes, so you can call templater method...
josevalim authored
178 end
179 end
180
181 it "returns to the previous state" do
1996cee @josevalim Small refactoring.
josevalim authored
182 runner.inside("foo") do
183 runner.in_root { }
c8217c8 @josevalim Replace root for destination_root.
josevalim authored
184 runner.destination_root.must == file
87d11bf @josevalim When calling :inside, the root changes, so you can call templater method...
josevalim authored
185 end
186 end
7470eaf @josevalim Added specs for run and moved root logic to actions (it's no included by...
josevalim authored
187 end
b081fcf @josevalim Added chmod action.
josevalim authored
188
daf4040 @josevalim Move apply from Rails Templates to Thor.
josevalim authored
189 describe "#apply" do
190 before(:each) do
191 @template = <<-TEMPLATE
192 @foo = "FOO"
193 say_status :cool, :padding
194 TEMPLATE
195 @template.instance_eval "def read; self; end" # Make the string respond to read
196
197 @file = "http://gist.github.com/103208.txt"
5e40038 Templates should send Accept: application/x-thor-template over HTTP
Carlhuda authored
198 runner.should_receive(:open).with(@file, "Accept" => "application/x-thor-template").and_return(@template)
daf4040 @josevalim Move apply from Rails Templates to Thor.
josevalim authored
199 end
200
201 it "opens a file and executes its content in the instance binding" do
202 action :apply, @file
203 runner.instance_variable_get("@foo").must == "FOO"
204 end
205
206 it "applies padding to the content inside the file" do
207 action(:apply, @file).must =~ /cool padding/
208 end
209
210 it "logs its status" do
211 action(:apply, @file).must =~ / apply #{@file}\n/
212 end
213
214 it "does not log status" do
215 content = action(:apply, @file, :verbose => false)
216 content.must =~ /cool padding/
217 content.must_not =~ /apply http/
218 end
219 end
220
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
221 describe "#run" do
222 before(:each) do
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
223 runner.should_receive(:`).with("ls")
b081fcf @josevalim Added chmod action.
josevalim authored
224 end
225
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
226 it "executes the command given" do
227 action :run, "ls"
b081fcf @josevalim Added chmod action.
josevalim authored
228 end
229
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
230 it "logs status" do
c8f09c3 @josevalim Add :with to run as official subinvocations API.
josevalim authored
231 action(:run, "ls").must == " run ls from \".\"\n"
b081fcf @josevalim Added chmod action.
josevalim authored
232 end
233
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
234 it "does not log status if required" do
235 action(:run, "ls", :verbose => false).must be_empty
236 end
3d5026a @josevalim Update actions to use the proper ruby command.
josevalim authored
237
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
238 it "accepts a color as status" do
f32bdbf @josevalim More work on changing from rr to spec mock framework
josevalim authored
239 runner.shell.should_receive(:say_status).with(:run, 'ls from "."', :yellow)
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
240 action :run, "ls", :verbose => :yellow
b081fcf @josevalim Added chmod action.
josevalim authored
241 end
242 end
191fd7e @josevalim Improving test coverage for invokable actions.
josevalim authored
243
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
244 describe "#run_ruby_script" do
b081fcf @josevalim Added chmod action.
josevalim authored
245 before(:each) do
f32bdbf @josevalim More work on changing from rr to spec mock framework
josevalim authored
246 Thor::Util.stub!(:ruby_command).and_return("/opt/jruby")
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
247 runner.should_receive(:`).with("/opt/jruby script.rb")
b081fcf @josevalim Added chmod action.
josevalim authored
248 end
249
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
250 it "executes the ruby script" do
251 action :run_ruby_script, "script.rb"
b081fcf @josevalim Added chmod action.
josevalim authored
252 end
253
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
254 it "logs status" do
c8f09c3 @josevalim Add :with to run as official subinvocations API.
josevalim authored
255 action(:run_ruby_script, "script.rb").must == " run jruby script.rb from \".\"\n"
b081fcf @josevalim Added chmod action.
josevalim authored
256 end
257
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
258 it "does not log status if required" do
259 action(:run_ruby_script, "script.rb", :verbose => false).must be_empty
5f747e1 @josevalim Added remove_file as action.
josevalim authored
260 end
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
261 end
5f747e1 @josevalim Added remove_file as action.
josevalim authored
262
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
263 describe "#thor" do
264 it "executes the thor command" do
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
265 runner.should_receive(:`).with("thor list")
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
266 action :thor, :list, :verbose => true
b081fcf @josevalim Added chmod action.
josevalim authored
267 end
268
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
269 it "converts extra arguments to command arguments" do
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
270 runner.should_receive(:`).with("thor list foo bar")
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
271 action :thor, :list, "foo", "bar"
b081fcf @josevalim Added chmod action.
josevalim authored
272 end
273
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
274 it "converts options hash to switches" do
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
275 runner.should_receive(:`).with("thor list foo bar --foo")
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
276 action :thor, :list, "foo", "bar", :foo => true
b081fcf @josevalim Added chmod action.
josevalim authored
277
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
278 runner.should_receive(:`).with("thor list --foo 1 2 3")
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
279 action :thor, :list, :foo => [1,2,3]
280 end
b081fcf @josevalim Added chmod action.
josevalim authored
281
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
282 it "logs status" do
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
283 runner.should_receive(:`).with("thor list")
c8f09c3 @josevalim Add :with to run as official subinvocations API.
josevalim authored
284 action(:thor, :list).must == " run thor list from \".\"\n"
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
285 end
b081fcf @josevalim Added chmod action.
josevalim authored
286
6a459b9 @josevalim Refactored actions files and their specs.
josevalim authored
287 it "does not log status if required" do
9e6e6e6 @josevalim run shuold return the result of the given command.
josevalim authored
288 runner.should_receive(:`).with("thor list --foo 1 2 3")
c8f09c3 @josevalim Add :with to run as official subinvocations API.
josevalim authored
289 action(:thor, :list, :foo => [1,2,3], :verbose => false).must be_empty
b081fcf @josevalim Added chmod action.
josevalim authored
290 end
291 end
0fb761c @josevalim Added more behavior to copy file action and actions in general.
josevalim authored
292 end
Something went wrong with that request. Please try again.