Add the ability to detect Coverage status
I want to detect current status of Coverage, which is enabled or not.
Now we can detect status only trying
Coverage.result and catch RuntimeError.
Attached patch enable us to detect status without any exceptions.
My daily work is developing a web application with Ruby.
I am using
Coverage.peek_result to record coverage information on a per-request basis.
When recording coverage information, I want to start Coverage on a part of production servers,
because Coverage tool may have some overhead.
In this case, the code of my program looks like
# Implement as Rack middleware class CoverageMiddleware def initialize(app) @app = app end def call(env) # To ensure return response, rescue exceptions before = Coverage.peek_result rescue nil res = @app.call(env) after = Coverage.peek_result rescue nil if (before && after) write_file(diff(before, after)) end return res end end
By using `Coverage.enabled?, I can
class CoverageMiddleware def initialize(app) @app = app end def call(env) if Coverage.enabled? before = Coverage.peek_result res = @app.call(env) after = Coverage.peek_result write_file(diff(before, after)) else res = @app.call(env) end return res end end