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