Skip to content

Commit

Permalink
Merge branch 'hugopeixoto-feature-uncovered-source-files', PR #422
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviershay committed Nov 29, 2015
2 parents 8113d50 + 3414dd6 commit 07985e5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
29 changes: 29 additions & 0 deletions features/config_tracked_files.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@test_unit
Feature:

Using the setting `tracked_files` should add files that were not
required to the report.

Scenario:
Given SimpleCov for Test/Unit is configured with:
"""
require 'simplecov'
SimpleCov.start do
track_files "lib/**/*.rb"
end
"""

When I open the coverage report generated with `bundle exec rake test`
Then I should see the groups:
| name | coverage | files |
| All Files | 76.81% | 7 |

And I should see the source files:
| name | coverage |
| lib/faked_project.rb | 100.0 % |
| lib/faked_project/untested_class.rb | 0.0 % |
| lib/faked_project/some_class.rb | 80.0 % |
| lib/faked_project/framework_specific.rb | 75.0 % |
| lib/faked_project/meta_magic.rb | 100.0 % |
| test/meta_magic_test.rb | 100.0 % |
| test/some_class_test.rb | 100.0 % |
19 changes: 18 additions & 1 deletion lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,29 @@ def start(profile = nil, &block)
end
end

#
# Finds files that were to be tracked but were not loaded and initializes
# their coverage to zero.
#
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)

result[absolute] ||= [0] * File.foreach(absolute).count
end
end

result
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(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
9 changes: 9 additions & 0 deletions lib/simplecov/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def coverage_path
coverage_path
end

#
# Coverage results will always include files matched by this glob, whether
# or not they were explicitly required. Without this, un-required files
# will not be present in the final report.
#
def track_files(glob)
@track_files_glob = glob
end

#
# Returns the list of configured filters. Add filters using SimpleCov.add_filter.
#
Expand Down
2 changes: 2 additions & 0 deletions lib/simplecov/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
add_group "Mailers", "app/mailers"
add_group "Helpers", "app/helpers"
add_group "Libraries", "lib"

track_files "{app,lib}/**/*.rb"
end

# Default configuration
Expand Down
2 changes: 1 addition & 1 deletion spec/faked_project/lib/faked_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.foo
end
end

Dir[File.join(File.dirname(__FILE__), "faked_project/*.rb")].each do |file|
Dir[File.join(File.dirname(__FILE__), "faked_project/*.rb")].reject { |f| /untested/.match(f) }.each do |file|
require file # Require all source files in project dynamically so we can inject some stuff depending on test situation
end

Expand Down
11 changes: 11 additions & 0 deletions spec/faked_project/lib/faked_project/untested_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UntestedClass
def initialize(yogurts)
@yogurts = yogurts
end

def power_level
@yogurts.map do |yo|
yo.experience_points**2
end.reduce(0, &:+)
end
end

0 comments on commit 07985e5

Please sign in to comment.