Skip to content
Simple toolbox for oneshot coverage
Branch: master
Clone or download
Latest commit 0803eb4 Apr 18, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin first commit Mar 3, 2019
lib Bump up version Apr 18, 2019
.gitignore first commit Mar 3, 2019 first commit Mar 3, 2019
Gemfile first commit Mar 3, 2019
Gemfile.lock first commit Mar 3, 2019
LICENSE.txt first commit Mar 3, 2019 Pass all logs to Logger Apr 16, 2019
Rakefile first commit Mar 3, 2019
oneshot_coverage.gemspec Release ruby version temperary Mar 19, 2019


This gem may not be very useful when you want to use Coverage onetshot mode, however, It could be good example to study how to implement by yourself.

This gem provides simple tools to use oneshot mode easier. It gives you:

  • Rack middleware for logging
  • Pluggable logger interface

Please notice that it records code executions under the target path(usually, project base path). If you have bundle gem path under target path, It will be ignored automatically.


Add this line to your application's Gemfile:

gem 'oneshot_coverage'

And then execute:

$ bundle

Or install it yourself as:

$ gem install oneshot_coverage



  target_path: '/base/project/path',
  emit_term: nil, # emit per `emit_term` seconds. It tries to emit per request when `nil`.

As default, OneshotCoverage supports 2 logger.

  • OneshotCoverage::Logger::NullLogger (default)
  • OneshotCoverage::Logger::StdoutLogger
  • OneshotCoverage::Logger::FileLogger

Only required interface is #post instance method, so you could implement by yourself easily.

class FileLogger
  def initialize(log_path)
    @log_path = log_path

  # new_logs:, :md5_hash, :lines)
  def post(new_logs)
    current_coverage = fetch

    new_logs.each do |new_log|
      key = "#{new_log.path}-#{new_log.md5_hash}"

      logged_lines = current_coverage.fetch(key, [])
      current_coverage[key] = logged_lines | new_log.lines


  def fetch
  rescue Errno::ENOENT

  def save(data)
    File.write(@log_path, JSON.dump(data))

Emit logs

With rack application

Please use OneshotCoverage::Middleware. This will emit logs per each request.

If you using Rails, middleware will be inserted automatically.

With job/batch application

If your job or batch are exit as soon as it finished(i.e. execute via rails runner), then you don't need to do anything. OneshotCoverage.start will set trap to emit via at_exit. On the other hand, it's not, then you need to emit it manually at proper timing(i.e. when batch finished)


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the OneshotCoverage project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

You can’t perform that action at this time.