Add a way to remove defaults filters and document them better
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
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!
@fadeltd hi there, so seems this was proposed as an intentional change and past me agreed :grin: https://github.com/colszowka/simplecov/pull/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
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.
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"
@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