Project

General

Profile

Actions

Feature #13838

closed

Add the ability to detect Coverage status

Added by yui-knk (Kaneko Yuichiro) about 7 years ago. Updated about 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:82461]

Description

Description

I want to detect current status of Coverage, which is enabled or not.
Now we can detect status only trying Coverage.peek_result or Coverage.result and catch RuntimeError.
Attached patch enable us to detect status without any exceptions.

Why

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

Example

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

Files

Coverage.patch (1.47 KB) Coverage.patch yui-knk (Kaneko Yuichiro), 08/25/2017 12:30 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0