diff --git a/features/config_tracked_files.feature b/features/config_tracked_files.feature new file mode 100644 index 00000000..ba0c4f0c --- /dev/null +++ b/features/config_tracked_files.feature @@ -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 % | diff --git a/lib/simplecov.rb b/lib/simplecov.rb index f4a555ea..95da3ebd 100644 --- a/lib/simplecov.rb +++ b/lib/simplecov.rb @@ -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 diff --git a/lib/simplecov/configuration.rb b/lib/simplecov/configuration.rb index 3e0d6c46..d7d20da5 100644 --- a/lib/simplecov/configuration.rb +++ b/lib/simplecov/configuration.rb @@ -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. # diff --git a/lib/simplecov/defaults.rb b/lib/simplecov/defaults.rb index 09aad1c1..c60d9d6b 100644 --- a/lib/simplecov/defaults.rb +++ b/lib/simplecov/defaults.rb @@ -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 diff --git a/spec/faked_project/lib/faked_project.rb b/spec/faked_project/lib/faked_project.rb index c33c7917..ba5c49d7 100644 --- a/spec/faked_project/lib/faked_project.rb +++ b/spec/faked_project/lib/faked_project.rb @@ -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 diff --git a/spec/faked_project/lib/faked_project/untested_class.rb b/spec/faked_project/lib/faked_project/untested_class.rb new file mode 100644 index 00000000..dc03d0f9 --- /dev/null +++ b/spec/faked_project/lib/faked_project/untested_class.rb @@ -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