Skip to content

Commit

Permalink
(Almost) all command line args can now override RSpec.configure
Browse files Browse the repository at this point in the history
- Closes #369.
  • Loading branch information
dchelimsky committed Nov 4, 2011
1 parent 62f8c43 commit 2de55b8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -8,6 +8,7 @@
* equivalent of `--order rand:SEED`
* SharedContext supports `let` (David Chelimsky)
* Override opposing tags from the command line (David Chelimsky)
* Override RSpec.configure tags from the command line (David Chelimsky)
* Eliminate Ruby warnings (Matijs van Zuijlen)
* Make reporter.report an API (David Chelimsky)
* supports extension tools like interative_rspec
Expand Down
7 changes: 6 additions & 1 deletion lib/rspec/core/configuration.rb
Expand Up @@ -40,7 +40,12 @@ def self.add_setting(name, opts={})
alias_method "#{name}=", "#{opts[:alias]}="
define_predicate_for name
else
attr_accessor name
attr_writer name
eval <<-CODE
def #{name}
value_for(#{name.inspect}, instance_variable_get("@#{name}"))
end
CODE
define_predicate_for name
end
if opts[:alias_with]
Expand Down
14 changes: 7 additions & 7 deletions lib/rspec/core/configuration_options.rb
Expand Up @@ -13,13 +13,7 @@ def configure(config)
formatters = options.delete(:formatters)

order(options.keys, :libs, :requires, :default_path, :pattern).each do |key|
# temp to get through refactoring - eventually all options will be
# set using force
if [:color, :inclusion_filter, :exclusion_filter].include? key
config.force key => options[key]
else
config.send("#{key}=", options[key]) if config.respond_to?("#{key}=")
end
force?(key) ? config.force(key => options[key]) : config.send("#{key}=", options[key])
end

formatters.each {|pair| config.add_formatter(*pair) } if formatters
Expand All @@ -39,6 +33,12 @@ def drb_argv

private

NON_FORCED_OPTIONS = [:requires, :libs, :files_or_directories_to_run]

def force?(key)
!NON_FORCED_OPTIONS.include?(key)
end

def order(keys, *ordered)
ordered.reverse.each do |key|
keys.unshift(key) if keys.delete(key)
Expand Down
2 changes: 2 additions & 0 deletions spec/rspec/core/command_line_spec.rb
Expand Up @@ -95,6 +95,8 @@ def config_options(argv=[])
# this is necessary to ensure that color works correctly on windows
config.should_receive(:error_stream=).ordered
config.should_receive(:output_stream=).ordered
config.should_receive(:force).with(:default_path => anything).ordered
config.should_receive(:force).with(:order => anything).ordered
config.should_receive(:force).with(:exclusion_filter => anything).ordered
config.should_receive(:force).with(:color => true).ordered
command_line.run(err, out) rescue nil
Expand Down
42 changes: 27 additions & 15 deletions spec/rspec/core/configuration_options_spec.rb
Expand Up @@ -35,15 +35,15 @@
it "sends default_path before files_or_directories_to_run" do
opts = config_options_object(*%w[--default_path spec])
config = double("config").as_null_object
config.should_receive(:default_path=).ordered
config.should_receive(:force).with(:default_path => 'spec').ordered
config.should_receive(:files_or_directories_to_run=).ordered
opts.configure(config)
end

it "sends pattern before files_or_directories_to_run" do
opts = config_options_object(*%w[--pattern **/*.spec])
config = double("config").as_null_object
config.should_receive(:pattern=).ordered
config.should_receive(:force).with(:pattern => '**/*.spec').ordered
config.should_receive(:files_or_directories_to_run=).ordered
opts.configure(config)
end
Expand Down Expand Up @@ -76,18 +76,30 @@
opts.configure(config)
end

it "sets full_description" do
opts = config_options_object("--example", "this and that")
config = RSpec::Core::Configuration.new
config.should_receive(:full_description=).with(/this\ and\ that/)
opts.configure(config)
end

it "sets line_numbers" do
opts = config_options_object(*%w[--line_number 37])
config = RSpec::Core::Configuration.new
config.should_receive(:line_numbers=).with(["37"])
opts.configure(config)
[
["--failure-exit-code", "3", :failure_exit_code, 3 ],
["--pattern", "foo/bar", :pattern, "foo/bar"],
["--example", "this and that", :full_description, /this\ and\ that/],
["--failure-exit-code", "37", :failure_exit_code, 37],
["--default_path", "behavior", :default_path, "behavior"],
["--order", "mtime", :order, "mtime"],
["--drb", nil, :drb, true],
["--drb-port", "37", :drb_port, 37],
["--line_number", "42", :line_numbers, ["42"]],
["--seed", "37", :order, "rand:37"],
["--backtrace", nil, :full_backtrace, true],
["--profile", nil, :profile_examples, true],
["--tty", nil, :tty, true]
].each do |cli_option, cli_value, config_key, config_value|
it "forces #{config_key}" do
opts = config_options_object(*[cli_option, cli_value].compact)
config = RSpec::Core::Configuration.new
config.should_receive(:force) do |pair|
pair.keys.first.should eq(config_key)
pair.values.first.should eq(config_value)
end
opts.configure(config)
end
end
end

Expand Down Expand Up @@ -288,7 +300,7 @@
describe "default_path" do
it "gets set before files_or_directories_to_run" do
config = double("config").as_null_object
config.should_receive(:default_path=).ordered
config.should_receive(:force).with(:default_path => 'foo').ordered
config.should_receive(:files_or_directories_to_run=).ordered
opts = config_options_object("--default_path", "foo")
opts.configure(config)
Expand Down

0 comments on commit 2de55b8

Please sign in to comment.