1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
4 describe "OptionParser" do
8 @parser = Spec::Runner::OptionParser.new(@err, @out)
16 it "should accept dry run option" do
17 options = parse(["--dry-run"])
18 options.dry_run.should be_true
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)
26 it "should support formatters with relative and absolute paths, even on windows" do
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"
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")
39 it "should not be verbose by default" do
41 options.verbose.should be_nil
44 it "should not use colour by default" do
46 options.colour.should == false
49 it "should print help to stdout if no args" do
50 pending 'A regression since 1.0.8' do
53 @out.read.should match(/Usage: spec \(FILE\|DIRECTORY\|GLOB\)\+ \[options\]/m)
57 it "should print help to stdout" do
58 options = parse(["--help"])
60 @out.read.should match(/Usage: spec \(FILE\|DIRECTORY\|GLOB\)\+ \[options\]/m)
63 it "should print instructions about how to require missing formatter" do
65 options = parse(["--format", "Custom::MissingFormatter"])
67 end.should raise_error(NameError)
68 @err.string.should match(/Couldn't find formatter class Custom::MissingFormatter/n)
71 it "should print version to stdout" do
72 options = parse(["--version"])
74 @out.read.should match(/RSpec-\d+\.\d+\.\d+.*\(build \d+\) - BDD for Ruby\nhttp:\/\/rspec.rubyforge.org\/\n/n)
77 it "should require file when require specified" do
79 parse(["--require", "whatever"])
80 end.should raise_error(LoadError)
83 it "should support c option" do
84 options = parse(["-c"])
85 options.colour.should be_true
88 it "should support queens colour option" do
89 options = parse(["--colour"])
90 options.colour.should be_true
93 it "should support us color option" do
94 options = parse(["--color"])
95 options.colour.should be_true
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"])
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"])
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"])
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."])
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([])
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)
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)
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)
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)
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'
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)
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)
165 it "should use quiet backtrace tweaker by default" do
167 options.backtrace_tweaker.should be_instance_of(Spec::Runner::QuietBacktraceTweaker)
170 it "should use progress bar formatter by default" do
172 options.formatters[0].class.should equal(Spec::Runner::Formatter::ProgressBarFormatter)
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)
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)
185 it "should support diff option when format is not specified" do
186 options = parse(["--diff"])
187 options.diff_format.should == :unified
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)
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
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)
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)
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)
217 it "should support --line to identify spec" do
218 spec_parser = mock("spec_parser")
219 @parser.instance_variable_set('@spec_parser', spec_parser)
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)
226 spec_parser.should_receive(:spec_name_for).with("fake_io", 169).and_return("some spec")
228 options = parse(["some file", "--line", "169"])
229 options.examples.should eql(["some spec"])
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)
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)
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)
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)
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)
255 options = parse(["some file", "--line", "169"])
256 @err.string.should match(/some file does not exist/n)
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)
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)
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)
271 options = parse(["some file", "--example", "some example", "--line", "169"])
272 @err.string.should match(/You cannot use both --line and --example/n)
275 if [/mswin/, /java/].detect{|p| p =~ RUBY_PLATFORM}
276 it "should barf when --heckle is specified (and platform is windows)" do
278 options = parse(["--heckle", "Spec"])
279 end.should raise_error(StandardError, "Heckle not supported on Windows")
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)
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
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)
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)
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'
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'
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"
324 Spec::Runner::DrbCommandLine.should_receive(:run).and_return do |options|
325 options.argv.should == ["some/spec.rb", "--diff", "--colour"]
327 parse(["some/spec.rb", "--diff", "--drb", "--colour"])
330 it "should reverse spec order when --reverse is specified" do
331 options = parse(["some/spec.rb", "--reverse"])
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).
340 runner.should_receive(:load_files).with(["most_recent_spec.rb", "command_line_spec.rb"])
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"
347 FileUtils.rm "most_recent_spec.rb"
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).
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).
365 options = parse(["--runner", "Custom::ExampleGroupRunner"])
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').
374 options = parse(["--runner", "Custom::ExampleGroupRunner:something"])