nukadoko 0.5.0 → 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 (203) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +553 -3
  3. data/README.ja.md +22 -6
  4. data/README.md +13 -7
  5. data/docs/ci.ja.md +542 -0
  6. data/docs/ci.md +635 -0
  7. data/docs/commands.ja.md +1524 -0
  8. data/docs/commands.md +1281 -0
  9. data/docs/engines.json +19 -0
  10. data/docs/execution-events.ja.md +336 -0
  11. data/docs/execution-events.md +423 -0
  12. data/docs/glossary.json +1348 -0
  13. data/docs/guide.ja.md +733 -0
  14. data/docs/guide.md +815 -0
  15. data/docs/intent.ja.md +133 -0
  16. data/docs/intent.md +156 -0
  17. data/docs/layout.ja.md +442 -0
  18. data/docs/layout.md +399 -0
  19. data/docs/releasing.ja.md +186 -0
  20. data/docs/releasing.md +237 -0
  21. data/docs/ruby/reference.ja.md +302 -0
  22. data/docs/ruby/reference.md +368 -0
  23. data/docs/spec.ja.md +8916 -0
  24. data/docs/spec.md +11250 -0
  25. data/docs/tutorial.ja.md +451 -0
  26. data/docs/tutorial.md +458 -0
  27. data/docs/typescript/adopting.ja.md +231 -0
  28. data/docs/typescript/adopting.md +251 -0
  29. data/docs/typescript/doors.ja.md +301 -0
  30. data/docs/typescript/doors.md +329 -0
  31. data/docs/typescript/migration-playwright-test.ja.md +189 -0
  32. data/docs/typescript/migration-playwright-test.md +212 -0
  33. data/docs/typescript/migration.ja.md +391 -0
  34. data/docs/typescript/migration.md +505 -0
  35. data/docs/typescript/reference.ja.md +176 -0
  36. data/docs/typescript/reference.md +197 -0
  37. data/docs/typescript/upgrading.ja.md +1774 -0
  38. data/docs/typescript/upgrading.md +2224 -0
  39. data/docs/web-app.ja.md +627 -0
  40. data/docs/web-app.md +659 -0
  41. data/docs/writing-scenarios.ja.md +544 -0
  42. data/docs/writing-scenarios.md +563 -0
  43. data/lib/nukadoko/base.rb +12 -5
  44. data/lib/nukadoko/changed_files.rb +95 -0
  45. data/lib/nukadoko/check.rb +459 -51
  46. data/lib/nukadoko/claim_drops.rb +271 -0
  47. data/lib/nukadoko/claim_line.rb +24 -4
  48. data/lib/nukadoko/claim_rules.rb +143 -1
  49. data/lib/nukadoko/claim_sides.rb +157 -9
  50. data/lib/nukadoko/cli.rb +403 -50
  51. data/lib/nukadoko/codes.json +246 -21
  52. data/lib/nukadoko/codes.rb +21 -4
  53. data/lib/nukadoko/command.rb +120 -13
  54. data/lib/nukadoko/command_help.rb +128 -0
  55. data/lib/nukadoko/compat.rb +469 -0
  56. data/lib/nukadoko/criteria.rb +1 -1
  57. data/lib/nukadoko/definition.rb +157 -7
  58. data/lib/nukadoko/error_frames.rb +6 -3
  59. data/lib/nukadoko/events/count.rb +146 -0
  60. data/lib/nukadoko/events/counted.rb +114 -0
  61. data/lib/nukadoko/events/format.rb +257 -0
  62. data/lib/nukadoko/events/lists.rb +154 -0
  63. data/lib/nukadoko/events/path.rb +285 -0
  64. data/lib/nukadoko/events/read.rb +284 -0
  65. data/lib/nukadoko/events/reader.rb +490 -0
  66. data/lib/nukadoko/events/selection.rb +252 -0
  67. data/lib/nukadoko/events/tally.rb +438 -0
  68. data/lib/nukadoko/features.rb +2 -2
  69. data/lib/nukadoko/fixtures.rb +160 -35
  70. data/lib/nukadoko/http_recording.rb +6 -2
  71. data/lib/nukadoko/intent.rb +364 -0
  72. data/lib/nukadoko/intent_file.rb +81 -0
  73. data/lib/nukadoko/intent_view.rb +190 -0
  74. data/lib/nukadoko/js_json.rb +2 -1
  75. data/lib/nukadoko/judge.rb +2 -2
  76. data/lib/nukadoko/judgment_file.rb +22 -1
  77. data/lib/nukadoko/judgments.rb +33 -3
  78. data/lib/nukadoko/measure_mcp.rb +202 -0
  79. data/lib/nukadoko/parts.rb +8 -2
  80. data/lib/nukadoko/path_view.rb +252 -0
  81. data/lib/nukadoko/place_rows.rb +161 -0
  82. data/lib/nukadoko/program.rb +1265 -51
  83. data/lib/nukadoko/program_log.rb +122 -0
  84. data/lib/nukadoko/program_shape.rb +385 -0
  85. data/lib/nukadoko/project_fixtures.rb +43 -10
  86. data/lib/nukadoko/project_program.rb +5 -4
  87. data/lib/nukadoko/repeated_prefix.rb +310 -0
  88. data/lib/nukadoko/report_run.rb +3 -3
  89. data/lib/nukadoko/run.rb +1156 -159
  90. data/lib/nukadoko/run_record.rb +70 -46
  91. data/lib/nukadoko/scaffold.rb +24 -6
  92. data/lib/nukadoko/schema.rb +15 -2
  93. data/lib/nukadoko/secrets.rb +55 -5
  94. data/lib/nukadoko/states.rb +478 -0
  95. data/lib/nukadoko/step.rb +71 -2
  96. data/lib/nukadoko/step_fixtures.rb +52 -3
  97. data/lib/nukadoko/summary.rb +803 -30
  98. data/lib/nukadoko/table.rb +4 -2
  99. data/lib/nukadoko/text_fit.rb +20 -7
  100. data/lib/nukadoko/trace.rb +152 -45
  101. data/lib/nukadoko/tree_passes.rb +95 -33
  102. data/lib/nukadoko/underneath.rb +311 -0
  103. data/lib/nukadoko/verdict.rb +18 -1
  104. data/lib/nukadoko/version.rb +1 -1
  105. data/lib/nukadoko/vocabulary.rb +166 -23
  106. data/lib/nukadoko/walk.rb +2 -2
  107. data/lib/nukadoko/walk_step.rb +1 -1
  108. data/lib/nukadoko.rb +23 -0
  109. data/llms.txt +42 -35
  110. data/sig/generated/nukadoko/base.rbs +4 -4
  111. data/sig/generated/nukadoko/changed_files.rbs +42 -0
  112. data/sig/generated/nukadoko/check.rbs +147 -10
  113. data/sig/generated/nukadoko/claim_drops.rbs +135 -0
  114. data/sig/generated/nukadoko/claim_line.rbs +11 -0
  115. data/sig/generated/nukadoko/claim_rules.rbs +49 -4
  116. data/sig/generated/nukadoko/claim_sides.rbs +63 -12
  117. data/sig/generated/nukadoko/cli.rbs +80 -7
  118. data/sig/generated/nukadoko/codes.rbs +19 -4
  119. data/sig/generated/nukadoko/command.rbs +68 -1
  120. data/sig/generated/nukadoko/command_help.rbs +18 -0
  121. data/sig/generated/nukadoko/compat.rbs +392 -0
  122. data/sig/generated/nukadoko/criteria.rbs +1 -1
  123. data/sig/generated/nukadoko/definition.rbs +50 -0
  124. data/sig/generated/nukadoko/error_frames.rbs +1 -1
  125. data/sig/generated/nukadoko/events/count.rbs +69 -0
  126. data/sig/generated/nukadoko/events/counted.rbs +70 -0
  127. data/sig/generated/nukadoko/events/format.rbs +163 -0
  128. data/sig/generated/nukadoko/events/lists.rbs +47 -0
  129. data/sig/generated/nukadoko/events/path.rbs +161 -0
  130. data/sig/generated/nukadoko/events/read.rbs +109 -0
  131. data/sig/generated/nukadoko/events/reader.rbs +226 -0
  132. data/sig/generated/nukadoko/events/selection.rbs +100 -0
  133. data/sig/generated/nukadoko/events/tally.rbs +227 -0
  134. data/sig/generated/nukadoko/features.rbs +6 -4
  135. data/sig/generated/nukadoko/fixtures.rbs +67 -11
  136. data/sig/generated/nukadoko/http_recording.rbs +1 -1
  137. data/sig/generated/nukadoko/intent.rbs +132 -0
  138. data/sig/generated/nukadoko/intent_file.rbs +46 -0
  139. data/sig/generated/nukadoko/intent_view.rbs +69 -0
  140. data/sig/generated/nukadoko/judgment_file.rbs +7 -1
  141. data/sig/generated/nukadoko/judgments.rbs +10 -0
  142. data/sig/generated/nukadoko/measure_mcp.rbs +104 -0
  143. data/sig/generated/nukadoko/parts.rbs +2 -2
  144. data/sig/generated/nukadoko/path_view.rbs +105 -0
  145. data/sig/generated/nukadoko/place_rows.rbs +43 -0
  146. data/sig/generated/nukadoko/program.rbs +627 -23
  147. data/sig/generated/nukadoko/program_log.rbs +67 -0
  148. data/sig/generated/nukadoko/program_shape.rbs +147 -0
  149. data/sig/generated/nukadoko/project_fixtures.rbs +16 -3
  150. data/sig/generated/nukadoko/project_program.rbs +2 -1
  151. data/sig/generated/nukadoko/repeated_prefix.rbs +172 -0
  152. data/sig/generated/nukadoko/report_run.rbs +1 -1
  153. data/sig/generated/nukadoko/run.rbs +286 -41
  154. data/sig/generated/nukadoko/run_record.rbs +20 -12
  155. data/sig/generated/nukadoko/scaffold.rbs +3 -0
  156. data/sig/generated/nukadoko/schema.rbs +5 -0
  157. data/sig/generated/nukadoko/secrets.rbs +26 -3
  158. data/sig/generated/nukadoko/states.rbs +214 -0
  159. data/sig/generated/nukadoko/step.rbs +58 -1
  160. data/sig/generated/nukadoko/step_fixtures.rbs +39 -0
  161. data/sig/generated/nukadoko/summary.rbs +256 -2
  162. data/sig/generated/nukadoko/table.rbs +3 -2
  163. data/sig/generated/nukadoko/trace.rbs +108 -33
  164. data/sig/generated/nukadoko/tree_passes.rbs +36 -8
  165. data/sig/generated/nukadoko/underneath.rbs +141 -0
  166. data/sig/generated/nukadoko/verdict.rbs +8 -0
  167. data/sig/generated/nukadoko/vocabulary.rbs +45 -6
  168. data/skills/nukadoko/SKILL.md +18 -18
  169. data/skills/nukadoko/references/checking-steps.md +16 -83
  170. data/skills/nukadoko/references/diagnosing.md +6 -6
  171. data/skills/nukadoko/references/done.md +22 -0
  172. data/skills/nukadoko/references/evidence.md +19 -6
  173. data/skills/nukadoko/references/exploring.md +22 -7
  174. data/skills/nukadoko/references/fixtures.md +12 -32
  175. data/skills/nukadoko/references/judgments.md +18 -0
  176. data/skills/nukadoko/references/maintenance.md +1 -1
  177. data/skills/nukadoko/references/merge-risk.md +1 -1
  178. data/skills/nukadoko/references/parts.md +46 -23
  179. data/skills/nukadoko/references/{perturbed-worlds.md → perturbed-systems.md} +30 -12
  180. data/skills/nukadoko/references/program.md +168 -169
  181. data/skills/nukadoko/references/reviewing.md +20 -20
  182. data/skills/nukadoko/references/ruby/checking-steps.md +106 -0
  183. data/skills/nukadoko/references/ruby/examples/cli.md +212 -0
  184. data/skills/nukadoko/references/ruby/fixtures.md +34 -0
  185. data/skills/nukadoko/references/ruby/program.md +112 -0
  186. data/skills/nukadoko/references/ruby/step-files.md +115 -0
  187. data/skills/nukadoko/references/running.md +34 -34
  188. data/skills/nukadoko/references/scenario-rules.md +7 -6
  189. data/skills/nukadoko/references/states.md +7 -1
  190. data/skills/nukadoko/references/step-files.md +41 -120
  191. data/skills/nukadoko/references/typescript/checking-steps.md +129 -0
  192. data/skills/nukadoko/references/{examples → typescript/examples}/cli.md +1 -1
  193. data/skills/nukadoko/references/{examples → typescript/examples}/http-api.md +6 -6
  194. data/skills/nukadoko/references/{examples → typescript/examples}/workflow.md +1 -1
  195. data/skills/nukadoko/references/typescript/fixtures.md +56 -0
  196. data/skills/nukadoko/references/typescript/program.md +111 -0
  197. data/skills/nukadoko/references/typescript/step-files.md +104 -0
  198. data/skills/nukadoko/references/verdicts.md +10 -10
  199. data/skills/nukadoko/references/writing-steps.md +67 -67
  200. data/skills/nukadoko/references/writing-the-feature.md +1 -1
  201. data/skills/nukadoko-verification/SKILL.md +13 -8
  202. metadata +103 -7
  203. data/skills/nukadoko/references/ruby.md +0 -80
