Module: Minitest::Retry

Defined in:
lib/minitest/retry.rb,
lib/minitest/retry/version.rb

Defined Under Namespace

Modules: ClassMethods, RunnableMethods

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.classes_to_retryObject



47
48
49
# File 'lib/minitest/retry.rb', line 47

def classes_to_retry
  @classes_to_retry
end

.consistent_failure_callbackObject



55
56
57
# File 'lib/minitest/retry.rb', line 55

def consistent_failure_callback
  @consistent_failure_callback
end

.exceptions_to_retryObject



39
40
41
# File 'lib/minitest/retry.rb', line 39

def exceptions_to_retry
  @exceptions_to_retry
end

.exceptions_to_skipObject



67
68
69
# File 'lib/minitest/retry.rb', line 67

def exceptions_to_skip
  @exceptions_to_skip
end

.failure_callbackObject



51
52
53
# File 'lib/minitest/retry.rb', line 51

def failure_callback
  @failure_callback
end

.failure_to_retry?(failures = [], klass_method_name, klass) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/minitest/retry.rb', line 71

def failure_to_retry?(failures = [], klass_method_name, klass)
  return false if failures.empty?

  if methods_to_retry.any?
    return methods_to_retry.include?(klass_method_name)
  end

  if exceptions_to_retry.any?
    errors = failures.map(&:error).map(&:class)
    return (errors & exceptions_to_retry).any?
  end

  if methods_to_skip.any?
    return !methods_to_skip.include?(klass_method_name)
  end

  if exceptions_to_skip.any?
    errors = failures.map(&:error).map(&:class)
    return !(errors & exceptions_to_skip).any?
  end

  return true if classes_to_retry.empty?
  ancestors = klass.ancestors.map(&:to_s)
  return classes_to_retry.any? { |class_to_retry| ancestors.include?(class_to_retry) }
end

.ioObject



31
32
33
# File 'lib/minitest/retry.rb', line 31

def io
  @io
end

.methods_to_retryObject



43
44
45
# File 'lib/minitest/retry.rb', line 43

def methods_to_retry
  @methods_to_retry
end

.methods_to_skipObject



63
64
65
# File 'lib/minitest/retry.rb', line 63

def methods_to_skip
  @methods_to_skip
end

.on_consistent_failure(&block) ⇒ Object



17
18
19
20
# File 'lib/minitest/retry.rb', line 17

def on_consistent_failure(&block)
  return unless block_given?
  @consistent_failure_callback = block
end

.on_failure(&block) ⇒ Object



12
13
14
15
# File 'lib/minitest/retry.rb', line 12

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

.on_retry(&block) ⇒ Object



22
23
24
25
# File 'lib/minitest/retry.rb', line 22

def on_retry(&block)
  return unless block_given?
  @retry_callback = block
end

.prepended(base) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/minitest/retry.rb', line 145

def self.prepended(base)
  if Minitest::VERSION > "6"
    class << Minitest::Runnable
      prepend RunnableMethods
    end
  else
    class << base
      prepend ClassMethods
    end
  end
end

.retry_callbackObject



59
60
61
# File 'lib/minitest/retry.rb', line 59

def retry_callback
  @retry_callback
end

.retry_countObject



27
28
29
# File 'lib/minitest/retry.rb', line 27

def retry_count
  @retry_count
end

.run_with_retry(klass, method_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/minitest/retry.rb', line 97

def run_with_retry(klass, method_name)
  klass_method_name = "#{klass.name}##{method_name}"
  result = yield

  return result unless failure_to_retry?(result.failures, klass_method_name, klass)
  return result if result.skipped?

  failure_callback&.call(klass, method_name, result)

  retry_count.times do |count|
    retry_callback&.call(klass, method_name, count + 1, result)

    if verbose && io
      msg = "[MinitestRetry] retry '%s' count: %s,  msg: %s\n" %
        [method_name, count + 1, result.failures.map(&:message).join(",")]
      io.puts(msg)
    end

    result = yield
    break if result.failures.empty?
  end

  if consistent_failure_callback && !result.failures.empty?
    consistent_failure_callback.call(klass, method_name, result)
  end

  result
end

.use!(retry_count: 3, io: $stdout, verbose: true, exceptions_to_retry: [], methods_to_retry: [], classes_to_retry: [], methods_to_skip: [], exceptions_to_skip: []) ⇒ Object



6
7
8
9
10
# File 'lib/minitest/retry.rb', line 6

def use!(retry_count: 3, io: $stdout, verbose: true, exceptions_to_retry: [], methods_to_retry: [], classes_to_retry: [], methods_to_skip: [], exceptions_to_skip: [])
  @retry_count, @io, @verbose, @exceptions_to_retry, @methods_to_retry, @classes_to_retry, @methods_to_skip, @exceptions_to_skip = retry_count, io, verbose, exceptions_to_retry, methods_to_retry, classes_to_retry, methods_to_skip, exceptions_to_skip
  @failure_callback, @consistent_failure_callback, @retry_callback = nil, nil, nil
  Minitest.prepend(self)
end

.verboseObject



35
36
37
# File 'lib/minitest/retry.rb', line 35

def verbose
  @verbose
end