Skip to content

Commit

Permalink
Allow for regular expression filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Steel committed Jun 16, 2017
1 parent 804b460 commit a0b14a1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
6 changes: 2 additions & 4 deletions lib/simplecov/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,10 @@ def add_group(group_name, filter_argument = nil, &filter_proc)
def parse_filter(filter_argument = nil, &filter_proc)
if filter_argument.is_a?(SimpleCov::Filter)
filter_argument
elsif filter_argument.is_a?(String)
SimpleCov::StringFilter.new(filter_argument)
elsif filter_proc
SimpleCov::BlockFilter.new(filter_proc)
elsif filter_argument.is_a?(Array)
SimpleCov::ArrayFilter.new(filter_argument)
elsif filter_argument
SimpleCov::Filter.class_for_argument(filter_argument).new(filter_argument)
else
raise ArgumentError, "Please specify either a string or a block to filter with"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/simplecov/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
SimpleCov.profiles.define "rails" do
load_profile "test_frameworks"

add_filter "/config/"
add_filter "/db/"
add_filter %r{^/config/}
add_filter %r{/^/db/}

add_group "Controllers", "app/controllers"
add_group "Channels", "app/channels" if defined?(ActionCable)
Expand Down
20 changes: 20 additions & 0 deletions lib/simplecov/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def passes?(source_file)
warn "#{Kernel.caller.first}: [DEPRECATION] #passes? is deprecated. Use #matches? instead."
matches?(source_file)
end

def self.class_for_argument(filter_argument)
if filter_argument.is_a?(String)
SimpleCov::StringFilter
elsif filter_argument.is_a?(Regexp)
SimpleCov::RegexFilter
elsif filter_argument.is_a?(Array)
SimpleCov::ArrayFilter
else
raise ArgumentError, "You have provided an unrecognized filter type"
end
end
end

class StringFilter < SimpleCov::Filter
Expand All @@ -34,6 +46,14 @@ def matches?(source_file)
end
end

class RegexFilter < SimpleCov::Filter
# Returns true when the given source file's filename matches the
# regex configured when initializing this Filter with RegexFilter.new(/someregex/)
def matches?(source_file)
(source_file.project_filename =~ filter_argument)
end
end

class BlockFilter < SimpleCov::Filter
# Returns true if the block given when initializing this filter with BlockFilter.new {|src_file| ... }
# returns true for the given source file.
Expand Down
6 changes: 6 additions & 0 deletions lib/simplecov/source_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def initialize(filename, coverage)
@coverage = coverage
end

# The path to this source file relative to the projects directory
def project_filename
project_dir = File.dirname(File.expand_path(File.basename(__FILE__)))
@filename.sub(/^#{project_dir}/, "")
end

# The source code for this file. Aliased as :source
def src
# We intentionally read source code lazily to
Expand Down
30 changes: 30 additions & 0 deletions spec/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
expect(SimpleCov::StringFilter.new("sample.rb")).to be_matches subject
end

it "matches a new SimpleCov::StringFilter '/fixtures/'" do
expect(SimpleCov::StringFilter.new("sample.rb")).to be_matches subject
end

it "matches a new SimpleCov::RegexFilter /\/fixtures\//" do
expect(SimpleCov::RegexFilter.new(/\/fixtures\//)).to be_matches subject
end

it "doesn't match a new SimpleCov::RegexFilter /^\/fixtures\//" do
expect(SimpleCov::RegexFilter.new(/^\/fixtures\//)).not_to be_matches subject
end

it "matches a new SimpleCov::RegexFilter /^\/spec\//" do
expect(SimpleCov::RegexFilter.new(/^\/spec\//)).to be_matches subject
end

it "doesn't match a new SimpleCov::BlockFilter that is not applicable" do
expect(SimpleCov::BlockFilter.new(proc { |s| File.basename(s.filename) == "foo.rb" })).not_to be_matches subject
end
Expand Down Expand Up @@ -94,5 +110,19 @@
expect(SimpleCov.filtered(subject)).to be_a SimpleCov::FileList
end
end

describe ".class_for_argument" do
it "returns SimpleCov::StringFilter for a string" do
expect(SimpleCov::Filter.class_for_argument("filestring")).to eq(SimpleCov::StringFilter)
end

it "returns SimpleCov::RegexFilter for a string" do
expect(SimpleCov::Filter.class_for_argument(/regex/)).to eq(SimpleCov::RegexFilter)
end

it "returns SimpleCov::RegexFilter for a string" do
expect(SimpleCov::Filter.class_for_argument(%w[file1 file2])).to eq(SimpleCov::ArrayFilter)
end
end
end
end
4 changes: 4 additions & 0 deletions spec/source_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
expect(subject.src).to eq(subject.source)
end

it "has a project filename which removes the project directory" do
expect(subject.project_filename).to eq("/spec/fixtures/sample.rb")
end

it "has source_lines equal to lines" do
expect(subject.lines).to eq(subject.source_lines)
end
Expand Down

0 comments on commit a0b14a1

Please sign in to comment.