Skip to content

Files with no coverage #16

Closed
Closed

Description

@rkistner

Files without any coverage are not shown at all. It is important for me to be able to view these files.

As this is not detected by the built-in Coverage module, perhaps an option could be added to search for files in the current folder?

I'll try to create a proof-of-concept for this.

Activity

  1. rkistner commented on Nov 15, 2010

    @rkistner
    Author

    The following approach doesn't know the difference between "lines" and "relevant lines", but at least all the files are there:

    all_files = Dir['**/*.rb']
    base_result = {}
    all_files.each do |file|
        absolute = File::expand_path(file)
        lines = File.readlines(absolute).size
        base_result[absolute] = [nil]*lines
    end
    
    SimpleCov.at_exit do
        merged = SimpleCov.result.original_result.merge_resultset(base_result)
        result = SimpleCov::Result.new(merged)
        result.command_name = SimpleCov.result.command_name
        result.format!
    end
    
  2. colszowka commented on Nov 15, 2010

    @colszowka
    Collaborator

    Thanks for the report, I'll have a look.

  3. isel commented on Sep 23, 2011

    @isel

    it would be really nice to have this

  4. sockmonk commented on Oct 4, 2011

    @sockmonk

    +1. If a file is added with no test coverage, it should show up on the reports as having no coverage.

  5. colszowka commented on Jan 27, 2012

    @colszowka
    Collaborator

    Added this for implementation to the 1.0 milestone

  6. tpendragon commented on Jan 17, 2013

    @tpendragon

    +1. This feels like a huge problem to me.

  7. voidus commented on Jan 19, 2013

    @voidus

    This was fixed a year ago, any idea when we'll see this?

  8. colszowka commented on Mar 23, 2013

    @colszowka
    Collaborator

    @voidus I'd gladly accept a Pull Request that includes a test

  9. nathanvda commented on Dec 22, 2013

    @nathanvda

    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.

  10. ScotterC commented on Apr 24, 2014

    @ScotterC

    Eager loading with Rails.application.eager_load! would have the same effect right?

  11. nathanvda commented on Apr 28, 2014

    @nathanvda

    @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).

  12. shedd commented on Aug 1, 2014

    @shedd

    @nathanvda thanks for the updated code. That works in my Rails 4.1 app!

  13. bf4 commented on Aug 3, 2014

    @bf4
    Collaborator

    @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)

  14. weimeng commented on Aug 8, 2014

    @weimeng

    If anyone is interested, I modified the following line in @nathanvda's code:

    l.empty? || l =~ /^end$/ || l[0] == '#' ? nil : 0

    to be

    l.empty? || l =~ /^else$/ || l =~ /^end$/ || l[0] == '#' ? nil : 0

    This ignores lines containing only else, in line with the default SimpleCov behaviour.

  15. adampope commented on Jun 18, 2015

    @adampope

    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

    config.eager_load_paths += %W(#{config.root}/lib)
    config.eager_load_paths += Dir["#{config.root}/lib/**/"]
    

    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.

  16. xaviershay commented on Nov 29, 2015

    @xaviershay
    Collaborator

    See the track_files option added in #422 above. Will be released in 0.11.

  17. added theissue type on May 6, 2026
  18. changed the issue type fromtoon May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions