File size: 930 Bytes
38367b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

require "yaml"
require "json"

module CoverageBaseline
  Result = Data.define(:line, :branch)

  def self.below?(current:, baseline:)
    current.line < baseline.line || current.branch < baseline.branch
  end

  def self.load_baseline(path)
    data = YAML.load_file(path, aliases: true)
    unless data.is_a?(Hash) && data.key?("line") && data.key?("branch")
      raise ArgumentError, "missing required coverage keys: 'line' and 'branch' in #{path}"
    end

    Result.new(line: data.fetch("line").to_f, branch: data.fetch("branch").to_f)
  end

  def self.load_last_run(path)
    data = JSON.parse(File.read(path))
    result = data["result"]
    unless result.is_a?(Hash) && result.key?("line") && result.key?("branch")
      raise ArgumentError, "missing result in coverage data: #{path}"
    end

    Result.new(line: result.fetch("line").to_f, branch: result.fetch("branch").to_f)
  end
end