Skip to content
Simple toolbox for oneshot coverage
Branch: master
Clone or download
Latest commit 0803eb4 Apr 18, 2019
Permalink
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
CODE_OF_CONDUCT.md 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
README.md Pass all logs to Logger Apr 16, 2019
Rakefile first commit Mar 3, 2019
oneshot_coverage.gemspec Release ruby version temperary Mar 19, 2019

README.md

OneshotCoverage

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.

Installation

Add this line to your application's Gemfile:

gem 'oneshot_coverage'

And then execute:

$ bundle

Or install it yourself as:

$ gem install oneshot_coverage

Usage

Configuration

OneshotCoverage.configure(
  target_path: '/base/project/path',
  logger: OneshotCoverage::Logger::NullLogger.new,
  emit_term: nil, # emit per `emit_term` seconds. It tries to emit per request when `nil`.
)
OneshotCoverage.start

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
  end

  # new_logs: Struct.new(:path, :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
    end
    save(current_coverage)
  end

  private

  def fetch
    JSON.load(File.read(@log_path))
  rescue Errno::ENOENT
    {}
  end

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

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)

License

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.