pbt 0.8.0 → 0.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.
@@ -1,361 +1,357 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pbt/stateful/errors"
4
+ require "pbt/stateful/types"
5
+ require "pbt/stateful/command"
6
+ require "pbt/stateful/spec"
7
+ require "pbt/stateful/shrinker"
8
+
3
9
  module Pbt
4
10
  module Stateful
5
- # Property-compatible wrapper for command-based stateful testing.
6
- # It provides `generate`, `shrink` and `run`, so existing runners can execute it.
11
+ # Property-compatible stateful test driver backing `Pbt.stateful { |m| ... }`.
7
12
  class Property
8
- ARG_AWARE_GENERATION_ATTEMPTS = 5
9
-
10
- REQUIRED_COMMAND_METHODS = %i[
11
- name
12
- arguments
13
- applicable?
14
- next_state
15
- run!
16
- verify!
17
- ].freeze
18
-
19
- Step = Struct.new(:command, :args) do
20
- def inspect
21
- "#<Pbt::Stateful::Step command=#{command_label}, args=#{args.inspect}>"
22
- end
13
+ PER_STEP_MAX_ATTEMPTS = 32
14
+ private_constant :PER_STEP_MAX_ATTEMPTS
23
15
 
24
- private
16
+ attr_reader :spec, :max_steps, :min_steps, :max_shrink_attempts
25
17
 
26
- def command_label
27
- if command.respond_to?(:name)
28
- command.name.inspect
29
- else
30
- command.class.name || command.class.inspect
31
- end
32
- end
33
- end
34
-
35
- # @param model [Object]
36
- # @param sut [Proc]
18
+ # @param spec [Spec]
37
19
  # @param max_steps [Integer]
38
- def initialize(model:, sut:, max_steps:)
39
- validate_model!(model)
40
- raise Pbt::InvalidConfiguration, "sut must be callable" unless sut.respond_to?(:call)
41
- raise Pbt::InvalidConfiguration, "max_steps must be an Integer" unless max_steps.is_a?(Integer)
42
- raise Pbt::InvalidConfiguration, "max_steps must be non-negative" if max_steps.negative?
43
-
44
- @model = model
45
- @sut_factory = sut
20
+ # @param min_steps [Integer]
21
+ # @param max_shrink_attempts [Integer]
22
+ def initialize(spec:, max_steps: 20, min_steps: 1, max_shrink_attempts: 1_000)
23
+ validate_options!(max_steps: max_steps, min_steps: min_steps, max_shrink_attempts: max_shrink_attempts)
24
+
25
+ @spec = spec
46
26
  @max_steps = max_steps
27
+ @min_steps = min_steps
28
+ @max_shrink_attempts = max_shrink_attempts
29
+ @shrinker = Shrinker.new(spec)
47
30
  end
48
31
 
49
- # Generate a sequence of commands valid for the current model state.
32
+ def stateful?
33
+ true
34
+ end
35
+
36
+ # Generate a pure immutable `Pbt::Stateful::Run` (compatible with `Pbt.statistics`).
50
37
  #
51
38
  # @param rng [Random]
52
- # @return [Array<Step>]
39
+ # @return [Run]
53
40
  def generate(rng = Random.new)
54
- length = rng.rand(0..@max_steps)
55
- state = @model.initial_state
56
- sequence = []
41
+ generate_for_check(rng).value
42
+ end
57
43
 
58
- length.times do
59
- candidates = generate_candidates_for(state, rng, context: "generate")
60
- break if candidates.empty?
44
+ # Generate a `RunSample` carrying contextual shrink provenance for `Pbt.check` / `Pbt.assert`.
45
+ #
46
+ # @param rng [Random]
47
+ # @return [RunSample]
48
+ def generate_for_check(rng = Random.new)
49
+ target_length = (@min_steps == @max_steps) ? @min_steps : rng.rand(@min_steps..@max_steps)
50
+ initial_state, initial_state_sample, initial_eligible = draw_initial_state(rng, target_length)
51
+
52
+ state = initial_state
53
+ steps = []
54
+ step_samples = {}
55
+ next_id = 0
56
+
57
+ target_length.times do |step_index|
58
+ eligible = (step_index == 0) ? initial_eligible : eligible_commands(state)
59
+ break if eligible.empty?
60
+
61
+ selected = select_step(eligible, state, step_index, next_id, rng)
62
+ if selected.nil?
63
+ break unless steps.empty?
64
+
65
+ raise GenerationError,
66
+ "all eligible commands produced empty argument domains at step #{step_index} " \
67
+ "(eligible: #{eligible.map(&:name).inspect})"
68
+ end
61
69
 
