test-unit 3.7.7 → 3.7.9

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.
@@ -142,8 +142,30 @@ module Test
142
142
  end
143
143
  end
144
144
 
145
+ # Omit the rest of the test. This is for Minitest
146
+ # compatibility. Use #omit in new tests.
147
+ #
148
+ # Example:
149
+ #
150
+ # def test_skip
151
+ # skip("Not supported yet")
152
+ # # Not reached here
153
+ # end
154
+ def skip(message=nil, _ignored=nil)
155
+ omit(message || "skipped.")
156
+ end
157
+
158
+ # Returns whether the test is omitted. This is for Minitest
159
+ # compatibility.
160
+ #
161
+ # @return [Boolean]
162
+ def skipped?
163
+ @internal_data.omitted?
164
+ end
165
+
145
166
  private
146
167
  def add_omission(omission)
168
+ @internal_data.omitted
147
169
  current_result.add_omission(omission)
148
170
  end
149
171
  end
@@ -119,13 +119,14 @@ module Test
119
119
  File.exist?(passed_file)
120
120
  end
121
121
 
122
+ BASE_DIR = File.dirname($0).freeze
122
123
  def result_dir
123
124
  components = [
124
125
  ".test-result",
125
126
  escape_class_name(@test.class.name || "AnonymousTestCase"),
126
127
  escaped_method_name,
127
128
  ]
128
- parent_directories = [File.dirname($0), Dir.pwd]
129
+ parent_directories = [BASE_DIR, Dir.pwd]
129
130
  if Process.respond_to?(:uid)
130
131
  parent_directories << File.join(Dir.tmpdir, Process.uid.to_s)
131
132
  end
@@ -7,6 +7,12 @@
7
7
  module Test
8
8
  module Unit
9
9
  class ProcessTestResult
10
+ class << self
11
+ def test_result?
12
+ true
13
+ end
14
+ end
15
+
10
16
  def initialize(output)
11
17
  @output = output
12
18
  end
@@ -8,8 +8,9 @@ require "optparse"
8
8
  require "socket"
9
9
 
10
10
  parser = OptionParser.new
11
+ load_paths = []
11
12
  parser.on("--load-path=PATH") do |path|
12
- $LOAD_PATH << path
13
+ load_paths << path
13
14
  end
14
15
  base_directory = nil
15
16
  parser.on("--base-directory=PATH") do |path|
@@ -27,9 +28,15 @@ remote_ip_port = nil
27
28
  parser.on("--ip-port=PORT", Integer) do |port|
28
29
  remote_ip_port = port
29
30
  end
31
+ notify_no_assertions = false
32
+ parser.on("--notify-no-assertions") do
33
+ notify_no_assertions = true
34
+ end
30
35
  test_paths = parser.parse!
36
+ $LOAD_PATH.unshift(*load_paths)
31
37
 
32
38
  require_relative "../unit"
39
+ Test::Unit::TestCase.notify_no_assertions = true if notify_no_assertions
33
40
  require_relative "collector/load"
34
41
  require_relative "process-test-result"
35
42
  require_relative "worker-context"
@@ -41,6 +48,8 @@ suite = collector.collect(*test_paths)
41
48
  io_open = lambda do |&block|
42
49
  if Gem.win_platform?
43
50
  TCPSocket.open(remote_ip_address, remote_ip_port) do |data_socket|
51
+ Marshal.dump(Process.pid, data_socket)
52
+ data_socket.flush
44
53
  block.call(data_socket, data_socket)
45
54
  end
46
55
  else
@@ -66,7 +75,7 @@ io_open.call do |data_input, data_output|
66
75
  Marshal.dump({status: :event, event_name: event_name, args: args}, data_output)
67
76
  data_output.flush
68
77
  end
69
- if test.is_a?(Test::Unit::TestSuite)
78
+ if test.class.test_suite?
70
79
  test_suite = test
71
80
  else
72
81
  test_suite = Test::Unit::TestSuite.new(test.class.name, test.class)
@@ -7,6 +7,12 @@
7
7
  module Test
8
8
  module Unit
9
9
  class SubTestResult
10
+ class << self
11
+ def test_result?
12
+ true
13
+ end
14
+ end
15
+
10
16
  attr_accessor :stop_tag
