Module: SimpleCov::Baseline::Parser

Extended by:
Parser
Included in:
Parser
Defined in:
lib/simplecov/baseline/parser.rb

Overview

Turns the YAML document of a baseline file into the {path => {criterion => Floor}} entries Baseline holds. The canonical shape is the criteria Hash to_yaml writes, but the lenient shapes stay readable: a bare number is a line-percent floor, and a criterion may carry a bare percent instead of the percent/missed pair. Anything else raises ConfigurationError, naming the file and the offending entry: a malformed policy must fail loudly rather than silently un-enforce every floor.

Instance Method Summary collapse

Instance Method Details

#call(data, path) ⇒ Object

Raises:



15
16
17
18
19
# File 'lib/simplecov/baseline/parser.rb', line 15

def call(data, path)
  raise ConfigurationError, "baseline file #{path} must map file paths to floors" unless data.is_a?(Hash)

  data.to_h { |file, entry| [file.to_s, parse_entry(file, entry, path)] }
end

#parse_criterion(file, criterion_key, path) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/simplecov/baseline/parser.rb', line 33

def parse_criterion(file, criterion_key, path)
  CRITERIA[criterion_key.to_s] || raise(
    ConfigurationError,
    "baseline file #{path}: unknown criterion #{criterion_key.inspect} for #{file} " \
    "(expected lines, branches, or methods)"
  )
end

#parse_entry(file, entry, path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simplecov/baseline/parser.rb', line 21

def parse_entry(file, entry, path)
  return {line: Floor.new(percent: entry, missed: nil)} if entry.is_a?(Numeric)

  unless entry.is_a?(Hash)
    raise ConfigurationError, "baseline file #{path}: entry for #{file} must be a number or a criteria Hash"
  end

  entry.to_h do |criterion_key, floor|
    [parse_criterion(file, criterion_key, path), parse_floor(file, floor, path)]
  end
end

#parse_floor(file, floor, path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simplecov/baseline/parser.rb', line 41

def parse_floor(file, floor, path)
  return Floor.new(percent: floor, missed: nil) if floor.is_a?(Numeric)

  percent = floor["percent"] if floor.is_a?(Hash)
  unless percent.is_a?(Numeric)
    raise ConfigurationError, "baseline file #{path}: floor for #{file} needs a numeric percent"
  end

  missed = floor["missed"]
  unless missed.nil? || missed.instance_of?(Integer)
    raise ConfigurationError, "baseline file #{path}: missed count for #{file} must be an integer"
  end

  Floor.new(percent: percent, missed: missed)
end