Module: GemHadar::SimpleCov

Extended by:
Warn, Term::ANSIColor
Defined in:
lib/gem_hadar/simplecov.rb

Overview

A module that provides SimpleCov-related functionality for code coverage analysis.

This module encapsulates the setup and configuration of SimpleCov for Ruby projects, including initialization, formatter selection, and warning message handling. It integrates with the GemHadar framework to provide detailed coverage reporting capabilities while maintaining consistent output formatting and error handling.

Examples:

Initializing SimpleCov with GemHadar GemHadar::SimpleCov.start

Defined Under Namespace

Classes: ContextFormatter

Class Method Summary collapse

Methods included from Warn

fail, warn

Class Method Details

.default_blockProc

The default_block method returns a lambda that configures SimpleCov with branch coverage and multiple formatters.

This method constructs a default configuration block for SimpleCov that enables branch coverage, adds a filter based on the caller’s directory, and sets up a multi-formatter including SimpleFormatter, HTMLFormatter, and a custom ContextFormatter.

and formatters

Returns:

  • (Proc)

    a lambda configuring SimpleCov with coverage settings



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/gem_hadar/simplecov.rb', line 162

def default_block
  filter = "#{File.basename(File.dirname(caller.first))}/"
  -> {
    enable_coverage :branch
    add_filter filter
    formatter SimpleCov::Formatter::MultiFormatter.new([
      SimpleCov::Formatter::SimpleFormatter,
      SimpleCov::Formatter::HTMLFormatter,
      GemHadar::SimpleCov::ContextFormatter,
    ])
  }
end

.start(profile = nil, &block) ⇒ Object

The start method initializes and configures SimpleCov for code coverage analysis.

This method sets up SimpleCov with optional profile and block configuration, but only if the START_SIMPLECOV environment variable is set to 1. It handles the case where SimpleCov is not available by displaying a warning and installation instructions.

configuration

Parameters:

  • profile (String, nil) (defaults to: nil)

    the SimpleCov profile to use for

  • block (Proc, nil)

    optional block containing custom configuration



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/gem_hadar/simplecov.rb', line 186

def start(profile = nil, &block)
  if ENV['START_SIMPLECOV'].to_i != 1
    STDERR.puts color(226) {
      "Skip starting Simplecov for code coverage, "\
      "enable by setting env var: START_SIMPLECOV=1"
    }
    return
  end
  require 'simplecov'
  STDERR.puts color(76) { "Configuring Simplecov for code coverage." }
  block ||= default_block
  SimpleCov.start(profile, &block)
rescue LoadError => e
  warn "Caught #{e.class}: #{e}"
  STDERR.puts "Install with: gem install simplecov"
end