11
17
 
12
18
  def initialize(parent_test_result)
@@ -17,8 +17,9 @@ module Test
17
17
  end
18
18
  end
19
19
 
20
- def initialize(test_case)
20
+ def initialize(test_case, skip_valid_check: false)
21
21
  @test_case = test_case
22
+ @skip_valid_check = skip_valid_check
22
23
  end
23
24
 
24
25
  def create
@@ -59,7 +60,7 @@ module Test
59
60
  def append_test(suite, test_name)
60
61
  test = @test_case.new(test_name)
61
62
  yield(test) if block_given?
62
- suite << test if test.valid?
63
+ suite << test if @skip_valid_check or test.valid?
63
64
  end
64
65
 
65
66
  def collect_test_names
@@ -67,6 +67,9 @@ module Test
67
67
  command_line << "--base-directory" << base_directory
68
68
  end
69
69
  command_line << "--worker-id" << (i + 1).to_s
70
+ if TestCase.notify_no_assertions?
71
+ command_line << "--notify-no-assertions"
72
+ end
70
73
  if Gem.win_platform?
71
74
  local_address = tcp_server.local_address
72
75
  command_line << "--ip-address" << local_address.ip_address
@@ -77,8 +80,9 @@ module Test
77
80
  # On Windows, file descriptors 3 and above cannot be passed to
78
81
  # child processes.
79
82
  pid = spawn(*command_line)
80
- data_socket = tcp_server.accept
81
- workers << Worker.new(pid, data_socket, data_socket)
83
+ # They are replaced later. This is just for ensuring calling
84
+ # `Process.waitpid(pid)` on error.
85
+ workers << Worker.new(pid, nil, nil)
82
86
  else
83
87
  main_to_worker_input, main_to_worker_output = IO.pipe
84
88
  worker_to_main_input, worker_to_main_output = IO.pipe
@@ -89,6 +93,13 @@ module Test
89
93
  workers << Worker.new(pid, main_to_worker_output, worker_to_main_input)
90
94
  end
91
95
  end
96
+ if Gem.win_platform?
97
+ workers = workers.collect do
98
+ data_socket = tcp_server.accept
99
+ pid = Marshal.load(data_socket)
100
+ Worker.new(pid, data_socket, data_socket)
101
+ end
102
+ end
92
103
 
93
104
  run_context = TestProcessRunContext.new(self)
94
105
  yield(run_context)
@@ -184,7 +195,7 @@ module Test
184
195
  run_context.test_names << test_suite.name
185
196
  else
186
197
  test_suite.tests.each do |test|
187
- if test.is_a?(TestSuite)
198
+ if test.class.test_suite?
188
199
  run_tests_recursive(test, worker_context, &progress_block)
189
200
  else
190
201
  run_context.test_names << test.name
@@ -53,8 +53,8 @@ module Test
53
53
  test_case.worker_id = worker_context.id
54
54
  begin
55
55
  test_case.startup
56
- rescue Exception
57
- raise unless handle_exception($!, worker_context.result)
56
+ rescue Exception => e
57
+ raise unless handle_exception(e, worker_context.result)
58
58
  end
59
59
  end
60
60
 
@@ -112,8 +112,8 @@ module Test
112
112
  test_case.worker_id = worker_context.id
113
113
  begin
114
114
  test_case.shutdown
115
- rescue Exception
116
- raise unless handle_exception($!, worker_context.result)
115
+ rescue Exception => e
116
+ raise unless handle_exception(e, worker_context.result)
117
117
  end
118
118
  end
119
119
 
@@ -73,7 +73,7 @@ module Test
73
73
  run_context.queue << task
74
74
  else
75
75
  test_suite.tests.each do |test|
76
- if test.is_a?(TestSuite)
76
+ if test.class.test_suite?
77
77
  run_tests_recursive(test, worker_context, &progress_block)
78
78
  elsif test_suite.parallel_safe?
79
79
  task = lambda do |stop_tag, worker_id|
@@ -21,6 +21,7 @@ require_relative 'testsuite'
21
21
  require_relative 'test-suite-creator'
22
22
  require_relative 'assertion-failed-error'
23
23
  require_relative 'auto-runner-loader'
