You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The three year old code above no longer works: the nil makes all lines never lines so coverage is 100%.
So I checked #252 and there he sets it to 0. Which is awesome. But all of a sudden files that are otherwise considered 100% covered, are suddenly a lower percentage, because of the zero (which trumps the nil I guess in merging), all empty or never lines are now instead considered as not-covered).
So in my spec_helper I placed the following at the top:
if ENV["COVERAGE"]
require 'simplecov'
SimpleCov.start 'rails'
SimpleCov.coverage_dir 'coverage/rspec'
all_files = Dir['**/*.rb']
base_result = {}
all_files.each do |file|
absolute = File::expand_path(file)
lines = File.readlines(absolute, :encoding => 'UTF-8')
base_result[absolute] = lines.map do |l|
l.strip!
l.empty? || l =~ /^end$/ || l[0] == '#' ? nil : 0
end
end
SimpleCov.at_exit do
merged = SimpleCov::Result.new(Coverage.result).original_result.merge_resultset(base_result)
result = SimpleCov::Result.new(merged)
result.format!
end
end
So I fake never lines when I encounter empty lines, comment lines, and the last end, and then my coverage results are imho a more valid representation.
@ScotterC Did you try this? I tried in a new rails 4.1 project, and for me it is not the same. But maybe this could be fixed by configuring my eager_load_namespaces, and to define a eager_load! in each added namespace? (e.g. I have a grape API which is also mounted, in namespace API, and eager_load! misses my API::Entities namespace).
@rkistner This kind of any impossible question, which is explained by understanding @nathanvda's code. The Coverage module tracking files as they are evaluated, i.e. when they are loaded and when any code in them is executed. Hence, it doesn't track files that aren't loaded. So, if you want to report files that exist that you never loaded, you need to do that yourself :) See my comment at #314 (comment)
We encountered a problem with this method of tracking all files in #394 as it was marking lines as uncovered that shouldn't be. So we went looking for another solution and we found that adding Rails.application.eager_load! after SimpleCov.start in test_helper.rb and making sure that our lib files were in eager_load_paths in application.rb
solved all of our problems. All files are being picked up for coverage purposes and we are not getting lines that should be ignored marked as uncovered.
Activity
rkistner commentedon Nov 15, 2010
The following approach doesn't know the difference between "lines" and "relevant lines", but at least all the files are there:
colszowka commentedon Nov 15, 2010
Thanks for the report, I'll have a look.
isel commentedon Sep 23, 2011
it would be really nice to have this
sockmonk commentedon Oct 4, 2011
+1. If a file is added with no test coverage, it should show up on the reports as having no coverage.
colszowka commentedon Jan 27, 2012
Added this for implementation to the 1.0 milestone
tpendragon commentedon Jan 17, 2013
+1. This feels like a huge problem to me.
voidus commentedon Jan 19, 2013
This was fixed a year ago, any idea when we'll see this?
colszowka commentedon Mar 23, 2013
@voidus I'd gladly accept a Pull Request that includes a test
nathanvda commentedon Dec 22, 2013
The three year old code above no longer works: the
nilmakes all linesneverlines so coverage is 100%.So I checked #252 and there he sets it to
0. Which is awesome. But all of a sudden files that are otherwise considered 100% covered, are suddenly a lower percentage, because of the zero (which trumps the nil I guess in merging), all empty orneverlines are now instead considered as not-covered).So in my
spec_helperI placed the following at the top:So I fake
neverlines when I encounter empty lines, comment lines, and the lastend, and then my coverage results are imho a more valid representation.ScotterC commentedon Apr 24, 2014
Eager loading with
Rails.application.eager_load!would have the same effect right?nathanvda commentedon Apr 28, 2014
@ScotterC Did you try this? I tried in a new rails 4.1 project, and for me it is not the same. But maybe this could be fixed by configuring my
eager_load_namespaces, and to define aeager_load!in each added namespace? (e.g. I have a grape API which is also mounted, in namespaceAPI, andeager_load!misses myAPI::Entitiesnamespace).shedd commentedon Aug 1, 2014
@nathanvda thanks for the updated code. That works in my Rails 4.1 app!
bf4 commentedon Aug 3, 2014
@rkistner This kind of any impossible question, which is explained by understanding @nathanvda's code. The Coverage module tracking files as they are evaluated, i.e. when they are loaded and when any code in them is executed. Hence, it doesn't track files that aren't loaded. So, if you want to report files that exist that you never loaded, you need to do that yourself :) See my comment at #314 (comment)
weimeng commentedon Aug 8, 2014
If anyone is interested, I modified the following line in @nathanvda's code:
to be
This ignores lines containing only
else, in line with the default SimpleCov behaviour.adampope commentedon Jun 18, 2015
We encountered a problem with this method of tracking all files in #394 as it was marking lines as uncovered that shouldn't be. So we went looking for another solution and we found that adding
Rails.application.eager_load!afterSimpleCov.startintest_helper.rband making sure that our lib files were in eager_load_paths inapplication.rbsolved all of our problems. All files are being picked up for coverage purposes and we are not getting lines that should be ignored marked as uncovered.
xaviershay commentedon Nov 29, 2015
See the
track_filesoption added in #422 above. Will be released in 0.11.