mutineer 1.8.1 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +733 -0
- data/README.md +85 -50
- data/lib/mutineer/changed_lines.rb +2 -1
- data/lib/mutineer/child_cause.rb +149 -0
- data/lib/mutineer/child_stdout.rb +12 -1
- data/lib/mutineer/cli.rb +540 -59
- data/lib/mutineer/command_redact.rb +38 -0
- data/lib/mutineer/concern_blocks.rb +95 -24
- data/lib/mutineer/config.rb +112 -6
- data/lib/mutineer/coverage_map.rb +1345 -156
- data/lib/mutineer/daemon_backend.rb +41 -18
- data/lib/mutineer/daemon_client.rb +104 -21
- data/lib/mutineer/daemon_server.rb +85 -14
- data/lib/mutineer/database_urls.rb +189 -0
- data/lib/mutineer/external_backend.rb +93 -6
- data/lib/mutineer/fs_path.rb +125 -0
- data/lib/mutineer/isolation.rb +380 -25
- data/lib/mutineer/job_plan.rb +352 -41
- data/lib/mutineer/minitest_integration/file_order.rb +56 -0
- data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
- data/lib/mutineer/minitest_integration.rb +26 -58
- data/lib/mutineer/mutant_id.rb +4 -1
- data/lib/mutineer/mutation.rb +10 -4
- data/lib/mutineer/mutator_registry.rb +6 -3
- data/lib/mutineer/mutators/collection_method.rb +195 -6
- data/lib/mutineer/mutators/literal_mutation.rb +33 -3
- data/lib/mutineer/mutators/range_literal.rb +78 -29
- data/lib/mutineer/mutators/rescue_body.rb +126 -0
- data/lib/mutineer/mutators/return_nil.rb +23 -6
- data/lib/mutineer/output_filter.rb +451 -0
- data/lib/mutineer/pairing.rb +463 -31
- data/lib/mutineer/parser.rb +53 -0
- data/lib/mutineer/project_path.rb +15 -8
- data/lib/mutineer/rails_worker_db.rb +366 -6
- data/lib/mutineer/reach_probe.rb +229 -0
- data/lib/mutineer/redaction.rb +301 -41
- data/lib/mutineer/reload_guards.rb +985 -0
- data/lib/mutineer/reporter.rb +467 -58
- data/lib/mutineer/result.rb +6 -7
- data/lib/mutineer/run_seed.rb +16 -0
- data/lib/mutineer/runner.rb +194 -55
- data/lib/mutineer/source_bytes.rb +187 -0
- data/lib/mutineer/statement_lines.rb +108 -14
- data/lib/mutineer/test_runners/rspec.rb +118 -10
- data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
- data/lib/mutineer/test_runners.rb +3 -2
- data/lib/mutineer/triage.rb +55 -10
- data/lib/mutineer/version.rb +1 -1
- data/lib/mutineer.rb +1 -0
- data/skills/mutineer/SKILL.md +12 -4
- metadata +13 -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
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "reload_guards"
|
|
4
|
+
|
|
3
5
|
module Mutineer
|
|
4
6
|
# Lets the reload strategy load a concern file a second time (#256).
|
|
5
7
|
#
|
|
@@ -15,10 +17,13 @@ module Mutineer
|
|
|
15
17
|
# ActiveSupport does not run the block again in a class that already includes
|
|
16
18
|
# the concern, so that class keeps what the original block defined. For a def
|
|
17
19
|
# in the block, the mutant file also passes the mutated def to
|
|
18
|
-
# {.store_hook_def}, just before the `included` or `prepended` call
|
|
19
|
-
# {.
|
|
20
|
-
#
|
|
21
|
-
#
|
|
20
|
+
# {.store_hook_def}, just before the `included` or `prepended` call. Under
|
|
21
|
+
# {.load_mutant} with a `hook_def`, {.store_hook_def} then calls
|
|
22
|
+
# {.place_hook_def} at once, which loads only that def into each class that
|
|
23
|
+
# has the method (#297). It runs before the rest of the file, so a later
|
|
24
|
+
# line of the file that calls the method while the file loads gets the
|
|
25
|
+
# mutant (#348). Nothing else in the block runs again. Discovery decides
|
|
26
|
+
# which defs can be placed this way (see {Subject#unplaceable?}).
|
|
22
27
|
#
|
|
23
28
|
# Call it only in a forked child that loads the mutant. Without
|
|
24
29
|
# ActiveSupport, {.replace} and {.forget} do nothing; {.store_hook_def} and
|
|
@@ -46,23 +51,49 @@ module Mutineer
|
|
|
46
51
|
end
|
|
47
52
|
end
|
|
48
53
|
|
|
49
|
-
# Loads the mutant file at `path` over `source_file`, through {.replace}
|
|
50
|
-
#
|
|
51
|
-
#
|
|
52
|
-
#
|
|
54
|
+
# Loads the mutant file at `path` over `source_file`, through {.replace}
|
|
55
|
+
# inside {ReloadGuards.during_load}, so Rails macros in the class body can
|
|
56
|
+
# run a second time (#378, #383, #384). The load assigns each of the file's
|
|
57
|
+
# constants a second time, so Ruby would warn twice per constant, per
|
|
58
|
+
# mutant (#364). Warnings are off for the load only, and back on before
|
|
59
|
+
# the tests run.
|
|
60
|
+
#
|
|
61
|
+
# With a `hook_def`, the file's {.store_hook_def} call places the mutated
|
|
62
|
+
# def during the load (#348). A load that never reached that call raises.
|
|
63
|
+
# When a handle to the original is live at placement, the kept-handle
|
|
64
|
+
# check runs again after the load, so a handle the file holds only while
|
|
65
|
+
# it loads does not count. Placement leaves no other way to reach the
|
|
66
|
+
# original, so no handle seen then means none after the load.
|
|
53
67
|
#
|
|
54
68
|
# @param source_file [String] the file the mutant replaces.
|
|
55
69
|
# @param path [String] the mutant file to load.
|
|
70
|
+
# @param hook_def [Hash{Symbol => Object}, nil] the original def's `line`,
|
|
71
|
+
# `name` and `singleton`, from {JobPlan.hook_mutant}.
|
|
72
|
+
# @raise [RuntimeError] from {.place_hook_def}, so the child scores `error`.
|
|
56
73
|
# @return [Object] what `load` returns.
|
|
57
|
-
def self.load_mutant(source_file, path)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
$VERBOSE =
|
|
74
|
+
def self.load_mutant(source_file, path, hook_def: nil)
|
|
75
|
+
@hook_target = hook_def && { source_file: source_file, **hook_def }
|
|
76
|
+
@hook_def = nil
|
|
77
|
+
@handle_at_placement = false
|
|
78
|
+
loaded = ReloadGuards.during_load(source_file, path) do
|
|
79
|
+
replace(source_file) do
|
|
80
|
+
verbose, $VERBOSE = $VERBOSE, nil
|
|
81
|
+
begin
|
|
82
|
+
load path
|
|
83
|
+
ensure
|
|
84
|
+
$VERBOSE = verbose
|
|
85
|
+
end
|
|
64
86
|
end
|
|
65
87
|
end
|
|
88
|
+
if @hook_target
|
|
89
|
+
# Still set: the load did not reach store_hook_def, so this raises.
|
|
90
|
+
place_hook_def(**@hook_target)
|
|
91
|
+
elsif @handle_at_placement
|
|
92
|
+
refuse_retained_hook_def(source_file: source_file, line: hook_def[:line], name: hook_def[:name])
|
|
93
|
+
end
|
|
94
|
+
loaded
|
|
95
|
+
ensure
|
|
96
|
+
@hook_target = nil
|
|
66
97
|
end
|
|
67
98
|
|
|
68
99
|
# Removes every stored `included` and `prepended` block that the source
|
|
@@ -93,13 +124,23 @@ module Mutineer
|
|
|
93
124
|
|
|
94
125
|
# Keeps the mutated hook-block def for {.place_hook_def}. The mutant file
|
|
95
126
|
# calls it in the concern's body, just before the hook call, so the block has
|
|
96
|
-
# the same constant scope as the hook block (#297).
|
|
127
|
+
# the same constant scope as the hook block (#297). Under {.load_mutant}
|
|
128
|
+
# with a `hook_def`, it places the def now, before the rest of the file
|
|
129
|
+
# runs (#348).
|
|
97
130
|
#
|
|
98
|
-
# @yield the mutated def, run
|
|
131
|
+
# @yield the mutated def, run with `class_eval` in each owner.
|
|
132
|
+
# @raise [RuntimeError] from {.place_hook_def}.
|
|
99
133
|
# @return [nil]
|
|
100
134
|
def self.store_hook_def(&block)
|
|
101
135
|
@hook_def = block
|
|
102
|
-
|
|
136
|
+
return unless @hook_target
|
|
137
|
+
|
|
138
|
+
target = @hook_target
|
|
139
|
+
# A handle may be one the file drops before its load ends: load_mutant checks again then.
|
|
140
|
+
@handle_at_placement = method_handle_at?(real_path(target[:source_file]), target[:line], target[:name])
|
|
141
|
+
place_hook_def(**target, check_retained: false)
|
|
142
|
+
# Cleared only after placement, so a raise the file rescues still errors after the load.
|
|
143
|
+
@hook_target = nil
|
|
103
144
|
end
|
|
104
145
|
|
|
105
146
|
# Takes the def {.store_hook_def} kept, clears it, and runs it with
|
|
@@ -112,12 +153,14 @@ module Mutineer
|
|
|
112
153
|
# @param line [Integer] the original def's first line.
|
|
113
154
|
# @param name [Symbol] the method name.
|
|
114
155
|
# @param singleton [Boolean] true for a class method.
|
|
156
|
+
# @param check_retained [Boolean] false when the caller checks for a kept
|
|
157
|
+
# method handle itself, after the load ({.load_mutant}).
|
|
115
158
|
# @raise [RuntimeError] when no def was kept (the mutant file did not reach
|
|
116
159
|
# its hook call), or when a module keeps the original under another name
|
|
117
160
|
# (see {.aliases}) or in a live method handle; the child then scores the
|
|
118
161
|
# mutant `error`.
|
|
119
162
|
# @return [Integer] how many owners got the mutated def.
|
|
120
|
-
def self.place_hook_def(source_file:, line:, name:, singleton:)
|
|
163
|
+
def self.place_hook_def(source_file:, line:, name:, singleton:, check_retained: true)
|
|
121
164
|
block = @hook_def
|
|
122
165
|
@hook_def = nil
|
|
123
166
|
where = "mutineer: could not place the mutant of #{name} (#{source_file}:#{line})"
|
|
@@ -127,14 +170,11 @@ module Mutineer
|
|
|
127
170
|
end
|
|
128
171
|
|
|
129
172
|
# Check before owner and alias lookup create temporary UnboundMethods.
|
|
130
|
-
|
|
131
|
-
raise "#{where}: the original is retained in a Method or UnboundMethod object, " \
|
|
132
|
-
"so the mutant scores error."
|
|
133
|
-
end
|
|
173
|
+
refuse_retained_hook_def(source_file: source_file, line: line, name: name) if check_retained
|
|
134
174
|
|
|
135
175
|
# ponytail: one ObjectSpace walk per mutant (about 11 ms with Rails loaded);
|
|
136
176
|
# index modules by method name if a large app makes it slow.
|
|
137
|
-
modules = ObjectSpace.each_object(Module).
|
|
177
|
+
modules = ObjectSpace.each_object(Module).reject { |mod| deprecation_proxy?(mod) }
|
|
138
178
|
found = owners(modules, source_file: source_file, line: line, name: name, singleton: singleton)
|
|
139
179
|
mod, other = aliases(modules, source_file: source_file, line: line, name: name, singleton: singleton).first
|
|
140
180
|
if mod
|
|
@@ -213,6 +253,21 @@ module Mutineer
|
|
|
213
253
|
method.original_name == original_name && def_line == line && !file.nil? && real_path(file) == path
|
|
214
254
|
end
|
|
215
255
|
|
|
256
|
+
# Raises when a live Method or UnboundMethod keeps the original definition
|
|
257
|
+
# (see {.retained_hook_def?}), so the child scores the mutant `error`.
|
|
258
|
+
#
|
|
259
|
+
# @param source_file [String] the file the mutant replaces.
|
|
260
|
+
# @param line [Integer] the original def's first line.
|
|
261
|
+
# @param name [Symbol] the original method name.
|
|
262
|
+
# @raise [RuntimeError] when such a handle is live.
|
|
263
|
+
# @return [nil]
|
|
264
|
+
def self.refuse_retained_hook_def(source_file:, line:, name:)
|
|
265
|
+
return unless retained_hook_def?(source_file: source_file, line: line, name: name)
|
|
266
|
+
|
|
267
|
+
raise "mutineer: could not place the mutant of #{name} (#{source_file}:#{line}): the original is " \
|
|
268
|
+
"retained in a Method or UnboundMethod object, so the mutant scores error."
|
|
269
|
+
end
|
|
270
|
+
|
|
216
271
|
# True when a live Method or UnboundMethod keeps the original definition.
|
|
217
272
|
# A wrapper can call that handle after the named method was replaced, so
|
|
218
273
|
# loading another def cannot make the wrapper test the mutant. A matching
|
|
@@ -248,6 +303,22 @@ module Mutineer
|
|
|
248
303
|
end
|
|
249
304
|
end
|
|
250
305
|
|
|
306
|
+
# True for Rails' `DeprecatedConstantProxy`: a Module that forwards every
|
|
307
|
+
# call to its target and prints a deprecation warning, so asking it for
|
|
308
|
+
# methods would print one per mutant (#387). `Kernel#class` reads its real
|
|
309
|
+
# class without forwarding.
|
|
310
|
+
#
|
|
311
|
+
# @param mod [Module] a module from the ObjectSpace walk.
|
|
312
|
+
# @return [Boolean]
|
|
313
|
+
def self.deprecation_proxy?(mod)
|
|
314
|
+
return false unless defined?(::ActiveSupport::Deprecation::DeprecatedConstantProxy)
|
|
315
|
+
|
|
316
|
+
(KERNEL_CLASS.bind_call(mod) <= ::ActiveSupport::Deprecation::DeprecatedConstantProxy) == true
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# `Kernel#class`, unbound, so a proxy that redefines `class` cannot answer.
|
|
320
|
+
KERNEL_CLASS = ::Kernel.instance_method(:class)
|
|
321
|
+
|
|
251
322
|
# The file's real path, so a symlinked or relative path matches the path
|
|
252
323
|
# Ruby recorded when it loaded the file.
|
|
253
324
|
#
|
data/lib/mutineer/config.rb
CHANGED
|
@@ -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
|
|
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
|
|
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
|
-
|
|
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.
|