62
- command, args = candidates[rng.rand(candidates.length)]
63
- sequence << Step.new(command:, args:)
64
- state = command.next_state(state, args)
70
+ step, sample, next_state = selected
71
+ next_id += 1
72
+ steps << step
73
+ step_samples[step.id] = sample
74
+ state = next_state
65
75
  end
66
76
 
67
- sequence
77
+ run = Run.new(initial_state: initial_state, steps: steps.freeze)
78
+ RunSample.new(
79
+ run,
80
+ initial_state_sample: initial_state_sample,
81
+ step_samples: step_samples,
82
+ session: ShrinkSession.new(max_attempts: @max_shrink_attempts)
83
+ )
68
84
  end
69
85
 
70
- # Shrink a sequence by trying shorter prefixes first.
86
+ # Execute a `Run` against a fresh SUT with full lifecycle, verification, and invariants.
71
87
  #
72
- # @param sequence [Array<Hash, Step>]
73
- # @return [Enumerator<Array<Hash, Step>>]
74
- def shrink(sequence)
75
- Enumerator.new do |y|
76
- seen = {}
77
- state = @model.initial_state
88
+ # @param run [Run]
89
+ # @return [void]
90
+ def run(run)
91
+ sym_state = Resolver.deep_freeze(Resolver.deep_dup(run.initial_state))
92
+ env = {}
93
+ sut_initial = Resolver.resolve(sym_state, env, detach: true)
78
94
 
79
- (sequence.length - 1).downto(0) do |length|
80
- yield_shrink_candidate(y, seen, sequence.first(length))
81
- end
95
+ @spec.with_sut(sut_initial) do |sut|
96
+ inv_initial = Resolver.resolve(sym_state, env)
97
+ check_invariants!(inv_initial, sut, step_index: nil, step: nil)
98
+
99
+ run.steps.each_with_index do |step, index|
100
+ command = @spec.command_by_name(step.command)
101
+ concrete_args = Resolver.resolve(step.args, env, detach: true)
102
+
103
+ result = execute_stage(stage: :run, target: command.name, step_index: index, step: step) do
104
+ command.run!(sut, concrete_args)
105
+ end
106
+
107
+ after_sym = command.next_state(sym_state, step.args, Ref.new(step_id: step.id))
108
+ env[step.id] = result
82
109
 
83
- sequence.each_with_index do |step, index|
84
- command, args = unpack_step(step)
85
- validate_command_protocol!(command, state:, context: "shrink step #{index}")
86
- break unless applicable?(command, state, args, context: "shrink step #{index}")
110
+ before_concrete = Resolver.resolve(sym_state, env)
111
+ after_concrete = Resolver.resolve(after_sym, env)
112
+ verify_args = Resolver.resolve(step.args, env)
87
113
 
88
- arbitrary_for(command, state, context: "shrink step #{index}").shrink(args).each do |shrunk_args|
89
- candidate = replace_step(sequence, index, command:, args: shrunk_args)
90
- next unless valid_sequence?(candidate)
114
+ context = Context.new(
115
+ step: step,
116
+ before: before_concrete,
117
+ after: after_concrete,
118
+ args: verify_args,
119
+ result: result,
120
+ sut: sut
121
+ )
91
122
 
92
- yield_shrink_candidate(y, seen, candidate)
123
+ execute_stage(stage: :verify, target: command.name, step_index: index, step: step) do
124
+ command.verify!(context)
93
125
  end
94
126
 
95
- state = command.next_state(state, args)
127
+ check_invariants!(after_concrete, sut, step_index: index, step: step)
128
+ sym_state = after_sym
96
129
  end
97
130
  end
98
131
  end
99
132
 
100
- # @return [Boolean]
101
- def stateful?
102
- true
103
- end
104
-
105
- # Stateful properties currently require sequential execution because the model,
106
- # commands and SUT factory are ordinary Ruby objects and are not guaranteed to be
107
- # Ractor-shareable.
133
+ # Deterministically replay a `Pbt::Stateful::Run` (e.g. `details.counterexample`).
108
134
  #
