Permalink
Browse files

Load spec/spec.opts automatcally if present

  • Loading branch information...
1 parent ae241a9 commit 98a71355e26c94f783a61ba656ef548af21c599d @dchelimsky dchelimsky committed Feb 7, 2010
@@ -0,0 +1,76 @@
+Feature: spec/spec.opts
+
+ For backwards compatibility with rspec-1, you can write command
+ line options in a spec/spec.opts file and it will be loaded
+ automatically.
+
+ Options declared in spec/spec.opts will override configuration
+ set up in Rspec::Core.configure blocks.
+
+ Background:
+ Given a directory named "spec"
+
+ Scenario: color set in Rspec::Core.configure
+ Given a file named "spec/spec_helper.rb" with:
+ """
+ Rspec::Core.configure do |c|
+ c.color_enabled = true
+ end
+ """
+ And a file named "spec/example_spec.rb" with:
+ """
+ require "spec_helper"
+ describe "color_enabled" do
+ context "when set with Rspec::Core.configure" do
+ it "is true" do
+ Rspec::Core.configuration.color_enabled?.should be_true
+ end
+ end
+ end
+ """
+ When I run "rspec spec/example_spec.rb"
+ Then the stdout should match "1 example, 0 failures"
+
+ Scenario: color set in spec/spec.opts
+ Given a file named "spec/spec.opts" with:
+ """
+ --color
+ """
+ And a file named "spec/example_spec.rb" with:
+ """
+ describe "color_enabled" do
+ context "when set with Rspec::Core.configure" do
+ it "is true" do
+ Rspec::Core.configuration.color_enabled?.should be_true
+ end
+ end
+ end
+ """
+ When I run "rspec spec/example_spec.rb"
+ Then the stdout should match "1 example, 0 failures"
+
+ Scenario: formatter set in both (spec.opts wins)
+ Given a file named "spec/spec.opts" with:
+ """
+ --formatter documentation
+ """
+
+ And a file named "spec/spec_helper.rb" with:
+ """
+ Rspec::Core.configure do |c|
+ c.formatter = 'pretty'
+ end
+ """
+ And a file named "spec/example_spec.rb" with:
+ """
+ describe "formatter" do
+ context "when set with Rspec::Core.configure and in spec.opts" do
+ it "takes the value set in spec.opts" do
+ Rspec::Core.configuration.formatter.should be_an(Rspec::Core::Formatters::DocumentationFormatter)
+ end
+ end
+ end
+ """
+ When I run "rspec spec/example_spec.rb"
+ Then the stdout should match "1 example, 0 failures"
+
@@ -64,10 +64,9 @@ def apply(config)
# 1) option file, cli options, rspec core configure
# TODO: Add options_file to configuration
# TODO: Store command line options for reference
- if options_file = options.delete(:options_file)
- merged_options = parse_spec_file_contents(options_file).merge!(options)
- options.replace merged_options
- end
+ options_file = options.delete(:options_file) || DEFAULT_OPTIONS_FILE
+ merged_options = parse_spec_file_contents(options_file).merge!(options)
+ options.replace merged_options
options.each do |key, value|
config.send("#{key}=", value)
@@ -78,7 +77,7 @@ def apply(config)
def parse_spec_file_contents(options_file)
return {} unless File.exist?(options_file)
- spec_file_contents = File.readlines(options_file)
+ spec_file_contents = File.readlines(options_file).map {|l| l.split}.flatten
self.class.new(spec_file_contents).parse.options
end
@@ -62,6 +62,24 @@ def options_from_args(*args)
options_from_args('-o').should include(:options_file => "spec/spec.opts")
options_from_args('--options').should include(:options_file => "spec/spec.opts")
end
+
+ it "loads automatically" do
+ cli_options = Rspec::Core::CommandLineOptions.new([]).parse
+ File.stub(:exist?) { true }
+ File.stub(:readlines) { ["--formatter", "doc"] }
+ config = OpenStruct.new
+ cli_options.apply(config)
+ config.formatter.should == 'doc'
+ end
+
+ it "allows options on one line" do
+ cli_options = Rspec::Core::CommandLineOptions.new([]).parse
+ File.stub(:exist?) { true }
+ File.stub(:readlines) { ["--formatter doc"] }
+ config = OpenStruct.new
+ cli_options.apply(config)
+ config.formatter.should == 'doc'
+ end
it "merges options from the CLI and file options gracefully" do
cli_options = Rspec::Core::CommandLineOptions.new(['--formatter', 'progress', '--options', 'spec/spec.opts']).parse

0 comments on commit 98a7135

Please sign in to comment.