minitest 1.3.0 → 6.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +2 -0
  3. data/History.rdoc +1860 -0
  4. data/Manifest.txt +38 -8
  5. data/README.rdoc +763 -0
  6. data/Rakefile +70 -56
  7. data/bin/minitest +5 -0
  8. data/design_rationale.rb +54 -0
  9. data/lib/hoe/minitest.rb +30 -0
  10. data/lib/minitest/assertions.rb +821 -0
  11. data/lib/minitest/autorun.rb +5 -0
  12. data/lib/minitest/benchmark.rb +452 -0
  13. data/lib/minitest/bisect.rb +304 -0
  14. data/lib/minitest/complete.rb +56 -0
  15. data/lib/minitest/compress.rb +94 -0
  16. data/lib/minitest/error_on_warning.rb +11 -0
  17. data/lib/minitest/expectations.rb +321 -0
  18. data/lib/minitest/find_minimal_combination.rb +127 -0
  19. data/lib/minitest/hell.rb +11 -0
  20. data/lib/minitest/manual_plugins.rb +4 -0
  21. data/lib/minitest/parallel.rb +72 -0
  22. data/lib/minitest/path_expander.rb +432 -0
  23. data/lib/minitest/pride.rb +4 -0
  24. data/lib/minitest/pride_plugin.rb +135 -0
  25. data/lib/minitest/server.rb +49 -0
  26. data/lib/minitest/server_plugin.rb +88 -0
  27. data/lib/minitest/spec.rb +306 -64
  28. data/lib/minitest/sprint.rb +105 -0
  29. data/lib/minitest/sprint_plugin.rb +39 -0
  30. data/lib/minitest/test.rb +232 -0
  31. data/lib/minitest/test_task.rb +331 -0
  32. data/lib/minitest.rb +1232 -0
  33. data/test/minitest/metametameta.rb +150 -0
  34. data/test/minitest/test_bisect.rb +249 -0
  35. data/test/minitest/test_find_minimal_combination.rb +138 -0
  36. data/test/minitest/test_minitest_assertions.rb +1729 -0
  37. data/test/minitest/test_minitest_benchmark.rb +151 -0
  38. data/test/minitest/test_minitest_reporter.rb +437 -0
  39. data/test/minitest/test_minitest_spec.rb +1095 -0
  40. data/test/minitest/test_minitest_test.rb +1295 -0
  41. data/test/minitest/test_minitest_test_task.rb +57 -0
  42. data/test/minitest/test_path_expander.rb +229 -0
  43. data/test/minitest/test_server.rb +146 -0
  44. data.tar.gz.sig +1 -0
  45. metadata +235 -62
  46. metadata.gz.sig +0 -0
  47. data/.autotest +0 -15
  48. data/History.txt +0 -114
  49. data/README.txt +0 -56
  50. data/lib/minitest/mock.rb +0 -31
  51. data/lib/minitest/unit.rb +0 -482
  52. data/test/test_mini_mock.rb +0 -77
  53. data/test/test_mini_spec.rb +0 -149
  54. data/test/test_mini_test.rb +0 -829
