Sha256: 469eb784fcf63202332bec15bbdd8d1137e35e8cb2420e07358b57b97eb454f9

Contents?: true

Size: 1.81 KB

Versions: 57

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true
require 'minitest/reporters'

module Minitest
  module Queue
    class TestTimeReporter < Minitest::Reporters::BaseReporter
      include ::CI::Queue::OutputHelpers

      def initialize(build:, limit: nil, percentile: nil, **options)
        super(options)
        @test_time_hash = build.fetch
        @limit = limit
        @percentile = percentile
        @success = true
      end

      def report
        return if limit.nil? || test_time_hash.empty?

        puts '+++ Test Time Report'

        if offending_tests.empty?
          msg = "The #{humanized_percentile} of test execution time is within #{limit} milliseconds."
          puts green(msg)
          return
        end

        @success = false
        puts <<~EOS
          #{red("Detected #{offending_tests.size} test(s) over the desired time limit.")}
          Please make them faster than #{limit}ms in the #{humanized_percentile} percentile.
        EOS
        offending_tests.each do |test_name, duration|
          puts "#{red(test_name)}: #{duration}ms"
        end
      end

      def success?
        @success
      end

      def record(*)
        raise NotImplementedError
      end

      private

      attr_reader :test_time_hash, :limit, :percentile

      def humanized_percentile
        percentile_in_percentage = percentile * 100
        "#{percentile_in_percentage.to_i}th"
      end

      def offending_tests
        @offending_tests ||= begin
          test_time_hash.each_with_object({}) do |(test_name, durations), offenders|
            duration = calculate_percentile(durations)
            next if duration <= limit
            offenders[test_name] = duration
          end
        end
      end

      def calculate_percentile(array)
        array.sort[(percentile * array.length).ceil - 1]
      end
    end
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
ci-queue-0.58.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.57.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.56.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.55.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.52.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.51.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.50.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.49.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.48.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.47.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.46.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.45.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.44.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.43.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.42.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.41.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.40.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.39.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.38.0 lib/minitest/queue/test_time_reporter.rb
ci-queue-0.37.0 lib/minitest/queue/test_time_reporter.rb