Sha256: 4d1958902724da0176acaf5b17c0fee4fdadf80062d0e0b1799e6964aff1dd3f

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require "minitest/retry/version"

module Minitest
  module Retry
    def self.use!(retry_count: 3, io: $stdout, verbose: true, exceptions_to_retry: [])
      @retry_count, @io, @verbose, @exceptions_to_retry = retry_count, io, verbose, exceptions_to_retry
      @failure_callback = nil
      Minitest.prepend(self)
    end

    def self.on_failure(&block)
      return unless block_given?
      @failure_callback = block
    end

    def self.retry_count
      @retry_count
    end

    def self.io
      @io
    end

    def self.verbose
      @verbose
    end

    def self.exceptions_to_retry
      @exceptions_to_retry
    end

    def self.failure_callback
      @failure_callback
    end

    def self.failure_to_retry?(failures = [])
      return false if failures.empty?
      return true if Minitest::Retry.exceptions_to_retry.empty?
      errors = failures.map(&:error).map(&:class)
      (errors & Minitest::Retry.exceptions_to_retry).any?
    end

    module ClassMethods
      def run_one_method(klass, method_name)
        result = super(klass, method_name)
        return result unless Minitest::Retry.failure_to_retry?(result.failures)
        if !result.skipped?
          Minitest::Retry.failure_callback.call if Minitest::Retry.failure_callback
          Minitest::Retry.retry_count.times do |count|
            if Minitest::Retry.verbose && Minitest::Retry.io
              msg = "[MinitestRetry] retry '%s' count: %s,  msg: %s\n" %
                [method_name, count + 1, result.failures.map(&:message).join(",")]
              Minitest::Retry.io.puts(msg)
            end

            result = super(klass, method_name)
            break if result.failures.empty?
          end
        end
        result
      end
    end

    def self.prepended(base)
      class << base
        prepend ClassMethods
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minitest-retry-0.1.7 lib/minitest/retry.rb