@@ -0,0 +1,232 @@
1
+ require_relative "../minitest" unless defined? Minitest::Runnable
2
+
3
+ module Minitest
4
+ ##
5
+ # Subclass Test to create your own tests. Typically you'll want a
6
+ # Test subclass per implementation class.
7
+ #
8
+ # See Minitest::Assertions
9
+
10
+ class Test < Runnable
11
+ require_relative "assertions"
12
+ include Minitest::Reportable
13
+ include Minitest::Assertions
14
+
15
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, SystemExit] # :nodoc:
16
+
17
+ SETUP_METHODS = %w[ before_setup setup after_setup ] # :nodoc:
18
+
19
+ TEARDOWN_METHODS = %w[ before_teardown teardown after_teardown ] # :nodoc:
20
+
21
+ # :stopdoc:
22
+ class << self; attr_accessor :io_lock; end
23
+ self.io_lock = Mutex.new
24
+ # :startdoc:
25
+
26
+ ##
27
+ # Call this at the top of your tests when you absolutely
28
+ # positively need to have ordered tests. In doing so, you're
29
+ # admitting that you suck and your tests are weak.
30
+
31
+ def self.i_suck_and_my_tests_are_order_dependent!
32
+ class << self
33
+ undef_method :run_order if method_defined? :run_order
34
+ define_method :run_order do :alpha end
35
+ end
36
+ end
37
+
38
+ ##
39
+ # Make diffs for this Test use #pretty_inspect so that diff
40
+ # in assert_equal can have more details. NOTE: this is much slower
41
+ # than the regular inspect but much more usable for complex
42
+ # objects.
43
+
44
+ def self.make_my_diffs_pretty!
45
+ require "pp"
46
+
47
+ define_method :mu_pp, &:pretty_inspect
48
+ end
49
+
50
+ ##
51
+ # Call this at the top of your tests (inside the +Minitest::Test+
52
+ # subclass or +describe+ block) when you want to run your tests in
53
+ # parallel. In doing so, you're admitting that you rule and your
54
+ # tests are awesome.
55
+
56
+ def self.parallelize_me!
57
+ return unless Minitest.parallel_executor
58
+ include Minitest::Parallel::Test
59
+ extend Minitest::Parallel::Test::ClassMethods
60
+ end
61
+
62
+ ##
63
+ # Returns all instance methods starting with "test_". Based on
64
+ # #run_order, the methods are either sorted, randomized
65
+ # (default), or run in parallel.
66
+
67
+ def self.runnable_methods
68
+ methods = methods_matching(/^test_/)
69
+
70
+ case self.run_order
71
+ when :random, :parallel then
72
+ srand Minitest.seed
73
+ methods.sort.shuffle
74
+ when :alpha, :sorted then
75
+ methods.sort
76
+ else
77
+ raise "Unknown_order: %p" % [self.run_order]
78
+ end
79
+ end
80
+
81
+ ##
82
+ # Runs a single test with setup/teardown hooks.
83
+
84
+ def run
85
+ time_it do
86
+ capture_exceptions do
87
+ SETUP_METHODS.each do |hook|
88
+ self.send hook
89
+ end
90
+
91
+ self.send self.name
92
+ end
93
+
94
+ TEARDOWN_METHODS.each do |hook|
95
+ capture_exceptions do
96
+ self.send hook
97
+ end
98
+ end
99
+ end
100
+
101
+ Result.from self # per contract
102
+ end
103
+
104
+ ##
105
+ # Provides before/after hooks for setup and teardown. These are
106
+ # meant for library writers, NOT for regular test authors. See
107
+ # #before_setup for an example.
108
+
109
+ module LifecycleHooks
110
+
111
+ ##
112
+ # Runs before every test, before setup. This hook is meant for
113
+ # libraries to extend minitest. It is not meant to be used by
114
+ # test developers.
115
+ #
116
+ # As a simplistic example:
117
+ #
118
+ # module MyMinitestPlugin
119
+ # def before_setup
120
+ # super
121
+ # # ... stuff to do before setup is run
122
+ # end
123
+ #
124
+ # def after_setup
125
+ # # ... stuff to do after setup is run
126
+ # super
127
+ # end
128
+ #
129
+ # def before_teardown
130
+ # super
131
+ # # ... stuff to do before teardown is run
132
+ # end
133
+ #
134
+ # def after_teardown
135
+ # # ... stuff to do after teardown is run
136
+ # super
137
+ # end
138
+ # end
139
+ #
140
+ # class Minitest::Test
141
+ # include MyMinitestPlugin
142
+ # end
143
+
144
+ def before_setup; end
145
+
146
+ ##
147
+ # Runs before every test. Use this to set up before each test
148
+ # run.
149
+
150
+ def setup; end
151
+
152
+ ##
153
+ # Runs before every test, after setup. This hook is meant for
154
+ # libraries to extend minitest. It is not meant to be used by
155
+ # test developers.
156
+ #
157
+ # See #before_setup for an example.
158
+
159
+ def after_setup; end
160
+
161
+ ##
162
+ # Runs after every test, before teardown. This hook is meant for
163
+ # libraries to extend minitest. It is not meant to be used by
164
+ # test developers.
165
+ #
166
+ # See #before_setup for an example.
167
+
168
+ def before_teardown; end
169
+
170
+ ##
171
+ # Runs after every test. Use this to clean up after each test
172
+ # run.
173
+
174
+ def teardown; end
175
+
176
+ ##
177
+ # Runs after every test, after teardown. This hook is meant for
178
+ # libraries to extend minitest. It is not meant to be used by
179
+ # test developers.
180
+ #
181
+ # See #before_setup for an example.
182
+
183
+ def after_teardown; end
184
+ end # LifecycleHooks
185
+
186
+ def capture_exceptions # :nodoc:
187
+ yield
188
+ rescue *PASSTHROUGH_EXCEPTIONS
189
+ raise
190
+ rescue Assertion => e
191
+ self.failures << e
192
+ rescue Exception => e
193
+ self.failures << UnexpectedError.new(sanitize_exception e)
194
+ end
195
+
196
+ def sanitize_exception e # :nodoc:
197
+ Marshal.dump e
198
+ e # good: use as-is
199
+ rescue
200
+ neuter_exception e
201
+ end
202
+
203
+ def neuter_exception e # :nodoc:
204
+ bt = e.backtrace
205
+ msg = e.message.dup
206
+
207
+ new_exception e.class, msg, bt # e.class can be a problem...
208
+ rescue
209
+ msg.prepend "Neutered Exception #{e.class}: "
210
+
211
+ new_exception RuntimeError, msg, bt, true # but if this raises, we die
212
+ end
213
+
214
+ def new_exception klass, msg, bt, kill = false # :nodoc:
215
+ ne = klass.new msg
216
+ ne.set_backtrace bt
217
+
218
+ if kill then
219
+ ne.instance_variables.each do |v|
220
+ ne.remove_instance_variable v
221
+ end
222
+ end
223
+
224
+ Marshal.dump ne # can raise TypeError
225
+ ne
226
+ end
227
+
228
+ include LifecycleHooks
229
+ include Guard
230
+ extend Guard
231
+ end # Test
232
+ end
@@ -0,0 +1,331 @@
1
+ require "shellwords"
2
+ require "rbconfig"
3
+
4
+ begin
5
+ require "rake/tasklib"
6
+ rescue LoadError => e
7
+ warn e.message
8
+ return
9
+ end
10
+
11
+ module Minitest # :nodoc:
12
+
13
+ ##
14
+ # Minitest::TestTask is a rake helper that generates several rake
15
+ # tasks under the main test task's name-space.
16
+ #
17
+ # task <name> :: the main test task
18
+ # task <name>:cmd :: prints the command to use
19
+ # task <name>:deps :: runs each test file by itself to find dependency errors
20
+ # task <name>:slow :: runs the tests and reports the slowest 25 tests.
21
+ #
22
+ # Examples:
23
+ #
24
+ # Minitest::TestTask.create
25
+ #
26
+ # The most basic and default setup.
27
+ #
28
+ # Minitest::TestTask.create :my_tests
29
+ #
30
+ # The most basic/default setup, but with a custom name
31
+ #
32
+ # Minitest::TestTask.create :unit do |t|
33
+ # t.test_globs = ["test/unit/**/*_test.rb"]
34
+ # t.warning = false
35
+ # end
36
+ #
37
+ # Customize the name and only run unit tests.
38
+ #
39
+ # NOTE: To hook this task up to the default, make it a dependency:
40
+ #
41
+ # task default: :unit
42
+
43
+ class TestTask < Rake::TaskLib
44
+ WINDOWS = RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ # :nodoc:
45
+
46
+ ##
47
+ # Create several test-oriented tasks under +name+ (default:
48
+ # "test"). Takes an optional block to customize variables.
49
+ # Examples:
50
+ #
51
+ # Minitest::TestTask.create # named "test", all defaults
52
+ #
53
+ # Minitest::TestTask.create do |t|
54
+ # t.warning = false # my tests are noisy
55
+ # t.framework = %(require_relative "./test/helper.rb")
56
+ # end
57
+
58
+ def self.create name = :test, &block
59
+ task = new name
60
+ task.instance_eval(&block) if block
61
+ task.process_env
62
+ task.define
63
+ task
64
+ end
65
+
66
+ ##
67
+ # Extra arguments to pass to the tests. Defaults empty but gets
68
+ # populated by a number of enviroment variables:
69
+ #
70
+ # N (-n flag) :: a string or regexp of tests to run.
71
+ # X (-e flag) :: a string or regexp of tests to exclude.
72
+ # A (arg) :: quick way to inject an arbitrary argument (eg A=--help).
73
+ #
74
+ # See #process_env
75
+
76
+ attr_accessor :extra_args
77
+
78
+ ##
79
+ # The code to load the framework. Defaults to requiring
80
+ # minitest/autorun.
81
+ #
82
+ # If you have a test helper file that requires minitest/autorun
83
+ # and anything else your project standardizes on, then you'll
84
+ # probably want to change this to:
85
+ #
86
+ # Minitest::TestTask.create do |t|
87
+ # t.framework = %(require_relative "./test/helper.rb")
88
+ # end
89
+ #
90
+ # or something similar. NOTE: if you do this, then helper must
91
+ # require "minitest/autorun" at the top to start the tests.
92
+
93
+ attr_accessor :framework
94
+
95
+ ##
96
+ # Extra library directories to include. Defaults to %w[lib test
97
+ # .]. Also uses $MT_LIB_EXTRAS allowing you to dynamically
98
+ # override/inject directories for custom runs.
99
+
100
+ attr_accessor :libs
101
+
102
+ ##
103
+ # The name of the task and base name for the other tasks generated.
104
+
105
+ attr_accessor :name
106
+
107
+ ##
108
+ # File globs to find test files. Defaults to something sensible to
109
+ # find test files under the test directory.
110
+
111
+ attr_accessor :test_globs
112
+
113
+ ##
114
+ # Turn on ruby warnings (-w flag). Defaults to true.
115
+
116
+ attr_accessor :warning
117
+
118
+ ##
119
+ # Optional: Additional ruby to run before the test framework is loaded.
120
+ # Example:
121
+ #
122
+ # Minitest::TestTask.create "test:cov" do |t|
123
+ # t.test_prelude = %(require "simplecov")
124
+ # end
125
+
126
+ attr_accessor :test_prelude
127
+
128
+ ##
129
+ # Print out commands as they run. Defaults to Rake's +trace+ (-t
130
+ # flag) option.
131
+
132
+ attr_accessor :verbose
133
+
134
+ ##
135
+ # Use TestTask.create instead.
136
+
137
+ def initialize name = :test # :nodoc:
138
+ self.extra_args = []
139
+ self.framework = %(require "minitest/autorun")
140
+ self.libs = %w[lib test .]
141
+ self.name = name
142
+ self.test_globs = ["test/**/test_*.rb",
143
+ "test/**/*_test.rb"]
144
+ self.test_prelude = nil
145
+ self.verbose = Rake.application.options.trace || Rake.verbose == true
146
+ self.warning = true
147
+ end
148
+
149
+ ##
150
+ # Extract variables from the environment and convert them to
151
+ # command line arguments. See #extra_args.
152
+ #
153
+ # Environment Variables:
154
+ #
155
+ # MT_LIB_EXTRAS :: Extra libs to dynamically override/inject for custom runs.
156
+ # N :: Tests to run (string or /regexp/).
157
+ # X :: Tests to exclude (string or /regexp/).
158
+ # A :: Any extra arguments. Honors shell quoting.
159
+ #
160
+ # Deprecated:
161
+ #
162
+ # TESTOPTS :: For argument passing, use +A+.
163
+ # N :: For parallel testing, use +MT_CPU+.
164
+ # FILTER :: Same as +TESTOPTS+.
165
+
166
+ def process_env
167
+ warn "TESTOPTS is deprecated in Minitest::TestTask. Use A instead" if
168
+ ENV["TESTOPTS"]
169
+ warn "FILTER is deprecated in Minitest::TestTask. Use A instead" if
170
+ ENV["FILTER"]
171
+
172
+ lib_extras = (ENV["MT_LIB_EXTRAS"] || "").split File::PATH_SEPARATOR
173
+ self.libs[0, 0] = lib_extras
174
+
175
+ extra_args << "-i" << ENV["N"] if ENV["N"]
176
+ extra_args << "-i" << ENV["I"] if ENV["I"]
177
+ extra_args << "-x" << ENV["X"] if ENV["X"]
178
+ extra_args << "-x" << ENV["E"] if ENV["E"]
179
+ extra_args.concat Shellwords.split(ENV["TESTOPTS"]) if ENV["TESTOPTS"]
180
+ extra_args.concat Shellwords.split(ENV["FILTER"]) if ENV["FILTER"]
181
+ extra_args.concat Shellwords.split(ENV["A"]) if ENV["A"]
182
+
183
+ # TODO? RUBY_DEBUG = ENV["RUBY_DEBUG"]
184
+ # TODO? ENV["RUBY_FLAGS"]
185
+
186
+ extra_args.compact!
187
+ end
188
+
189
+ def define # :nodoc:
190
+ desc "Run the test suite. Use I, X, and A to add flags/args."
191
+ task name do
192
+ ruby make_test_cmd, verbose: verbose
193
+ end
194
+
195
+ desc "Run the test suite, filtering for 'FU' in name (focused units?)."
196
+ task "#{name}:fu" do
197
+ ruby make_test_cmd(include:"/FU/"), verbose: verbose
198
+ end
199
+
200
+ desc "Print out the test command. Good for profiling and other tools."
201
+ task "#{name}:cmd" do
202
+ puts "ruby #{make_test_cmd}"
203
+ end
204
+
205
+ desc "Show which test files fail when run in isolation."
206
+ task "#{name}:isolated" do
207
+ tests = Dir[*self.test_globs].uniq
208
+
209
+ # 3 seems to be the magic number... (tho not by that much)
210
+ bad, good, n = {}, [], (ENV.delete("K") || 3).to_i
211
+ file = ENV.delete "F"
212
+ times = {}
213
+
214
+ tt0 = Time.now
215
+
216
+ n.threads_do tests.sort do |path|
217
+ t0 = Time.now
218
+ output = `#{Gem.ruby} #{make_test_cmd path:path} 2>&1`
219
+ t1 = Time.now - t0
220
+
221
+ times[path] = t1
222
+
223
+ if $?.success?
224
+ $stderr.print "."
225
+ good << path
226
+ else
227
+ $stderr.print "x"
228
+ bad[path] = output
229
+ end
230
+ end
231
+
232
+ puts "done"
233
+ puts "Ran in %.2f seconds" % [ Time.now - tt0 ]
234
+
235
+ if file then
236
+ require "json"
237
+ File.open file, "w" do |io|
238
+ io.puts JSON.pretty_generate times
239
+ end
240
+ end
241
+
242
+ unless good.empty?
243
+ puts
244
+ puts "# Good tests:"
245
+ puts
246
+ good.sort.each do |path|
247
+ puts "%.2fs: %s" % [times[path], path]
248
+ end
249
+ end
250
+
251
+ unless bad.empty?
252
+ puts
253
+ puts "# Bad tests:"
254
+ puts
255
+ bad.keys.sort.each do |path|
256
+ puts "%.2fs: %s" % [times[path], path]
257
+ end
258
+ puts
259
+ puts "# Bad Test Output:"
260
+ puts
261
+ bad.sort.each do |path, output|
262
+ puts
263
+ puts "# #{path}:"
264
+ puts output
265
+ end
266
+ exit 1
267
+ end
268
+ end
269
+
270
+ task "#{name}:deps" => "#{name}:isolated" # now just an alias
271
+
272
+ desc "Run the test suite and report the slowest 25 tests."
273
+ task "#{name}:slow" do
274
+ sh ["rake #{name} A=-v",
275
+ "egrep '#test_.* s = .'",
276
+ "sort -n -k2 -t=",
277
+ "tail -25"].join " | "
278
+ end
279
+ end
280
+
281
+ ##
282
+ # Generate the test command-line.
283
+
284
+ def make_test_cmd **option
285
+ globs = option[:path] || test_globs
286
+
287
+ tests = []
288
+ tests.concat Dir[*globs].sort.shuffle # TODO: SEED -> srand first?
289
+ tests.map! { |f| %(require "#{f}") }
290
+
291
+ runner = []
292
+ runner << test_prelude if test_prelude
293
+ runner << framework
294
+ runner.concat tests
295
+ runner = runner.join "; "
296
+
297
+ extra_args << "-i" << option[:include] if option[:include]
298
+
299
+ args = []
300
+ args << "-I#{libs.join File::PATH_SEPARATOR}" unless libs.empty?
301
+ args << "-w" if warning
302
+ args << "-e"
303
+ args << "'#{runner}'"
304
+ args << "--"
305
+ args << extra_args.map(&:shellescape)
306
+
307
+ args.join " "
308
+ end
309
+ end
310
+ end
311
+
312
+ class Work < Queue # :nodoc:
313
+ def initialize jobs # :nodoc:
314
+ super
315
+ close
316
+ end
317
+ end
318
+
319
+ class Integer # :nodoc:
320
+ def threads_do jobs # :nodoc:
321
+ q = Work.new jobs
322
+
323
+ Array.new(self) {
324
+ Thread.new do
325
+ while job = q.pop # go until quit value
326
+ yield job
327
+ end
328
+ end
329
+ }.each(&:join)
330
+ end
331
+ end