lib/undercover.rb in undercover-0.5.0 vs lib/undercover.rb in undercover-0.6.0
- old
+ new
@@ -20,25 +20,30 @@
def_delegators :changeset, :validate
attr_reader :changeset,
:lcov,
:results,
- :code_dir
+ :code_dir,
+ :glob_filters
# Initializes a new Undercover::Report
#
# @param changeset [Undercover::Changeset]
# @param opts [Undercover::Options]
def initialize(changeset, opts)
@lcov = LcovParser.parse(File.open(opts.lcov))
@code_dir = opts.path
@changeset = changeset.update
+ @glob_filters = {
+ allow: opts.glob_allow_filters,
+ reject: opts.glob_reject_filters
+ }
@loaded_files = {}
@results = {}
end
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def build
changeset.each_changed_line do |filepath, line_no|
dist_from_line_no = lambda do |res|
return BigDecimal::INFINITY if line_no < res.first_line
@@ -48,10 +53,11 @@
line_no - res.first_line
end
dist_from_line_no_sorter = lambda do |res1, res2|
dist_from_line_no[res1] <=> dist_from_line_no[res2]
end
+
load_and_parse_file(filepath)
next unless loaded_files[filepath]
res = loaded_files[filepath].min(&dist_from_line_no_sorter)
@@ -59,11 +65,11 @@
results[filepath] ||= Set.new
results[filepath] << res
end
self
end
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
def build_warnings
warn('Undercover::Report#build_warnings is deprecated! ' \
'Please use the #flagged_results accessor instead.')
all_results.select(&:flagged?)
@@ -90,12 +96,13 @@
def load_and_parse_file(filepath)
key = filepath.gsub(/^\.\//, '')
return if loaded_files[key]
coverage = lcov.coverage(filepath)
- return if coverage.empty?
+ return unless include_file?(filepath)
+
root_ast = Imagen::Node::Root.new.build_from_file(
File.join(code_dir, filepath)
)
return if root_ast.children.empty?
@@ -104,7 +111,12 @@
root_ast.children[0].find_all(->(_) { true }).each do |imagen_node|
loaded_files[key] << Result.new(imagen_node, coverage, filepath)
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
+
+ def include_file?(filepath)
+ fnmatch = proc { |glob| File.fnmatch(glob, filepath) }
+ glob_filters[:allow].any?(fnmatch) && glob_filters[:reject].none?(fnmatch)
+ end
end
end