mutineer 1.8.2 → 1.9.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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +611 -0
  3. data/README.md +84 -49
  4. data/lib/mutineer/changed_lines.rb +2 -1
  5. data/lib/mutineer/child_cause.rb +149 -0
  6. data/lib/mutineer/child_stdout.rb +12 -1
  7. data/lib/mutineer/cli.rb +536 -58
  8. data/lib/mutineer/command_redact.rb +38 -0
  9. data/lib/mutineer/config.rb +112 -6
  10. data/lib/mutineer/coverage_map.rb +1345 -156
  11. data/lib/mutineer/daemon_backend.rb +40 -17
  12. data/lib/mutineer/daemon_client.rb +63 -15
  13. data/lib/mutineer/daemon_server.rb +75 -5
  14. data/lib/mutineer/database_urls.rb +189 -0
  15. data/lib/mutineer/external_backend.rb +47 -1
  16. data/lib/mutineer/fs_path.rb +125 -0
  17. data/lib/mutineer/isolation.rb +374 -20
  18. data/lib/mutineer/job_plan.rb +349 -39
  19. data/lib/mutineer/minitest_integration/file_order.rb +56 -0
  20. data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
  21. data/lib/mutineer/minitest_integration.rb +26 -58
  22. data/lib/mutineer/mutant_id.rb +4 -1
  23. data/lib/mutineer/mutation.rb +6 -1
  24. data/lib/mutineer/mutator_registry.rb +6 -3
  25. data/lib/mutineer/mutators/collection_method.rb +195 -6
  26. data/lib/mutineer/mutators/literal_mutation.rb +33 -3
  27. data/lib/mutineer/mutators/range_literal.rb +78 -29
  28. data/lib/mutineer/mutators/rescue_body.rb +126 -0
  29. data/lib/mutineer/mutators/return_nil.rb +23 -6
  30. data/lib/mutineer/output_filter.rb +451 -0
  31. data/lib/mutineer/pairing.rb +463 -31
  32. data/lib/mutineer/parser.rb +14 -0
  33. data/lib/mutineer/project_path.rb +15 -8
  34. data/lib/mutineer/rails_worker_db.rb +356 -6
  35. data/lib/mutineer/reach_probe.rb +229 -0
  36. data/lib/mutineer/redaction.rb +264 -32
  37. data/lib/mutineer/reload_guards.rb +460 -39
  38. data/lib/mutineer/reporter.rb +465 -57
  39. data/lib/mutineer/result.rb +6 -7
  40. data/lib/mutineer/run_seed.rb +16 -0
  41. data/lib/mutineer/runner.rb +175 -51
  42. data/lib/mutineer/source_bytes.rb +187 -0
  43. data/lib/mutineer/statement_lines.rb +108 -14
  44. data/lib/mutineer/test_runners/rspec.rb +118 -10
  45. data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
  46. data/lib/mutineer/test_runners.rb +3 -2
  47. data/lib/mutineer/triage.rb +55 -10
  48. data/lib/mutineer/version.rb +1 -1
  49. data/lib/mutineer.rb +1 -0
  50. data/skills/mutineer/SKILL.md +12 -4
  51. metadata +12 -1
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Loaded through RUBYOPT in a Ruby process that `--test-command` starts,
4
+ # before that program runs. The copier then sees a DATABASE_URL the program
5
+ # assigns itself (#457). Stdlib only, so an app Ruby older than the tool can
6
+ # load it. Not required by the gem: loading it installs the copier.
7
+
8
+ require_relative "output_filter"
9
+ require_relative "database_urls"
10
+
11
+ module Mutineer
12
+ # Starts {OutputFilter} for one `--test-command` Ruby process (#457).
13
+ module CommandRedact
14
+ module_function
15
+
16
+ # Remember dotenv URLs in this working directory and copy stdout and
17
+ # stderr through redaction. A failed install leaves the descriptors
18
+ # where they were, and the command still runs.
19
+ #
20
+ # @return [Symbol] the result of {OutputFilter.install}.
21
+ def install
22
+ Redaction.extra_urls = DatabaseUrls.from_files(working_directory)
23
+ OutputFilter.install
24
+ end
25
+
26
+ # The working directory, or nil when it was removed. A command that runs
27
+ # from a deleted directory must still start.
28
+ #
29
+ # @return [String, nil]
30
+ def working_directory
31
+ Dir.pwd
32
+ rescue SystemCallError
33
+ nil
34
+ end
35
+ end
36
+ end
37
+
38
+ Mutineer::CommandRedact.install
@@ -3,6 +3,7 @@
3
3
  require "did_you_mean"
4
4
  require "etc"
5
5
  require "yaml"
6
+ require_relative "fs_path"
6
7
 
7
8
  module Mutineer
8
9
  # The phrase every 1.x warning uses for behaviour that 2.0 turns into an
@@ -64,6 +65,8 @@ module Mutineer
64
65
  ConfigOption.new(field: :cache_dir, type: :string, yaml_key: "cache_dir", flag: "--cache-dir"),
65
66
  ConfigOption.new(field: :allow_empty, type: :bool, yaml_key: "allow_empty", flag: "--allow-empty"),
