]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/spec/spec/runner/option_parser_spec.rb
8c91ad19ffe4ee692acf77f08e99b2754c679607
[rails.git] / vendor / gems / rspec-1.1.2 / spec / spec / runner / option_parser_spec.rb
1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
2 require 'fileutils'
3
4 describe "OptionParser" do
5   before(:each) do
6     @out = StringIO.new
7     @err = StringIO.new
8     @parser = Spec::Runner::OptionParser.new(@err, @out)
9   end
10
11   def parse(args)
12     @parser.parse(args)
13     @parser.options
14   end
15
16   it "should accept dry run option" do
17     options = parse(["--dry-run"])
18     options.dry_run.should be_true
19   end
20
21   it "should eval and use custom formatter when none of the builtins" do
22     options = parse(["--format", "Custom::Formatter"])
23     options.formatters[0].class.should be(Custom::Formatter)
24   end
25   
26   it "should support formatters with relative and absolute paths, even on windows" do
27     options = parse([
28       "--format", "Custom::Formatter:C:\\foo\\bar",
29       "--format", "Custom::Formatter:foo/bar",
30       "--format", "Custom::Formatter:foo\\bar",
31       "--format", "Custom::Formatter:/foo/bar"
32     ])
33     options.formatters[0].where.should eql("C:\\foo\\bar")
34     options.formatters[1].where.should eql("foo/bar")
35     options.formatters[2].where.should eql("foo\\bar")
36     options.formatters[3].where.should eql("/foo/bar")
37   end
38
39   it "should not be verbose by default" do
40     options = parse([])
41     options.verbose.should be_nil
42   end
43
44   it "should not use colour by default" do
45     options = parse([])
46     options.colour.should == false
47   end
48
49   it "should print help to stdout if no args" do
50     pending 'A regression since 1.0.8' do
51       options = parse([])
52       @out.rewind
53       @out.read.should match(/Usage: spec \(FILE\|DIRECTORY\|GLOB\)\+ \[options\]/m)
54     end
55   end
56
57   it "should print help to stdout" do
58     options = parse(["--help"])
59     @out.rewind
60     @out.read.should match(/Usage: spec \(FILE\|DIRECTORY\|GLOB\)\+ \[options\]/m)
61   end
62
63   it "should print instructions about how to require missing formatter" do
64     lambda do 
65       options = parse(["--format", "Custom::MissingFormatter"]) 
66       options.formatters
67     end.should raise_error(NameError)
68     @err.string.should match(/Couldn't find formatter class Custom::MissingFormatter/n)
69   end
70
71   it "should print version to stdout" do
72     options = parse(["--version"])
73     @out.rewind
74     @out.read.should match(/RSpec-\d+\.\d+\.\d+.*\(build \d+\) - BDD for Ruby\nhttp:\/\/rspec.rubyforge.org\/\n/n)
75   end
76   
77   it "should require file when require specified" do
78     lambda do
79       parse(["--require", "whatever"])
80     end.should raise_error(LoadError)
81   end
82
83   it "should support c option" do
84     options = parse(["-c"])
85     options.colour.should be_true
86   end
87
88   it "should support queens colour option" do
89     options = parse(["--colour"])
90     options.colour.should be_true
91   end
92
93   it "should support us color option" do
94     options = parse(["--color"])
95     options.colour.should be_true
96   end
97
98   it "should support single example with -e option" do
99     options = parse(["-e", "something or other"])
100     options.examples.should eql(["something or other"])
101   end
102
103   it "should support single example with -s option (will be removed when autotest supports -e)" do
104     options = parse(["-s", "something or other"])
105     options.examples.should eql(["something or other"])
106   end
107
108   it "should support single example with --example option" do
109     options = parse(["--example", "something or other"])
110     options.examples.should eql(["something or other"])
111   end
112
113   it "should read several example names from file if --example is given an existing file name" do
114     options = parse(["--example", File.dirname(__FILE__) + '/examples.txt'])
115     options.examples.should eql([
116       "Sir, if you were my husband, I would poison your drink.", 
117       "Madam, if you were my wife, I would drink it."])
118   end
119   
120   it "should read no examples if given an empty file" do
121     options = parse(["--example", File.dirname(__FILE__) + '/empty_file.txt'])
122     options.examples.should eql([])
123   end
124
125   it "should use html formatter when format is h" do
126     options = parse(["--format", "h"])
127     options.formatters[0].class.should equal(Spec::Runner::Formatter::HtmlFormatter)
128   end
129
130   it "should use html story formatter when format is h" do
131     options = parse(["--format", "h"])
132     options.story_formatters[0].class.should equal(Spec::Runner::Formatter::Story::HtmlFormatter)
133   end
134
135   it "should use html formatter when format is html" do
136     options = parse(["--format", "html"])
137     options.formatters[0].class.should equal(Spec::Runner::Formatter::HtmlFormatter)
138   end
139
140   it "should use html story formatter when format is html" do
141     options = parse(["--format", "html"])
142     options.story_formatters[0].class.should equal(Spec::Runner::Formatter::Story::HtmlFormatter)
143   end
144
145   it "should use html formatter with explicit output when format is html:test.html" do
146     FileUtils.rm 'test.html' if File.exist?('test.html')
147     options = parse(["--format", "html:test.html"])
148     options.formatters # creates the file
149     File.should be_exist('test.html')
150     options.formatters[0].class.should equal(Spec::Runner::Formatter::HtmlFormatter)
151     options.formatters[0].close
152     FileUtils.rm 'test.html'
153   end
154
155   it "should use noisy backtrace tweaker with b option" do
156     options = parse(["-b"])
157     options.backtrace_tweaker.should be_instance_of(Spec::Runner::NoisyBacktraceTweaker)
158   end
159
160   it "should use noisy backtrace tweaker with backtrace option" do
161     options = parse(["--backtrace"])
162     options.backtrace_tweaker.should be_instance_of(Spec::Runner::NoisyBacktraceTweaker)
163   end
164
165   it "should use quiet backtrace tweaker by default" do
166     options = parse([])
167     options.backtrace_tweaker.should be_instance_of(Spec::Runner::QuietBacktraceTweaker)
168   end
169
170   it "should use progress bar formatter by default" do
171     options = parse([])
172     options.formatters[0].class.should equal(Spec::Runner::Formatter::ProgressBarFormatter)
173   end
174
175   it "should use specdoc formatter when format is s" do
176     options = parse(["--format", "s"])
177     options.formatters[0].class.should equal(Spec::Runner::Formatter::SpecdocFormatter)
178   end
179
180   it "should use specdoc formatter when format is specdoc" do
181     options = parse(["--format", "specdoc"])
182     options.formatters[0].class.should equal(Spec::Runner::Formatter::SpecdocFormatter)
183   end
184
185   it "should support diff option when format is not specified" do
186     options = parse(["--diff"])
187     options.diff_format.should == :unified
188   end
189
190   it "should use unified diff format option when format is unified" do
191     options = parse(["--diff", "unified"])
192     options.diff_format.should == :unified
193     options.differ_class.should equal(Spec::Expectations::Differs::Default)
194   end
195
196   it "should use context diff format option when format is context" do
197     options = parse(["--diff", "context"])
198     options.diff_format.should == :context
199     options.differ_class.should == Spec::Expectations::Differs::Default
200   end
201
202   it "should use custom diff format option when format is a custom format" do
203     Spec::Expectations.differ.should_not be_instance_of(Custom::Differ)
204
205     options = parse(["--diff", "Custom::Differ"])
206     options.parse_diff "Custom::Differ"
207     options.diff_format.should == :custom
208     options.differ_class.should == Custom::Differ
209     Spec::Expectations.differ.should be_instance_of(Custom::Differ)
210   end
211
212   it "should print instructions about how to fix missing differ" do
213     lambda { parse(["--diff", "Custom::MissingFormatter"]) }.should raise_error(NameError)
214     @err.string.should match(/Couldn't find differ class Custom::MissingFormatter/n)
215   end
216
217   it "should support --line to identify spec" do
218     spec_parser = mock("spec_parser")
219     @parser.instance_variable_set('@spec_parser', spec_parser)
220
221     file_factory = mock("File")
222     file_factory.should_receive(:file?).and_return(true)
223     file_factory.should_receive(:open).and_return("fake_io")
224     @parser.instance_variable_set('@file_factory', file_factory)
225
226     spec_parser.should_receive(:spec_name_for).with("fake_io", 169).and_return("some spec")
227
228     options = parse(["some file", "--line", "169"])
229     options.examples.should eql(["some spec"])
230     File.rspec_verify
231   end
232
233   it "should fail with error message if file is dir along with --line" do
234     spec_parser = mock("spec_parser")
235     @parser.instance_variable_set('@spec_parser', spec_parser)
236
237     file_factory = mock("File")
238     file_factory.should_receive(:file?).and_return(false)
239     file_factory.should_receive(:directory?).and_return(true)
240     @parser.instance_variable_set('@file_factory', file_factory)
241
242     options = parse(["some file", "--line", "169"])
243     @err.string.should match(/You must specify one file, not a directory when using the --line option/n)
244   end
245
246   it "should fail with error message if file does not exist along with --line" do
247     spec_parser = mock("spec_parser")
248     @parser.instance_variable_set('@spec_parser', spec_parser)
249
250     file_factory = mock("File")
251     file_factory.should_receive(:file?).and_return(false)
252     file_factory.should_receive(:directory?).and_return(false)
253     @parser.instance_variable_set('@file_factory', file_factory)
254
255     options = parse(["some file", "--line", "169"])
256     @err.string.should match(/some file does not exist/n)
257   end
258
259   it "should fail with error message if more than one files are specified along with --line" do
260     spec_parser = mock("spec_parser")
261     @parser.instance_variable_set('@spec_parser', spec_parser)
262
263     options = parse(["some file", "some other file", "--line", "169"])
264     @err.string.should match(/Only one file can be specified when using the --line option/n)
265   end
266
267   it "should fail with error message if --example and --line are used simultaneously" do
268     spec_parser = mock("spec_parser")
269     @parser.instance_variable_set('@spec_parser', spec_parser)
270
271     options = parse(["some file", "--example", "some example", "--line", "169"])
272     @err.string.should match(/You cannot use both --line and --example/n)
273   end
274
275   if [/mswin/, /java/].detect{|p| p =~ RUBY_PLATFORM}
276     it "should barf when --heckle is specified (and platform is windows)" do
277       lambda do
278         options = parse(["--heckle", "Spec"])
279       end.should raise_error(StandardError, "Heckle not supported on Windows")
280     end
281   else
282     it "should heckle when --heckle is specified (and platform is not windows)" do
283       options = parse(["--heckle", "Spec"])
284       options.heckle_runner.should be_instance_of(Spec::Runner::HeckleRunner)
285     end
286   end
287
288   it "should read options from file when --options is specified" do
289     options = parse(["--options", File.dirname(__FILE__) + "/spec.opts"])
290     options.diff_format.should_not be_nil
291     options.colour.should be_true
292   end
293
294   it "should default the formatter to ProgressBarFormatter when using options file" do
295     options = parse(["--options", File.dirname(__FILE__) + "/spec.opts"])
296     options.formatters.first.should be_instance_of(::Spec::Runner::Formatter::ProgressBarFormatter)
297   end
298
299   it "should read spaced and multi-line options from file when --options is specified" do
300     options = parse(["--options", File.dirname(__FILE__) + "/spec_spaced.opts"])
301     options.diff_format.should_not be_nil
302     options.colour.should be_true
303     options.formatters.first.should be_instance_of(::Spec::Runner::Formatter::SpecdocFormatter)
304   end
305    
306   it "should save config to file when --generate-options is specified" do
307     FileUtils.rm 'test.spec.opts' if File.exist?('test.spec.opts')
308     options = parse(["--colour", "--generate-options", "test.spec.opts", "--diff"])
309     IO.read('test.spec.opts').should == "--colour\n--diff\n"
310     FileUtils.rm 'test.spec.opts'
311   end
312
313   it "should save config to file when -G is specified" do
314     FileUtils.rm 'test.spec.opts' if File.exist?('test.spec.opts')
315     options = parse(["--colour", "-G", "test.spec.opts", "--diff"])
316     IO.read('test.spec.opts').should == "--colour\n--diff\n"
317     FileUtils.rm 'test.spec.opts'
318   end
319
320   it "when --drb is specified, calls DrbCommandLine all of the other ARGV arguments" do
321     options = Spec::Runner::OptionParser.parse([
322       "some/spec.rb", "--diff", "--colour"
323     ], @err, @out)
324     Spec::Runner::DrbCommandLine.should_receive(:run).and_return do |options|
325       options.argv.should == ["some/spec.rb", "--diff", "--colour"]
326     end
327     parse(["some/spec.rb", "--diff", "--drb", "--colour"])
328   end
329   
330   it "should reverse spec order when --reverse is specified" do
331     options = parse(["some/spec.rb", "--reverse"])
332   end
333
334   it "should set an mtime comparator when --loadby mtime" do
335     options = parse(["--loadby", 'mtime'])
336     runner = Spec::Runner::ExampleGroupRunner.new(options)
337     Spec::Runner::ExampleGroupRunner.should_receive(:new).
338       with(options).
339       and_return(runner)
340     runner.should_receive(:load_files).with(["most_recent_spec.rb", "command_line_spec.rb"])
341
342     Dir.chdir(File.dirname(__FILE__)) do
343       options.files << 'command_line_spec.rb'
344       options.files << 'most_recent_spec.rb'
345       FileUtils.touch "most_recent_spec.rb"
346       options.run_examples
347       FileUtils.rm "most_recent_spec.rb"
348     end
349   end
350
351   it "should use the standard runner by default" do
352     runner = ::Spec::Runner::ExampleGroupRunner.new(@parser.options)
353     ::Spec::Runner::ExampleGroupRunner.should_receive(:new).
354       with(@parser.options).
355       and_return(runner)
356     options = parse([])
357     options.run_examples
358   end
359
360   it "should use a custom runner when given" do
361     runner = Custom::ExampleGroupRunner.new(@parser.options, nil)
362     Custom::ExampleGroupRunner.should_receive(:new).
363       with(@parser.options, nil).
364       and_return(runner)
365     options = parse(["--runner", "Custom::ExampleGroupRunner"])
366     options.run_examples
367   end
368
369   it "should use a custom runner with extra options" do
370     runner = Custom::ExampleGroupRunner.new(@parser.options, 'something')
371     Custom::ExampleGroupRunner.should_receive(:new).
372       with(@parser.options, 'something').
373       and_return(runner)
374     options = parse(["--runner", "Custom::ExampleGroupRunner:something"])
375     options.run_examples
376   end
377 end