Module: Aspera::Coverage

Defined in:
lib/aspera/coverage.rb

Overview

Test coverage with SimpleCov, enabled with env var ENABLE_COVERAGE. Each tested process stores only its own result in parts/<uuid>: merging all results at each process exit is slow when there are many processes. The report is generated by report, which merges all parts (called by the test Rake tasks).

Constant Summary collapse

ROOT =

Development top folder, computed from this source location

File.dirname(File.realpath(__FILE__), 3)
DIR =

Report folder, relative to ROOT

'tmp/coverage'
PARTS =

One sub-folder per process, each with its own .resultset.json

File.join(DIR, 'parts')
BADGE =

Coverage badge (shields.io endpoint), relative to ROOT

'docs/coverage.json'

Class Method Summary collapse

Class Method Details

.filters(config) ⇒ Object

Configure coverage criteria and filters (in SimpleCov.start or SimpleCov.collate block) Branch coverage is measured in addition to line coverage (shown in the report, the badge shows lines).

Parameters:

  • config (SimpleCov) —

    Receiver of the configuration block



93
94
95
96
97
98
99
100
101
102
# File 'lib/aspera/coverage.rb', line 93

def filters(config)
  config.enable_coverage(:branch)
  config.skip do |source_file|
    source_file.lines.each do |line|
      line.skipped! if NO_COV_FUNCTIONS.any? { |i| line.src.include?(i) }
    end
    false
  end
  NO_COV_PATHS.each { |path| config.skip(path) }
end

.report ⇒ Float?

Merge the results of all processes and generate the report in DIR. Results older than MERGE_TIMEOUT are deleted.

Returns:

  • (Float, nil) —

    Percentage of covered lines, or nil if there is no result



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aspera/coverage.rb', line 57

def report
  require 'simplecov'
  require 'fileutils'
  require 'etc'
  parts = Dir.glob(File.join(ROOT, PARTS, '*'))
  old, parts = parts.partition { |dir| Time.now - File.mtime(dir) > MERGE_TIMEOUT }
  FileUtils.rm_rf(old)
  results = parts.map { |dir| File.join(dir, '.resultset.json') }.select { |file| File.exist?(file) }
  return if results.empty?
  # Else the merged result would be added to the previous one
  FileUtils.rm_f(File.join(ROOT, DIR, '.resultset.json'))
  SimpleCov.root(ROOT)
  SimpleCov.coverage_dir(DIR)
  percent = nil
  # The merged command name lists all process UUIDs: show their number instead
  SimpleCov.at_exit do
    result = SimpleCov.result
    result.command_name = "#{result.command_names.size} processes"
    result.format!
    percent = result.covered_percent
  end
  SimpleCov.collate(results, processes: Etc.nprocessors) { Aspera::Coverage.filters(self) }
  percent
end

.start ⇒ Object

Start coverage of current process. Must be called before any library is loaded.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aspera/coverage.rb', line 38

def start
  require 'simplecov'
  require 'securerandom'
  name = SecureRandom.uuid
  SimpleCov.root(ROOT)
  SimpleCov.coverage_dir(File.join(PARTS, name))
  SimpleCov.command_name(name)
  SimpleCov.merge_subprocesses(true)
  SimpleCov.merge_timeout(MERGE_TIMEOUT)
  # The trend of a single process is meaningless
  SimpleCov.history_limit(0)
  # Only store this process result (in its own folder): no report
  SimpleCov.at_exit { SimpleCov.result }
  SimpleCov.start { Aspera::Coverage.filters(self) }
end

.write_badge(percent) ⇒ Object

Write the coverage badge: a shields.io endpoint file, shown in the top README.md

Parameters:

  • percent (Float) —

    Percentage of covered lines



84
85
86
87
88
# File 'lib/aspera/coverage.rb', line 84

def write_badge(percent)
  require 'json'
  color = BADGE_COLORS.find { |limit, _| percent >= limit }&.last || 'red'
  File.write(File.join(ROOT, BADGE), "#{JSON.pretty_generate(schemaVersion: 1, label: 'coverage', message: format('%.1f%%', percent), color: color)}\n")
end