From 1e72f5281c8dec37ca713c1ee35d53ee17afd801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 19 Jun 2020 11:22:35 +0200 Subject: [PATCH] Fix regexp for filtering out non-root results being set too early 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. --- lib/simplecov/useless_results_remover.rb | 8 +++++--- spec/useless_results_remover_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/simplecov/useless_results_remover.rb b/lib/simplecov/useless_results_remover.rb index 464576bb..5d45604a 100644 --- a/lib/simplecov/useless_results_remover.rb +++ b/lib/simplecov/useless_results_remover.rb @@ -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 diff --git a/spec/useless_results_remover_spec.rb b/spec/useless_results_remover_spec.rb index 223b8f55..249dec11 100644 --- a/spec/useless_results_remover_spec.rb +++ b/spec/useless_results_remover_spec.rb @@ -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