simplecov-ruby / simplecov

Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

simplecov not covering lib directory in Rails engine -- NOT a missing require

lastobelus opened this issue · comments

In my rails engine simplecov does not (or rather will not) provide coverage for the lib directory.

I tried adding the root directory of the engine to the loadpath in spec_helper, and doing require "lib/myengine", which did not work.

HOWEVER If I simply rename lib to bob and change the corresponding require in the spec to require 'bob/myclass', the coverage DOES get created.

The issue seems to be purely with the name lib. 🐤

Adding a group with the lib path does not help the matter.

After some work at debugging this, we realized the problem exists only in the rubygems.org version of the gem, and not in HEAD

We discovered we needed to use the HEAD version of the gem AND do this in the spec_helper:

Dir["lib/**/*.rb"].each {|file| puts file; puts load(file); }

Although this does work, it feels a little hackie. I'll try to get some time to make a pull request to get simplecov to detect this and load. Thanks for the workaround!

Actually, loading the files above didn't work for me correctly as it's order,directory dependent => flaky.

Instead I did the following and it worked nicely:

https://gist.github.com/bsodmike/5866873

SimpleCov.start('rails') do
adapters.delete(:root_filter)
filters.clear
add_filter do |src|
!(src.filename =~ /^#{SimpleCov.root}/) unless src.filename =~ /my_engine/
end
end if ENV['COVERAGE']

@lsaffie Thanks I'll try that next time I work on an engine.

Great this got fixed by the recent release, thanks for reporting back!

@lsaffie That's a cool idea, I'll add this to the README documentation.

@lsaffie, how can I use add_filter and add_group to coverage several engines in different group.

@lsaffie , thank you, that's work well.But the simplecov just coverage code that spec code include.How to
coverage code that doesn't invoke by rspec test case.
image

Above is the engine that doesn't call by spec code.But our goal is to detect every engine's code coverage rate.

👍 @hongweikang I have the same problem as you do.

Please create a new issue (or submit a PR) on how to have SimpleCov report coverage for files outside of SimpleCov.root. Perhaps see #340

For reference, the config example in at @lsaffie's link is

  adapters.delete(:root_filter)
  filters.clear
  add_filter do |src|
    !(src.filename =~ /^#{SimpleCov.root}/) unless src.filename =~ /\/my_engine\//
  end