1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
3 describe "OptionParser" do
7 @parser = Spec::Runner::OptionParser.new(@err, @out)
15 it "should accept dry run option" do
16 options = parse(["--dry-run"])
17 options.dry_run.should be_true
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)
25 it "should support formatters with relative and absolute paths, even on windows" do
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"
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")
38 it "should not be verbose by default" do
40 options.verbose.should be_nil
43 it "should not use colour by default" do
45 options.colour.should == false
48 it "should print help to stdout if no args" do
49 pending 'A regression since 1.0.8' do
52 @out.read.should match(/Usage: spec \(FILE\|DIRECTORY\|GLOB\)\+ \[options\]/m)
56 it "should print help to stdout" do
57 options = parse(["--help"])
59 @out.read.should match(/Usage: spec \(FILE\|DIRECTORY\|GLOB\)\+ \[options\]/m)
62 it "should print instructions about how to require missing formatter" do
64 options = parse(["--format", "Custom::MissingFormatter"])
66 end.should raise_error(NameError)
67 @err.string.should match(/Couldn't find formatter class Custom::MissingFormatter/n)
70 it "should print version to stdout" do
71 options = parse(["--version"])
73 @out.read.should match(/RSpec-\d+\.\d+\.\d+.*\(r\d+\) - BDD for Ruby\nhttp:\/\/rspec.rubyforge.org\/\n/n)
76 it "should require file when require specified" do
78 parse(["--require", "whatever"])
79 end.should raise_error(LoadError)
82 it "should support c option" do
83 options = parse(["-c"])
84 options.colour.should be_true
87 it "should support queens colour option" do
88 options = parse(["--colour"])
89 options.colour.should be_true
92 it "should support us color option" do
93 options = parse(["--color"])
94 options.colour.should be_true
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"])
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"])
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"])
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."])
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([])
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)
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)
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'
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)
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)
154 it "should use quiet backtrace tweaker by default" do
156 options.backtrace_tweaker.should be_instance_of(Spec::Runner::QuietBacktraceTweaker)
159 it "should use progress bar formatter by default" do
161 options.formatters[0].class.should equal(Spec::Runner::Formatter::ProgressBarFormatter)
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)
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)
174 it "should support diff option when format is not specified" do
175 options = parse(["--diff"])
176 options.diff_format.should == :unified
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)
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
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)
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)
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"])
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)
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)
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)
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)
264 if [/mswin/, /java/].detect{|p| p =~ RUBY_PLATFORM}
265 it "should barf when --heckle is specified (and platform is windows)" do
267 options = parse(["--heckle", "Spec"])
268 end.should raise_error(StandardError, "Heckle not supported on Windows")
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)
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
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)
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)
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'
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'
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"
313 Spec::Runner::DrbCommandLine.should_receive(:run).and_return do |options|
314 options.argv.should == ["some/spec.rb", "--diff", "--colour"]
316 parse(["some/spec.rb", "--diff", "--drb", "--colour"])
319 it "should reverse spec order when --reverse is specified" do
320 options = parse(["some/spec.rb", "--reverse"])
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).
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"
336 FileUtils.rm "most_recent_spec.rb"
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).
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).
354 options = parse(["--runner", "Custom::ExampleGroupRunner"])
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').
363 options = parse(["--runner", "Custom::ExampleGroupRunner:something"])