Skip to content

Commit

Permalink
Opposite filters (tags) with higher precedence cancel out those with …
Browse files Browse the repository at this point in the history
…lower precedence

Beginning to address rspec#369 and rspec#327.

This commit deals with command line, ENV and file options, but does not
address options defined with RSpec.configure. Once that's addressed we
can close rspec#369 and rspec#327.

This does, implicitly, let --tag options set in rake tasks overwrite
those in option files.
  • Loading branch information
dchelimsky committed Oct 29, 2011
1 parent b2da24d commit 7ec4903
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/rspec/core/configuration_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def configure(config)
end

def parse_options
@options ||= [file_options, command_line_options, env_options].inject {|merged, o| merged.merge o}
@options ||= [*file_options, command_line_options, env_options].inject do |merged, pending|
resolve_opposite_filters(merged, pending, :inclusion_filter, :exclusion_filter)
resolve_opposite_filters(merged, pending, :exclusion_filter, :inclusion_filter)
merged.merge(pending)
end
end

def drb_argv
Expand All @@ -33,6 +37,16 @@ def drb_argv

private

def resolve_opposite_filters(current,pending,filter,opposite_filter)
if current[filter] && pending[opposite_filter]
current[filter].each_pair do |key, value|
if pending[opposite_filter][key] == value
current[filter].delete(key)
end
end
end
end

def order(keys, *ordered)
ordered.reverse.each do |key|
keys.unshift(key) if keys.delete(key)
Expand All @@ -41,7 +55,7 @@ def order(keys, *ordered)
end

def file_options
custom_options_file ? custom_options : global_options.merge(local_options)
custom_options_file ? [custom_options] : [global_options, local_options]
end

def env_options
Expand Down
1 change: 0 additions & 1 deletion lib/rspec/core/shared_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def shared_context(*args, &block)
host.class_eval(&block)
end
RSpec.configuration.extend(mod, *args)
else
end
end

Expand Down
24 changes: 24 additions & 0 deletions spec/rspec/core/configuration_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,30 @@
parse_options[:formatters].should eq([['local']])
end

it "overwrites opposite tags according to precedence (inclusion overwrites exclusion)" do
File.open("./.rspec", "w") {|f| f << "--tag foo"}
File.open("~/.rspec", "w") {|f| f << "--tag ~foo"}
parsed = parse_options
parsed[:inclusion_filter].should eq(:foo => true)
parsed[:exclusion_filter].should be_empty
end

it "does not overwrite opposite tags with different values" do
File.open("./.rspec", "w") {|f| f << "--tag foo:baa"}
File.open("~/.rspec", "w") {|f| f << "--tag ~foo:brr"}
parsed = parse_options
parsed[:inclusion_filter].should eq(:foo => 'baa')
parsed[:exclusion_filter].should eq(:foo => 'brr')
end

it "overwrites opposite tags according to precedence (exclusion overwrites inclusion)" do
File.open("./.rspec", "w") {|f| f << "--tag ~foo"}
File.open("~/.rspec", "w") {|f| f << "--tag foo"}
parsed = parse_options
parsed[:inclusion_filter].should be_empty
parsed[:exclusion_filter].should eq(:foo => true)
end

context "with custom options file" do
it "ignores local and global options files" do
File.open("./.rspec", "w") {|f| f << "--format local"}
Expand Down

0 comments on commit 7ec4903

Please sign in to comment.