66
67
  ConfigOption.new(field: :shared_databases, type: :string_list, yaml_key: "shared_databases"),
68
+ # YAML-only: pairs source patterns with extra test patterns for pairing (#382).
69
+ ConfigOption.new(field: :test_map, type: :test_map, yaml_key: "tests"),
67
70
  ConfigOption.new(field: :coverage_from, type: :string, flag: "--coverage-from"),
68
71
  ConfigOption.new(field: :format, type: :enum, flag: "--format", values: %w[human json html]),
69
72
  ConfigOption.new(field: :strategy, type: :enum, flag: "--strategy", values: %w[reload redefine],
@@ -88,10 +91,18 @@ module Mutineer
88
91
  # `shared_databases` (file only) names test database configs that parallel
89
92
  # `--daemon` workers may share; see DaemonBackend.worker_count_for.
90
93
  #
94
+ # `test_map` (file key `tests`, no flag) holds checked source→test pattern
95
+ # entries that pairing adds to a source's paired tests (#382). Distinct from
96
+ # `:tests`, the CLI `--test` file list.
97
+ #
91
98
  # Boot mode adds: boot (a file to require ONCE in the parent so the app env,
92
99
  # e.g. Rails, is booted before forking; sources are then NOT manually required)
93
100
  # and rails (sugar: defaults boot to config/environment, prefers redefine without
94
101
  # a daemon, and reconnects ActiveRecord per fork).
102
+ #
103
+ # `unpaired_sources` is code only. Pairing stores each source it skips
104
+ # because no conventional test exists. An explicit test list leaves it empty.
105
+ # There is no flag and no config-file key.
95
106
  Config = Struct.new(
96
107
  :sources, :tests, :operators, :threshold, :only, :dry_run,
97
108
  :cache_dir, :project_root, :load_paths,
@@ -99,15 +110,16 @@ module Mutineer
99
110
  :boot, :rails, :since, :framework, :verbose, :ignore, :ignore_reasons,
100
111
  # :daemon is user-facing (--daemon flag + KNOWN_KEYS + boolean coerce).
101
112
  # :daemon_timeout stays programmatic (set by tests/Runner; no flag yet).
113
+ # :unpaired_sources stays programmatic (set by pairing; no flag yet).
102
114
  :baseline, :baseline_epsilon, :fail_fast, :test_command,
103
115
  :daemon, :daemon_timeout, :timeout, :capture_timeout, :matrix, :allow_empty, :coverage_from,
104
- :shared_databases,
116
+ :shared_databases, :unpaired_sources, :test_map,
105
117
  keyword_init: true
106
118
  ) do
107
119
  # Config file name.
108
120
  CONFIG_FILE = ".mutineer.yml"
109
- # Keys accepted in .mutineer.yml, derived from the schema. `require` maps to the
110
- # :require_paths field.
121
+ # Keys accepted in .mutineer.yml, derived from the schema. `require` maps to
122
+ # :require_paths; `tests` maps to :test_map (not the CLI :tests field).
111
123
  KNOWN_KEYS = CONFIG_OPTIONS.filter_map(&:yaml_key).freeze
112
124
 
113
125
  # @param explicit [Array<Symbol>] fields the user wrote (CLI or file). Derived
@@ -123,7 +135,7 @@ module Mutineer
123
135
  self.threshold ||= 0.0
124
136
  self.dry_run ||= false
125
137
  self.cache_dir ||= ".mutineer"
126
- self.project_root ||= Dir.pwd
138
+ self.project_root = FsPath.read(project_root || Dir.pwd)
127
139
  self.load_paths ||= ["lib"]
128
140
  self.jobs ||= Etc.nprocessors
129
141
  self.format ||= "human"
@@ -139,6 +151,8 @@ module Mutineer
139
151
  self.matrix = false if matrix.nil?
140
152
  self.allow_empty = false if allow_empty.nil?
141
153
  self.shared_databases ||= []
154
+ self.unpaired_sources ||= []
155
+ self.test_map ||= []
142
156
  end
143
157
 
144
158
  # True when the user wrote `key`, on the command line or in the config
@@ -297,12 +311,14 @@ module Mutineer
297
311
  specs > tests.length / 2.0 ? "rspec" : "minitest"
298
312
  end
299
313
 
300
- # Maps a config key to its Struct field.
314
+ # Maps a config key to its Struct field. Reads the schema row so YAML keys
315
+ # that differ from the field (`require` → :require_paths, `tests` →
316
+ # :test_map) stay correct without a hand-maintained alias list.
301
317
  #
302
318
  # @param known_key [String] config key.
303
319
  # @return [Symbol] struct field name.
304
320
  def self.field_for(known_key)
305
- known_key == "require" ? :require_paths : known_key.to_sym
321
+ CONFIG_OPTIONS.find { |o| o.yaml_key == known_key }&.field || known_key.to_sym
306
322
  end
307
323
 
308
324
  # Parses one raw value into the typed value for `field`. A value that does
@@ -352,6 +368,8 @@ module Mutineer
352
368
  raise ConfigError, "#{prefix}unknown #{field} #{value.to_s.inspect}. Expected: #{opt.values.join(', ')}"
353
369
  when :ignore_list
354
370
  parse_ignore_list(value, file)
371
+ when :test_map
372
+ parse_test_map(value, file)
355
373
  when :string_list
356
374
  items = Array(value).map(&:to_s)
357
375
  # Only `operators` treats [] as "run these" rather than "use the
@@ -404,6 +422,94 @@ module Mutineer
404
422
  f if f&.finite?
405
423
  end
406
424
 
425
+ # Reads a `tests:` map into checked entries. Each entry is a Hash with
426
+ # `:sources` and `:tests` arrays of relative pattern strings. A single
427
+ # string pattern becomes a one-element list. `[]` is valid (no map).
428
+ # Absolute paths, blank patterns, unknown keys, and non-mapping entries
429
+ # raise ConfigError naming the file and `tests entry N`.
430
+ #
431
+ # @api private
432
+ # @param value [Object] the raw YAML value.
433
+ # @param file [String, nil] config file name.
434
+ # @return [Array<Hash{Symbol => Array<String>}>]
435
+ # @raise [Mutineer::ConfigError] when the value or an entry is malformed.
436
+ def self.parse_test_map(value, file)
437
+ label = file || CONFIG_FILE
438
+ origin = "#{label}: tests"
439
+ got = "(got: #{value.inspect})"
440
+ unless value.is_a?(Array)
441
+ raise ConfigError, "#{origin} must be a list of mappings with sources and tests #{got}"
442
+ end
443
+
444
+ value.each_with_index.map do |entry, index|
445
+ number = index + 1
446
+ entry_origin = "#{origin} entry #{number}"
447
+ unless entry.is_a?(Hash)
448
+ raise ConfigError, "#{entry_origin} must be a mapping with sources and tests " \
449
+ "(got: #{entry.inspect})"
450
+ end
451
+
452
+ keys = entry.keys.map(&:to_s)
453
+ unknown = keys - %w[sources tests]
454
+ unless unknown.empty?
455
+ names = unknown.map(&:inspect).join(", ")
456
+ raise ConfigError, "#{entry_origin} has unknown key #{names}"
457
+ end
458
+ raise ConfigError, "#{entry_origin} is missing sources" unless keys.include?("sources")
459
+ raise ConfigError, "#{entry_origin} is missing tests" unless keys.include?("tests")
460
+
461
+ sources = entry.key?("sources") ? entry["sources"] : entry[:sources]
462
+ tests = entry.key?("tests") ? entry["tests"] : entry[:tests]
463
+ {
464
+ sources: parse_test_map_patterns(sources, entry_origin, "sources"),
465
+ tests: parse_test_map_patterns(tests, entry_origin, "tests")
466
+ }
467
+ end
468
+ end
469
+
470
+ # Turns one `sources` or `tests` value into a non-empty list of relative
471
+ # pattern strings. A bare string becomes a one-element list.
472
+ #
473
+ # @api private
474
+ # @param value [Object] raw YAML value for the key.
475
+ # @param entry_origin [String] `file: tests entry N` prefix for errors.
476
+ # @param key [String] `"sources"` or `"tests"`.
477
+ # @return [Array<String>]
478
+ # @raise [Mutineer::ConfigError] when the list or a pattern is invalid.
479
+ def self.parse_test_map_patterns(value, entry_origin, key)
480
+ patterns = case value
481
+ when String then [value]
482
+ when Array then value
483
+ else
484
+ raise ConfigError, "#{entry_origin} #{key} must be a pattern or a list of " \
485
+ "patterns (got: #{value.inspect})"
486
+ end
487
+ raise ConfigError, "#{entry_origin} #{key} must not be empty" if patterns.empty?
488
+
489
+ patterns.map do |pattern|
490
+ unless pattern.is_a?(String)
491
+ raise ConfigError, "#{entry_origin} #{key} pattern must be a string " \
492
+ "(got: #{pattern.inspect})"
493
+ end
494
+ if pattern.strip.empty?
495
+ raise ConfigError, "#{entry_origin} #{key} pattern must not be blank " \
496
+ "(got: #{pattern.inspect})"
497
+ end
498
+ if File.absolute_path?(pattern)
499
+ raise ConfigError, "#{entry_origin} #{key} pattern must be relative, not absolute " \
500
+ "(got: #{pattern.inspect})"
501
+ end
502
+ # Patterns resolve against the run directory. A `..` segment can leave
503
+ # that root, so reject it here the same way absolute paths are rejected.
504
+ if pattern.split(%r{[/\\]}).include?("..")
505
+ raise ConfigError, "#{entry_origin} #{key} pattern must stay under the run directory " \
506
+ "(got: #{pattern.inspect})"
507
+ end
508
+
509
+ pattern
510
+ end
511
+ end
512
+
407
513
  # Reads an `ignore:` value into ids plus a reason for each mapping.
408
514
  # A bare string or integer is an id with no reason. YAML reads an unquoted
409
515
  # all-digit id as an integer, and the id text is those decimal digits.