I would like a way to be able to make the following test past:
require "test/unit"
require "coverage"
require 'tmpdir'
class TestCoverage < Test::Unit::TestCase
def test_restarting_coverage
Dir.mktmpdir {|tmp|
Dir.chdir(tmp) {
File.open("test.rb", "w") do |f|
f.puts <<-EOS
def coverage_test_method
puts :ok
end
EOS
end
Coverage.start
require tmp + '/test.rb'
Coverage.result
Coverage.start
coverage_test_method
assert_equal 1, Coverage.result.size
}
}
end
end
The problem is that Coverage.start doesn't track any files loaded before it is called. This is probably desired behaviour so that stdlib files are not tracked, but it limits the usefulness of Coverage. Specifically, I am trying to collate coverage reports from workers in multiple processes. Also I want to associate coverages with specific tests (this test executed this code, etc...). This is very difficult without being able to restart.
What would be involved in doing this? If you point me in the right direction I can perhaps have a go.
The problem is that Coverage.start doesn't track any files loaded before it is called. This is probably desired behaviour so that stdlib files are not tracked, but it limits the usefulness of Coverage.
First of all, it is difficult to track files loaded before start' is first called. This is because the VM compiler inserts instructions to measure coverage after start' is first called. Since the mechanism
have considerable overhead, it cannot be enabled by default.
You are talking about second (or later) calls to Coverage.start, right?
Specifically, I am trying to collate coverage reports from workers in multiple processes.
I cannot understand why the feature is needed for this use case.
Could you elaborate this?
Also I want to associate coverages with specific tests (this test executed this code, etc...). This is very difficult without being able to restart.
You are talking about second (or later) calls to Coverage.start, right?
Yes, that would be fine.
I cannot understand why the feature is needed for this use case.
Could you elaborate this?
It's not required, it just would of made my life easier. I was trying to integrate with the parallel and SimpleCov gems, and ended up with a work around which involves conditionally hooking at_exit and a file lock, which is a bit of a mess.
Here is a patch.
I will try it out and report back. Thanks!
class TestCoverage < Test::Unit::TestCase
def test_result_without_start
@@ -14,4 +15,29 @@ class TestCoverage < Test::Unit::TestCase
assert_kind_of(Array, val)
end
end
+
This issue was solved with changeset r32404.
Xavier, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.