Rails-8-API-Image-Processing / spec /lib /coverage_baseline_spec.rb
denise-oneill-inbox-ru's picture
D8 staging from 92665ef4298ecff6735b080ff7901f2c19f719a7 (part 2)
38367b5 verified
Raw History Blame Contribute Delete
2.86 kB
# frozen_string_literal: true
require "spec_helper"
require "tempfile"
require_relative "../../lib/coverage_baseline"
RSpec.describe CoverageBaseline do
describe ".below?" do
let(:baseline) { CoverageBaseline::Result.new(line: 75.0, branch: 60.0) }
it "returns false when current meets or exceeds both line and branch baseline" do
current_equal = CoverageBaseline::Result.new(line: 75.0, branch: 60.0)
current_higher = CoverageBaseline::Result.new(line: 80.0, branch: 65.0)
expect(described_class.below?(current: current_equal, baseline: baseline)).to be false
expect(described_class.below?(current: current_higher, baseline: baseline)).to be false
end
it "returns true when current line coverage drops below baseline" do
current = CoverageBaseline::Result.new(line: 74.9, branch: 60.0)
expect(described_class.below?(current: current, baseline: baseline)).to be true
end
it "returns true when current branch coverage drops below baseline" do
current = CoverageBaseline::Result.new(line: 75.0, branch: 59.9)
expect(described_class.below?(current: current, baseline: baseline)).to be true
end
it "returns true when both line and branch coverage drop below baseline" do
current = CoverageBaseline::Result.new(line: 70.0, branch: 50.0)
expect(described_class.below?(current: current, baseline: baseline)).to be true
end
end
describe ".load_baseline" do
let(:temp_yaml) { Tempfile.new([ "coverage_baseline", ".yml" ]) }
after { temp_yaml.unlink }
it "loads line and branch thresholds from a YAML file" do
temp_yaml.write("line: 75.5\nbranch: 62.3\n")
temp_yaml.flush
result = described_class.load_baseline(temp_yaml.path)
expect(result.line).to eq(75.5)
expect(result.branch).to eq(62.3)
end
it "raises ArgumentError if required keys are missing" do
temp_yaml.write("line: 75.5\n")
temp_yaml.flush
expect {
described_class.load_baseline(temp_yaml.path)
}.to raise_error(ArgumentError, /missing required coverage keys/)
end
end
describe ".load_last_run" do
let(:temp_json) { Tempfile.new([ "last_run", ".json" ]) }
after { temp_json.unlink }
it "loads line and branch percentages from SimpleCov .last_run.json" do
temp_json.write('{"result":{"line":82.45,"branch":68.12}}')
temp_json.flush
result = described_class.load_last_run(temp_json.path)
expect(result.line).to eq(82.45)
expect(result.branch).to eq(68.12)
end
it "raises ArgumentError if file is malformed or missing result" do
temp_json.write('{"other":123}')
temp_json.flush
expect {
described_class.load_last_run(temp_json.path)
}.to raise_error(ArgumentError, /missing result in coverage data/)
end
end
end