24
+ require_relative 'worker-context'
24
25
  require_relative 'util/backtracefilter'
25
26
  require_relative 'util/output'
26
27
  require_relative 'util/method-owner-finder'
@@ -116,13 +117,13 @@ module Test
116
117
  include Util::BacktraceFilter
117
118
  include Util::Output
118
119
 
119
- STARTED = name + "::STARTED" # :nodoc:
120
- FINISHED = name + "::FINISHED" # :nodoc:
121
- STARTED_OBJECT = name + "::STARTED::OBJECT" # :nodoc:
122
- FINISHED_OBJECT = name + "::FINISHED::OBJECT" # :nodoc:
120
+ STARTED = (name + "::STARTED").freeze # :nodoc:
121
+ FINISHED = (name + "::FINISHED").freeze # :nodoc:
122
+ STARTED_OBJECT = (name + "::STARTED::OBJECT").freeze # :nodoc:
123
+ FINISHED_OBJECT = (name + "::FINISHED::OBJECT").freeze # :nodoc:
123
124
 
124
125
  DESCENDANTS = [] # :nodoc:
125
- AVAILABLE_ORDERS = [:alphabetic, :random, :defined] # :nodoc:
126
+ AVAILABLE_ORDERS = [:alphabetic, :random, :defined].freeze # :nodoc:
126
127
 
127
128
  class << self
128
129
  # Indicates whether the test is parallel safe.
@@ -150,6 +151,48 @@ module Test
150
151
  true
151
152
  end
152
153
 
154
+ # Indicates whether tests without assertions are notified.
155
+ # This returns `false` by default. Use
156
+ # {.notify_no_assertions=} to change it. The default can also
157
+ # be changed by `--notify-no-assertions`.
158
+ #
159
+ # @return [Boolean]
160
+ #
161
+ # @since 3.7.9
162
+ def notify_no_assertions?
163
+ false
164
+ end
165
+
166
+ # Sets whether tests without assertions are notified. Sub
167
+ # test cases inherit it.
168
+ #
169
+ # @example Notify tests without assertions
170
+ # class TestMyClass < Test::Unit::TestCase
171
+ # self.notify_no_assertions = true
172
+ #
173
+ # def test_no_assertions
174
+ # # This is notified.
175
+ # end
176
+ # end
177
+ #
178
+ # @param boolean [Boolean]
179
+ # @return [void]
180
+ #
181
+ # @since 3.7.9
182
+ def notify_no_assertions=(boolean)
183
+ define_singleton_method(:notify_no_assertions?) do
184
+ boolean
185
+ end
186
+ end
187
+
188
+ def test_suite?
189
+ false
190
+ end
191
+
192
+ def test_case?
193
+ true
194
+ end
195
+
153
196
  def inherited(sub_class) # :nodoc:
154
197
  DESCENDANTS << sub_class
155
198
  super
@@ -614,7 +657,7 @@ module Test
614
657
  # and errors in result.
615
658
  def run(worker_context)
616
659
  begin
617
- unless worker_context.is_a?(WorkerContext)
660
+ if worker_context.class.test_result?
618
661
  result = worker_context
619
662
  worker_context = WorkerContext.new(nil, nil, result)
620
663
  end
@@ -634,9 +677,9 @@ module Test
634
677
  run_test
635
678
  run_cleanup
636
679
  add_pass
637
- rescue Exception
680
+ rescue Exception => e
638
681
  @internal_data.interrupted
639
- unless handle_exception($!)
682
+ unless handle_exception(e)
640
683
  processed_exception_in_setup = true
641
684
  raise
642
685
  end
@@ -644,30 +687,31 @@ module Test
644
687
  end
645
688
  end
646
689
  end
647
- rescue Exception
690
+ rescue Exception => e
648
691
  if processed_exception_in_setup
649
692
  raise
650
693
  else
651
694
  @internal_data.interrupted
652
- raise unless handle_exception($!)
695
+ raise unless handle_exception(e)
653
696
  end
654
697
  ensure
655
698
  begin
656
699
  run_teardown
657
- rescue Exception
658
- raise unless handle_exception($!)
700
+ rescue Exception => e
701
+ raise unless handle_exception(e)
659
702
  end
