Permalink
Browse files

Restore --options CLI option from rspec-1

- sets path to custom options file
- ignores ~/.rspec and ./.rspec
- Closes #228.
  • Loading branch information...
1 parent d3456f3 commit d7e40339e9b329c661f9326407aea94c705dc223 @dchelimsky dchelimsky committed Dec 29, 2010
@@ -8,7 +8,7 @@ Feature: read command line configuration options from files
Options declared in the local file override those in the global file, while
those declared in RSpec.configure will override any ".rspec" file.
-
+
Scenario: color set in .rspec
Given a file named ".rspec" with:
"""
@@ -33,6 +33,43 @@ Feature: read command line configuration options from files
When I run "rspec ./spec/example_spec.rb"
Then the output should contain "1 example, 0 failures"
+ Scenario: custom options file
+ Given a file named "my.options" with:
+ """
+ --format documentation
+ """
+ And a file named "spec/example_spec.rb" with:
+ """
+ describe "formatter set in custom options file" do
+ it "sets formatter" do
+ RSpec.configuration.formatter.
+ should be_a(RSpec::Core::Formatters::DocumentationFormatter)
+ end
+ end
+ """
+ When I run "rspec spec/example_spec.rb --options my.options"
+ Then the output should contain "1 example, 0 failures"
+
+ Scenario: RSpec ignores ./.rspec when custom options file is used
+ Given a file named "my.options" with:
+ """
+ --format documentation
+ """
+ And a file named ".rspec" with:
+ """
+ --color
+ """
+ And a file named "spec/example_spec.rb" with:
+ """
+ describe "custom options file" do
+ it "causes .rspec to be ignored" do
+ RSpec.configuration.color_enabled.should be_false
+ end
+ end
+ """
+ When I run "rspec spec/example_spec.rb --options my.options"
+ Then the output should contain "1 example, 0 failures"
+
Scenario: formatter set in RSpec.configure overrides .rspec
Given a file named ".rspec" with:
"""
@@ -1,5 +1,5 @@
-require 'optparse'
# http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html
+require 'optparse'
module RSpec
module Core
@@ -19,7 +19,7 @@ def configure(config)
keys.unshift(:requires) if keys.delete(:requires)
keys.unshift(:libs) if keys.delete(:libs)
keys.each do |key|
- config.send("#{key}=", options[key])
+ config.send("#{key}=", options[key]) if config.respond_to?("#{key}=")
end
end
@@ -32,6 +32,7 @@ def drb_argv
argv << "--fail-fast" if options[:fail_fast]
argv << "--format" << options[:formatter] if options[:formatter]
argv << "--line_number" << options[:line_number] if options[:line_number]
+ argv << "--options" << options[:custom_options_file] if options[:custom_options_file]
argv << "--example" << options[:full_description].source if options[:full_description]
(options[:libs] || []).each do |path|
argv << "-I" << path
@@ -44,41 +45,52 @@ def drb_argv
def parse_options
@options = begin
- command_line_options = parse_command_line_options
- local_options = parse_local_options(command_line_options)
- global_options = parse_global_options
- env_options = parse_env_options
+ options_to_merge = []
+ if custom_options_file
+ options_to_merge << custom_options
+ else
+ options_to_merge << global_options
+ options_to_merge << local_options
+ end
+ options_to_merge << env_options
+ options_to_merge << command_line_options
- [global_options, local_options, command_line_options, env_options].inject do |merged, options|
+ options_to_merge.inject do |merged, options|
merged.merge(options)
end
end
end
private
- def parse_env_options
+ def env_options
ENV["SPEC_OPTS"] ? Parser.parse!(ENV["SPEC_OPTS"].split) : {}
end
- def parse_command_line_options
- options = Parser.parse!(@args)
- options[:files_or_directories_to_run] = @args
- options
+ def command_line_options
+ @command_line_options ||= begin
+ options = Parser.parse!(@args)
+ options[:files_or_directories_to_run] = @args
+ options
+ end
end
- def parse_local_options(options)
- parse_options_file(local_options_file(options))
+ def custom_options
+ options_from(custom_options_file)
end
- def parse_global_options
- parse_options_file(GLOBAL_OPTIONS_FILE)
+ def local_options
+ @local_options ||= options_from(LOCAL_OPTIONS_FILE)
end
- def parse_options_file(path)
+ def global_options
+ @global_options ||= options_from(GLOBAL_OPTIONS_FILE)
+ end
+
+ def options_from(path)
Parser.parse(args_from_options_file(path))
end
-
+
def args_from_options_file(path)
return [] unless File.exist?(path)
config_string = options_file_as_erb_string(path)
@@ -90,9 +102,8 @@ def options_file_as_erb_string(path)
ERB.new(IO.read(path)).result(binding)
end
- def local_options_file(options)
- return options[:options_file] if options[:options_file]
- LOCAL_OPTIONS_FILE
+ def custom_options_file
+ command_line_options[:custom_options_file]
end
end
end
@@ -69,6 +69,10 @@ def parser(options)
options[:line_number] = o
end
+ parser.on('-O', '--options PATH', 'Specify the path to an options file') do |path|
+ options[:custom_options_file] = path
+ end
+
parser.on('-p', '--profile', 'Enable profiling of examples with output of the top 10 slowest examples') do |o|
options[:profile_examples] = o
end
Oops, something went wrong.

0 comments on commit d7e4033

Please sign in to comment.