minitest 5.12.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ module Minitest
2
+ module Parallel #:nodoc:
3
+
4
+ ##
5
+ # The engine used to run multiple tests in parallel.
6
+
7
+ class Executor
8
+
9
+ ##
10
+ # The size of the pool of workers.
11
+
12
+ attr_reader :size
13
+
14
+ ##
15
+ # Create a parallel test executor of with +size+ workers.
16
+
17
+ def initialize size
18
+ @size = size
19
+ @queue = Queue.new
20
+ @pool = nil
21
+ end
22
+
23
+ ##
24
+ # Start the executor
25
+
26
+ def start
27
+ @pool = size.times.map {
28
+ Thread.new(@queue) do |queue|
29
+ Thread.current.abort_on_exception = true
30
+ while (job = queue.pop)
31
+ klass, method, reporter = job
32
+ reporter.synchronize { reporter.prerecord klass, method }
33
+ result = Minitest.run_one_method klass, method
34
+ reporter.synchronize { reporter.record result }
35
+ end
36
+ end
37
+ }
38
+ end
39
+
40
+ ##
41
+ # Add a job to the queue
42
+
43
+ def << work; @queue << work; end
44
+
45
+ ##
46
+ # Shuts down the pool of workers by signalling them to quit and
47
+ # waiting for them all to finish what they're currently working
48
+ # on.
49
+
50
+ def shutdown
51
+ size.times { @queue << nil }
52
+ @pool.each(&:join)
53
+ end
54
+ end
55
+
56
+ module Test # :nodoc:
57
+ def _synchronize; Minitest::Test.io_lock.synchronize { yield }; end # :nodoc:
58
+
59
+ module ClassMethods # :nodoc:
60
+ def run_one_method klass, method_name, reporter
61
+ Minitest.parallel_executor << [klass, method_name, reporter]
62
+ end
63
+
64
+ def test_order
65
+ :parallel
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+ require "minitest"
2
+
3
+ Minitest.load_plugins
4
+ Minitest::PrideIO.pride!
@@ -0,0 +1,142 @@
1
+ require "minitest"
2
+
3
+ module Minitest
4
+ def self.plugin_pride_options opts, _options # :nodoc:
5
+ opts.on "-p", "--pride", "Pride. Show your testing pride!" do
6
+ PrideIO.pride!
7
+ end
8
+ end
9
+
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]
14
+
15
+ self.reporter.reporters.grep(Minitest::Reporter).each do |rep|
16
+ rep.io = io if rep.io.tty?
17
+ end
18
+ end
19
+ end
20
+
21
+ ##
22
+ # Show your testing pride!
23
+
24
+ class PrideIO
25
+ ##
26
+ # Activate the pride plugin. Called from both -p option and minitest/pride
27
+
28
+ def self.pride!
29
+ @pride = true
30
+ end
31
+
32
+ ##
33
+ # Are we showing our testing pride?
34
+
35
+ def self.pride?
36
+ @pride ||= false
37
+ end
38
+
39
+ # Start an escape sequence
40
+ ESC = "\e["
41
+
42
+ # End the escape sequence
43
+ NND = "#{ESC}0m"
44
+
45
+ # The IO we're going to pipe through.
46
+ attr_reader :io
47
+
48
+ def initialize io # :nodoc:
49
+ @io = io
50
+ # stolen from /System/Library/Perl/5.10.0/Term/ANSIColor.pm
51
+ # also reference http://en.wikipedia.org/wiki/ANSI_escape_code
52
+ @colors ||= (31..36).to_a
53
+ @size = @colors.size
54
+ @index = 0
55
+ end
56
+
57
+ ##
58
+ # Wrap print to colorize the output.
59
+
60
+ def print o
61
+ case o
62
+ when "." then
63
+ io.print pride o
64
+ when "E", "F" then
65
+ io.print "#{ESC}41m#{ESC}37m#{o}#{NND}"
66
+ when "S" then
67
+ io.print pride o
68
+ else
69
+ io.print o
70
+ end
71
+ end
72
+
73
+ def puts *o # :nodoc:
74
+ o.map! { |s|
75
+ s.to_s.sub(/Finished/) {
76
+ @index = 0
77
+ "Fabulous run".split(//).map { |c|
78
+ pride(c)
79
+ }.join
80
+ }
81
+ }
82
+
83
+ io.puts(*o)
84
+ end
85
+
86
+ ##
87
+ # Color a string.
88
+
89
+ def pride string
90
+ string = "*" if string == "."
91
+ c = @colors[@index % @size]
92
+ @index += 1
93
+ "#{ESC}#{c}m#{string}#{NND}"
94
+ end
95
+
96
+ def method_missing msg, *args # :nodoc:
97
+ io.send(msg, *args)
98
+ end
99
+ end
100
+
101
+ ##
102
+ # If you thought the PrideIO was colorful...
103
+ #
104
+ # (Inspired by lolcat, but with clean math)
105
+
106
+ class PrideLOL < PrideIO
107
+ PI_3 = Math::PI / 3 # :nodoc:
108
+
109
+ def initialize io # :nodoc:
110
+ # walk red, green, and blue around a circle separated by equal thirds.
111
+ #
112
+ # To visualize, type this into wolfram-alpha:
113
+ #
114
+ # plot (3*sin(x)+3), (3*sin(x+2*pi/3)+3), (3*sin(x+4*pi/3)+3)
115
+
116
+ # 6 has wide pretty gradients. 3 == lolcat, about half the width
117
+ @colors = (0...(6 * 7)).map { |n|
118
+ n *= 1.0 / 6
119
+ 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.
126
+
127
+ 36 * r + 6 * g + b + 16
128
+ }
129
+
130
+ super
131
+ end
132
+
133
+ ##
134
+ # Make the string even more colorful. Damnit.
135
+
136
+ def pride string
137
+ c = @colors[@index % @size]
138
+ @index += 1
139
+ "#{ESC}38;5;#{c}m#{string}#{NND}"
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,335 @@
1
+ require "minitest/test"
2
+
3
+ class Module # :nodoc:
4
+ def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
5
+ block = dont_flip == :block
6
+ dont_flip = false if block
7
+
8
+ # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
9
+ self.class_eval <<-EOM, __FILE__, __LINE__ + 1
10
+ def #{new_name} *args
11
+ where = Minitest.filter_backtrace(caller).first
12
+ where = where.split(/:in /, 2).first # clean up noise
13
+ warn "DEPRECATED: global use of #{new_name} from #\{where}. Use _(obj).#{new_name} instead. This will fail in Minitest 6."
14
+ Minitest::Expectation.new(self, Minitest::Spec.current).#{new_name}(*args)
15
+ end
16
+ EOM
17
+
18
+ Minitest::Expectation.class_eval <<-EOM, __FILE__, __LINE__ + 1
19
+ def #{new_name} *args
20
+ raise "Calling ##{new_name} outside of test." unless ctx
21
+ case
22
+ when #{!!dont_flip} then
23
+ ctx.#{meth}(target, *args)
24
+ when #{block} && Proc === target then
25
+ ctx.#{meth}(*args, &target)
26
+ else
27
+ ctx.#{meth}(args.first, target, *args[1..-1])
28
+ end
29
+ end
30
+ EOM
31
+ end
32
+ end
33
+
34
+ Minitest::Expectation = Struct.new :target, :ctx # :nodoc:
35
+
36
+ ##
37
+ # Kernel extensions for minitest
38
+
39
+ module Kernel
40
+ ##
41
+ # Describe a series of expectations for a given target +desc+.
42
+ #
43
+ # Defines a test class subclassing from either Minitest::Spec or
44
+ # from the surrounding describe's class. The surrounding class may
45
+ # subclass Minitest::Spec manually in order to easily share code:
46
+ #
47
+ # class MySpec < Minitest::Spec
48
+ # # ... shared code ...
49
+ # end
50
+ #
51
+ # class TestStuff < MySpec
52
+ # it "does stuff" do
53
+ # # shared code available here
54
+ # end
55
+ # describe "inner stuff" do
56
+ # it "still does stuff" do
57
+ # # ...and here
58
+ # end
59
+ # end
60
+ # end
61
+ #
62
+ # For more information on getting started with writing specs, see:
63
+ #
64
+ # http://www.rubyinside.com/a-minitestspec-tutorial-elegant-spec-style-testing-that-comes-with-ruby-5354.html
65
+ #
66
+ # For some suggestions on how to improve your specs, try:
67
+ #
68
+ # http://betterspecs.org
69
+ #
70
+ # but do note that several items there are debatable or specific to
71
+ # rspec.
72
+ #
73
+ # For more information about expectations, see Minitest::Expectations.
74
+
75
+ def describe desc, *additional_desc, &block # :doc:
76
+ stack = Minitest::Spec.describe_stack
77
+ name = [stack.last, desc, *additional_desc].compact.join("::")
78
+ sclas = stack.last || if Class === self && kind_of?(Minitest::Spec::DSL) then
79
+ self
80
+ else
81
+ Minitest::Spec.spec_type desc, *additional_desc
82
+ end
83
+
84
+ cls = sclas.create name, desc
85
+
86
+ stack.push cls
87
+ cls.class_eval(&block)
88
+ stack.pop
89
+ cls
90
+ end
91
+ private :describe
92
+ end
93
+
94
+ ##
95
+ # Minitest::Spec -- The faster, better, less-magical spec framework!
96
+ #
97
+ # For a list of expectations, see Minitest::Expectations.
98
+
99
+ class Minitest::Spec < Minitest::Test
100
+
101
+ def self.current # :nodoc:
102
+ Thread.current[:current_spec]
103
+ end
104
+
105
+ def initialize name # :nodoc:
106
+ super
107
+ Thread.current[:current_spec] = self
108
+ end
109
+
110
+ ##
111
+ # Oh look! A Minitest::Spec::DSL module! Eat your heart out DHH.
112
+
113
+ module DSL
114
+ ##
115
+ # Contains pairs of matchers and Spec classes to be used to
116
+ # calculate the superclass of a top-level describe. This allows for
117
+ # automatically customizable spec types.
118
+ #
119
+ # See: register_spec_type and spec_type
120
+
121
+ TYPES = [[//, Minitest::Spec]]
122
+
123
+ ##
124
+ # Register a new type of spec that matches the spec's description.
125
+ # This method can take either a Regexp and a spec class or a spec
126
+ # class and a block that takes the description and returns true if
127
+ # it matches.
128
+ #
129
+ # Eg:
130
+ #
131
+ # register_spec_type(/Controller$/, Minitest::Spec::Rails)
132
+ #
133
+ # or:
134
+ #
135
+ # register_spec_type(Minitest::Spec::RailsModel) do |desc|
136
+ # desc.superclass == ActiveRecord::Base
137
+ # end
138
+
139
+ def register_spec_type *args, &block
140
+ if block then
141
+ matcher, klass = block, args.first
142
+ else
143
+ matcher, klass = *args
144
+ end
145
+ TYPES.unshift [matcher, klass]
146
+ end
147
+
148
+ ##
149
+ # Figure out the spec class to use based on a spec's description. Eg:
150
+ #
151
+ # spec_type("BlahController") # => Minitest::Spec::Rails
152
+
153
+ def spec_type desc, *additional
154
+ TYPES.find { |matcher, _klass|
155
+ if matcher.respond_to? :call then
156
+ matcher.call desc, *additional
157
+ else
158
+ matcher === desc.to_s
159
+ end
160
+ }.last
161
+ end
162
+
163
+ def describe_stack # :nodoc:
164
+ Thread.current[:describe_stack] ||= []
165
+ end
166
+
167
+ def children # :nodoc:
168
+ @children ||= []
169
+ end
170
+
171
+ def nuke_test_methods! # :nodoc:
172
+ self.public_instance_methods.grep(/^test_/).each do |name|
173
+ self.send :undef_method, name
174
+ end
175
+ end
176
+
177
+ ##
178
+ # Define a 'before' action. Inherits the way normal methods should.
179
+ #
180
+ # NOTE: +type+ is ignored and is only there to make porting easier.
181
+ #
182
+ # Equivalent to Minitest::Test#setup.
183
+
184
+ def before _type = nil, &block
185
+ define_method :setup do
186
+ super()
187
+ self.instance_eval(&block)
188
+ end
189
+ end
190
+
191
+ ##
192
+ # Define an 'after' action. Inherits the way normal methods should.
193
+ #
194
+ # NOTE: +type+ is ignored and is only there to make porting easier.
195
+ #
196
+ # Equivalent to Minitest::Test#teardown.
197
+
198
+ def after _type = nil, &block
199
+ define_method :teardown do
200
+ self.instance_eval(&block)
201
+ super()
202
+ end
203
+ end
204
+
205
+ ##
206
+ # Define an expectation with name +desc+. Name gets morphed to a
207
+ # proper test method name. For some freakish reason, people who
208
+ # write specs don't like class inheritance, so this goes way out of
209
+ # its way to make sure that expectations aren't inherited.
210
+ #
211
+ # This is also aliased to #specify and doesn't require a +desc+ arg.
212
+ #
213
+ # Hint: If you _do_ want inheritance, use minitest/test. You can mix
214
+ # and match between assertions and expectations as much as you want.
215
+
216
+ def it desc = "anonymous", &block
217
+ block ||= proc { skip "(no tests defined)" }
218
+
219
+ @specs ||= 0
220
+ @specs += 1
221
+
222
+ name = "test_%04d_%s" % [ @specs, desc ]
223
+
224
+ undef_klasses = self.children.reject { |c| c.public_method_defined? name }
225
+
226
+ define_method name, &block
227
+
228
+ undef_klasses.each do |undef_klass|
229
+ undef_klass.send :undef_method, name
230
+ end
231
+
232
+ name
233
+ end
234
+
235
+ ##
236
+ # Essentially, define an accessor for +name+ with +block+.
237
+ #
238
+ # Why use let instead of def? I honestly don't know.
239
+
240
+ def let name, &block
241
+ name = name.to_s
242
+ pre, post = "let '#{name}' cannot ", ". Please use another name."
243
+ methods = Minitest::Spec.instance_methods.map(&:to_s) - %w[subject]
244
+ raise ArgumentError, "#{pre}begin with 'test'#{post}" if
245
+ name =~ /\Atest/
246
+ raise ArgumentError, "#{pre}override a method in Minitest::Spec#{post}" if
247
+ methods.include? name
248
+
249
+ define_method name do
250
+ @_memoized ||= {}
251
+ @_memoized.fetch(name) { |k| @_memoized[k] = instance_eval(&block) }
252
+ end
253
+ end
254
+
255
+ ##
256
+ # Another lazy man's accessor generator. Made even more lazy by
257
+ # setting the name for you to +subject+.
258
+
259
+ def subject &block
260
+ let :subject, &block
261
+ end
262
+
263
+ def create name, desc # :nodoc:
264
+ cls = Class.new(self) do
265
+ @name = name
266
+ @desc = desc
267
+
268
+ nuke_test_methods!
269
+ end
270
+
271
+ children << cls
272
+
273
+ cls
274
+ end
275
+
276
+ def name # :nodoc:
277
+ defined?(@name) ? @name : super
278
+ end
279
+
280
+ def to_s # :nodoc:
281
+ name # Can't alias due to 1.8.7, not sure why
282
+ end
283
+
284
+ attr_reader :desc # :nodoc:
285
+ alias :specify :it
286
+
287
+ ##
288
+ # Rdoc... why are you so dumb?
289
+
290
+ module InstanceMethods
291
+ ##
292
+ # Returns a value monad that has all of Expectations methods
293
+ # available to it.
294
+ #
295
+ # Also aliased to #value and #expect for your aesthetic pleasure:
296
+ #
297
+ # _(1 + 1).must_equal 2
298
+ # value(1 + 1).must_equal 2
299
+ # expect(1 + 1).must_equal 2
300
+ #
301
+ # This method of expectation-based testing is preferable to
302
+ # straight-expectation methods (on Object) because it stores its
303
+ # test context, bypassing our hacky use of thread-local variables.
304
+ #
305
+ # At some point, the methods on Object will be deprecated and then
306
+ # removed.
307
+
308
+ def _ value = nil, &block
309
+ Minitest::Expectation.new block || value, self
310
+ end
311
+
312
+ alias value _
313
+ alias expect _
314
+
315
+ def before_setup # :nodoc:
316
+ super
317
+ Thread.current[:current_spec] = self
318
+ end
319
+ end
320
+
321
+ def self.extended obj # :nodoc:
322
+ obj.send :include, InstanceMethods
323
+ end
324
+ end
325
+
326
+ extend DSL
327
+
328
+ TYPES = DSL::TYPES # :nodoc:
329
+ end
330
+
331
+ require "minitest/expectations"
332
+
333
+ class Object # :nodoc:
334
+ include Minitest::Expectations unless ENV["MT_NO_EXPECTATIONS"]
335
+ end