660
703
  end
704
+ notify_no_assertions
661
705
  @internal_data.test_finished
662
706
  result.add_run
663
707
  yield(FINISHED, name)
664
- yield(FINISHED_OBJECT, self)
665
708
  ensure
666
709
  # @_result = nil # For test-spec's after_all :<
667
710
  (instance_variables - instance_variables_before).each do |name|
668
711
  remove_instance_variable(name)
669
712
  end
670
713
  end
714
+ yield(FINISHED_OBJECT, self)
671
715
  end
672
716
 
673
717
  # Called before every test method runs. Can be used
@@ -901,6 +945,29 @@ module Test
901
945
  @internal_data.passed?
902
946
  end
903
947
 
948
+ # Returns the number of assertions in this test. This is for
949
+ # Minitest compatibility.
950
+ #
951
+ # @return [Integer]
952
+ def assertions
953
+ @internal_data.n_assertions
954
+ end
955
+
956
+ # Sets the number of assertions in this test. Increased
957
+ # assertions are also added to the current result. Decreasing
958
+ # is ignored. This is for Minitest compatibility.
959
+ #
960
+ # @example Add an assertion without an assertion method
961
+ # self.assertions += 1
962
+ #
963
+ # @param n_assertions [Integer]
964
+ # @return [void]
965
+ def assertions=(n_assertions)
966
+ (n_assertions - assertions).times do
967
+ add_assertion
968
+ end
969
+ end
970
+
904
971
  # Notify that a problem is occurred in the test. It means that
905
972
  # the test is a failed test. If any failed tests exist in test
906
973
  # suites, the test process exits with failure exit status.
@@ -970,6 +1037,16 @@ module Test
970
1037
  end
971
1038
  end
972
1039
 
1040
+ def notify_no_assertions
1041
+ return unless self.class.notify_no_assertions?
1042
+ return unless passed?
1043
+ return if skipped?
1044
+ return unless assertions.zero?
1045
+ path, line = self.class.instance_method(@method_name).source_location
1046
+ notify("<#{self.class}\##{@method_name}> has no assertions",
1047
+ :backtrace => ["#{path}:#{line}"])
1048
+ end
1049
+
973
1050
  def handle_exception(exception)
974
1051
  self.class.exception_handlers.each do |handler|
975
1052
  if handler.respond_to?(:call)
@@ -983,6 +1060,7 @@ module Test
983
1060
  end
984
1061
 
985
1062
  def add_assertion
1063
+ @internal_data.n_assertions += 1
986
1064
  current_result.add_assertion
987
1065
  end
988
1066
 
@@ -991,15 +1069,19 @@ module Test
991
1069
  attr_reader :test_data_label, :test_data
992
1070
  attr_accessor :run_context
993
1071
  attr_accessor :worker_id
1072
+ attr_accessor :n_assertions
994
1073
  def initialize
995
1074
  @start_time = nil
996
1075
  @elapsed_time = nil
997
1076
  @passed = true
998
1077
  @interrupted = false
1078
+ @omitted = false
1079
+ @errored = false
999
1080
  @test_data_label = nil
1000
1081
  @test_data = nil
1001
1082
  @run_context = nil
1002
1083
  @worker_id = nil
1084
+ @n_assertions = 0
1003
1085
  end
1004
1086
 
1005
1087
  def passed?
@@ -1010,6 +1092,14 @@ module Test
1010
1092
  @interrupted
1011
1093
  end
1012
1094
 
1095
+ def omitted?
1096
+ @omitted
1097
+ end
1098
+
1099
+ def errored?
1100
+ @errored
1101
+ end
1102
+
1013
1103
  def assign_test_data(label, data)
1014
1104
  @test_data_label = label
1015
1105
  @test_data = data
@@ -1035,13 +1125,24 @@ module Test
1035
1125
  @interrupted = true
1036
1126
  end
1037
1127
 
1128
+ def omitted
1129
+ @omitted = true
1130
+ end
1131
+
1132
+ def errored
1133
+ @errored = true
1134
+ end
1135
+
1038
1136
  def marshal_dump
