Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to remove defaults filters and document them better #803

Open
fadeltd opened this issue Jan 2, 2020 · 5 comments
Open

Add a way to remove defaults filters and document them better #803

fadeltd opened this issue Jan 2, 2020 · 5 comments
Labels

Comments

@fadeltd
Copy link

fadeltd commented Jan 2, 2020

ruby version: 2.6.3
simplecov version: 0.17.1
test-unit version: 3.3.4

Issue
I have a project with some ruby scripts for CI purposes, and just now I'm creating unit tests for my ruby scripts, and I'm trying to get coverage from it with simplecov, but simplecov always gives me 0.0 / 0.0 LOC (100.0%) covered.

Here's sample script to reproduce the issue

test_calculator.rb

require 'simplecov'
SimpleCov.start

require 'test/unit'
require_relative '.scripts/tool/calculator'

# Test calculator
class CalculatorTest < Test::Unit::TestCase
  def test_add_2_different_numbers
    assert sum(2, 2) == 4
  end

  def test_add_same_numbers
    assert sum(2) == 4
  end
end

.scripts/tool/calculator.rb

def sum(first, second = nil)
  first + second unless second.nil?
  first + first
end
Loaded suite test_calculator
Started
..
Finished in 0.000728 seconds.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2747.25 tests/s, 2747.25 assertions/s
Coverage report generated for Unit Tests to /Users/fadeltd/project/ruby-scripts/coverage. 0.0 / 0.0 LOC (100.0%) covered.

It seems that simplecov can't generate coverage for require_relative when the first directory name has . prefix
I've tried renaming my directory to scripts/.tool/calculator, and it works

Loaded suite test_calculator
Started
..
Finished in 0.000402 seconds.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4975.12 tests/s, 4975.12 assertions/s
Coverage report generated for Unit Tests to /Users/fadeltd/project/ruby-scripts/coverage. 3 / 3 LOC (100.0%) covered.

How can I fix this issue without having to rename my directory?
Thanks

@PragTob
Copy link
Collaborator

PragTob commented Jan 2, 2020

Hi there @fadeltd ! Thanks for the bug report and the repro script! I'll try it out and see if I can reproduce it over here.

Seems like a very unlikely bug but who am I to judge, maybe something in the code that checks if the file exists thinks it's hidden and doesn't exist. Not sure.

edit: can reproduce!

@PragTob
Copy link
Collaborator

PragTob commented Jan 2, 2020

@fadeltd hi there, so seems this was proposed as an intentional change and past me agreed 😁 #721 - .folder usually denotes a hidden folder although I guess our checking could be better. Or can't since that seems to be literally how unix does it.

Problem is, that hidden filter is in the defaults and right now that's a bit harder to circumvent. Here's a little script shwocasing what you could do:

require 'simplecov/no_defaults'
require "simplecov-html"

SimpleCov.start do
  formatter SimpleCov::Formatter::HTMLFormatter
end

at_exit do
  SimpleCov.set_exit_exception
  SimpleCov.run_exit_tasks!
end

require_relative ".scripts/foo"

For me however, the tasks here are more like:

  • make it easier to remove filters
  • document the default filters

@PragTob PragTob changed the title simplecov does work not with require_relative directory with . prefix Add a way to remove defaults filters and document them better Jan 2, 2020
@fadeltd
Copy link
Author

fadeltd commented Jan 3, 2020

Thank you so much for checking this and also detailed explanation.

Problem is, that hidden filter is in the defaults and right now that's a bit harder to circumvent.

I'm confused as hidden sub-directories like scripts/.tools is still allowed, I'll use the suggested workaround for the time being.

But thanks anyway 😊 good luck with the action plan.

@fadeltd
Copy link
Author

fadeltd commented Jan 3, 2020

I've tried the workaround but it seems that simplecov now also includes all the libs on root .rvm directory, maybe the filter is there for a good reason

Edit: I just need to add bundler filter root_filter to exclude .rvm

require 'simplecov/no_defaults'
require 'simplecov-html'
require 'simplecov/profiles/root_filter'

SimpleCov.start do
  formatter SimpleCov::Formatter::HTMLFormatter
  load_profile 'root_filter'
end

at_exit do
  SimpleCov.set_exit_exception
  SimpleCov.run_exit_tasks!
end

require_relative ".scripts/foo"

@PragTob
Copy link
Collaborator

PragTob commented Jan 3, 2020

@fadeltd yeah the filter only removes paths where the first folder is a hidden folder. Which, I think is fair-ish. But yeah root filter or manual "add_filter"s might need to be added.

There's a surprising amount of things in the defaults such as the command guesser as well: https://github.com/colszowka/simplecov/blob/master/lib/simplecov/defaults.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants