Skip to content

Commit

Permalink
Fix regexp for filtering out non-root results being set too early
Browse files Browse the repository at this point in the history
If user configures a custom `SimpleCov.root`, results outside of the new
root wouldn't be properly filtered out since the regexp for filtering
was being set at require time, before the `SimpleCov.root` configuration
had been applied.

Instead, lazily set this regexp, so that by the time it is used, the new
`SimpleCov.root` value is already setup.
  • Loading branch information
deivid-rodriguez committed Jun 21, 2020
1 parent 311d9db commit 1e72f52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/simplecov/useless_results_remover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module SimpleCov
# Select the files that related to working scope directory of SimpleCov
#
module UselessResultsRemover
ROOT_REGX = /\A#{Regexp.escape(SimpleCov.root + File::SEPARATOR)}/io.freeze

def self.call(coverage_result)
coverage_result.select do |path, _coverage|
path =~ ROOT_REGX
path =~ root_regx
end
end

def self.root_regx
@root_regx ||= /\A#{Regexp.escape(SimpleCov.root + File::SEPARATOR)}/i.freeze
end
end
end
21 changes: 21 additions & 0 deletions spec/useless_results_remover_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,25 @@
expect(subject).to have_key(source_path)
expect(subject[source_path]["lines"]).to be_kind_of(Array)
end

context "when changing coverage root" do
around do |example|
begin
previous_root_regx = SimpleCov::UselessResultsRemover.instance_variable_get(:@root_regx)
SimpleCov::UselessResultsRemover.instance_variable_set(:@root_regx, nil)
previous_root = SimpleCov.root

SimpleCov.root "lib"

example.run
ensure
SimpleCov.root previous_root
SimpleCov::UselessResultsRemover.instance_variable_set(:@root_regx, previous_root_regx)
end
end

it "ignores stuff outside of the new coverage root" do
expect(subject).to be_empty
end
end
end

0 comments on commit 1e72f52

Please sign in to comment.