1039
1137
  {
1040
1138
  start_time: @start_time,
1041
1139
  elapsed_time: @elapsed_time,
1042
1140
  passed: @passed,
1043
1141
  interrupted: @interrupted,
1142
+ omitted: @omitted,
1143
+ errored: @errored,
1044
1144
  test_data_label: @test_data_label,
1145
+ n_assertions: @n_assertions,
1045
1146
  }
1046
1147
  end
1047
1148
 
@@ -1050,7 +1151,10 @@ module Test
1050
1151
  @elapsed_time = data[:elapsed_time]
1051
1152
  @passed = data[:passed]
1052
1153
  @interrupted = data[:interrupted]
1154
+ @omitted = data[:omitted]
1155
+ @errored = data[:errored]
1053
1156
  @test_data_label = data[:test_data_label]
1157
+ @n_assertions = data[:n_assertions]
1054
1158
  end
1055
1159
  end
1056
1160
  end
@@ -31,10 +31,17 @@ module Test
31
31
  include TestResultOmissionSupport
32
32
  include TestResultNotificationSupport
33
33
 
34
- FINISHED = name + "::FINISHED"
35
- CHANGED = name + "::CHANGED"
36
- PASS_ASSERTION = name + "::PASS_ASSERTION"
37
- FAULT = name + "::FAULT"
34
+ class << self
35
+ # Returns true if it should be wrapped by WorkerContext
36
+ def test_result?
37
+ true
38
+ end
39
+ end
40
+
41
+ FINISHED = (name + "::FINISHED").freeze
42
+ CHANGED = (name + "::CHANGED").freeze
43
+ PASS_ASSERTION = (name + "::PASS_ASSERTION").freeze
44
+ FAULT = (name + "::FAULT").freeze
38
45
 
39
46
  attr_reader :run_count, :pass_count, :assertion_count, :faults
40
47
 
@@ -19,16 +19,26 @@ module Test
19
19
  # has a suite method as simply providing a way to get a
20
20
  # meaningful TestSuite instance.
21
21
  class TestSuite
22
+ class << self
23
+ def test_suite?
24
+ true
25
+ end
26
+
27
+ def test_case?
28
+ false
29
+ end
30
+ end
31
+
22
32
  attr_reader :name, :tests, :test_case, :start_time, :elapsed_time
23
33
 
24
34
  # Test suite that has higher priority is ran prior to
25
35
  # test suites that have lower priority.
26
36
  attr_accessor :priority
27
37
 
28
- STARTED = name + "::STARTED"
29
- STARTED_OBJECT = name + "::STARTED::OBJECT"
30
- FINISHED = name + "::FINISHED"
31
- FINISHED_OBJECT = name + "::FINISHED::OBJECT"
38
+ STARTED = (name + "::STARTED").freeze
39
+ STARTED_OBJECT = (name + "::STARTED::OBJECT").freeze
40
+ FINISHED = (name + "::FINISHED").freeze
41
+ FINISHED_OBJECT = (name + "::FINISHED::OBJECT").freeze
32
42
 
33
43
  # Creates a new TestSuite with the given name.
34
44
  def initialize(name="Unnamed TestSuite", test_case=nil)
@@ -43,7 +53,7 @@ module Test
43
53
  def find(name)
44
54
  return self if @name == name
45
55
  @tests.each do |test|
46
- if test.is_a?(self.class)
56
+ if test.class.test_suite?
47
57
  t = test.find(name)
48
58
  return t if t
49
59
  else
@@ -15,9 +15,9 @@ module Test
15
15
  # Provides an interface to write any given UI against,
16
16
  # hopefully making it easy to write new UIs.
17
17
  class TestRunnerMediator
18
- RESET = name + "::RESET"
19
- STARTED = name + "::STARTED"
20
- FINISHED = name + "::FINISHED"
18
+ RESET = (name + "::RESET").freeze
19
+ STARTED = (name + "::STARTED").freeze
20
+ FINISHED = (name + "::FINISHED").freeze
21
21
 
22
22
  include Util::Observable
23
23
 
@@ -15,7 +15,7 @@ module Test
15
15
  # events.
16
16
  module Observable
17
17
  # We use this for defaults since nil might mean something
18
- NOTHING = "NOTHING/#{__id__}"
18
+ NOTHING = "NOTHING/#{__id__}".freeze
19
19
 
