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

Expose ConfigurationOptions to Configuration #832

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rspec/core/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(options, configuration=RSpec::configuration, world=RSpec::world)
end
@options = options
@configuration = configuration
@configuration.load_options options.parse_options
@world = world
end

Expand Down
27 changes: 27 additions & 0 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def #{name}
CODE
end

# @private
def self.define_option_reader(name)
define_method("configured_option_#{name}") { option_for name }
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I always prefer define_method over eval'ing a string like this. Tenderlove recently wrote up a summary comparison of the two approaches including benchmarks:

http://tenderlovemaking.com/2013/03/03/dynamic_method_definitions.html

We haven't always followed this in the rspec code base, both because we've never really talked about it as a team (and I'm not sure that others share my preference for define_method) and also because we've been forced to use the eval approach on occasion due to the fact that 1.8.6 (and earlier) didn't allow a define_method-defined method to accept a block...but moving forward, I'd like to use define_method unless we have a specific reason not to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally, I'd be happy to switch it, but I was following the convention of the other methods.


# @private
def self.deprecate_alias_key
RSpec.warn_deprecation <<-MESSAGE
Expand Down Expand Up @@ -171,6 +176,17 @@ def self.add_setting(name, opts={})
# end
add_setting :treat_symbols_as_metadata_keys_with_true_values

# Allow access to the configuration options that RSpec was started with
define_option_reader :debug
define_option_reader :requires
define_option_reader :libs
define_option_reader :drb
define_option_reader :line_numbers
define_option_reader :full_backtrace
define_option_reader :profile
define_option_reader :files_or_directories_to_run
define_option_reader :full_description

# @private
add_setting :tty
# @private
Expand Down Expand Up @@ -212,6 +228,13 @@ def initialize
@fixed_color = :blue
@detail_color = :cyan
@profile_examples = false
@configuration_options = {}
end

# @private
# Used to load in configuration options
def load_options(options)
@configuration_options = options
end

# @private
Expand Down Expand Up @@ -962,6 +985,10 @@ def value_for(key, default=nil)
@preferred_options.has_key?(key) ? @preferred_options[key] : default
end

def option_for(key)
@configuration_options.fetch(key)
end

def assert_no_example_groups_defined(config_option)
if RSpec.world.example_groups.any?
raise MustBeConfiguredBeforeExampleGroupsError.new(
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/core/command_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ module RSpec::Core
expect(command_line.instance_eval { @options }).to be(config_options)
end

it "loads submitted ConfigurationOptions into @configuration" do
config_options = ConfigurationOptions.new(%w[--color])
config.should_receive(:load_options).with(config_options.parse_options)
CommandLine.new(config_options)
end

describe "#run" do
context "running files" do
include_context "spec files"
Expand Down
23 changes: 23 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1372,5 +1372,28 @@ def metadata_hash(*args)
expect(groups.ordered).to eq([4, 3, 2, 1])
end
end

describe 'accessing command line options' do
let(:options) do
{
:debug => 'debug', :requires => 'requires', :libs => 'libs', :profile => 'profile',
:drb => 'drb', :files_or_directories_to_run => 'files_or_directories_to_run',
:line_numbers => 'line_numbers', :full_description => 'full_description',
:full_backtrace => 'full_backtrace'
}
end
before do
config.load_options options
end
specify { expect(config.configured_option_debug).to eq 'debug' }
specify { expect(config.configured_option_requires).to eq 'requires' }
specify { expect(config.configured_option_libs).to eq 'libs' }
specify { expect(config.configured_option_profile).to eq 'profile' }
specify { expect(config.configured_option_drb).to eq 'drb' }
specify { expect(config.configured_option_files_or_directories_to_run).to eq 'files_or_directories_to_run' }
specify { expect(config.configured_option_line_numbers).to eq 'line_numbers' }
specify { expect(config.configured_option_full_description).to eq 'full_description' }
specify { expect(config.configured_option_full_backtrace).to eq 'full_backtrace' }
end
end
end