Sha256: bdd13d85572c3b4a8187e8669444cbaf5921d8f82b9f8459fb50ea76da15742c

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

require 'fileutils'

fail 'simplecov-lcov requires simplecov' unless defined?(SimpleCov)

module SimpleCov
  module Formatter
    # Custom Formatter to generate lcov style coverage for simplecov
    class LcovFormatter
      # generate lcov style coverage.
      # ==== Args
      # _result_ :: [SimpleCov::Result] abcoverage result instance.
      def format(result)
        create_output_directory!
        result.files.each { |file| write_lcov!(file) }
      end

      # Output directory for generated files.
      # ==== Return
      # Path for output directory.
      def self.output_directory
        File.join(SimpleCov.coverage_path, 'lcov')
      end

      private

      def create_output_directory!
        return if Dir.exist?(self.class.output_directory)
        FileUtils.mkdir_p(self.class.output_directory)
      end

      def write_lcov!(file)
        File.open(File.join(self.class.output_directory, output_filename(file.filename)), 'w') do |f|
          f.write format_file(file)
        end
      end

      def output_filename(filename)
        filename.gsub("#{SimpleCov.root}/", '').gsub('/', '-')
          .tap { |name| name << '.lcov' }
      end

      def format_file(file)
        "SF:#{file.filename}\n".tap do |content|
          file.lines.reject(&:never?).reject(&:skipped?)
            .each do |line|
            content << "DA:#{line.number},#{line.coverage}\n"
          end
          content << "end_of_record\n"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simplecov-lcov-0.1.3 lib/simplecov-lcov.rb
simplecov-lcov-0.1.2 lib/simplecov-lcov.rb
simplecov-lcov-0.1.1 lib/simplecov-lcov.rb