Sha256: 6643d7d5ee64721d4ba9ae589d49c5109a76c16c98ab2fe0036f54d7632d2860

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true
module Coverband
  module Collectors
    class Delta
      @semaphore = Mutex.new
      @@previous_coverage = nil
      attr_reader :current_coverage

      def initialize(current_coverage)
        @current_coverage = current_coverage
      end

      class RubyCoverage
        def self.results
          ::Coverage.peek_result.dup
        end
      end

      def self.results(process_coverage = RubyCoverage)
        @semaphore.synchronize do
          set_default_results
          new(process_coverage.results).results
        end
      end

      def self.previous_results
        @@previous_coverage
      end

      def self.set_default_results
        @@previous_coverage ||= {}
      end

      def results
        new_results = generate
        @@previous_coverage = current_coverage
        new_results
      end

      def self.reset
        @@previous_coverage = nil
      end

      private

      def generate
        current_coverage.each_with_object({}) do |(file, line_counts), new_results|
          if @@previous_coverage[file]
            new_results[file] = array_diff(line_counts, @@previous_coverage[file])
          else
            new_results[file] = line_counts
          end
        end
      end

      def array_diff(latest, original)
        latest.map.with_index do |v, i|
          if (v && original[i])
            [0, v - original[i]].max
          else
            nil
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
coverband-4.2.0 lib/coverband/collectors/delta.rb
coverband-4.2.0.rc3 lib/coverband/collectors/delta.rb