Skip to content

Commit

Permalink
When a file is not loaded, mark it completely as not covered
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopeixoto committed Nov 29, 2015
1 parent 8fe4cdd commit b48311f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
2 changes: 1 addition & 1 deletion features/config_tracked_files.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Feature:
When I open the coverage report generated with `bundle exec rake test`
Then I should see the groups:
| name | coverage | files |
| All Files | 81.54% | 7 |
| All Files | 76.81% | 7 |

And I should see the source files:
| name | coverage |
Expand Down
26 changes: 5 additions & 21 deletions lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,28 @@ def start(profile = nil, &block)
end

#
# Finds files that were to be tracked but were not covered and initializes
# their coverage to zero, with an estimation of the line count.
# Finds files that were to be tracked but were not loaded and initializes
# their coverage to zero.
#
def add_uncovered_files(result)
def add_not_loaded_files(result)
if @track_files_glob
result = result.dup
Dir[@track_files_glob].each do |file|
absolute = File.expand_path(file)

unless result[absolute]
lines = File.readlines(absolute)
result[absolute] = [0] * lines.count { |l| relevant_line?(l) }
end
result[absolute] ||= [0] * File.foreach(absolute).count
end
end

result
end

#
# Determines if a line should be considered by coverage tools.
# This method is just an estimation, but it is not very important
# that it is accurate. It is only used when all lines in a file
# are not covered. As soon as a single line is covered, the counting
# method will be the one provided by the Coverage module.
#
def relevant_line?(line)
l = line.strip

!(l.empty? || l =~ /^else$/ || l =~ /^end$/ || l[0] == '#' || l =~ /^rescue(\s+.*)?$/)
end

#
# Returns the result for the current coverage run, merging it across test suites
# from cache using SimpleCov::ResultMerger if use_merging is activated (default)
#
def result
@result ||= SimpleCov::Result.new(add_uncovered_files(Coverage.result)) if running
@result ||= SimpleCov::Result.new(add_not_loaded_files(Coverage.result)) if running
# If we're using merging of results, store the current result
# first, then merge the results and return those
if use_merging
Expand Down

0 comments on commit b48311f

Please sign in to comment.