data/lib/nukadoko/cli.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
  # rbs_inline: enabled
3
3
 
4
+ require_relative "command_help"
5
+
4
6
  # The `nuka` command. With --json it prints one envelope in the shape the
5
7
  # TypeScript engine prints, so an agent reads both engines the same way.
6
8
  module Nukadoko
7
9
  class CLI
8
- FLAGS = %w[--json --no-check --dry-run --then --replace --allure --records --meets --misses --step].freeze
9
- REPEATED = %w[--tag --env --use].freeze
10
+ FLAGS = %w[--json --no-check --dry-run --then --replace --allure --records --meets --misses --step --trailer --codes].freeze
11
+ REPEATED = %w[--tag --env --use --fail-on].freeze
10
12
  VALUED = %w[--format --out --base --repeat --concurrency --order --proof-runs --args --pattern --description --feature --command --request --from --cwd --body --argv
11
13
  --run --by --because --walk --timeout].freeze
12
14
 
@@ -44,7 +46,8 @@ module Nukadoko
44
46
  alone
45
47
  nuka verdict say whether the newest run judged the working tree as
46
48
  it is now and left nothing red; exits 1 when it did
47
- not, and never starts a run
49
+ not, and never starts a run; --trailer prints one git
50
+ trailer line to stamp a commit, and exits 0
48
51
  nuka judge <target> show the values a run read for a criterion whose Then
49
52
  asks for a judgment (<feature file>:<scenario line>),