109
- # @param _sequence [Array<Hash, Step>]
110
- # @raise [Pbt::InvalidConfiguration]
111
- def run_in_ractor(_sequence)
112
- raise Pbt::InvalidConfiguration, "Pbt.stateful does not support worker: :ractor yet; use worker: :none"
135
+ # @param run [Run]
136
+ # @return [void]
137
+ def replay(run)
138
+ unless run.is_a?(Run)
139
+ raise ArgumentError, "replay expects a Pbt::Stateful::Run, got #{run.class}"
140
+ end
141
+
142
+ validate_replay_run!(run)
143
+ self.run(run)
113
144
  end
114
145
 
115
- # Run the command sequence against a fresh SUT and verify each step.
146
+ # Determine whether a shrink candidate failure matches the original failure signature.
147
+ # A shrink candidate that introduces a teardown error is always accepted so that `shrink_case`
148
+ # immediately halts shrinking and surfaces the cleanup breakage instead of masking it.
116
149
  #
117
- # @param sequence [Array<Hash, Step>]
118
- # @return [void]
119
- def run(sequence)
120
- state = @model.initial_state
121
- sut = @sut_factory.call
122
-
123
- sequence.each_with_index do |step, index|
124
- command, args = unpack_step(step)
125
- validate_command_protocol!(command, state:, context: "run step #{index}")
150
+ # @param initial_err [Exception]
151
+ # @param candidate_err [Exception]
152
+ # @return [Boolean]
153
+ def same_failure?(initial_err, candidate_err)
154
+ return false unless initial_err.is_a?(StepFailure) && candidate_err.is_a?(StepFailure)
155
+ return false if initial_err.teardown_error
156
+ return true if candidate_err.teardown_error
126
157
 
127
- unless applicable?(command, state, args, context: "run step #{index}")
128
- raise "invalid stateful sequence at step #{index}: #{command_name(command)}"
129
- end
158
+ initial_err.signature == candidate_err.signature
159
+ end
130
160
 
