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