50
53
  or record a judgment of them with --meets or --misses
@@ -89,6 +92,10 @@ module Nukadoko
89
92
  message = @command.nil? ? "Not enough non-option arguments: got 0, need at least 1" : "Unknown argument: #{@command}"
90
93
  return refuse(Engine::Refusal.new("invalid-arguments", message, run: "nuka --help"))
91
94
  end
95
+ if @words.include?("--help") && CommandHelp::COMMANDS.key?(@command)
96
+ @out.write(CommandHelp.text(@command))
97
+ return 0
98
+ end
92
99
  return refuse(Engine::Refusal.new("invalid-arguments", "Unknown argument: #{@unknown.first}")) if @unknown.any?
93
100
  return verdict if @command == "verdict"
94
101
  return Judge.new(@project, @rest, @options, json: @json, out: @out).call if @command == "judge"
@@ -97,6 +104,10 @@ module Nukadoko
97
104
  return Walked.new(@project, @rest, @options, json: @json, out: @out).call if @command == "step"
98
105
  return skill if @command == "skill"
99
106
  return init if @command == "init"
107
+ return codes if @command == "check" && @options.key?("--codes")
108
+ if @command == "check" && (stop = fail_on_refusal)
109
+ return refuse(stop)
110
+ end
100
111
 
101
112
  begin
102
113
  Nukadoko.load_project(@project)
@@ -105,16 +116,27 @@ module Nukadoko
105
116
  end
106
117
  steps = Nukadoko.load_steps(File.join(@project, "features", "steps"))
107
118
  problems = []
108
- scenarios = Dir.chdir(@project) { Engine.load_features("features", problems: problems) }
119
+ # nuka check with targets reads those feature files alone, and
120
+ # computes no notes: a note is a fact about the whole project.
121
+ targets = @command == "check" ? @rest : []
122
+ begin
123
+ paths = targets.empty? ? "features" : check_targets(targets)
124
+ rescue Engine::Refusal => e
125
+ return refuse(e)
126
+ end
127
+ scenarios = Dir.chdir(@project) { Engine.load_features(paths, problems: problems) }
109
128
  declared = (Nukadoko.program_options[:environments] || {}).values.flat_map { (_1[:conditions] || {}).keys.map(&:to_s) }
110
129
  # condition-unmet is judged for the selection a run without --env
111
130
  # starts; a selection --env names is a choice of where to run.
112
131
  selected = (Engine::Program.new(Nukadoko.program_options).environments if Array(@options["--env"]).empty?) rescue nil
113
- check = Engine::Check.new(steps, scenarios, features: Dir.exist?(File.join(@project, "features")), full: @command == "check",
114
- conditions: declared, environments: selected, problems: problems, config: env_findings)
132
+ check = Engine::Check.new(steps, scenarios, features: Dir.exist?(File.join(@project, "features")), full: @command == "check" && targets.empty?,
133
+ conditions: declared, environments: selected, problems: problems, config: env_findings, systems: started_systems)
115
134
  findings = check.findings.map { relative(_1).to_h }
135
+ # steps and describe print what they could read and name the step
136
+ # files they could not, never the rest of what check reports.
137
+ findings = findings.select { DEFINITION_CODES.include?(_1[:code]) } if %w[steps describe].include?(@command)
116
138
  case @command
117
- when "check" then finish("check", check.passed?, findings: findings)
139
+ when "check" then checked(check, findings)
118
140
  when "sig"
119
141
  path = File.join(@project, "sig", "nukadoko", "steps.rbs")
120
142
  FileUtils.mkdir_p(File.dirname(path))
@@ -122,11 +144,25 @@ module Nukadoko
122
144
  @out.puts "wrote #{path.delete_prefix("#{@project}/")}"
123
145
  0
124
146
  when "steps"
125
- entries = check.usable.sort_by(&:file)
126
- return finish("steps", true, findings: findings, steps: entries.map { Vocabulary.entry(_1, @project) }) if @json
147
+ entries = check.usable.each_with_index.sort_by { |step, i| [step.file, i] }.map(&:first)
148
+ places = entries.map { [_1, {}] } #: Array[[singleton(Step), Hash[Symbol, untyped]]]
149
+ unbound = [] #: Array[Hash[Symbol, untyped]]
150
+ unless @rest.empty?
151
+ begin
152
+ selection = Engine::Selection.new(@project, @rest, [])
153
+ rescue Engine::Refusal => e
154
+ return refuse(e)
155
+ end
156
+ places, unbound = Vocabulary.of_targets(entries, selection.scenarios)
157
+ end
158
+ if @json
159
+ return finish("steps", unbound.empty?, findings: findings + unbound,
160
+ steps: places.map { |step, place| Vocabulary.entry(step, @project, entries).merge(place) })
161
+ end
127
162
 
128
- @out.puts entries.map { Vocabulary.text(_1) }.join("\n\n")
129
- 0
163
+ @out.puts places.map { |step, place| Vocabulary.text(step, entries, place) }.join("\n\n")
164
+ emit({}, unbound, "steps", io: $stderr)
165
+ unbound.empty? ? 0 : 1
130
166
  when "describe"
131
167
  step = check.usable.find { _1.step_name == @rest.first }
132
168
  @json = true
@@ -135,7 +171,7 @@ module Nukadoko
135
171
  return finish("describe", false, findings: findings + [refusal], verdict: "refused")
136
172
  end
137
173
 
138
- finish("describe", true, findings: findings, **Vocabulary.describe(step, @project))
174
+ finish("describe", true, findings: findings, **Vocabulary.describe(step, @project, check.usable.sort_by(&:file)))
139
175
  when "run" then @options.key?("--step") ? walk(steps) : run(steps)
140
176
  when "do" then run_one(check)
141
177
  when "scaffold" then scaffold(steps, scenarios)
@@ -225,8 +261,8 @@ module Nukadoko
225
261
  raise Engine::Refusal.new("step-needs-one-scenario", "nuka run --step walks one execution of one scenario, and #{reason}", run: again)
226
262
  end
227
263
 
228
- # A run checks the scenarios it selected, then runs them in each world
229
- # it selected; a target or a world it cannot reach is refused first.
264
+ # A run checks the scenarios it selected, then runs them in each system
265
+ # it selected; a target or a system it cannot reach is refused first.
230
266
  #: (Array[singleton(Step)]) -> Integer
231
267
  def run(steps)
232
268
  repeat = count_flag("--repeat")
@@ -244,7 +280,7 @@ module Nukadoko
244
280
  progress = @json ? File.open(File::NULL, "w") : $stderr
245
281
  ref = @options["--base"].last
246
282
  if @walk && (repeat > 1 || ref)
247
- why = repeat > 1 ? "--repeat #{repeat} runs it #{repeat} times" : "--base runs it in more than one world"
283
+ why = repeat > 1 ? "--repeat #{repeat} runs it #{repeat} times" : "--base runs it in more than one system"
248
284
  raise Engine::Refusal.new("step-needs-one-scenario", "nuka run --step walks one execution of one scenario, and #{why}",
249
285
  run: Engine::Walk.restart_command(@walk_target))
250
286
  end
@@ -261,7 +297,7 @@ module Nukadoko
261
297
  default = @options["--env"].empty?
262
298
  declared = program.declared.values.flat_map { (_1[:conditions] || {}).keys.map(&:to_s) }
263
299
  check = Engine::Check.new(steps, default ? selection.scenarios : kept, conditions: declared,
264
- environments: (environments if default), problems: selection.problems, config: env_findings)
300
+ environments: (environments if default), problems: selection.problems, config: env_findings, systems: started_systems)
265
301
  # The TypeScript engine refuses a declaration as its step file loads, so
