henitai 0.5.3 → 0.7.0

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 (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +332 -1
  3. data/README.md +123 -19
  4. data/assets/schema/henitai.schema.json +26 -0
  5. data/lib/henitai/arid_node_filter.rb +4 -19
  6. data/lib/henitai/bootstrap_error_message.rb +38 -0
  7. data/lib/henitai/canonical_report_merger.rb +23 -2
  8. data/lib/henitai/checkpoint_reporter.rb +5 -0
  9. data/lib/henitai/cli/operator_command.rb +10 -3
  10. data/lib/henitai/cli/run_command.rb +25 -7
  11. data/lib/henitai/cli/run_options.rb +1 -1
  12. data/lib/henitai/cli.rb +6 -2
  13. data/lib/henitai/composite_progress_reporter.rb +15 -2
  14. data/lib/henitai/configuration.rb +9 -1
  15. data/lib/henitai/configuration_validator/rules.rb +9 -0
  16. data/lib/henitai/configuration_validator/scalars.rb +22 -0
  17. data/lib/henitai/configuration_validator.rb +4 -2
  18. data/lib/henitai/constant_path.rb +48 -0
  19. data/lib/henitai/control_gate.rb +78 -0
  20. data/lib/henitai/control_run.rb +67 -0
  21. data/lib/henitai/coverage_bootstrapper.rb +46 -11
  22. data/lib/henitai/coverage_formatter.rb +26 -3
  23. data/lib/henitai/coverage_report_reader.rb +21 -0
  24. data/lib/henitai/equivalence_detector/operand_predicates.rb +16 -0
  25. data/lib/henitai/equivalence_detector.rb +46 -38
  26. data/lib/henitai/example_summary.rb +24 -0
  27. data/lib/henitai/execution_engine/env_scope.rb +3 -3
  28. data/lib/henitai/execution_engine/fork_server_scope.rb +57 -0
  29. data/lib/henitai/execution_engine.rb +109 -119
  30. data/lib/henitai/execution_provenance.rb +67 -0
  31. data/lib/henitai/fidelity_check.rb +173 -0
  32. data/lib/henitai/flaky_detection_baseline.rb +63 -0
  33. data/lib/henitai/flaky_retry_decision.rb +23 -0
  34. data/lib/henitai/flaky_test_registry.rb +63 -0
  35. data/lib/henitai/fork_server/client.rb +222 -0
  36. data/lib/henitai/fork_server/group_killer.rb +48 -0
  37. data/lib/henitai/fork_server/launcher.rb +38 -0
  38. data/lib/henitai/fork_server/mutant_runner.rb +110 -0
  39. data/lib/henitai/fork_server/protocol.rb +73 -0
  40. data/lib/henitai/fork_server/reaper.rb +24 -0
  41. data/lib/henitai/fork_server/runtime.rb +109 -0
  42. data/lib/henitai/fork_server/server.rb +108 -0
  43. data/lib/henitai/fork_server/server_process.rb +73 -0
  44. data/lib/henitai/fork_server/status.rb +52 -0
  45. data/lib/henitai/fork_server.rb +42 -0
  46. data/lib/henitai/generated_artifacts.rb +3 -0
  47. data/lib/henitai/git_diff_analyzer.rb +28 -11
  48. data/lib/henitai/history_checkpointer.rb +72 -0
  49. data/lib/henitai/incremental_filter.rb +20 -10
  50. data/lib/henitai/integration/base.rb +78 -19
  51. data/lib/henitai/integration/capped_log_sink.rb +34 -0
  52. data/lib/henitai/integration/child_bootstrap.rb +7 -0
  53. data/lib/henitai/integration/child_report_store.rb +65 -0
  54. data/lib/henitai/integration/minitest.rb +8 -13
  55. data/lib/henitai/integration/minitest_autorun.rb +20 -0
  56. data/lib/henitai/integration/minitest_fail_fast.rb +43 -0
  57. data/lib/henitai/integration/minitest_outcome_reporter.rb +44 -0
  58. data/lib/henitai/integration/minitest_preload.rb +30 -0
  59. data/lib/henitai/integration/minitest_runnable_filter.rb +61 -0
  60. data/lib/henitai/integration/minitest_test_runner.rb +47 -18
  61. data/lib/henitai/integration/mutant_run_support.rb +80 -17
  62. data/lib/henitai/integration/rspec_child_runner.rb +49 -9
  63. data/lib/henitai/integration/rspec_file_order.rb +37 -0
  64. data/lib/henitai/integration/rspec_preload.rb +35 -0
  65. data/lib/henitai/integration/rspec_process_runner.rb +4 -20
  66. data/lib/henitai/integration/scenario_log_support.rb +31 -18
  67. data/lib/henitai/integration.rb +18 -6
  68. data/lib/henitai/kill_history/store.rb +67 -0
  69. data/lib/henitai/kill_history.rb +85 -0
  70. data/lib/henitai/minitest_coverage_reporter.rb +22 -6
  71. data/lib/henitai/minitest_result_file.rb +62 -0
  72. data/lib/henitai/mutant/activation.rb +32 -0
  73. data/lib/henitai/mutant/activator.rb +75 -55
  74. data/lib/henitai/mutant/def_injection.rb +136 -0
  75. data/lib/henitai/mutant/lexical_scope_check.rb +120 -0
  76. data/lib/henitai/mutant/lexical_target.rb +105 -0
  77. data/lib/henitai/mutant/replacement.rb +52 -0
  78. data/lib/henitai/mutant.rb +32 -8
  79. data/lib/henitai/mutant_generator.rb +62 -18
  80. data/lib/henitai/mutant_history_store/database.rb +61 -0
  81. data/lib/henitai/mutant_history_store/sql.rb +9 -3
  82. data/lib/henitai/mutant_history_store/subject_scans.rb +101 -0
  83. data/lib/henitai/mutant_history_store/verdict_cache.rb +48 -18
  84. data/lib/henitai/mutant_history_store.rb +77 -51
  85. data/lib/henitai/mutant_identity.rb +5 -1
  86. data/lib/henitai/mutant_verdict.rb +71 -0
  87. data/lib/henitai/operator.rb +6 -2
  88. data/lib/henitai/operators/array_declaration.rb +4 -0
  89. data/lib/henitai/operators/assignment_expression.rb +2 -0
  90. data/lib/henitai/operators/conditional_expression.rb +21 -1
  91. data/lib/henitai/operators/equality_identity_operator.rb +2 -2
  92. data/lib/henitai/operators/hash_key_type.rb +11 -1
  93. data/lib/henitai/operators/hash_literal.rb +37 -9
  94. data/lib/henitai/operators/method_expression.rb +14 -3
  95. data/lib/henitai/operators/regex_metacharacters.rb +89 -0
  96. data/lib/henitai/operators/regex_mutator.rb +23 -28
  97. data/lib/henitai/operators/return_value.rb +1 -1
  98. data/lib/henitai/operators/string_literal.rb +17 -4
  99. data/lib/henitai/operators.rb +1 -0
  100. data/lib/henitai/per_test_coverage.rb +11 -7
  101. data/lib/henitai/per_test_coverage_collector.rb +30 -31
  102. data/lib/henitai/per_test_coverage_delta.rb +60 -0
  103. data/lib/henitai/per_test_coverage_selector.rb +26 -13
  104. data/lib/henitai/process_wakeup.rb +8 -3
  105. data/lib/henitai/process_worker_runner.rb +41 -7
  106. data/lib/henitai/reporter.rb +15 -8
  107. data/lib/henitai/result.rb +32 -16
  108. data/lib/henitai/reuse_context.rb +52 -0
  109. data/lib/henitai/rspec_coverage_formatter.rb +4 -0
  110. data/lib/henitai/rspec_outcomes.rb +74 -0
  111. data/lib/henitai/runner.rb +112 -21
  112. data/lib/henitai/runner_dependencies.rb +44 -3
  113. data/lib/henitai/sampling_strategy.rb +36 -6
  114. data/lib/henitai/scenario_execution_result.rb +51 -5
  115. data/lib/henitai/signal_deferral.rb +25 -0
  116. data/lib/henitai/signature_line.rb +21 -0
  117. data/lib/henitai/slot_scheduler/control_slots.rb +58 -0
  118. data/lib/henitai/slot_scheduler/draining.rb +35 -6
  119. data/lib/henitai/slot_scheduler/process_control.rb +33 -0
  120. data/lib/henitai/slot_scheduler/retry_policy.rb +20 -6
  121. data/lib/henitai/slot_scheduler.rb +64 -21
  122. data/lib/henitai/source_file_selection.rb +1 -1
  123. data/lib/henitai/source_parser.rb +29 -9
  124. data/lib/henitai/static_filter.rb +45 -14
  125. data/lib/henitai/stillborn_filter.rb +70 -2
  126. data/lib/henitai/subject.rb +8 -3
  127. data/lib/henitai/subject_resolver.rb +65 -31
  128. data/lib/henitai/suite_boot_clock.rb +48 -0
  129. data/lib/henitai/survivor_activation_cache.rb +18 -11
  130. data/lib/henitai/survivor_rerun_strategy.rb +47 -43
  131. data/lib/henitai/test_file_outcomes.rb +93 -0
  132. data/lib/henitai/test_prioritizer.rb +10 -2
  133. data/lib/henitai/timeout_calibrator.rb +31 -9
  134. data/lib/henitai/timeout_ceiling.rb +37 -0
  135. data/lib/henitai/verdict_fingerprint.rb +29 -30
  136. data/lib/henitai/version.rb +1 -1
  137. data/lib/henitai.rb +21 -1
  138. data/sig/configuration_validator.rbs +1 -0
  139. data/sig/henitai.rbs +254 -56
  140. metadata +63 -5
  141. data/lib/henitai/survivor_test_filter.rb +0 -72
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ module Integration
5
+ # Writes captured child output to a log file up to a byte cap, then
6
+ # discards the overflow and leaves a single truncation marker behind.
7
+ # Not interrupt-safe: the drain thread must be stopped cooperatively,
8
+ # never killed mid-write.
9
+ class CappedLogSink
10
+ def initialize(file, cap)
11
+ @file = file
12
+ @cap = cap
13
+ @written = 0
14
+ @truncated = false
15
+ end
16
+
17
+ def write(chunk)
18
+ return unless @written < @cap
19
+
20
+ slice = chunk.byteslice(0, @cap - @written)
21
+ @file.write(slice)
22
+ @written += slice.bytesize
23
+ write_truncation_marker if @written >= @cap && !@truncated
24
+ end
25
+
26
+ private
27
+
28
+ def write_truncation_marker
29
+ @file.write("\n[henitai] output truncated at #{@cap} bytes\n")
30
+ @truncated = true
31
+ end
32
+ end
33
+ end
34
+ end
@@ -9,6 +9,12 @@ module Henitai
9
9
  # without forking, and so both the RSpec and Minitest paths -- which share
10
10
  # MutantRunSupport#spawn_mutant -- get identical treatment.
11
11
  module ChildBootstrap
12
+ # Signals the parent traps for its own shutdown (INT/TERM/HUP) and for
13
+ # its event-loop wakeup (CHLD). A child that kept those handlers would
14
+ # ignore the SIGTERM sent while draining it and could write into the
15
+ # parent's wakeup pipe.
16
+ INHERITED_TRAPS = %w[INT TERM HUP CHLD].freeze
17
+
12
18
  # @param parent_pid [Integer] captured in the parent *before* Process.fork.
13
19
  # Reading Process.ppid here instead would race the very death the
14
20
  # watchdog is looking for: a parent that dies between fork and this
@@ -18,6 +24,7 @@ module Henitai
18
24
  # First, so that even a crash later in this method releases the
19
25
  # reports-directory lock rather than pinning it with an inherited fd.
20
26
  InheritedFdRegistry.close_all!
27
+ INHERITED_TRAPS.each { |signal| Signal.trap(signal, "DEFAULT") }
21
28
  Process.setpgid(0, 0)
22
29
  OrphanWatchdog.start(parent_pid:)
23
30
  nil
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "json"
5
+
6
+ module Henitai
7
+ module Integration
8
+ # Carries a mutant child's report to the parent through a small JSON file
9
+ # next to the child's logs. The parent clears the file before each fork so
10
+ # a retry can never read the previous attempt's report.
11
+ #
12
+ # The child also keeps a running-file marker next to the report: the test
13
+ # file it is executing. A timeout leaves the marker behind, so the parent
14
+ # learns which file hung.
15
+ class ChildReportStore
16
+ RUNNING_SUFFIX = ".running"
17
+
18
+ def write(path, data)
19
+ return unless path
20
+
21
+ write_atomically(path, JSON.generate(data))
22
+ nil
23
+ end
24
+
25
+ def read(path)
26
+ return unless path && File.exist?(path)
27
+
28
+ JSON.parse(File.read(path))
29
+ rescue JSON::ParserError
30
+ nil
31
+ end
32
+
33
+ def write_running(report_path, file)
34
+ return unless report_path
35
+
36
+ write_atomically(running_path(report_path), file.to_s)
37
+ nil
38
+ end
39
+
40
+ def read_running(report_path)
41
+ return unless report_path && File.exist?(running_path(report_path))
42
+
43
+ File.read(running_path(report_path))
44
+ end
45
+
46
+ def clear(path)
47
+ return nil unless path
48
+
49
+ FileUtils.rm_f([path, running_path(path)])
50
+ nil
51
+ end
52
+
53
+ private
54
+
55
+ def running_path(report_path) = "#{report_path}#{RUNNING_SUFFIX}"
56
+
57
+ def write_atomically(path, content)
58
+ FileUtils.mkdir_p(File.dirname(path))
59
+ temp_path = "#{path}.#{Process.pid}.tmp"
60
+ File.write(temp_path, content)
61
+ File.rename(temp_path, path)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -5,6 +5,7 @@ require "fileutils"
5
5
  # tail, by which point Base is already defined, so the require is skipped.
6
6
  require_relative "../integration" unless defined?(Henitai::Integration::Base)
7
7
  require_relative "minitest_load_path"
8
+ require_relative "minitest_preload"
8
9
  require_relative "minitest_suite_command"
9
10
  require_relative "minitest_test_runner"
10
11
  require_relative "rails_environment_preloader"
@@ -18,22 +19,16 @@ module Henitai
18
19
  # but implements its own Minitest-specific test invocation and suite command.
19
20
  # Per-test coverage collection is not yet wired into this path.
20
21
  class Minitest < Base
22
+ include MinitestPreload
21
23
  include MutantRunSupport
22
24
  include RspecTestSelection
23
25
 
24
- DEFAULT_SUITE_TIMEOUT = 300.0
25
-
26
26
  def test_files = spec_files
27
27
 
28
28
  def per_test_coverage_supported?
29
29
  true
30
30
  end
31
31
 
32
- def run_mutant(mutant:, test_files:, timeout:)
33
- MinitestLoadPath.ensure!
34
- super
35
- end
36
-
37
32
  def spawn_mutant(mutant:, test_files:)
38
33
  MinitestLoadPath.ensure!
39
34
  super
@@ -45,7 +40,7 @@ module Henitai
45
40
  super
46
41
  end
47
42
 
48
- def run_suite(test_files, timeout: DEFAULT_SUITE_TIMEOUT)
43
+ def run_suite(test_files, timeout: Configuration::DEFAULT_SUITE_TIMEOUT)
49
44
  log_paths = scenario_log_paths("baseline")
50
45
  pid = spawn_suite_process(test_files, log_paths)
51
46
  wait_result = wait_with_timeout(pid, timeout)
@@ -60,8 +55,8 @@ module Henitai
60
55
 
61
56
  private
62
57
 
63
- def run_tests(test_files)
64
- MinitestTestRunner.new.call(test_files)
58
+ def run_tests(test_files, outcomes, &)
59
+ MinitestTestRunner.new(preloaded: @minitest_preloaded == true).call(test_files, outcomes, &)
65
60
  end
66
61
 
67
62
  def subprocess_env
@@ -81,7 +76,7 @@ module Henitai
81
76
  File.open(log_paths[:stdout_path], "w") do |stdout_file|
82
77
  File.open(log_paths[:stderr_path], "w") do |stderr_file|
83
78
  Process.spawn(
84
- subprocess_env,
79
+ suite_process_env,
85
80
  *suite_command(test_files),
86
81
  out: stdout_file,
87
82
  err: stderr_file,
@@ -101,8 +96,8 @@ module Henitai
101
96
  end
102
97
 
103
98
  def spec_files
104
- (Dir.glob("test/**/*_test.rb") + Dir.glob("test/**/*_spec.rb"))
105
- .reject { |f| f.start_with?("test/system/") }
99
+ @spec_files ||= (Dir.glob("test/**/*_test.rb") + Dir.glob("test/**/*_spec.rb"))
100
+ .reject { |f| f.start_with?("test/system/") }
106
101
  end
107
102
  end
108
103
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ module Integration
5
+ # Turns Minitest.autorun into a no-op, so that requiring test files never
6
+ # registers the at_exit hook that would run the suite a second time.
7
+ module MinitestAutorun
8
+ SUPPRESSOR = Module.new { def autorun = nil }
9
+
10
+ def self.suppress!
11
+ require "minitest"
12
+ singleton_class = ::Minitest.singleton_class
13
+ singleton_class.prepend(SUPPRESSOR) unless singleton_class.ancestors.include?(SUPPRESSOR)
14
+ nil
15
+ rescue LoadError, NameError
16
+ nil
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest"
4
+
5
+ module Henitai
6
+ module Integration
7
+ # Ends a mutant child's Minitest run at its first failing test: one
8
+ # failure already kills the mutant. Minitest has no fail-fast of its own;
9
+ # once stopped, every test class that has not started yet reports no test
10
+ # methods, so Minitest skips it. The class running at the failure still
11
+ # finishes. Armed only around the child's Minitest.run, never for the
12
+ # baseline suite.
13
+ module MinitestFailFast
14
+ # Prepended to Minitest::Test's singleton class; Minitest 5 and 6 both
15
+ # ask a test class for its runnable methods when it starts.
16
+ module StoppableSuites
17
+ def runnable_methods
18
+ MinitestFailFast.stopped? ? [] : super
19
+ end
20
+ end
21
+
22
+ class << self
23
+ def arm!
24
+ suites = ::Minitest::Test.singleton_class
25
+ suites.prepend(StoppableSuites) unless suites.include?(StoppableSuites)
26
+ @state = :armed
27
+ end
28
+
29
+ def disarm!
30
+ @state = nil
31
+ end
32
+
33
+ def stop!
34
+ @state = :stopped if @state
35
+ end
36
+
37
+ def armed? = !@state.nil?
38
+
39
+ def stopped? = @state == :stopped
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest"
4
+ require "henitai/test_file_outcomes"
5
+ require "henitai/minitest_result_file"
6
+
7
+ module Henitai
8
+ module Integration
9
+ # Records the outcome of each finished Minitest result per test file.
10
+ # +on_failure+ is called for every failing (not skipped) result.
11
+ class MinitestOutcomeReporter < ::Minitest::Reporter
12
+ def initialize(outcomes, report_path: nil, io: $stdout, on_failure: nil)
13
+ super(io, {})
14
+ @outcomes = outcomes
15
+ @report_path = report_path
16
+ @on_failure = on_failure
17
+ end
18
+
19
+ # Reports the file of the test about to run, so a timeout can name it.
20
+ def prerecord(klass, name)
21
+ super
22
+ file = MinitestResultFile.class_file_of(klass.name.to_s) || method_file(klass, name)
23
+ @outcomes.started(file) if file
24
+ end
25
+
26
+ def record(result)
27
+ super
28
+ MinitestResultFile.record(@outcomes, result)
29
+ @on_failure&.call if !result.passed? && !result.skipped?
30
+ end
31
+
32
+ def report
33
+ super
34
+ @outcomes.write(@report_path) if @report_path
35
+ end
36
+
37
+ private
38
+
39
+ def method_file(klass, name)
40
+ MinitestResultFile.usable(klass.instance_method(name).source_location&.first)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "minitest_autorun"
4
+ require_relative "minitest_load_path"
5
+ require_relative "rails_environment_preloader"
6
+
7
+ module Henitai
8
+ module Integration
9
+ # Fork-server support for the Minitest integration (ADR-15): the server
10
+ # loads the environment and the union of the run's test files once, without
11
+ # running anything; each mutant process then runs only its own files.
12
+ module MinitestPreload
13
+ def fork_server_supported?
14
+ true
15
+ end
16
+
17
+ def preload_environment(test_files)
18
+ MinitestAutorun.suppress!
19
+ CoverageRuntimeSuppressors.suppress_simplecov!
20
+ CoverageRuntimeSuppressors.suppress_coverage!
21
+ subprocess_env.each { |key, value| ENV[key] = value }
22
+ MinitestLoadPath.ensure!
23
+ RailsEnvironmentPreloader.new.call
24
+ test_files.each { |file| require File.expand_path(file) }
25
+ @minitest_preloaded = true
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "henitai/minitest_result_file"
4
+
5
+ module Henitai
6
+ module Integration
7
+ # Narrows Minitest's registry of test classes to those defined in the given
8
+ # test files, for a process that inherited the union of a run's test files
9
+ # from the fork server (ADR-15). A class belongs to a file when one of its
10
+ # test methods is defined there.
11
+ class MinitestRunnableFilter
12
+ def initialize(test_files)
13
+ @wanted = test_files.to_set { |file| canonical(file) }
14
+ end
15
+
16
+ def apply!
17
+ with_seed { ::Minitest::Runnable.runnables.select! { |suite| defined_in_wanted_file?(suite) } }
18
+ end
19
+
20
+ private
21
+
22
+ # runnable_methods shuffles with Minitest.seed, which is unset before a run.
23
+ def with_seed
24
+ original = ::Minitest.seed
25
+ ::Minitest.seed ||= 0
26
+ yield
27
+ ensure
28
+ ::Minitest.seed = original
29
+ end
30
+
31
+ # A suite belongs to a file when its class is defined there, or when one
32
+ # of its test methods is. The class file covers tests inherited from a
33
+ # shared module, which report the module's file; the method check covers
34
+ # a class reopened in another file.
35
+ def defined_in_wanted_file?(suite)
36
+ wanted_class_file?(suite) || method_in_wanted_file?(suite)
37
+ rescue NotImplementedError, NameError
38
+ false
39
+ end
40
+
41
+ def wanted_class_file?(suite)
42
+ file = suite.name && MinitestResultFile.class_file_of(suite.name)
43
+ !file.nil? && @wanted.include?(canonical(file))
44
+ end
45
+
46
+ def method_in_wanted_file?(suite)
47
+ suite.runnable_methods.any? do |name|
48
+ location = suite.instance_method(name).source_location
49
+ location && @wanted.include?(canonical(location.first))
50
+ end
51
+ end
52
+
53
+ # Symlinked temp or checkout directories must not make one file two paths.
54
+ def canonical(path)
55
+ File.realpath(path)
56
+ rescue SystemCallError
57
+ File.expand_path(path)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,38 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "coverage_suppression"
3
+ require "henitai/integration/minitest_autorun"
4
+ require "henitai/integration/minitest_fail_fast"
5
+ require "henitai/integration/minitest_outcome_reporter"
6
+ require "henitai/integration/minitest_runnable_filter"
7
+ require "henitai/test_file_outcomes"
4
8
 
5
9
  module Henitai
6
10
  module Integration
7
11
  # Runs Minitest test files in-process and normalizes the result to an
8
12
  # exit code, for Minitest#run_tests during a single mutant activation.
9
13
  class MinitestTestRunner
10
- def call(test_files)
11
- CoverageRuntimeSuppressors.suppress_simplecov!
12
- suppress_autorun!
13
- test_files.each { |file| require File.expand_path(file) }
14
+ PLUGIN_NAME = "henitai_outcomes"
15
+
16
+ # +preloaded+: the fork server already required the union of the run's
17
+ # test files, so only the ones requested here are kept to run.
18
+ def initialize(preloaded: false)
19
+ @preloaded = preloaded
20
+ end
21
+
22
+ # Yields once the test files are required and before Minitest runs; a
23
+ # falsy block result skips the run. Per-file outcomes are recorded into
24
+ # +outcomes+ through a Minitest reporter plugin.
25
+ def call(test_files, outcomes = TestFileOutcomes.new, &after_load)
26
+ MinitestAutorun.suppress!
27
+ load_tests(test_files)
28
+ return 1 if after_load && !after_load.call
29
+
30
+ install_outcome_plugin(outcomes)
31
+ exit_code(run_fail_fast)
32
+ end
33
+
34
+ private
35
+
36
+ # One failing test kills the mutant, so the run stops there.
37
+ def run_fail_fast
38
+ MinitestFailFast.arm!
14
39
  # @type var empty_args: Array[String]
15
40
  empty_args = []
16
- status = ::Minitest.run(empty_args)
41
+ ::Minitest.run(empty_args)
42
+ ensure
43
+ MinitestFailFast.disarm!
44
+ end
45
+
46
+ def exit_code(status)
17
47
  return status if status.is_a?(Integer)
18
48
 
19
49
  status == true ? 0 : 1
20
50
  end
21
51
 
22
- private
23
-
24
- def suppress_autorun!
25
- require "minitest"
26
- singleton_class = ::Minitest.singleton_class
27
- suppressor = @autorun_suppressor ||= Module.new.tap do |mod|
28
- mod.define_method(:autorun) { nil }
52
+ def install_outcome_plugin(outcomes)
53
+ ::Minitest.extensions << PLUGIN_NAME unless ::Minitest.extensions.include?(PLUGIN_NAME)
54
+ init = :"plugin_#{PLUGIN_NAME}_init"
55
+ ::Minitest.singleton_class.send(:remove_method, init) if ::Minitest.respond_to?(init)
56
+ ::Minitest.define_singleton_method(init) do |_options|
57
+ reporter.reporters << MinitestOutcomeReporter.new(outcomes, on_failure: -> { MinitestFailFast.stop! })
29
58
  end
30
- return if singleton_class.ancestors.include?(suppressor)
59
+ end
31
60
 
32
- singleton_class.prepend(suppressor)
33
- nil
34
- rescue LoadError, NameError
35
- nil
61
+ def load_tests(test_files)
62
+ return MinitestRunnableFilter.new(test_files).apply! if @preloaded
63
+
64
+ test_files.each { |file| require File.expand_path(file) }
36
65
  end
37
66
  end
38
67
  end
@@ -10,15 +10,14 @@ module Henitai
10
10
  # The framework-specific test invocation is delegated to #run_tests, which
11
11
  # including classes must implement.
12
12
  module MutantRunSupport
13
+ # The previous attempt's report is cleared before the fork, so a retried
14
+ # child that dies early can never be classified by stale data.
13
15
  def spawn_mutant(mutant:, test_files:)
14
- log_paths = scenario_log_paths(mutant_log_name(mutant))
16
+ log_paths = mutant_log_paths(mutant)
17
+ child_report_store.clear(log_paths[:report_path])
15
18
  RspecProcessRunner.new.spawn_mutant(self, mutant:, test_files:, log_paths:)
16
19
  end
17
20
 
18
- def run_mutant(mutant:, test_files:, timeout:)
19
- RspecProcessRunner.new.run_mutant(self, mutant:, test_files:, timeout:)
20
- end
21
-
22
21
  def scenario_log_paths(name)
23
22
  reports_dir = ENV.fetch("HENITAI_REPORTS_DIR", "reports")
24
23
  log_dir = File.join(reports_dir, "mutation-logs")
@@ -29,17 +28,27 @@ module Henitai
29
28
  }
30
29
  end
31
30
 
31
+ # Mutant runs carry a report path and are classified from the child's
32
+ # report; the baseline suite has none and keeps exit-status semantics.
33
+ def mutant_log_paths(mutant)
34
+ log_paths = scenario_log_paths(mutant_log_name(mutant))
35
+ log_paths.merge(report_path: log_paths[:log_path].sub(/\.log\z/, ".report.json"))
36
+ end
37
+
32
38
  def build_result(wait_result, log_paths)
33
39
  stdout = scenario_log_support.read_log_file(log_paths[:stdout_path])
34
40
  stderr = scenario_log_support.read_log_file(log_paths[:stderr_path])
35
41
  scenario_log_support.write_combined_log(log_paths[:log_path], stdout, stderr)
42
+ output = { stdout:, stderr:, log_path: log_paths[:log_path] }
43
+ return ScenarioExecutionResult.build(wait_result:, **output) unless log_paths.key?(:report_path)
36
44
 
37
- ScenarioExecutionResult.build(
38
- wait_result:,
39
- stdout:,
40
- stderr:,
41
- log_path: log_paths[:log_path]
42
- )
45
+ build_mutant_result(wait_result, log_paths, output)
46
+ end
47
+
48
+ def build_mutant_result(wait_result, log_paths, output)
49
+ report = child_report_store.read(log_paths[:report_path])
50
+ running_test = child_report_store.read_running(log_paths[:report_path])
51
+ ScenarioExecutionResult.build_for_mutant(wait_result:, report:, running_test:, **output)
43
52
  end
44
53
 
45
54
  def run_in_child(mutant:, test_files:, log_paths:)
@@ -61,17 +70,71 @@ module Henitai
61
70
  private
62
71
 
63
72
  def run_child_activation_and_tests(mutant:, test_files:, log_paths:)
73
+ report = ->(data) { child_report_store.write(log_paths[:report_path], data) }
74
+ running = ->(file) { child_report_store.write_running(log_paths[:report_path], file) }
64
75
  scenario_log_support.with_coverage_dir(mutant.id) do
65
76
  scenario_log_support.capture_child_output(log_paths) do
66
- child_debug_log.mutant_meta(mutant)
67
- child_debug_log.activation_start(mutant.id)
68
- activation_result = Mutant::Activator.activate!(mutant)
69
- child_debug_log.activation_check
70
- child_debug_log.activation_end(activation_result, test_files:)
71
- activation_result == :compile_error ? 2 : run_tests(test_files)
77
+ run_tests_in_child(mutant, test_files, report, running)
72
78
  end
73
79
  end
74
80
  end
81
+
82
+ # Any failure to put the mutant in place is a harness outcome: the tests
83
+ # never ran against the mutation, so nothing may count as a kill.
84
+ def activate_in_child(mutant, test_files, report)
85
+ return true unless activate_with_debug_log(mutant, test_files) == :compile_error
86
+
87
+ report.call("outcome" => "activation_failed", "reason" => "the activation source does not compile")
88
+ false
89
+ rescue StandardError, ScriptError => e
90
+ warn "henitai: mutant activation failed: #{e.class}: #{e.message}"
91
+ report.call("outcome" => "activation_failed", "reason" => "#{e.class}: #{e.message}")
92
+ false
93
+ end
94
+
95
+ def activate_with_debug_log(mutant, test_files)
96
+ log = child_debug_log
97
+ log.mutant_meta(mutant)
98
+ log.activation_start(mutant.id)
99
+ activation_result = Mutant::Activator.activate!(mutant)
100
+ log.activation_check
101
+ log.activation_end(activation_result, test_files:)
102
+ activation_result
103
+ end
104
+
105
+ # RSpec and Minitest let SystemExit through by design. Raised by the
106
+ # code under test it ends the run midway, so it is reported instead of
107
+ # becoming the child's exit status.
108
+ #
109
+ # The mutant is activated from #run_tests' after-load hook: once the test
110
+ # files (and the code they require) are loaded, before any test runs.
111
+ # Activating earlier would load the subject file on its own, without the
112
+ # superclasses and constants the rest of the application defines.
113
+ # +running+ records the test file being executed, so a timeout can name it.
114
+ def run_tests_in_child(mutant, test_files, report, running)
115
+ activated = nil
116
+ outcomes = TestFileOutcomes.new(on_started: running)
117
+ exit_code = run_tests(test_files, outcomes) { activated = activate_in_child(mutant, test_files, report) }
118
+ return finish_without_tests(activated, report) unless activated
119
+
120
+ report.call(
121
+ "outcome" => "tests_finished",
122
+ "exit_code" => exit_code,
123
+ "failed_tests" => outcomes.failed_files,
124
+ "unattributed_failures" => outcomes.unattributed_failures?
125
+ )
126
+ exit_code
127
+ rescue SystemExit => e
128
+ report.call("outcome" => "system_exit", "status" => e.status)
129
+ 1
130
+ end
131
+
132
+ # A nil activation means the hook never ran: the test files failed to
133
+ # load, before the mutant was in place.
134
+ def finish_without_tests(activated, report)
135
+ report.call("outcome" => "environment_failed") if activated.nil?
136
+ 2
137
+ end
75
138
  end
76
139
  end
77
140
  end