minitest 5.20.0 → 5.25.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/minitest/mock.rb CHANGED
@@ -8,9 +8,9 @@ module Minitest # :nodoc:
8
8
  # All mock objects are an instance of Mock
9
9
 
10
10
  class Mock
11
- alias :__respond_to? :respond_to?
11
+ alias __respond_to? respond_to?
12
12
 
13
- overridden_methods = %w[
13
+ overridden_methods = %i[
14
14
  ===
15
15
  class
16
16
  inspect
@@ -23,8 +23,10 @@ module Minitest # :nodoc:
23
23
  to_s
24
24
  ]
25
25
 
26
+ overridden_methods << :singleton_method_added if defined?(::DEBUGGER__)
27
+
26
28
  instance_methods.each do |m|
27
- undef_method m unless overridden_methods.include?(m.to_s) || m =~ /^__/
29
+ undef_method m unless overridden_methods.include?(m) || m =~ /^__/
28
30
  end
29
31
 
30
32
  overridden_methods.map(&:to_sym).each do |method_id|
@@ -91,7 +93,7 @@ module Minitest # :nodoc:
91
93
  def expect name, retval, args = [], **kwargs, &blk
92
94
  name = name.to_sym
93
95
 
94
- if block_given?
96
+ if blk then
95
97
  raise ArgumentError, "args ignored when block given" unless args.empty?
96
98
  raise ArgumentError, "kwargs ignored when block given" unless kwargs.empty?
97
99
  @expected_calls[name] << { :retval => retval, :block => blk }
@@ -104,7 +106,7 @@ module Minitest # :nodoc:
104
106
  kwargs = args.pop
105
107
  else
106
108
  unless @@KW_WARNED then
107
- from = caller.first
109
+ from = caller(1..1).first
108
110
  warn "Using MT_KWARGS_HAC\K yet passing kwargs. From #{from}"
109
111
  @@KW_WARNED = true
110
112
  end
@@ -139,7 +141,7 @@ module Minitest # :nodoc:
139
141
 
140
142
  def verify
141
143
  @expected_calls.each do |name, expected|
142
- actual = @actual_calls.fetch(name, nil)
144
+ actual = @actual_calls.fetch name, nil # defaults to []
143
145
  raise MockExpectationError, "expected #{__call name, expected[0]}" unless actual
144
146
  raise MockExpectationError, "expected #{__call name, expected[actual.size]}, got [#{__call name, actual}]" if
145
147
  actual.size < expected.size
@@ -148,7 +150,7 @@ module Minitest # :nodoc:
148
150
  end
149
151
 
150
152
  def method_missing sym, *args, **kwargs, &block # :nodoc:
151
- unless @expected_calls.key?(sym) then
153
+ unless @expected_calls.key? sym then
152
154
  if @delegator && @delegator.respond_to?(sym)
153
155
  if kwargs.empty? then # FIX: drop this after 2.7 dead
154
156
  return @delegator.public_send(sym, *args, &block)
@@ -170,9 +172,9 @@ module Minitest # :nodoc:
170
172
  end
171
173
 
172
174
  expected_args, expected_kwargs, retval, val_block =
173
- expected_call.values_at(:args, :kwargs, :retval, :block)
175
+ expected_call.values_at :args, :kwargs, :retval, :block
174
176
 
175
- expected_kwargs = kwargs.map { |ak, av| [ak, Object] }.to_h if
177
+ expected_kwargs = kwargs.to_h { |ak, av| [ak, Object] } if
176
178
  Hash == expected_kwargs
177
179
 
178
180
  if val_block then
@@ -195,7 +197,7 @@ module Minitest # :nodoc:
195
197
  [sym, expected_kwargs.size, kwargs]
196
198
  end
197
199
 
198
- zipped_args = expected_args.zip(args)
200
+ zipped_args = expected_args.zip args
199
201
  fully_matched = zipped_args.all? { |mod, a|
200
202
  mod === a or mod == a
201
203
  }
@@ -210,10 +212,10 @@ module Minitest # :nodoc:
210
212
  raise MockExpectationError, fmt % [sym, expected_kwargs.keys, kwargs.keys]
211
213
  end
212
214
 
213
- zipped_kwargs = expected_kwargs.map { |ek, ev|
215
+ zipped_kwargs = expected_kwargs.to_h { |ek, ev|
214
216
  av = kwargs[ek]
215
217
  [ek, [ev, av]]
216
- }.to_h
218
+ }
217
219
 
218
220
  fully_matched = zipped_kwargs.all? { |ek, (ev, av)|
219
221
  ev === av or ev == av
@@ -226,8 +228,8 @@ module Minitest # :nodoc:
226
228
 
227
229
  @actual_calls[sym] << {
228
230
  :retval => retval,
229
- :args => zipped_args.map { |e, a| e === a ? e : a },
230
- :kwargs => zipped_kwargs.map { |k, (e, a)| [k, e === a ? e : a] }.to_h,
231
+ :args => zipped_args.map { |e, a| e === a ? e : a },
232
+ :kwargs => zipped_kwargs.to_h { |k, (e, a)| [k, e === a ? e : a] },
231
233
  }
232
234
 
233
235
  retval
@@ -236,7 +238,7 @@ module Minitest # :nodoc:
236
238
  def respond_to? sym, include_private = false # :nodoc:
237
239
  return true if @expected_calls.key? sym.to_sym
238
240
  return true if @delegator && @delegator.respond_to?(sym, include_private)
239
- __respond_to?(sym, include_private)
241
+ __respond_to? sym, include_private
240
242
  end
241
243
  end
242
244
  end
@@ -1,5 +1,5 @@
1
1
  module Minitest
2
- module Parallel #:nodoc:
2
+ module Parallel # :nodoc:
3
3
 
4
4
  ##
5
5
  # The engine used to run multiple tests in parallel.
@@ -16,7 +16,7 @@ module Minitest
16
16
 
17
17
  def initialize size
18
18
  @size = size
19
- @queue = Queue.new
19
+ @queue = Thread::Queue.new
20
20
  @pool = nil
21
21
  end
22
22
 
@@ -24,10 +24,10 @@ module Minitest
24
24
  # Start the executor
25
25
 
26
26
  def start
27
- @pool = size.times.map {
28
- Thread.new(@queue) do |queue|
27
+ @pool = Array.new(size) {
28
+ Thread.new @queue do |queue|
29
29
  Thread.current.abort_on_exception = true
30
- while (job = queue.pop)
30
+ while job = queue.pop do
31
31
  klass, method, reporter = job
32
32
  reporter.synchronize { reporter.prerecord klass, method }
33
33
  result = Minitest.run_one_method klass, method
@@ -8,13 +8,13 @@ module Minitest
8
8
  end
9
9
 
10
10
  def self.plugin_pride_init options # :nodoc:
11
- if PrideIO.pride? then
12
- klass = ENV["TERM"] =~ /^xterm|-256color$/ ? PrideLOL : PrideIO
13
- io = klass.new options[:io]
11
+ return unless PrideIO.pride?
14
12
 
15
- self.reporter.reporters.grep(Minitest::Reporter).each do |rep|
16
- rep.io = io if rep.io.tty?
17
- end
13
+ klass = ENV["TERM"] =~ /^xterm|-256color$/ ? PrideLOL : PrideIO
14
+ io = klass.new options[:io]
15
+
16
+ self.reporter.reporters.grep(Minitest::Reporter).each do |rep|
17
+ rep.io = io if rep.io.tty?
18
18
  end
19
19
  end
20
20
 
@@ -59,12 +59,10 @@ module Minitest
59
59
 
60
60
  def print o
61
61
  case o
62
- when "." then
62
+ when ".", "S" then
63
63
  io.print pride o
64
64
  when "E", "F" then
65
65
  io.print "#{ESC}41m#{ESC}37m#{o}#{NND}"
66
- when "S" then
67
- io.print pride o
68
66
  else
69
67
  io.print o
70
68
  end
@@ -72,11 +70,9 @@ module Minitest
72
70
 
73
71
  def puts *o # :nodoc:
74
72
  o.map! { |s|
75
- s.to_s.sub(/Finished/) {
73
+ s.to_s.sub("Finished") {
76
74
  @index = 0
77
- "Fabulous run".split(//).map { |c|
78
- pride(c)
79
- }.join
75
+ "Fabulous run".chars.map { |c| pride(c) }.join
80
76
  }
81
77
  }
82
78
 
@@ -113,19 +109,16 @@ module Minitest
113
109
  #
114
110
  # plot (3*sin(x)+3), (3*sin(x+2*pi/3)+3), (3*sin(x+4*pi/3)+3)
115
111
 
116
- # 6 has wide pretty gradients. 3 == lolcat, about half the width
117
- @colors = (0...(6 * 7)).map { |n|
118
- n *= 1.0 / 6
112
+ @colors = Array.new(6 * 7) { |n|
113
+ n *= 1.0 / 3
119
114
  r = (3 * Math.sin(n ) + 3).to_i
120
- g = (3 * Math.sin(n + 2 * PI_3) + 3).to_i
121
- b = (3 * Math.sin(n + 4 * PI_3) + 3).to_i
122
-
123
- # Then we take rgb and encode them in a single number using base 6.
124
- # For some mysterious reason, we add 16... to clear the bottom 4 bits?
125
- # Yes... they're ugly.
115
+ g = (3 * Math.sin(n + 4 * PI_3) + 3).to_i
116
+ b = (3 * Math.sin(n + 2 * PI_3) + 3).to_i
126
117
 
118
+ # Then we take rgb and encode them in a single number using
119
+ # base 6, shifted by 16 for the base 16 ansi colors.
127
120
  36 * r + 6 * g + b + 16
128
- }
121
+ }.rotate(4) # puts "red" first
129
122
 
130
123
  super
131
124
  end
data/lib/minitest/spec.rb CHANGED
@@ -4,11 +4,11 @@ class Module # :nodoc:
4
4
  def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
5
5
  block = dont_flip == :block
6
6
  dont_flip = false if block
7
- target_obj = block ? '_{obj.method}' : '_(obj)'
7
+ target_obj = block ? "_{obj.method}" : "_(obj)"
8
8
 
9
9
  # https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html
10
10
  # Drop this when we can drop ruby 2.6 (aka after rails 6.1 EOL, ~2024-06)
11
- kw_extra = "ruby2_keywords %p" % [new_name] if respond_to?(:ruby2_keywords, true)
11
+ kw_extra = "ruby2_keywords %p" % [new_name] if respond_to? :ruby2_keywords, true
12
12
 
13
13
  # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
14
14
  self.class_eval <<-EOM, __FILE__, __LINE__ + 1
@@ -249,7 +249,7 @@ class Minitest::Spec < Minitest::Test
249
249
  pre, post = "let '#{name}' cannot ", ". Please use another name."
250
250
  methods = Minitest::Spec.instance_methods.map(&:to_s) - %w[subject]
251
251
  raise ArgumentError, "#{pre}begin with 'test'#{post}" if
252
- name =~ /\Atest/
252
+ name.start_with? "test"
253
253
  raise ArgumentError, "#{pre}override a method in Minitest::Spec#{post}" if
254
254
  methods.include? name
255
255
 
@@ -268,7 +268,7 @@ class Minitest::Spec < Minitest::Test
268
268
  end
269
269
 
270
270
  def create name, desc # :nodoc:
271
- cls = Class.new(self) do
271
+ cls = Class.new self do
272
272
  @name = name
273
273
  @desc = desc
274
274
 
@@ -289,7 +289,7 @@ class Minitest::Spec < Minitest::Test
289
289
  end
290
290
 
291
291
  attr_reader :desc # :nodoc:
292
- alias :specify :it
292
+ alias specify it
293
293
 
294
294
  ##
295
295
  # Rdoc... why are you so dumb?
data/lib/minitest/test.rb CHANGED
@@ -52,9 +52,10 @@ module Minitest
52
52
  end
53
53
 
54
54
  ##
55
- # Call this at the top of your tests when you want to run your
56
- # tests in parallel. In doing so, you're admitting that you rule
57
- # and your tests are awesome.
55
+ # Call this at the top of your tests (inside the +Minitest::Test+
56
+ # subclass or +describe+ block) when you want to run your tests in
57
+ # parallel. In doing so, you're admitting that you rule and your
58
+ # tests are awesome.
58
59
 
59
60
  def self.parallelize_me!
60
61
  include Minitest::Parallel::Test
@@ -84,20 +85,18 @@ module Minitest
84
85
  # Runs a single test with setup/teardown hooks.
85
86
 
86
87
  def run
87
- with_info_handler do
88
- time_it do
89
- capture_exceptions do
90
- SETUP_METHODS.each do |hook|
91
- self.send hook
92
- end
93
-
94
- self.send self.name
88
+ time_it do
89
+ capture_exceptions do
90
+ SETUP_METHODS.each do |hook|
91
+ self.send hook
95
92
  end
96
93
 
97
- TEARDOWN_METHODS.each do |hook|
98
- capture_exceptions do
99
- self.send hook
100
- end
94
+ self.send self.name
95
+ end
96
+
97
+ TEARDOWN_METHODS.each do |hook|
98
+ capture_exceptions do
99
+ self.send hook
101
100
  end
102
101
  end
103
102
  end
@@ -229,16 +228,6 @@ module Minitest
229
228
  ne
230
229
  end
231
230
 
232
- def with_info_handler &block # :nodoc:
233
- t0 = Minitest.clock_time
234
-
235
- handler = lambda do
236
- warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Minitest.clock_time - t0]
237
- end
238
-
239
- self.class.on_signal ::Minitest.info_signal, handler, &block
240
- end
241
-
242
231
  include LifecycleHooks
243
232
  include Guard
244
233
  extend Guard
@@ -1,6 +1,12 @@
1
1
  require "shellwords"
2
2
  require "rbconfig"
3
- require "rake/tasklib"
3
+
4
+ begin
5
+ require "rake/tasklib"
6
+ rescue LoadError => e
7
+ warn e.message
8
+ return
9
+ end
4
10
 
5
11
  module Minitest # :nodoc:
6
12
 
@@ -114,7 +120,7 @@ module Minitest # :nodoc:
114
120
  self.test_globs = ["test/**/test_*.rb",
115
121
  "test/**/*_test.rb"]
116
122
  self.test_prelude = nil
117
- self.verbose = Rake.application.options.trace
123
+ self.verbose = Rake.application.options.trace || Rake.verbose == true
118
124
  self.warning = true
119
125
  end
120
126
 
@@ -144,7 +150,7 @@ module Minitest # :nodoc:
144
150
  ENV["N"] && ENV["N"].to_i > 0
145
151
 
146
152
  lib_extras = (ENV["MT_LIB_EXTRAS"] || "").split File::PATH_SEPARATOR
147
- self.libs[0,0] = lib_extras
153
+ self.libs[0, 0] = lib_extras
148
154
 
149
155
  extra_args << "-n" << ENV["N"] if ENV["N"]
150
156
  extra_args << "-e" << ENV["X"] if ENV["X"]
@@ -163,7 +169,7 @@ module Minitest # :nodoc:
163
169
  def define # :nodoc:
164
170
  desc "Run the test suite. Use N, X, A, and TESTOPTS to add flags/args."
165
171
  task name do
166
- ruby make_test_cmd, verbose:verbose
172
+ ruby make_test_cmd, verbose: verbose
167
173
  end
168
174
 
169
175
  desc "Print out the test command. Good for profiling and other tools."
@@ -177,7 +183,7 @@ module Minitest # :nodoc:
177
183
 
178
184
  # 3 seems to be the magic number... (tho not by that much)
179
185
  bad, good, n = {}, [], (ENV.delete("K") || 3).to_i
180
- file = ENV.delete("F")
186
+ file = ENV.delete "F"
181
187
  times = {}
182
188
 
183
189
  tt0 = Time.now
@@ -238,7 +244,7 @@ module Minitest # :nodoc:
238
244
 
239
245
  task "#{name}:deps" => "#{name}:isolated" # now just an alias
240
246
 
241
- desc "Show bottom 25 tests wrt time."
247
+ desc "Run the test suite and report the slowest 25 tests."
242
248
  task "#{name}:slow" do
243
249
  sh ["rake #{name} A=-v",
244
250
  "egrep '#test_.* s = .'",
@@ -262,11 +268,11 @@ module Minitest # :nodoc:
262
268
  runner = runner.join "; "
263
269
 
264
270
  args = []
265
- args << "-I#{libs.join(File::PATH_SEPARATOR)}" unless libs.empty?
271
+ args << "-I#{libs.join File::PATH_SEPARATOR}" unless libs.empty?
266
272
  args << "-w" if warning
267
- args << '-e'
273
+ args << "-e"
268
274
  args << "'#{runner}'"
269
- args << '--'
275
+ args << "--"
270
276
  args << extra_args.map(&:shellescape)
271
277
 
272
278
  args.join " "
@@ -287,11 +293,10 @@ class Work < Queue # :nodoc:
287
293
  end
288
294
 
289
295
  class Integer # :nodoc:
290
- def threads_do(jobs) # :nodoc:
291
- require "thread"
296
+ def threads_do jobs # :nodoc:
292
297
  q = Work.new jobs
293
298
 
294
- self.times.map {
299
+ Array.new(self) {
295
300
  Thread.new do
296
301
  while job = q.pop # go until quit value
297
302
  yield job