266
302
  # such a step file stops its run even under --no-check, naming the
267
303
  # first one.
@@ -287,6 +323,10 @@ module Nukadoko
287
323
  fix: "Correct each error the findings name, or pass --no-check to run anyway.", do: check_command }
288
324
  return finish("run", false, findings: errors + [stop], verdict: "refused", exit_code: 2)
289
325
  end
326
+ # A missing checkout stops the run under --no-check too, before any
327
+ # system builds, as the TypeScript engine stops it.
328
+ rootless = Engine::Program.service_root_refusal(environments.map { [_1.name, program.recipe(_1)] }, @project, Engine::CONFIG)
329
+ return refuse(rootless) if rootless
290
330
  if kept.empty? && left_out.any?
291
331
  return finish("run", false, findings: [left_out_stop(left_out, program)], verdict: "failed", exit_code: 1)
292
332
  end
@@ -296,7 +336,8 @@ module Nukadoko
296
336
  base = if commit
297
337
  targets = @rest.empty? ? nil : @rest.map { _1.sub(/:\d+\z/, "") }
298
338
  Engine::Base.new(project: @project, ref: ref, commit: commit, run_dir: dir, selected: kept.map { Engine::Criteria.key(_1.uri, _1.line) },
299
- targets: targets, envs: @options["--env"], head_program: Nukadoko.program_options)
339
+ targets: targets, envs: @options["--env"], head_program: Nukadoko.program_options,
340
+ paths_wanted: environments.any? { program.writes_events?(_1) })
300
341
  end
301
342
  # Workers are handed whole feature files, so one file has nothing to
302
343
  # spread across them.
@@ -310,11 +351,12 @@ module Nukadoko
310
351
  repeat: repeat, concurrency: concurrency, order: order, records: records, walk: @walk,
311
352
  rerun: { targets: @rest, tags: @options["--tag"].flat_map { ["--tag", _1] }, whole_files: selection.whole_files }).call
312
353
  passed = result.delete(:passed)
313
- findings += result.delete(:failures)
314
- started = result.slice(:run_id, :run_dir, :started_at, :finished_at, :commit, :environments, :worlds)
354
+ teardown = result.delete(:teardown_warnings)
355
+ findings += (@json ? teardown : []) + result.delete(:failures)
356
+ started = result.slice(:run_id, :run_dir, :started_at, :finished_at, :commit, :environments, :systems)
315
357
  rest = result.except(*started.keys)
316
- envelope = { **head("run", passed ? "passed" : "failed", findings, check: @no_check ? "skipped" : "ran"),
317
- **started, selection: selection.selection, tag_selection: selection.tag_selection, **rest } #: Hash[Symbol, untyped]
358
+ envelope = { **head("run", passed ? "passed" : "failed", findings), **started, selection: selection.selection, tag_selection: selection.tag_selection,
359
+ **rest.slice(:order), check: @no_check ? "skipped" : "ran", **rest.except(:order) } #: Hash[Symbol, untyped]
318
360
  envelope.delete(:tag_selection) unless envelope[:tag_selection]
319
361
  # The limits restate the fields above, so they are read from them,
320
362
  # and a run with none has no field.
@@ -362,28 +404,37 @@ module Nukadoko
362
404
  end
363
405
 
364
406
  # One pass of a `--base` tree, in the tree's own directory: the plan
365
- # names the world, the environment, and the rows at their place in this
407
+ # names the system, the environment, and the rows at their place in this
366
408
  # tree, and the result goes to the file it names.
367
409
  #: (Array[singleton(Step)]) -> Integer
368
410
  def tree(steps)
369
411
  plan = JSON.parse(File.read(@rest.first), symbolize_names: true)
370
- found = Dir.chdir(@project) { Engine.load_features(plan[:targets].map { _1[:feature] }.uniq) }
371
- scenarios = plan[:targets].filter_map { |target| found.find { _1.uri == target[:feature] && _1.line == target[:line] } }
372
412
  result = begin
373
- environment = Engine::Program.new(Nukadoko.program_options).environments([plan[:environment]]).first
374
- usable = Engine::Check.new(steps, scenarios).usable
375
- run = Engine::Run.new(@project, usable, scenarios, Nukadoko.program_options, environments: [environment],
376
- world: plan[:world], dir: plan[:dir], out: @out, head: plan[:head])
377
- { results: run.tree_pass, worlds: run.world_entries }
413
+ program = Engine::Program.new(Nukadoko.program_options)
414
+ environment = program.environments([plan[:environment]]).first
415
+ # Paths are compared only where both sides write execution events:
416
+ # a base that names no {events} would run a pass for nothing to
417
+ # compare.
418
+ targets = plan[:targets] + (program.writes_events?(environment) ? plan[:for_paths] || [] : [])
419
+ found = Dir.chdir(@project) { Engine.load_features(targets.map { _1[:feature] }.uniq) }
420
+ scenarios = targets.filter_map { |target| found.find { _1.uri == target[:feature] && _1.line == target[:line] } }
421
+ if scenarios.empty?
422
+ { results: [], systems: [] }
423
+ else
424
+ usable = Engine::Check.new(steps, scenarios).usable
425
+ run = Engine::Run.new(@project, usable, scenarios, Nukadoko.program_options, environments: [environment],
426
+ system: plan[:system], dir: plan[:dir], out: @out, head: plan[:head])
427
+ { results: run.tree_pass, systems: run.system_entries, builds: run.builds }
428
+ end
378
429
  rescue Engine::Refusal => e
379
- { not_run: "the #{plan[:world]} tree's program does not give this environment: #{e.message}" }
430
+ { not_run: "the #{plan[:system]} tree's program does not give this environment: #{e.message}" }
380
431
  end
381
432
  File.write(plan[:result], JSON.generate(result))
382
433
  0
383
434
  end
384
435
 
385
436
  # `nuka do <step>`: one step alone, its args from --args and from the
386
- # records --use names, in a world started for it. The step record goes
437
+ # records --use names, in a system started for it. The step record goes
387
438
  # to stdout and to .nukadoko/do/<id>/record.json, where a later
388
439
  # `nuka do --use` reads it. A refusal goes to stderr, since stdout is
389
440
  # the record's.
@@ -430,6 +481,10 @@ module Nukadoko
430
481
  name = @rest.first
431
482
  step = check.usable.find { _1.step_name == name }
432
483
  return refuse(Engine::Refusal.new("unknown-step", "Unknown step: #{name}", fix: "Name a step that nuka steps lists.", run: "nuka steps")) unless step
484
+ if Vocabulary.compat?(step)
485
+ return refuse(Engine::Refusal.new("compat-step-not-runnable", %(Step "#{name}" is a compat step and has no typed contract, so nuka do cannot run it alone),
486
+ fix: "Promote the step to a Nukadoko::Step to run it with nuka do.", run: "nuka do --help"))
487
+ end
433
488
 
434
489
  describe = "nuka describe #{name}"
435
490
  args_fix = "Pass --args as a JSON object with the keys that nuka describe <step> lists."
@@ -442,30 +497,56 @@ module Nukadoko
442
497
  end
443
498
  return refuse(Engine::Refusal.new("step-args-invalid", "--args must be a JSON object", fix: args_fix, run: describe)) unless given.is_a?(Hash)
444
499
  end