20
20
  # Adds the passed proc as a listener on the
21
21
  # channel indicated by channel_name. listener_key
@@ -1,3 +1,5 @@
1
+ require "monitor"
2
+
1
3
  module Test
2
4
  module Unit
3
5
  module Util
@@ -6,6 +8,12 @@ module Test
6
8
  # Returns output for standard output and standard
7
9
  # error as string.
8
10
  #
11
+ # Calls from multiple threads are serialized. Note that
12
+ # output written by other threads while the block is
13
+ # running is also captured. Don't call #capture_output in
14
+ # another thread or fiber while the block is running. It
15
+ # blocks or raises ThreadError.
16
+ #
9
17
  # Example:
10
18
  #
11
19
  # capture_output do
@@ -13,17 +21,40 @@ module Test
13
21
  # warn("stderr")
14
22
  # end # -> ["stdout\n", "stderr\n"]
15
23
  def capture_output
16
- require 'stringio'
24
+ require "stringio"
17
25
 
18
- output = StringIO.new
19
- error = StringIO.new
20
- stdout_save, stderr_save = $stdout, $stderr
21
- $stdout, $stderr = output, error
22
- begin
23
- yield
24
- [output.string, error.string]
25
- ensure
26
- $stdout, $stderr = stdout_save, stderr_save
26
+ capture_output_monitor.synchronize do
27
+ output = StringIO.new
28
+ error = StringIO.new
29
+ stdout_save, stderr_save = $stdout, $stderr
30
+ $stdout, $stderr = output, error
31
+ begin
32
+ yield
33
+ [output.string, error.string]
34
+ ensure
35
+ $stdout, $stderr = stdout_save, stderr_save
36
+ end
37
+ end
38
+ end
39
+
40
+ # Same as #capture_output. This is for Minitest
41
+ # compatibility.
42
+ alias_method :capture_io, :capture_output
43
+
44
+ private
45
+ # Monitor, not Mutex, to allow nested #capture_output. A
46
+ # monitor per Ractor because $stdout/$stderr are Ractor
47
+ # local.
48
+ if defined?(Ractor.store_if_absent)
49
+ def capture_output_monitor
50
+ Ractor.store_if_absent(:test_unit_capture_output_monitor) do
51
+ Monitor.new
52
+ end
53
+ end
54
+ else
55
+ CAPTURE_MONITOR = Monitor.new # :nodoc:
56
+ def capture_output_monitor
57
+ CAPTURE_MONITOR
27
58
  end
28
59
  end
29
60
  end
@@ -1,5 +1,5 @@
1
1
  module Test
2
2
  module Unit
3
- VERSION = "3.7.7"
3
+ VERSION = "3.7.9"
4
4
  end
5
5
  end
@@ -7,6 +7,13 @@
7
7
  module Test
8
8
  module Unit
9
9
  class WorkerContext
10
+ class << self
11
+ # Returns false to not re-wrap this by WorkerContext
12
+ def test_result?
13
+ false
14
+ end
15
+ end
16
+
10
17
  attr_reader :id
11
18
  attr_reader :run_context
12
19
  attr_reader :result
data/lib/test/unit.rb CHANGED
@@ -508,6 +508,31 @@ module Test # :nodoc:
508
508
  hook.call
509
509
  end
510
510
  end
511
+
512
+ # @api private
513
+ @@box_available_flag = nil
514
+
515
+ # @api private
516
+ def disable_box_forcibly
517
+ v = @@box_available_flag
518
+ @@box_available_flag = false
519
+ yield
520
+ ensure
521
+ @@box_available_flag = v
522
+ end
523
+
524
+ # @api private
525
+ def box_available?
526
+ return @@box_available_flag unless @@box_available_flag.nil?
527
+ @@box_available_flag = defined?(Ruby::Box) && Ruby::Box.enabled?
528
+ if @@box_available_flag
529
+ major, minor, _ = Ruby::VERSION.split('.')
530
+ if major.to_i < 4 || major.to_i == 4 && minor.to_i < 1
531
+ @@box_available_flag = false
532
+ end
533
+ end
534
+ @@box_available_flag
535
+ end
511
536
  end
512
537
  end
513
538
  end