Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Restore --options CLI option from rspec-1
Browse files Browse the repository at this point in the history
- sets path to custom options file
- ignores ~/.rspec and ./.rspec
- Closes #228.
  • Loading branch information
dchelimsky committed Dec 29, 2010
1 parent d3456f3 commit d7e4033
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 168 deletions.
39 changes: 38 additions & 1 deletion features/configuration/read_options_from_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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:
"""
Expand Down
53 changes: 32 additions & 21 deletions lib/rspec/core/configuration_options.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'optparse'
# http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html
require 'optparse'

module RSpec
module Core
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/rspec/core/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit d7e4033

Please sign in to comment.