131
- before_state = state
132
-
133
- begin
134
- after_state = command.next_state(before_state, args)
135
- result = command.run!(sut, args)
136
- command.verify!(
137
- before_state:,
138
- after_state:,
139
- args:,
140
- result:,
141
- sut:
161
+ # Shrink a failing runner `Case` while preserving provenance and shared shrink session.
162
+ #
163
+ # @param test_case [Pbt::Check::Case]
164
+ # @return [Enumerator<RunSample>]
165
+ def shrink_case(test_case)
166
+ return [].each if test_case.exception.is_a?(StepFailure) && test_case.exception.teardown_error
167
+
168
+ run_sample =
169
+ if test_case.sample.is_a?(RunSample)
170
+ test_case.sample
171
+ else
172
+ RunSample.new(
173
+ test_case.val,
174
+ initial_state_sample: nil,
175
+ step_samples: {},
176
+ session: ShrinkSession.new(max_attempts: @max_shrink_attempts)
142
177
  )
143
- rescue *Check::FATAL_EXCEPTIONS
144
- raise
145
- rescue Exception => e # standard:disable Lint/RescueException:
146
- raise e.class,
147
- "stateful step #{index} (#{command_name(command)}): #{e.message} [args=#{args.inspect}]",
148
- e.backtrace
149
178
  end
150
179
 
151
- state = after_state
152
- end
180
+ @shrinker.shrink(run_sample, failure: test_case.exception)
153
181
  end
154
182
 
155
- private
156
-
157
- # @param step [Hash, Step]
158
- # @return [Array<Object, Object>]
159
- def unpack_step(step)
160
- case step
161
- in Step(command:, args:)
162
- [command, args]
163
- in {command:, args:}
164
- [command, args]
165
- else
166
- raise ArgumentError, "invalid stateful step: #{step.inspect}"
183
+ # Plain-value shrink fallback yielding `Run` objects.
184
+ #
185
+ # @param run [Run]
186
+ # @return [Enumerator<Run>]
187
+ def shrink(run)
188
+ run_sample = RunSample.new(
189
+ run,
190
+ initial_state_sample: nil,
191
+ step_samples: {},
192
+ session: ShrinkSession.new(max_attempts: @max_shrink_attempts)
193
+ )
194
+ Enumerator.new do |y|
195
+ @shrinker.shrink(run_sample, failure: nil).each do |sample|
196
+ y << sample.value
197
+ end
167
198
  end
168
199
  end
169
200
 
170
- # @param command [Object]
171
- # @return [String]
172
- def command_name(command)
173
- command.respond_to?(:name) ? command.name.to_s : (command.class.name || command.class.inspect)
201
+ def run_in_ractor(_run)
202
+ raise Pbt::InvalidConfiguration, "Pbt.stateful does not support worker: :ractor. Use worker: :none instead."
174
203
  end
175
204
 
176
- # @param model [Object]
177
- # @return [void]
178
- def validate_model!(model)
179
- missing_methods = %i[initial_state commands].reject { |method_name| model.respond_to?(method_name) }
180
- return if missing_methods.empty?
205
+ private
181
206
 
182
- raise Pbt::InvalidConfiguration,
183
- "Pbt.stateful model must respond to #{missing_methods.join(", ")}"
207
+ def validate_options!(max_steps:, min_steps:, max_shrink_attempts:)
208
+ unless min_steps.is_a?(Integer) && min_steps >= 0
209
+ raise InvalidSpec, "min_steps must be a non-negative Integer, got #{min_steps.inspect}"
210
+ end
211
+ unless max_steps.is_a?(Integer) && max_steps.positive? && max_steps >= min_steps
212
+ raise InvalidSpec, "max_steps must be a positive Integer >= min_steps (#{min_steps}), got #{max_steps.inspect}"
213
+ end
214
+ unless max_shrink_attempts.is_a?(Integer) && max_shrink_attempts >= 0
215
+ raise InvalidSpec, "max_shrink_attempts must be a non-negative Integer, got #{max_shrink_attempts.inspect}"
216
+ end
184
217
  end
185
218
 
186
- # @param state [Object]
187
- # @param context [String]
188
- # @return [Array<Object>]
189
- def commands_for(state, context:)
190
- commands = @model.commands(state)
219
+ def draw_initial_state(rng, target_length)
220
+ initial_state, initial_state_sample = @spec.sample_initial_state(rng)
221
+ initial_eligible = eligible_commands(initial_state)
191
222
 
192
- unless commands.is_a?(Array)
193
- raise Pbt::InvalidConfiguration,
194
- "Pbt.stateful model.commands(state) must return Array, got #{commands.class} (context=#{context})"
223
+ unless @spec.arbitrary_initial_state?
224
+ if initial_eligible.empty?
225
+ raise InvalidSpec,
226
+ "no command guard is truthy at initial_state #{Resolver.format_value(initial_state)}"
227
+ end
228
+ return [initial_state, initial_state_sample, initial_eligible]
195
229
  end
196
230
 
197
- commands.each { |command| validate_command_protocol!(command, state:, context:) }
198
- commands
199
- end
231
+ return [initial_state, initial_state_sample, initial_eligible] unless initial_eligible.empty?
200
232
 
201
- # @param command [Object]
202
- # @param state [Object]
203
- # @param context [String]
204
- # @return [void]
205
- def validate_command_protocol!(command, state:, context:)
206
- missing_methods = REQUIRED_COMMAND_METHODS.reject { |method_name| command.respond_to?(method_name) }
207
- unless missing_methods.empty?
208
- raise Pbt::InvalidConfiguration,
209
- "Pbt.stateful command protocol mismatch for #{command.class} " \
210
- "(name=#{safe_command_label(command)}, missing: #{missing_methods.join(", ")}, context=#{context})"
233
+ if target_length.zero?
234
+ probe_rng = rng.dup
235
+ PER_STEP_MAX_ATTEMPTS.times do
236
+ probed_state, = @spec.sample_initial_state(probe_rng)
237
+ return [initial_state, initial_state_sample, initial_eligible] unless eligible_commands(probed_state).empty?
238
+ end
239
+ raise GenerationError,
240
+ "no command guard was truthy across #{PER_STEP_MAX_ATTEMPTS} initial_state samples"
211
241
  end
212
- end
213
242
 
214
- # @param command [Object]
215
- # @param arguments [Object]
216
- # @param context [String]
217
- # @return [void]
218
- def validate_arguments_protocol!(command, arguments, context:)
219
- missing_methods = %i[generate shrink].reject { |method_name| arguments.respond_to?(method_name) }
220
- return if missing_methods.empty?
243
+ (PER_STEP_MAX_ATTEMPTS - 1).times do
244
+ initial_state, initial_state_sample = @spec.sample_initial_state(rng)
245
+ initial_eligible = eligible_commands(initial_state)
246
+ return [initial_state, initial_state_sample, initial_eligible] unless initial_eligible.empty?
247
+ end
221
248
 
222
- raise Pbt::InvalidConfiguration,
223
- "Pbt.stateful command arguments protocol mismatch for #{command.class} " \
224
- "(name=#{safe_command_label(command)}, missing: #{missing_methods.join(", ")}, context=#{context})"
249
+ raise GenerationError,
250
+ "no command guard was truthy across #{PER_STEP_MAX_ATTEMPTS} initial_state samples"
225
251
  end
226
252
 
227
- # @param command [Object]
228
- # @return [String]
229
- def safe_command_label(command)
230
- command.respond_to?(:name) ? command.name.inspect : "<unknown>"
253
+ def validate_replay_run!(run)
254
+ available_ids = Set.new
255
+ run.steps.each_with_index do |step, index|
256
+ @spec.command_by_name(step.command)
257
+ ref_ids = Resolver.collect_ref_ids(step.args)
258
+ unbound = ref_ids.reject { |id| available_ids.include?(id) }
259
+ unless unbound.empty?
260
+ refs_str = unbound.map { |id| "v#{id}" }.join(", ")
261
+ raise InvalidSpec,
262
+ "cannot replay run: unbound Ref #{refs_str} in step #{index + 1} #{step.format}"
263
+ end
264
+ available_ids << step.id
265
+ end
231
266
  end
232
267
 
233
- # @param state [Object]
234
- # @param rng [Random]
235
- # @param context [String]
236
- # @return [Array<Array<Object, Object>>]
237
- def generate_candidates_for(state, rng, context:)
238
- commands_for(state, context:).filter_map do |command|
239
- args = generate_applicable_args(command, state, rng, context:)
240
- next if args.equal?(NoApplicableArgs)
241
-
242
- [command, args]
243
- end
268
+ def eligible_commands(state)
269
+ @spec.commands.select { |cmd| cmd.guard?(state) }
244
270
  end
245
271
 
246
- # @param command [Object]
247
- # @param state [Object]
248
- # @param rng [Random]
249
- # @param context [String]
250
- # @return [Object]
251
- def generate_applicable_args(command, state, rng, context:)
252
- arbitrary = arbitrary_for(command, state, context:)
272
+ # Returns `[step, sample, next_state]`, or `nil` if all eligible commands had empty domains.
273
+ # Raises `GenerationError` if eligible commands had non-empty domains but preconditions never passed.
274
+ def select_step(eligible, state, step_index, step_id, rng)
275
+ candidates = eligible.dup
276
+ attempts = 0
277
+
278
+ while attempts < PER_STEP_MAX_ATTEMPTS && !candidates.empty?
279
+ attempts += 1
280
+ command = pick_weighted(candidates, rng)
281
+
282
+ arb =
283
+ begin
284
+ command.arguments_for(state)
285
+ rescue Pbt::Arbitrary::EmptyDomainError
286
+ candidates.delete(command)
287
+ next
288
+ end
253
289
 
254
- ARG_AWARE_GENERATION_ATTEMPTS.times do
255
- args = generate_args_for(command, arbitrary, rng)
256
- return NoApplicableArgs if args.equal?(NoApplicableArgs)
290
+ sample =
291
+ begin
292
+ Pbt::Arbitrary::Sample.capture(arb, rng)
293
+ rescue Pbt::Arbitrary::EmptyDomainError
294
+ candidates.delete(command)
295
+ next
296
+ end
257
297
 
258
- return args if applicable?(command, state, args, context:)
298
+ frozen_args = Resolver.deep_freeze(Resolver.deep_dup(sample.value))
299
+ if command.precondition?(state, frozen_args)
300
+ ref = Ref.new(step_id: step_id)
301
+ next_state = command.next_state(state, frozen_args, ref)
302
+ step = Step.new(id: step_id, command: command.name, args: frozen_args)
303
+ return [step, sample, next_state]
304
+ end
259
305
  end
260
306
 
261
- NoApplicableArgs
262
- rescue Pbt::Arbitrary::EmptyDomainError
263
- NoApplicableArgs
264
- end
307
+ return nil if candidates.empty?
265
308
 
266
- # @param command [Object]
267
- # @param state [Object]
268
- # @param context [String]
269
- # @return [Object]
270
- def arguments_for(command, state, context:)
271
- command.arguments(state)
309
+ raise GenerationError,
310
+ "could not satisfy preconditions at step #{step_index} after #{PER_STEP_MAX_ATTEMPTS} attempts " \
311
+ "(eligible: #{eligible.map(&:name).inspect})"
272
312
  end
273
313
 
274
- # @param command [Object]
275
- # @param state [Object]
276
- # @param context [String]
277
- # @return [Object]
278
- def arbitrary_for(command, state, context:)
279
- arguments = arguments_for(command, state, context:)
280
- validate_arguments_protocol!(command, arguments, context:)
281
- arguments
282
- end
314
+ def pick_weighted(commands, rng)
315
+ return commands.first if commands.size == 1
283
316
 
284
- # @param command [Object]
285
- # @param arbitrary [Object]
286
- # @param rng [Random]
287
- # @return [Object]
288
- def generate_args_for(command, arbitrary, rng)
289
- arbitrary.generate(rng)
290
- rescue Pbt::Arbitrary::EmptyDomainError
291
- NoApplicableArgs
292
- end
293
-
294
- # @param command [Object]
295
- # @param state [Object]
296
- # @param args [Object]
297
- # @param context [String]
298
- # @return [Boolean]
299
- def applicable?(command, state, args, context:)
300
- command.applicable?(state, args)
301
- end
302
-
303
- # @param sequence [Array<Hash, Step>]
304
- # @param index [Integer]
305
- # @param command [Object]
306
- # @param args [Object]
307
- # @return [Array<Hash, Step>]
308
- def replace_step(sequence, index, command:, args:)
309
- candidate = sequence.dup
310
- candidate[index] = rebuild_step(sequence[index], command:, args:)
311
- candidate
312
- end
313
-
314
- # @param step [Hash, Step]
315
- # @param command [Object]
316
- # @param args [Object]
317
- # @return [Hash, Step]
318
- def rebuild_step(step, command:, args:)
319
- case step
320
- in Step
321
- Step.new(command:, args:)
322
- in Hash
323
- {command:, args:}
324
- else
325
- raise ArgumentError, "invalid stateful step: #{step.inspect}"
317
+ total = commands.sum(&:weight)
318
+ ticket = rng.rand(total)
319
+ cumulative = 0
320
+ commands.each do |cmd|
321
+ cumulative += cmd.weight
322
+ return cmd if ticket < cumulative
326
323
  end
324
+ commands.last
327
325
  end
328
326
 
329
- # @param sequence [Array<Hash, Step>]
330
- # @return [Boolean]
331
- def valid_sequence?(sequence)
332
- state = @model.initial_state
333
-
334
- sequence.each do |step|
335
- command, args = unpack_step(step)
336
- return false unless applicable?(command, state, args, context: "validate sequence")
337
-
338
- state = command.next_state(state, args)
327
+ def check_invariants!(state, sut, step_index:, step:)
328
+ @spec.invariants.each_with_index do |inv, idx|
329
+ target = inv.name || :"invariant_#{idx}"
330
+ label = inv.name ? "invariant #{inv.name.inspect}" : "invariant"
331
+ execute_stage(stage: :invariant, target: target, target_label: label, step_index: step_index, step: step) do
332
+ Resolver.invoke_with_arity(inv.block, state, sut)
333
+ end
339
334
  end
340
-
341
- true
342
- rescue
343
- false
344
335
  end
345
336
 
346
- # @param y [Enumerator::Yielder]
347
- # @param seen [Hash{Array<Hash, Step> => true}]
348
- # @param candidate [Array<Hash, Step>]
349
- # @return [void]
350
- def yield_shrink_candidate(y, seen, candidate)
351
- return if seen[candidate]
352
-
353
- seen[candidate] = true
354
- y << candidate
337
+ def execute_stage(stage:, target:, step_index:, step:, target_label: nil)
338
+ yield
339
+ rescue *Check::FATAL_EXCEPTIONS, InvalidSpec, ModelError, PreconditionNotMet
340
+ raise
341
+ rescue Exception => e # standard:disable Lint/RescueException
342
+ step_label = step_index ? "step #{step_index + 1} #{step.format}" : "initial"
343
+ label = target_label || stage.to_s
344
+ failure = StepFailure.new(
345
+ "#{step_label} [#{label}]: #{e.message}",
346
+ step_index: step_index,
347
+ step: step,
348
+ stage: stage,
349
+ target: target,
350
+ exception_class: e.class
351
+ )
352
+ failure.set_backtrace(e.backtrace) if e.backtrace
353
+ raise failure
355
354
  end
356
-
357
- NoApplicableArgs = Object.new
358
- private_constant :NoApplicableArgs
359
355
  end
360
356
  end
361
357
  end