https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112016-06-11T03:34:08ZRuby Issue Tracking SystemRuby master - Bug #12480: Restarting Coverage does not capture additional coverage for already loaded fileshttps://bugs.ruby-lang.org/issues/12480?journal_id=591442016-06-11T03:34:08Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li><li><strong>Assignee</strong> set to <i>mame (Yusuke Endoh)</i></li></ul><p>Michael Grosser wrote:</p>
<blockquote>
<ul>
<li>A: when forking, previous coverage is lost</li>
</ul>
</blockquote>
<p>This is intended. Consider the following program:</p>
<pre><code># reproduce.rb
require "coverage"
Coverage.start
require_relative "test"
def save_coverage
r = Coverage.result
# merge coverage data
if File.readable?("coverage.dat")
r2 = Marshal.load(File.binread("coverage.dat"))
r = merge(r, r2)
end
File.binwrite("coverage.dat", Marshal.dump(r))
end
simple_test
fork do
heavy_test_1
save_coverage
end
heavy_test_2
save_coverage
</code></pre>
<p>Note that the <code>simple_test</code> method is executed once. If <code>fork</code> does not clear the previous coverage, the method will be wrongly counted twice.</p>
<blockquote>
<ul>
<li>B: when restarting coverage, old files do not get added to</li>
</ul>
</blockquote>
<p>This is also intended. See <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Why does Coverage keep previously-loaded files as empty arrays? (Closed)" href="https://bugs.ruby-lang.org/issues/12220">#12220</a>.</p>
<p>I recommend you not to use <code>fork</code>. If you want to do so at any cost, you can pass the coverage data via pipe and do manual merge:</p>
<pre><code>require "coverage"
Coverage.start
require_relative "test"
a
r, w = IO.pipe("BINARY")
fork do
w.close
c1 = Marshal.load(r.read)
b
c2 = Coverage.result
puts "NEW: #{ merge(c1, c2) }"
end
r.close
w << Marshal.dump(Coverage.result)
w.close
</code></pre> Ruby master - Bug #12480: Restarting Coverage does not capture additional coverage for already loaded fileshttps://bugs.ruby-lang.org/issues/12480?journal_id=593262016-06-23T22:36:40Zgrosser (Michael Grosser)michael@grosser.it
<ul></ul><p>Solved this by using Coverage.peek_result in the before-fork part of my code to capture the current state and then merged it after the fork is done :D</p>
<p>Feel free to close this now!</p> Ruby master - Bug #12480: Restarting Coverage does not capture additional coverage for already loaded fileshttps://bugs.ruby-lang.org/issues/12480?journal_id=764422019-01-21T08:33:53Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Closed</i></li></ul><p>Closing due to OP's request.</p>
<p>FYI: Since 2.6, coverage.so has supported <code>Coverage.result(stop: false, clear: false)</code>. You may want to check it out.</p>