445
- args, origins, used = used_args(step, @options["--use"])
500
+ args, origins, used, files, events, drops = used_args(step, @options["--use"])
446
501
  if @options["--args"].empty? && (step.args.required_names - args.keys).any?
447
502
  return refuse(Engine::Refusal.new("step-args-invalid", "--args is required unless --use supplies this step's arguments", fix: args_fix, run: describe))
448
503
  end
449
504
 
450
505
  args = given.merge(args.reject { |key, _| given.key?(key) })
451
506
  given.each_key { origins[_1] = "command_line:#{_1}" }
452
- environment = Engine::Program.new(Nukadoko.program_options).environments(@options["--env"].last(1)).first
507
+ program = Engine::Program.new(Nukadoko.program_options)
508
+ # Without --env, nuka do runs in the environment named default,
509
+ # whatever a run would start.
510
+ environment = program.environments(@options["--env"].last(1).then { _1.empty? ? ["default"] : _1 }).first
511
+ if step.mutating? && program.policy(environment.name) == "read-only"
512
+ return refuse(Engine::Refusal.new("read-only-environment", %(Step "#{step.step_name}" mutates state but environment "#{environment.name}" has policy "read-only")))
513
+ end
514
+ rootless = program.file? ? Engine::Program.service_root_refusal([[environment.name, program.recipe(environment)]], @project, Engine::CONFIG) : nil
515
+ return refuse(rootless) if rootless
453
516
  id = "step-#{Time.now.utc.strftime("%Y%m%d-%H%M%S")}-#{SecureRandom.alphanumeric(4).downcase}"
454
517
  dir = File.join(@project, ".nukadoko", "do", id)
455
518
  run = Engine::Run.new(@project, check.usable, [], Nukadoko.program_options, environments: [environment], dir: dir, out: $stderr)
456
519
  texts = given.to_h { |key, value| [key, JSON.generate(value)] }
457
- record = run.do_step(step, args, origins, texts, environment)
520
+ record = run.do_step(step, args, origins, texts, environment, files, events, drops)
521
+ # A system of more than one service names the service whose log the
522
+ # lines are in.
523
+ named = (record.dig(:program_log, :services) || {}).size > 1
524
+ (record[:program_log_late] || {}).each do |service, late|
525
+ where = late[:file] ? "see #{File.join(".nukadoko", "do", id, late[:file])}" : "they could not be written beside the record"
526
+ whose = named ? "service #{service}'s log" : "the program's log"
527
+ message = "system #{environment.name}: #{late[:lines]} line(s) of #{whose} carry the step's trace id and arrived after the step ended; #{where}"
528
+ $stderr.puts ["note", "program-log-late", "-", message].join("\t"), " fix: #{Engine::Codes.fix("program-log-late")}"
529
+ end
530
+ (record[:events_late] || {}).each do |service, late|
531
+ where = late[:file] ? "see #{File.join(".nukadoko", "do", id, late[:file])}" : "they could not be written beside the record"
532
+ whose = named ? "service #{service}'s execution events" : "the program's execution events"
533
+ message = "system #{environment.name}: #{late[:lines]} line(s) of #{whose} carry the step's trace id and arrived after the step ended; #{where}"
534
+ $stderr.puts ["note", "events-late", "-", message].join("\t"), " fix: #{Engine::Codes.fix("events-late")}"
535
+ end
458
536
  written = run.redact(do_record(record, id, step, given, origins, used, environment))
459
537
  File.write(File.join(dir, "record.json"), JSON.pretty_generate(written))
460
538
  $stdout.puts JSON.pretty_generate(written)
461
539
  record[:status] == "passed" ? 0 : 1
462
540
  rescue Engine::Refusal => e
463
541
  refuse(e)
542
+ rescue Engine::Program::StartFailed => e
543
+ finish("do", false, findings: [{ severity: "failed", code: "program-start-failed", message: e.message, fix: Engine::Codes.fix("program-start-failed") }])
464
544
  end
465
545
 
466
546
  # Each from key of the step, from the newest record --use names whose
467
- # step gives it, and where it came from.
468
- #: (singleton(Step), Array[String]) -> [Hash[untyped, untyped], Hash[untyped, String], Array[Hash[Symbol, untyped]]]
547
+ # step gives it, and where it came from; and for each files key, the
548
+ # record of the one --use value whose step its `of` names.
549
+ #: (singleton(Step), Array[String]) -> [Hash[untyped, untyped], Hash[untyped, String], Array[Hash[Symbol, untyped]], ^(String, untyped) -> Hash[Symbol, untyped], ^(String, untyped) -> Hash[Symbol, untyped], Hash[String, Hash[Symbol, untyped]]]
469
550
  def used_args(step, ids)
470
551
  records = ids.map do |id|
471
552
  path = [File.join(@project, ".nukadoko", "do", id, "record.json"), *Dir.glob(File.join(@project, ".nukadoko", "runs", "*", "steps", id, "record.json"))].find { File.exist?(_1) }
@@ -473,11 +554,13 @@ module Nukadoko
473
554
  raise Engine::Refusal.new("step-inputs-invalid", "--use #{id}: no such step record. A step record is removed with its run once the run is older than the newest 20 runs, " \
474
555
  "and a nuka do record once it is older than 7 days (retention in nukadoko.config.rb)")
475
556
  end
557
+ (@use_dirs ||= {})[id] = File.dirname(path)
476
558
  [id, JSON.parse(File.read(path))]
477
559
  end
478
560
  args = {}
479
561
  origins = {}
480
562
  used = []
563
+ drops = {}
481
564
  step.from.each do |key, sources|
482
565
  sources.each do |cls, source_key|
483
566
  id, found = records.reverse.find { |_, record| record["step"] == cls.step_name && record["result"].is_a?(Hash) && record["result"].key?(source_key.to_s) }
@@ -485,11 +568,119 @@ module Nukadoko
485
568
 
486
569
  args[key] = found["result"][source_key.to_s]
487
570
  origins[key] = "from:#{cls.step_name}@#{id}.#{source_key}"
571
+ # A record the --use value came from says which keys its returns dropped; one written before it did says nothing, and no claim is held back.
572
+ drops[key.to_s] = { step: cls.step_name, key: source_key, dropped: found["returns_dropped"]&.map { { at: _1["at"] || [], keys: _1["keys"] || [] } } }
488
573
  used << { step_record_id: id, step: cls.step_name, result: found["result"] } unless used.any? { _1[:step_record_id] == id }
489
574
  break
490
575
  end
491
576
  end
