Sha256: dc3fb24c37bfe640ee631bf60029e1e41628aa3a8d5ed16aaf6495a91508d126

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'simplecov'
require 'json'
require_relative 'simplecov-shield-json/version'

module SimpleCov
  module Formatter
    # shield json generator
    class ShieldJSONFormatter
      def format(result)
        json = shield_json(result)

        File.open(output_filepath, 'w+') do |file|
          file.puts json
        end

        puts output_message(result)

        json
      end

      private

      def output_filename
        'shield-coverage.json'
      end

      def output_filepath
        File.join(output_path, output_filename)
      end

      def output_message(result)
        "Coverage report generated for #{result.command_name} to #{output_filepath}.  \
        #{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered."
      end

      def output_path
        SimpleCov.coverage_path
      end

      def color(coverage_percent)
        case coverage_percent
        when 90..100
          'brightgreen'
        when 80..89
          'yellow'
        else
          'red'
        end
      end

      def shield_json(result)
        {
          schemaVersion: 1,
          label: 'Coverage',
          message: "#{result.covered_percent.round(2)}%",
          color: color(result.covered_percent),
          cacheSeconds: 1800
        }.to_json
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simplecov-shield-json-0.0.6 lib/simplecov-shield-json.rb
simplecov-shield-json-0.0.5 lib/simplecov-shield-json.rb
simplecov-shield-json-0.0.4 lib/simplecov-shield-json.rb