simplecov icon indicating copy to clipboard operation
simplecov copied to clipboard

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

Open lastobelus opened this issue 13 years ago • 10 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. :baby_chick:

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

lastobelus avatar May 03 '13 23:05 lastobelus

~~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); }

lastobelus avatar May 04 '13 00:05 lastobelus

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!

lsaffie avatar Oct 15 '13 18:10 lsaffie

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 avatar Oct 16 '13 16:10 lsaffie

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

lastobelus avatar Oct 17 '13 00:10 lastobelus

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.

colszowka avatar Nov 20 '13 09:11 colszowka

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

hongweikang avatar Dec 08 '14 02:12 hongweikang

@hongweikang check out these examples: https://gist.github.com/bsodmike/5866873

lsaffie avatar Dec 08 '14 02:12 lsaffie

@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 avatar Dec 08 '14 04:12 hongweikang

:+1: @hongweikang I have the same problem as you do.

mhenrixon avatar Dec 31 '14 12:12 mhenrixon

Please create a new issue (or submit a PR) on how to have SimpleCov report coverage for files outside of SimpleCov.root. Perhaps see https://github.com/colszowka/simplecov/issues/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

bf4 avatar Dec 31 '14 15:12 bf4