492
- [args, origins, used]
577
+ [args, origins, used, used_files(step, records), used_events(step, records), drops]
578
+ end
579
+
580
+ # A list is the files of one execution, so two --use records for one
581
+ # files key are refused, and a key no --use supplies is unjudged: under
582
+ # nuka do no earlier line ran, and --use is how a producer is named.
583
+ #: (singleton(Step), Array[untyped]) -> ^(String, untyped) -> Hash[Symbol, untyped]
584
+ def used_files(step, records)
585
+ chosen = {} #: Hash[String, [String, Hash[String, untyped]]]
586
+ step.files.each do |key, entry|
587
+ names = Engine::ChangedFiles.producers(entry).map(&:step_name)
588
+ records.each do |id, record|
589
+ next unless names.include?(record["step"])
590
+
591
+ if (earlier = chosen[key]) && earlier.first != id
592
+ raise Engine::Refusal.new("step-inputs-invalid", %(--use: files key "#{key}" is supplied by both step record #{earlier.first} (step "#{earlier.last["step"]}") and ) +
593
+ %(step record #{id} (step "#{record["step"]}"), and nuka do reads the files of one step record per key))
594
+ end
595
+ chosen[key] = [id, record]
596
+ end
597
+ end
598
+ lambda do |key, entry|
599
+ if (found = chosen[key])
600
+ id, record = found
601
+ processes = record.dig("observed", "processes")
602
+ next { of: "#{record["step"]}@#{id}", record: { step_record_id: id, processes: processes && JSON.parse(JSON.generate(processes), symbolize_names: true) } }
603
+ end
604
+
605
+ names = Engine::ChangedFiles.producers(entry).map(&:step_name)
606
+ { of: names.join(" or "),
607
+ unchosen: "files.#{key}.of names #{names.map { %("#{_1}") }.join(" or ")}, and no --use value names a step record of #{names.size == 1 ? "it" : "any of them"}, so nuka do has no files to read" }
608
+ end
609
+ end
610
+
611
+ # Each events key reads the record --use named for it, one record per
612
+ # key, since a count is of one execution. A key no --use supplies is
613
+ # unjudged with the reason: under nuka do no earlier line ran, and
614
+ # --use is how a producer is named.
615
+ #: (singleton(Step), Array[untyped]) -> ^(String, untyped) -> Hash[Symbol, untyped]
616
+ def used_events(step, records)
617
+ chosen = {} #: Hash[String, [String, Hash[String, untyped]]]
618
+ step.events.each do |key, entry|
619
+ names = Engine::ChangedFiles.producers(entry).map(&:step_name)
620
+ records.each do |id, record|
621
+ next unless names.include?(record["step"])
622
+
623
+ if (earlier = chosen[key]) && earlier.first != id
624
+ raise Engine::Refusal.new("step-inputs-invalid", %(--use: events key "#{key}" is supplied by both step record #{earlier.first} (step "#{earlier.last["step"]}") and ) +
625
+ %(step record #{id} (step "#{record["step"]}"), and nuka do counts the execution events of one step record per key))
626
+ end
627
+ chosen[key] = [id, record]
628
+ end
629
+ end
630
+ lambda do |key, entry|
631
+ if (found = chosen[key])
632
+ id, record = found
633
+ next { of: "#{record["step"]}@#{id}", producer: recorded_events_producer(id, record) }
634
+ end
635
+
636
+ names = Engine::ChangedFiles.producers(entry).map(&:step_name)
637
+ { of: names.join(" or "),
638
+ unchosen: "events.#{key}.of names #{names.map { %("#{_1}") }.join(" or ")}, and no --use value names a step record of #{names.size == 1 ? "it" : "any of them"}, " \
639
+ "so nuka do has no execution events to count" }
640
+ end
641
+ end
642
+
643
+ # Where a finished step record's execution events are: its directory
644
+ # with its slice and late lines, whether the late lines are all there,
645
+ # and the system's copy, whose kinds count as reported. A late file
646
+ # whose count no record names may be cut short, so it counts as
647
+ # truncated: an absence then stays unjudged.
648
+ #: (String, Hash[String, untyped]) -> Hash[Symbol, untyped]
649
+ def recorded_events_producer(id, record)
650
+ dir = (@use_dirs || {}).fetch(id)
651
+ symbolized = JSON.parse(JSON.generate(record), symbolize_names: true)
652
+ run_id = symbolized[:run_id]
653
+ run = run_id && begin
654
+ JSON.parse(File.read(File.join(@project, ".nukadoko", "runs", run_id, "run.json")), symbolize_names: true)
655
+ rescue SystemCallError, JSON::ParserError
656
+ nil
657
+ end
658
+ scenario = run && run[:scenarios].find { |entry| entry[:steps].any? { _1[:step_record_id] == id } }
659
+ step = scenario && scenario[:steps].find { _1[:step_record_id] == id }
660
+ of = { step_record_id: id, step: symbolized[:step], line: step && step[:line] }
661
+ observed = symbolized[:observed] || {}
662
+ slice = observed[:events]
663
+ return { source: "unread", of: of, gap: "system-unread" } if observed.key?(:events) && slice.nil?
664
+ log_read = (observed.dig(:program_log, :services) || {}).values.any?
665
+ return { source: "unread", of: of, gap: log_read ? "inlet-unnamed" : "system-unread" } unless slice
666
+
667
+ # Every service whose lines the record holds: its late file is beside
668
+ # the record under its name, and its copy under the system's.
669
+ services = (slice[:services] || {}).keys.map(&:to_s)
670
+ may_miss = lambda do |late|
671
+ next services.any? { File.exist?(File.join(dir, Engine::Events.late_file(_1))) } if late.nil?
672
+
673
+ late.each_value.any? { _1[:truncated] == true || (_1[:lines].to_i.positive? && _1[:file].nil?) }
674
+ end
675
+ if run_id.nil?
676
+ copies = services.map { File.join(dir, "system", _1, Engine::Events::SYSTEM_FILE) }.select { File.exist?(_1) }
677
+ return { source: "recorded", of: of, dir: dir, slice: slice, late_truncated: may_miss.(symbolized[:events_late]), **(copies.empty? ? {} : { system: copies }) }
678
+ end
679
+
680
+ ran = scenario && run[:systems]&.find { _1[:name] == scenario[:environment] && _1[:tree] == (scenario[:system] || "head") }
681
+ # A run.json an earlier build wrote has no services: its copies are not found.
682
+ copies = (ran&.[](:services) || []).filter_map { _1.dig(:events, :file) }.map { File.join(@project, ".nukadoko", "runs", run_id, _1) }
683
+ { source: "recorded", of: of, dir: dir, slice: slice, late_truncated: may_miss.(step&.[](:events_late)), **(copies.empty? ? {} : { system: copies }) }
493
684
  end
494
685
 
495
686
  # The record `nuka do` prints, in the TypeScript engine's shape.
@@ -505,16 +696,18 @@ module Nukadoko
505
696
  end
506
697
  end
507
698
  passed = record[:status] == "passed"
508
- outcome = passed ? { result: record[:result] || {}, returns_dropped: record[:returns_dropped] || [] } : { error: { message: record[:error], kind: record[:error_kind] } }
699
+ outcome = passed ? { result: record[:result] || {}, **(step.claims ? {} : { returns_dropped: record[:returns_dropped] || [] }) } : { error: { message: record[:error], kind: record[:error_kind] } }
509
700
  processes = record[:processes]
510
701
  { schema_version: Engine::RunRecord::SCHEMA_VERSION, nukadoko: VERSION, step_record_id: id, step: step.step_name, kind: "do",
511
702
  args: record[:args] || given, args_origin: args_origin, **outcome, status: record[:status], environment: environment.name,
512
- world_name: nil, scenario_record_id: nil, run_id: nil, started_at: record[:started_at], finished_at: record[:finished_at],
703
+ system_name: Engine::Program.new(Nukadoko.program_options).file? ? environment.name : nil, **(record[:target_version] ? { target_version: record[:target_version] } : {}),
704
+ scenario_record_id: nil, run_id: nil, started_at: record[:started_at], finished_at: record[:finished_at],
513
705
  evidence: { dir: File.join(".nukadoko", "do", id), screenshots: [] },
514
706
  observed: { processes: processes&.any? ? processes : nil,
515
- http: record[:http_count]&.positive? ? record[:http_count] : nil, files: nil, unmeasured: [] },
516
- mutates: record[:mutates], **(used.any? ? { used: used } : {}),
517
- **(record[:claim_records] ? { claims: record[:claim_records], observation: record[:observation] || "observed", attempts: 1 } : {}) }
707
+ http: record[:http_count]&.positive? ? record[:http_count] : nil, files: nil, unmeasured: [],
708
+ **(record.key?(:program_log) ? { program_log: record[:program_log] } : {}), **(record.key?(:events) ? { events: record[:events] } : {}) },
709
+ mutates: record[:mutates], **(used.any? ? { used: used } : {}), **(record[:events_late] ? { events_late: record[:events_late] } : {}),
710
+ **(record[:claim_records] ? { claims: record[:claim_records], observation: record[:observation] || "observed", attempts: 1, **Engine::RunRecord.read_fields(record) } : {}) }
518
711
  end
519
712
 
520
713
  #: (Engine::Scenario, Array[Engine::Environment]) -> bool
@@ -523,6 +716,92 @@ module Nukadoko
523
716
  environments.any? { Engine::Table.role(tags, _1.conditions) == "required" }
524
717
  end
525
718
 
719
+ # `nuka check --codes`: every code `nuka check` and `nuka run` can print,
720
+ # as findings with no location, and no project read. It answers what
721
+ # could ever be reported, so it holds while the project's config is
722
+ # broken, which is when a reader often reaches for it.
723
+ #: () -> Integer
724
+ def codes
725
+ listed = Engine::Codes.listed
726
+ if @json
727
+ @out.puts JSON.pretty_generate({ **head("check", "passed", []), codes: listed })
728
+ else
729
+ emit({}, listed, "codes")
730
+ end
731
+ 0
732
+ end
733
+
734
+ # What makes a step file's definition unreadable, as check names it.
735
+ DEFINITION_CODES = %w[step-file-import-failed duplicate-capture-key invalid-capture-key unknown-parameter-type unnamed-capture unterminated-capture].freeze
736
+
737
+ # `--fail-on` codes, repeated or comma-separated, each once.
738
+ #: () -> Array[String]
739
+ def fail_on = @options["--fail-on"].flat_map { _1.split(",") }.map(&:strip).reject(&:empty?).uniq
740
+
741
+ # The feature files `nuka check <targets>` reads, relative to the
742
+ # project: a file, or each feature file under a directory. A target
743
+ # with a `:line` is refused, since check analyzes whole files.
744
+ #: (Array[String]) -> Array[String]
745
+ def check_targets(targets)
746
+ targets.flat_map do |target|
747
+ absolute = File.expand_path(target, @project)
748
+ relative = absolute.delete_prefix("#{File.expand_path(@project)}/")
749
+ shown = relative.start_with?("/") ? absolute : relative
750
+ unless File.exist?(absolute)
751
+ line = shown.match(/\A(.+):(\d+)\z/)
752
+ if line && File.exist?(File.expand_path(line[1], @project))
753
+ raise Engine::Refusal.new("line-not-supported", "nuka check: #{line[1]}:#{line[2]}: :line is not supported; check analyzes the whole file, not one scenario",
754
+ run: "nuka check #{Engine::Selection.shell_word(line[1])}")
755
+ end
756
+ raise Engine::Refusal.new("target-not-found", "nuka check: feature file not found: #{shown}")
757
+ end
758
+ next [shown] unless File.directory?(absolute)
759
+
760
+ Dir.glob("**/*.feature", base: absolute).sort.map { File.join(shown, _1) }
761
+ end.uniq
762
+ end
763
+
764
+ # A `--fail-on` gate that could never fire is refused before anything
765
+ # is read: a code the check never reports (unknown, or a run or refusal
766
+ # code), and a note under targets, since a note is a fact about the
767
+ # whole project.
768
+ #: () -> Engine::Refusal?
769
+ def fail_on_refusal
770
+ bad = fail_on.reject { Engine::Codes.check?(_1) }
771
+ if bad.any?
772
+ unknown, elsewhere = bad.partition { Engine::Codes.severity(_1).nil? }
773
+ quoted = ->(codes) { codes.map { %("#{_1}") }.join(", ") }
774
+ parts = []
775
+ parts << "#{quoted.(unknown)} #{unknown.size == 1 ? "is not a code" : "are not codes"} nuka check reports" if unknown.any?
776
+ if elsewhere.any?
777
+ parts << "#{quoted.(elsewhere)} #{elsewhere.size == 1 ? "is a nuka run or refusal code" : "are nuka run or refusal codes"}, " \
778
+ "which a check report never contains"
779
+ end
780
+ return Engine::Refusal.new("fail-on-unknown-code", "--fail-on: #{parts.join("; ")}", run: "nuka check --codes")
781
+ end
782
+ notes = @rest.empty? ? [] : fail_on.select { Engine::Codes.severity(_1) == "note" }
783
+ return nil if notes.empty?
784
+
785
+ whole = ["nuka", "check", "--fail-on", fail_on.join(","), *@options["--env"].flat_map { ["--env", _1] }].map { Engine::Selection.shell_word(_1) }.join(" ")
786
+ Engine::Refusal.new("fail-on-note-with-targets",
787
+ "--fail-on: #{notes.map { %("#{_1}") }.join(", ")} #{notes.size == 1 ? "is a note" : "are notes"}, and a nuka check with " \
788
+ "targets computes no notes, since a note is a fact about the whole project", run: whole)
789
+ end
790
+
791
+ # The report, then one line for each `--fail-on` code it holds, so the
792
+ # exit code never changes silently. The finding keeps its severity: what
793
+ # changed is the project's policy, not the finding.
794
+ #: (Engine::Check, Array[Hash[Symbol, untyped]]) -> Integer
795
+ def checked(check, findings)
796
+ hits = fail_on.filter_map do |code|
797
+ count = findings.count { _1[:code] == code }
798
+ { code: code, count: count } if count.positive?
799
+ end
800
+ exit_code = finish("check", check.passed? && hits.empty?, findings: findings, fail_on_hits: hits)
801
+ hits.each { $stderr.puts "fail-on: #{_1[:code]} reported #{_1[:count]} #{_1[:count] == 1 ? "finding" : "findings"}" } unless @json
802
+ exit_code
803
+ end
804
+
526
805
  # The `nuka check` that reports the same errors: the files the targets
527
806
  # name, and each --env.
528
807
  #: () -> String
@@ -761,13 +1040,18 @@ module Nukadoko
761
1040
  #: () -> Integer
762
1041
  def verdict
763
1042
  answer = Engine::Verdict.new(@project)
1043
+ # --trailer observes: a stamp never blocks, so only the exit code
1044
+ # moves, and the envelope still says what the verdict was.
1045
+ trailer = @options.key?("--trailer")
764
1046
  if @json
765
1047
  emit({ **head("verdict", answer.open? ? "failed" : "passed", answer.findings), **answer.envelope_fields }, answer.findings, "verdict")
1048
+ elsif trailer
1049
+ @out.puts answer.trailer
766
1050
  else
767
1051
  @out.puts answer.headline
768
1052
  emit({}, answer.findings, "verdict")
769
1053
  end
770
- answer.open? ? 1 : 0
1054
+ answer.open? && !trailer ? 1 : 0
771
1055
  end
772
1056
 
773
1057
  # What check says about the env files; a file it cannot read, or a key
@@ -775,11 +1059,71 @@ module Nukadoko
775
1059
  #: () -> Array[Engine::Finding]
776
1060
  def env_findings
777
1061
  Engine::EnvCheck.validate!
778
- Engine::EnvCheck.findings(@project)
1062
+ Engine::EnvCheck.findings(@project) + placeholder_findings
779
1063
  rescue Engine::EnvFile::Unreadable => e
780
1064
  raise Engine::Refusal.new("env-file-unreadable", e.message)
781
1065
  end
782
1066
 
1067
+ # A placeholder a system this invocation starts has no value for fails
1068
+ # that system before its program starts, and the program alone says so.
1069
+ # The systems a run with this invocation's --env starts, each with how
1070
+ # it is reached, whether its program names {events}, whether it
1071
+ # declares setup, and what it declares with stepsReach; nil with no
1072
+ # program, and none when --env names no environment.
1073
+ #: () -> Array[Hash[Symbol, untyped]]?
1074
+ def started_systems
1075
+ program = Engine::Program.new(Nukadoko.program_options)
1076
+ return nil unless program.file?
1077
+
1078
+ program.environments(Array(@options["--env"])).map do |environment|
1079
+ recipe = program.recipe(environment)
1080
+ primary = recipe.primary_service
1081
+ # Any served service that names {events} gives the system a file,
1082
+ # so the inlet is there even when the primary names none.
1083
+ { name: environment.name, kind: primary.reach[:kind], events: program.writes_events?(environment),
1084
+ setup: !recipe.setup.nil?, steps_reach: recipe.steps_reach, services: recipe.services.map { _1.name.to_s } }
1085
+ end
1086
+ rescue Engine::Refusal
1087
+ []
1088
+ end
1089
+
1090
+ # A system no command of this invocation starts is not checked.
1091
+ #: () -> Array[Engine::Finding]
1092
+ def placeholder_findings
1093
+ program = Engine::Program.new(Nukadoko.program_options)
1094
+ environments = begin
1095
+ program.environments(Array(@options["--env"]))
1096
+ rescue Engine::Refusal
1097
+ []
1098
+ end
1099
+ # The one old name nukadoko can still see in a config, reported
1100
+ # before the gaps: a field that names it has no gap at all, since
1101
+ # the name is not one nukadoko fills.
1102
+ renamed = environments.flat_map do |environment|
1103
+ Engine::Program.renamed_placeholder_list(program.recipe(environment)).map do |old|
1104
+ Engine::Finding.new("error", "program-placeholder-renamed", Engine::CONFIG, nil, nil,
1105
+ "system #{environment.name}: #{Engine::Program.describe_renamed_placeholder(old)}",
1106
+ Engine::Program.renamed_placeholder_fix(old, Engine::CONFIG), nil)
1107
+ end
1108
+ end
1109
+ gaps = environments.flat_map do |environment|
1110
+ Engine::Program.placeholder_gap_list(program.recipe(environment)).map do |gap|
1111
+ text = "#{gap[:where]} names {#{gap[:name]}}, which has no value here: #{gap[:why]}"
1112
+ fix = Engine::Program.placeholder_gap_fix(gap, environment.name, Engine::CONFIG, Nukadoko.program_options[:environments] || {})
1113
+ Engine::Finding.new("error", "program-placeholder-unfilled", Engine::CONFIG, nil, nil, "system #{environment.name}: #{text}", fix, nil)
1114
+ end
1115
+ end
1116
+ # A checkout this invocation's systems run in and the machine does not
1117
+ # have: an environment that reaches the service at a url names no
1118
+ # root of the top level's, so a run against deployed services never
1119
+ # needs the clone.
1120
+ rootless = Engine::Program.missing_service_roots(environments.map { [_1.name, program.recipe(_1)] }, @project).map do |missing|
1121
+ Engine::Finding.new("error", "program-service-root-missing", Engine::CONFIG, nil, nil, Engine::Program.describe_missing_service_root(missing),
1122
+ Engine::Program.missing_service_root_fix(missing, Engine::CONFIG), nil)
1123
+ end
1124
+ renamed + gaps + rootless
1125
+ end
1126
+
783
1127
  #: (Engine::Refusal) -> Integer
784
1128
  def refuse(refusal)
785
1129
  finish(@command, false, findings: [refusal.to_h(@command)], verdict: refusal.severity, exit_code: refusal.exit_code)
@@ -800,19 +1144,28 @@ module Nukadoko
800
1144
  def finish(command, passed, findings:, verdict: passed ? "passed" : "failed", exit_code: passed ? 0 : 1, **rest)
801
1145
  # What a walk that stops here ends with.
802
1146
  @stopped_with = findings
803
- emit({ **head(command, verdict, findings), **rest }, findings, command)
1147
+ # A refusal is not the command's output: the TypeScript engine writes
1148
+ # it to stderr, where a shell that keeps stdout for a result leaves it.
1149
+ emit({ **head(command, verdict, findings), **rest }, findings, command, io: verdict == "refused" ? $stderr : @out)
804
1150
  exit_code
805
1151
  end
806
1152
 
1153
+ # A finding is one line and its fix and do one line each, so a reader
1154
+ # that splits on newlines and tabs gets the fields back.
1155
+ #: (untyped) -> String
1156
+ def one_line(text)
1157
+ text.to_s.gsub(/\s*[\r\n

]\s*/, " ").tr("\t", " ")
1158
+ end
1159
+
807
1160
  #: (Hash[Symbol, untyped], Array[untyped], String?, ?io: IO) -> void
808
1161
  def emit(envelope, findings, command, io: @out)
809
1162
  if @json
810
1163
  @out.puts JSON.pretty_generate(envelope)
811
1164
  else
812
1165
  findings.each do |f|
813
- io.puts [f[:severity], f[:code], Engine::Summary.where_of(f), f[:message]].join("\t")
814
- io.puts " fix: #{f[:fix]}" if f[:fix]
815
- io.puts " do: #{f[:do]}" if f[:do]
1166
+ io.puts [f[:severity], f[:code], one_line(Engine::Summary.where_of(f)), one_line(f[:message])].join("\t")
1167
+ io.puts " fix: #{one_line(f[:fix])}" if f[:fix]
1168
+ io.puts " do: #{one_line(f[:do])}" if f[:do]
816
1169
  end
817
1170
  io.puts "ok: no issues found" if command == "check" && findings.empty?
818
1171
  end
@@ -837,7 +1190,7 @@ module Nukadoko
837
1190
  table = envelope[:table] #: Hash[Symbol, untyped]
838
1191
  rows = table[:rows] #: Array[Hash[Symbol, untyped]]
839
1192
  based = !table[:base].nil?
840
- if based || counts[:controls].positive?
1193
+ if based || counts[:controls].positive? || envelope[:scenarios].any? { !_1[:system] && Engine::Underneath.demoting(_1) }
841
1194
  count = ->(word) { rows.count { _1[:verdict] == word } }
842
1195
  extra = based ? ", #{count.("unproven")} unproven, #{count.("moved")} moved, #{count.("removed")} removed" : ""
843
1196
  io.puts "criteria: #{count.("held")} held, #{count.("proven")} proven, #{count.("failed")} failed#{extra}"
@@ -863,7 +1216,7 @@ module Nukadoko
863
1216
  #: (Array[Hash[Symbol, untyped]], IO) -> void
864
1217
  def repeat_tallies(scenarios, io)
865
1218
  named = @options["--env"].size > 1
866
- scenarios.reject { _1[:world] }.group_by { _1.values_at(:feature, :line, :environment) }.each_value do |runs|
1219
+ scenarios.reject { _1[:system] }.group_by { _1.values_at(:feature, :line, :environment) }.each_value do |runs|
867
1220
  passed = runs.count { _1[:status] == "passed" }
868
1221
  next if passed == runs.size
869
1222