Sha256: d4bde87cda4d6226dc3435341c30e2e3f7eb7d70216167d330f0e9e41f8171b7

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module SimpleCov
  module ExitCodes
    class MinimumCoverageByFileCheck
      def initialize(result, minimum_coverage_by_file)
        @result = result
        @minimum_coverage_by_file = minimum_coverage_by_file
      end

      def failing?
        minimum_violations.any?
      end

      def report
        minimum_violations.each do |violation|
          $stderr.printf(
            "%<criterion>s coverage (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%).\n",
            covered: SimpleCov.round_coverage(violation.fetch(:actual)),
            minimum_coverage: violation.fetch(:minimum_expected),
            criterion: violation.fetch(:criterion).capitalize
          )
        end
      end

      def exit_code
        SimpleCov::ExitCodes::MINIMUM_COVERAGE
      end

    private

      attr_reader :result, :minimum_coverage_by_file

      def minimum_violations
        @minimum_violations ||=
          compute_minimum_coverage_data.select do |achieved|
            achieved.fetch(:actual) < achieved.fetch(:minimum_expected)
          end
      end

      def compute_minimum_coverage_data
        minimum_coverage_by_file.flat_map do |criterion, expected_percent|
          result.coverage_statistics_by_file[criterion].map do |actual_percent|
            {
              criterion: criterion,
              minimum_expected: expected_percent,
              actual: SimpleCov.round_coverage(actual_percent)
            }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simplecov-0.21.0 lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb