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
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # A system's program.log, the one file that holds what its build, prepare,
5
+ # serve, and lists commands printed, and what its setup and url function
6
+ # printed, each line redacted as it arrives, as the TypeScript engine
7
+ # writes it. Which commands run is Program's.
8
+ #
9
+ # A child process writes into a pipe, never into the file: a program
10
+ # that wrote the file itself would leave every secret it printed there,
11
+ # and each step's slice and late lines copy the file. Each stream of each
12
+ # command is a pipe of its own, read by a thread of its own, and split
13
+ # into lines before it is redacted, so a secret a read cuts in two is
14
+ # still one line when it is redacted, and a stdout line and a stderr line
15
+ # never splice. A line is written as soon as it is complete, so a step
16
+ # that reads the log at its end sees what the program printed while the
17
+ # step ran. The file opens at the first line, so a system that runs no
18
+ # command leaves no empty log behind.
19
+ module Nukadoko
20
+ module Engine
21
+ class ProgramLog
22
+ # How long a stopped command's streams may stay open: a process it
23
+ # left behind can hold them open for good.
24
+ STREAMS_CLOSE = 1.0
25
+
26
+ attr_reader :path #: String
27
+
28
+ #: (String, Secrets) -> void
29
+ def initialize(path, secrets)
30
+ @path = path
31
+ @secrets = secrets
32
+ @lock = Mutex.new
33
+ @file = nil #: File?
34
+ @written = false
35
+ @closed = false
36
+ @drains = [] #: Array[Thread]
37
+ end
38
+
39
+ # Whether anything was written.
40
+ #: () -> bool
41
+ def written? = @written
42
+
43
+ # Appends one line nukadoko writes itself, such as the command about
44
+ # to run, and gives it back as it was written.
45
+ #: (String) -> String
46
+ def note(line)
47
+ shown = @secrets.redact_string(line.dup.force_encoding(Encoding::UTF_8).scrub)
48
+ @lock.synchronize do
49
+ if @closed
50
+ # A line a stopped program's last child prints after the log
51
+ # closed still belongs in it.
52
+ File.open(@path, "a") { _1.write("#{shown}\n") }
53
+ else
54
+ file = @file ||= File.open(@path, "a").tap { _1.sync = true }
55
+ file.write("#{shown}\n")
56
+ end
57
+ @written = true
58
+ end
59
+ shown
60
+ end
61
+
62
+ # The write end of a pipe for one stream of one child process, and
63
+ # the thread that reads its other end into the log until every
64
+ # writer has closed it. `echo` also gets each line, redacted.
65
+ #: (?echo: untyped) -> [IO, Thread]
66
+ def pipe(echo: nil)
67
+ reader, writer = IO.pipe
68
+ drain = Thread.new do
69
+ carry = ""
70
+ begin
71
+ loop do
72
+ lines, carry = ProgramLog.lines(carry, reader.readpartial(65_536))
73
+ lines.each { print_line(_1, echo) }
74
+ end
75
+ rescue EOFError, IOError
76
+ print_line(carry, echo) unless carry.empty?
77
+ ensure
78
+ reader.close
79
+ end
80
+ end
81
+ @drains << drain
82
+ [writer, drain]
83
+ end
84
+
85
+ # The complete lines of `carry` and then `chunk`, without their
86
+ # newlines, and the bytes after the last newline, held for the next
87
+ # chunk. A read can end inside a line or inside a character; both are
88
+ # bytes, and a newline byte is never part of a UTF-8 character, so
89
+ # splitting on it before decoding keeps every character whole.
90
+ #: (String, String) -> [Array[String], String]
91
+ def self.lines(carry, chunk)
92
+ *lines, rest = (carry.b + chunk.b).split("\n", -1)
93
+ [lines, rest.to_s]
94
+ end
95
+
96
+ # Waits until each of `drains`, every pipe of this log unless named,
97
+ # has read its pipe to the end, for STREAMS_CLOSE at most in all.
98
+ #: (?Array[Thread]) -> void
99
+ def settle(drains = @drains)
100
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + STREAMS_CLOSE
101
+ drains.each { _1.join([deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max) }
102
+ end
103
+
104
+ #: () -> void
105
+ def close
106
+ @lock.synchronize do
107
+ @file&.close
108
+ @file = nil
109
+ @closed = true
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ #: (String, untyped) -> void
116
+ def print_line(line, echo)
117
+ shown = note(line)
118
+ echo&.puts(shown)
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,385 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The shape of what the program lambda of nukadoko.config.rb returns, as
5
+ # the TypeScript engine's program schema reads it: every key is one this
6
+ # engine reads, every value is one it can use, and `serve` gets its
7
+ # defaults. A key the program does not know is refused, never skipped: a
8
+ # misspelt `ready` left the system ready on any answer from "/", which no
9
+ # record would show. A key the TypeScript engine reads and this one does
10
+ # not yet is refused too, naming it, so a program never describes a step
11
+ # that silently does not happen. Boundary: shape only; starting the
12
+ # program is Program's.
13
+ module Nukadoko
14
+ module Engine
15
+ module ProgramShape
16
+ MAX_TIMEOUT_MS = 2_147_483_647
17
+ DEFAULT_SERVE_TIMEOUT_MS = 60_000
18
+ ENVIRONMENT_NAME = /\A[a-z0-9_-]+\z/
19
+ CONDITION_NAME = /\A[A-Za-z0-9_.-]+\z/
20
+ CONDITION_VALUE = /\A[^\s@]+\z/
21
+ SERVICE_NAME = /\A[a-z0-9_-]+\z/
22
+
23
+ SERVE_KEYS = %i[command port ready timeout_ms].freeze
24
+ PROGRAM_KEYS = %i[build prepare serve url env setup lists stepsReach].freeze
25
+ # How far the code that runs with the scenarios may reach into the
26
+ # program: a service URL only, or the program's command line too.
27
+ STEPS_REACH = %w[url any].freeze
28
+ LIST_KEYS = %i[routes flags tables].freeze
29
+ TOP_KEYS = [*PROGRAM_KEYS, :services, :primary, :environments, :defaultEnvironments].freeze
30
+ ENVIRONMENT_KEYS = [*PROGRAM_KEYS, :conditions, :policy, :envFiles, :version, :services].freeze
31
+ COMMAND_KEYS = %i[command timeout_ms].freeze
32
+ # What one service is, under services: how nukadoko builds, prepares,
33
+ # and starts it, or where it reaches it.
34
+ SERVICE_KEYS = %i[build prepare serve url env lists root external].freeze
35
+ # What an environment changes of one service.
36
+ ENVIRONMENT_SERVICE_KEYS = %i[build prepare serve url env lists root].freeze
37
+ # The fields that belong to one service alone: env is the program's
38
+ # too, under every service's own.
39
+ SERVICE_ONLY_KEYS = %i[build prepare serve url lists].freeze
40
+ # The fields external: true leaves a service without.
41
+ NOT_EXTERNAL_KEYS = %i[root build prepare serve env lists].freeze
42
+ EXTERNAL_WHY = "is not a field of a service with external: true, which nukadoko only reaches at its url and runs nothing of"
43
+
44
+ # The program with `serve`'s defaults filled, or a Refusal naming
45
+ # each problem, as `Invalid program at <file>: program.<path>: <why>`.
46
+ #: (Hash[Symbol, untyped], String) -> Hash[Symbol, untyped]
47
+ def self.read(program, file)
48
+ issues = [] #: Array[String]
49
+ read = fields(program, TOP_KEYS, [], issues)
50
+ read[:services] = services(program[:services], SERVICE_KEYS, ["services"], issues) if program.key?(:services)
51
+ environments = program[:environments]
52
+ if environments.is_a?(Hash)
53
+ read[:environments] = environments.to_h do |name, environment|
54
+ issues << "environments.#{name}: must be a name of lowercase letters, digits, \"_\", or \"-\"" unless name.to_s.match?(ENVIRONMENT_NAME)
55
+ next [name, environment] unless environment.is_a?(Hash)
56
+
57
+ at = ["environments", name.to_s]
58
+ level = fields(environment, ENVIRONMENT_KEYS, at, issues)
59
+ level[:services] = services(environment[:services], ENVIRONMENT_SERVICE_KEYS, [*at, "services"], issues) if environment.key?(:services)
60
+ [name, level]
61
+ end
62
+ elsif !environments.nil?
63
+ issues << "environments: must be a Hash of environment names to environments"
64
+ end
65
+ default_environments(program, issues)
66
+ services_shaped(read, issues)
67
+ return read if issues.empty?
68
+
69
+ message = issues.map { _1.start_with?("unknown key") ? _1 : "program.#{_1}" }.join("; ")
70
+ raise Refusal.new("config-invalid", "Invalid program at #{file}: #{message}", fix: "Correct the fields the message names in #{file}.")
71
+ end
72
+
73
+ # One level's fields: the keys it may hold, each value checked, and
74
+ # `serve` with its defaults.
75
+ #: (Hash[Symbol, untyped], Array[Symbol], Array[String], Array[String]) -> Hash[Symbol, untyped]
76
+ def self.fields(level, keys, path, issues)
77
+ at = ->(key) { [*path, key.to_s].join(".") }
78
+ unknown = level.keys.map(&:to_sym) - keys
79
+ issues << "unknown key(s): #{unknown.map { described(_1, keys) }.join(", ")}" if unknown.any?
80
+ read = level.dup
81
+ read[:serve] = serve(level[:serve], at.(:serve), issues) if level.key?(:serve)
82
+ %i[build prepare].each { commands(level[_1], at.(_1), issues) if level.key?(_1) }
83
+ url(level[:url], at.(:url), issues) if level.key?(:url)
84
+ lists(level[:lists], at.(:lists), issues) if level.key?(:lists)
85
+ setup(level[:setup], at.(:setup), issues) if level.key?(:setup)
86
+ if level.key?(:serve) && level.key?(:url)
87
+ issues << "#{at.(:url)}: serve and url are two ways to reach the program: serve starts it, url names one nukadoko does not start. Keep one."
88
+ end
89
+ issues << %(#{at.(:policy)}: must be "read-only") if level.key?(:policy) && level[:policy].to_s != "read-only"
90
+ if level.key?(:stepsReach) && !STEPS_REACH.include?(level[:stepsReach].to_s)
91
+ issues << %(#{at.(:stepsReach)}: must be "url" (a step reaches each service only at its URL) or "any")
92
+ end
93
+ issues << "#{at.(:version)}: must be a lambda returning a String" if level.key?(:version) && !level[:version].respond_to?(:call)
94
+ env(level[:env], at.(:env), issues) if level.key?(:env)
95
+ conditions(level[:conditions], at.(:conditions), issues) if level.key?(:conditions)
96
+ if level.key?(:envFiles) && !(level[:envFiles].is_a?(Array) && level[:envFiles].all?(String))
97
+ issues << "#{at.(:envFiles)}: must be an Array of file names"
98
+ end
99
+ read
100
+ end
101
+
102
+ # Each service's fields, by name, as `keys` allows them: a service of
103
+ # the top level, or what an environment changes of one.
104
+ #: (untyped, Array[Symbol], Array[String], Array[String]) -> untyped
105
+ def self.services(services, keys, path, issues)
106
+ unless services.is_a?(Hash)
107
+ issues << "#{path.join(".")}: must be a Hash of service names to services"
108
+ return services
109
+ end
110
+
111
+ services.to_h do |name, service|
112
+ where = [*path, name.to_s]
113
+ unless service.is_a?(Hash)
114
+ issues << "#{where.join(".")}: must be a Hash of the service's fields"
115
+ next [name, service]
116
+ end
117
+
118
+ read = fields(service, keys, where, issues)
119
+ if service.key?(:root) && !(service[:root].is_a?(String) && !service[:root].empty?)
120
+ issues << "#{where.join(".")}.root: must name a directory; leave it out for the tree's root"
121
+ end
122
+ external(service, where.join("."), issues) if service.key?(:external)
123
+ [name, read]
124
+ end
125
+ end
126
+
127
+ # A service nukadoko only reaches has a url and nothing else: it runs
128
+ # none of its commands and starts no process to give an env.
129
+ #: (Hash[Symbol, untyped], String, Array[String]) -> void
130
+ def self.external(service, at, issues)
131
+ return issues << "#{at}.external: must be true, or left out" unless service[:external] == true
132
+
133
+ NOT_EXTERNAL_KEYS.each { issues << "#{at}.#{_1}: #{EXTERNAL_WHY}" if service.key?(_1) }
134
+ issues << "#{at}.url: is required for a service with external: true: it is where nukadoko reaches the service" unless service.key?(:url)
135
+ end
136
+
137
+ # services replaces the top level's service fields, never sits beside
138
+ # them, so one system has one way to be written; with more than one
139
+ # service, primary names the one the steps reach; an environment
140
+ # changes only services the top level names; and no two services of
141
+ # one system take the same fixed port.
142
+ #: (Hash[Symbol, untyped], Array[String]) -> void
143
+ def self.services_shaped(program, issues)
144
+ services = program[:services]
145
+ environments = (program[:environments].is_a?(Hash) ? program[:environments] : {}).select { _2.is_a?(Hash) }.to_h { [_1.to_s, _2] }
146
+ unless services.is_a?(Hash)
147
+ return if program.key?(:services)
148
+
149
+ issues << "primary: names one of services, and this program writes none: leave it out, or write services" if program.key?(:primary)
150
+ environments.each do |name, environment|
151
+ next unless environment.key?(:services)
152
+
153
+ issues << "environments.#{name}.services: changes services, and this program writes none: write the fields the environment changes beside it"
154
+ end
155
+ return
156
+ end
157
+ SERVICE_ONLY_KEYS.each do |key|
158
+ issues << "#{key}: is a field of one service, and this program writes services: move it under services.<name>" if program.key?(key)
159
+ end
160
+ names = services.keys.map(&:to_s)
161
+ issues << "services: must name at least one service; leave it out for a program of one" if names.empty?
162
+ names.each { issues << %(services.#{_1}: must be a service name of lowercase letters, digits, "_", or "-") unless _1.match?(SERVICE_NAME) }
163
+ listed = names.map { %("#{_1}") }.join(", ")
164
+ if !program.key?(:primary)
165
+ issues << "primary: must name the service the steps reach, one of #{listed}, since services names more than one" if names.size > 1
166
+ elsif !names.include?(program[:primary].to_s)
167
+ issues << %(primary: names "#{program[:primary]}", which is no service of this program (it has #{listed}))
168
+ end
169
+ environments.each do |environment_name, environment|
170
+ SERVICE_ONLY_KEYS.each do |key|
171
+ next unless environment.key?(key)
172
+
173
+ issues << "environments.#{environment_name}.#{key}: is a field of one service, and this program writes services: move it under environments.#{environment_name}.services.<name>"
174
+ end
175
+ overlays = environment[:services].is_a?(Hash) ? environment[:services] : {}
176
+ overlays.each do |name, overlay|
177
+ at = "environments.#{environment_name}.services.#{name}"
178
+ service = services.find { |key, _| key.to_s == name.to_s }&.last
179
+ next issues << %(#{at}: names "#{name}", which is no service of this program (it has #{listed})) unless service
180
+ next unless service.is_a?(Hash) && service[:external] == true && overlay.is_a?(Hash)
181
+
182
+ overlay.each_key { issues << "#{at}.#{_1}: #{EXTERNAL_WHY}" unless _1.to_sym == :url }
183
+ end
184
+ end
185
+ ports_shared(services, environments, issues)
186
+ end
187
+
188
+ # No two services of one system take the same fixed port. The top
189
+ # level's ports are checked once, for the default system; an
190
+ # environment's only where it writes a port of its own, so a clash
191
+ # the top level makes is named once.
192
+ #: (Hash[untyped, untyped], Hash[String, Hash[Symbol, untyped]], Array[String]) -> void
193
+ def self.ports_shared(services, environments, issues)
194
+ systems = [["default", nil], *environments.map { |name, environment| [name, environment[:services].is_a?(Hash) ? environment[:services].transform_keys(&:to_s) : {}] }] #: Array[[String, Hash[String, untyped]?]]
195
+ systems.each do |system, overlays|
196
+ taken = {} #: Hash[Integer, String]
197
+ services.each do |key, service|
198
+ name = key.to_s
199
+ overlay = overlays&.[](name)
200
+ own = overlay.is_a?(Hash) && (overlay.key?(:serve) || overlay.key?(:url))
201
+ serve = own ? overlay[:serve] : (service.is_a?(Hash) ? service[:serve] : nil)
202
+ port = serve.is_a?(Hash) ? serve[:port] : nil #: untyped
203
+ next unless port.is_a?(Integer)
204
+
205
+ other = taken[port]
206
+ if other && (overlays.nil? || own)
207
+ at = overlays.nil? || !own ? "services.#{name}.serve.port" : "environments.#{system}.services.#{name}.serve.port"
208
+ issues << %(#{at}: is #{port}, which service #{other} takes too in system #{system}: two services cannot answer on one port; write port: "auto" for one of them)
209
+ end
210
+ taken[port] ||= name
211
+ end
212
+ end
213
+ end
214
+
215
+ # A key it does not know, with the known key it matches when only its
216
+ # case differs: that is a fact about the shape, not a guess.
217
+ #: (Symbol, Array[Symbol]) -> String
218
+ def self.described(key, known)
219
+ match = known.find { _1.to_s.casecmp?(key.to_s) }
220
+ match ? %(#{key} (did you mean "#{match}"?)) : key.to_s
221
+ end
222
+
223
+ #: (untyped, String, Array[String]) -> untyped
224
+ def self.serve(serve, at, issues)
225
+ unless serve.is_a?(Hash)
226
+ issues << "#{at}: must be a Hash with command, and port, ready, and timeout_ms when they differ from the defaults"
227
+ return serve
228
+ end
229
+ unknown = serve.keys.map(&:to_sym) - SERVE_KEYS
230
+ issues << "unknown key(s): #{unknown.map { described(_1, SERVE_KEYS) }.join(", ")}" if unknown.any?
231
+ issues << "#{at}.command: must name a program to run, as a String or an Array of words" unless command?(serve[:command])
232
+ port = serve.fetch(:port, "auto")
233
+ unless port == "auto" || (port.is_a?(Integer) && port.between?(1, 65_535))
234
+ issues << %(#{at}.port: must be "auto" or a port number from 1 to 65535)
235
+ end
236
+ ready = serve.fetch(:ready, "/")
237
+ problem = ready.is_a?(String) ? ready_problem(ready) : 'must be a path starting with "/"'
238
+ issues << "#{at}.ready: #{problem}" if problem
239
+ timeout = serve.fetch(:timeout_ms, DEFAULT_SERVE_TIMEOUT_MS)
240
+ problem = timeout_problem(timeout)
241
+ issues << "#{at}.timeout_ms: #{problem}" if problem
242
+ { **serve, port: port, ready: ready, timeout_ms: timeout }
243
+ end
244
+
245
+ # A build or prepare list: each entry a command, or a command with a
246
+ # timeout_ms of its own.
247
+ #: (untyped, String, Array[String]) -> void
248
+ def self.commands(list, at, issues)
249
+ return issues << "#{at}: must be an Array of commands" unless list.is_a?(Array)
250
+
251
+ list.each_with_index do |entry, index|
252
+ where = "#{at}.#{index}"
253
+ if entry.is_a?(Hash)
254
+ unknown = entry.keys.map(&:to_sym) - COMMAND_KEYS
255
+ issues << "unknown key(s): #{unknown.map { described(_1, COMMAND_KEYS) }.join(", ")}" if unknown.any?
256
+ problem = entry.key?(:timeout_ms) ? timeout_problem(entry[:timeout_ms]) : nil
257
+ issues << "#{where}.timeout_ms: #{problem}" if problem
258
+ entry = entry[:command]
259
+ where = "#{where}.command"
260
+ end
261
+ issues << "#{where}: must name a program to run, as a String or an Array of words" unless command?(entry)
262
+ end
263
+ end
264
+
265
+ # `lists`: one command per kind of list the program prints, each a
266
+ # build entry's shape.
267
+ #: (untyped, String, Array[String]) -> void
268
+ def self.lists(lists, at, issues)
269
+ return issues << "#{at}: must be a Hash of routes, flags, or tables to a command" unless lists.is_a?(Hash)
270
+
271
+ unknown = lists.keys.map(&:to_sym) - LIST_KEYS
272
+ issues << "unknown key(s): #{unknown.map { described(_1, LIST_KEYS) }.join(", ")}" if unknown.any?
273
+ lists.each { |kind, entry| commands([entry], "#{at}.#{kind}", issues) if LIST_KEYS.include?(kind.to_sym) }
274
+ end
275
+
276
+ #: (untyped) -> bool
277
+ def self.command?(command)
278
+ words = command.is_a?(Array) ? command : command.to_s.split
279
+ (command.is_a?(String) || (command.is_a?(Array) && command.all?(String))) && !words.first.to_s.empty?
280
+ end
281
+
282
+ # A program nukadoko does not start: a URL, a lambda that returns one,
283
+ # or `{ resolve:, timeout_ms: }`.
284
+ #: (untyped, String, Array[String]) -> void
285
+ def self.url(url, at, issues)
286
+ if url.is_a?(String)
287
+ problem = Program.url_problem(url)
288
+ issues << "#{at}: #{problem}" if problem
289
+ elsif url.is_a?(Hash)
290
+ unknown = url.keys.map(&:to_sym) - %i[resolve timeout_ms]
291
+ issues << "unknown key(s): #{unknown.map { described(_1, %i[resolve timeout_ms]) }.join(", ")}" if unknown.any?
292
+ issues << "#{at}.resolve: must be a lambda returning a URL" unless url[:resolve].respond_to?(:call)
293
+ problem = url.key?(:timeout_ms) ? timeout_problem(url[:timeout_ms]) : nil
294
+ issues << "#{at}.timeout_ms: #{problem}" if problem
295
+ elsif !url.respond_to?(:call)
296
+ issues << "#{at}: must be a URL or a lambda returning one"
297
+ end
298
+ end
299
+
300
+ # A lambda run once the program answers, or `{ run:, timeout_ms: }`.
301
+ #: (untyped, String, Array[String]) -> void
302
+ def self.setup(setup, at, issues)
303
+ return if setup.respond_to?(:call)
304
+ return issues << "#{at}: must be a lambda" unless setup.is_a?(Hash)
305
+
306
+ unknown = setup.keys.map(&:to_sym) - %i[run timeout_ms]
307
+ issues << "unknown key(s): #{unknown.map { described(_1, %i[run timeout_ms]) }.join(", ")}" if unknown.any?
308
+ issues << "#{at}.run: must be a lambda" unless setup[:run].respond_to?(:call)
309
+ problem = setup.key?(:timeout_ms) ? timeout_problem(setup[:timeout_ms]) : nil
310
+ issues << "#{at}.timeout_ms: #{problem}" if problem
311
+ end
312
+
313
+ # Why `ready` cannot be the path polled on the system's own server: a
314
+ # second "/" or a "\" after the first names another host, which could
315
+ # answer for a program that never started.
316
+ #: (String) -> String?
317
+ def self.ready_problem(ready)
318
+ return 'must be a path starting with "/"' unless ready.start_with?("/")
319
+
320
+ if ready.start_with?("//") || ready.start_with?("/\\")
321
+ 'must be a path on the system\'s own server, starting with one "/": a second "/" or a "\\" after it names another host'
322
+ end
323
+ end
324
+
325
+ #: (untyped) -> String?
326
+ def self.timeout_problem(timeout)
327
+ return "must be a whole number of milliseconds" unless timeout.is_a?(Integer)
328
+ return "must be at least 1 ms" if timeout < 1
329
+
330
+ "must be at most #{MAX_TIMEOUT_MS} ms" if timeout > MAX_TIMEOUT_MS
331
+ end
332
+
333
+ #: (untyped, String, Array[String]) -> void
334
+ def self.env(env, at, issues)
335
+ return issues << "#{at}: must be a Hash of variable names to String values" unless env.is_a?(Hash)
336
+
337
+ env.each { |key, value| issues << "#{at}.#{key}: must be a String" unless value.is_a?(String) }
338
+ end
339
+
340
+ # Each condition is a fact a `@with:` tag reads: a boolean, or a word
341
+ # with no whitespace and no "@". "true" and "false" as strings are
342
+ # refused, since a string reads as on whatever it says.
343
+ #: (untyped, String, Array[String]) -> void
344
+ def self.conditions(conditions, at, issues)
345
+ return issues << "#{at}: must be a Hash of condition names to values" unless conditions.is_a?(Hash)
346
+
347
+ conditions.each do |name, value|
348
+ unless name.to_s.match?(CONDITION_NAME)
349
+ issues << %(#{at}.#{name}: must be a condition name of letters, digits, "_", ".", or "-", as @with:<name> names it)
350
+ end
351
+ next if [true, false].include?(value)
352
+
353
+ if !value.is_a?(String) || !value.match?(CONDITION_VALUE)
354
+ issues << %(#{at}.#{name}: must be true, false, or a value with no whitespace and no "@", as @with:<name>=<value> names it)
355
+ elsif %w[true false].include?(value)
356
+ issues << "#{at}.#{name}: is a boolean written as a string, which @with:<name> reads as on whatever it says: write the boolean, without quotes"
357
+ end
358
+ end
359
+ end
360
+
361
+ # `defaultEnvironments` names environments of this program, each once.
362
+ #: (Hash[Symbol, untyped], Array[String]) -> void
363
+ def self.default_environments(program, issues)
364
+ return unless program.key?(:defaultEnvironments)
365
+
366
+ named = program[:defaultEnvironments]
367
+ unless named.is_a?(Array) && named.all? { _1.is_a?(String) || _1.is_a?(Symbol) }
368
+ return issues << "defaultEnvironments: must be an Array of environment names"
369
+ end
370
+ return issues << "defaultEnvironments: must name at least one environment; leave it out for the default selection" if named.empty?
371
+
372
+ defined = (program[:environments].is_a?(Hash) ? program[:environments].keys : []).map(&:to_s)
373
+ named.map(&:to_s).each_with_index do |name, index|
374
+ unless name == "default" || defined.include?(name)
375
+ listed = defined.empty? ? "it defines none" : "it defines #{defined.map { %("#{_1}") }.join(", ")}"
376
+ issues << %(defaultEnvironments.#{index}: names "#{name}", which is neither default nor an environment of this file (#{listed}))
377
+ end
378
+ if named.map(&:to_s).index(name) != index
379
+ issues << %(defaultEnvironments.#{index}: names "#{name}" more than once, which would run every scenario twice under one name)
380
+ end
381
+ end
382
+ end
383
+ end
384
+ end
385
+ end
@@ -5,6 +5,8 @@
5
5
  # `use` and calls it with the value; whatever follows that call is the
6
6
  # teardown, as with Playwright's `use()`. The block runs in a Fiber, so the
7
7
  # teardown waits until the scope ends, and runs whether the steps passed.
8
+ # `use` returns how the scope ended, "passed" or "failed", so a teardown
9
+ # can keep what a failure left for a person to look at.
8
10
  #
9
11
  # Nukadoko.fixture(:store) { |use| use.(Signup::Store.new) }
10
12
  # Nukadoko.fixture(:db, scope: :process) { |use, env:| db = connect(env); use.(db); db.close }
@@ -46,29 +48,60 @@ module Nukadoko
46
48
 
47
49
  #: (Fixture, Hash[Symbol, untyped]) -> untyped
48
50
  def build(fixture, deps)
51
+ # A second `use` would suspend the fiber again after teardown resumed
52
+ # it once, so the code after it would never run and nothing would
53
+ # say so. It raises instead, inside the fixture, and teardown
54
+ # reports it as that fixture's error.
55
+ used = false
56
+ use = lambda do |value|
57
+ raise "Fixture \"#{fixture.name}\" called use(...) more than once; a fixture may call it exactly once" if used
58
+
59
+ used = true
60
+ Fiber.yield(value)
61
+ end
49
62
  fiber = Fiber.new do
50
- fixture.block.call(->(value) { Fiber.yield(value) }, **deps)
63
+ fixture.block.call(use, **deps)
51
64
  :done
52
65
  end
53
66
  value = fiber.resume
54
67
  raise "fixture #{fixture.name} returned without calling use" if value == :done && !fiber.alive?
55
68
 
56
- @fibers << fiber
69
+ @fibers << [fixture.name, fiber]
57
70
  @values[fixture.name] = value
58
71
  end
59
72
 
60
- # Every teardown runs; the first failure is raised after the rest.
61
- #: () -> void
62
- def teardown
63
- failure = nil #: StandardError?
64
- @fibers.reverse_each do |fiber|
65
- fiber.resume if fiber.alive?
73
+ # Every teardown runs, and each one that raised is returned by name. A
74
+ # cleanup error does not change the outcome of steps that already
75
+ # passed: the caller records it and warns. Each fixture's `use`
76
+ # returns `outcome`: a scope that did not finish is "failed".
77
+ #: (?String) -> Array[{ fixture: String, message: String }]
78
+ def teardown(outcome = "failed")
79
+ errors = [] #: Array[{ fixture: String, message: String }]
80
+ @fibers.reverse_each do |name, fiber|
81
+ fiber.resume(outcome) if fiber.alive?
66
82
  rescue StandardError => e
67
- failure ||= e
83
+ errors << { fixture: name.to_s, message: e.message }
68
84
  end
69
85
  @fibers.clear
70
86
  @values.clear
71
- raise failure if failure
87
+ errors
88
+ end
89
+
90
+ # The warnings that report teardown errors, one finding each.
91
+ #: (Array[{ fixture: String, message: String }]) { (String) -> String } -> Array[Hash[Symbol, untyped]]
92
+ def self.findings(errors, &redact)
93
+ errors.map do |error|
94
+ message = redact.("fixture \"#{error[:fixture]}\" teardown failed: #{error[:message]}").gsub(/\s*[\r\n]\s*/, " ")
95
+ { severity: "warning", code: "fixture-teardown-failed", message: message, fix: Codes.fix("fixture-teardown-failed") }
96
+ end
97
+ end
98
+
99
+ # The same warnings as stderr lines.
100
+ #: (Array[{ fixture: String, message: String }]) { (String) -> String } -> Array[String]
101
+ def self.warnings(errors, &redact)
102
+ findings(errors, &redact).flat_map do |finding|
103
+ [["warning", finding[:code], "-", finding[:message]].join("\t"), " fix: #{finding[:fix]}"]
104
+ end
72
105
  end
73
106
  end
74
107
  end
@@ -8,7 +8,7 @@ require "rbconfig"
8
8
 
9
9
  # A tree's program under test: the `program` lambda of its
10
10
  # nukadoko.config.rb, called once. A config with no program has none, and a
11
- # run starts no world for it.
11
+ # run starts no system for it.
12
12
  #
13
13
  # The program goes with the code and the criteria with the config, so a
14
14
  # `nuka run --base` tree can hold one side's config and the other side's
@@ -25,20 +25,21 @@ module Nukadoko
25
25
  UPGRADING = 'CHANGELOG.md, under "The program moves into the config"'
26
26
 
27
27
  # The program of the tree at `root`, whose config is already loaded.
28
+ # Its shape is checked whichever file holds it (ProgramShape).
28
29
  #: (String) -> Hash[Symbol, untyped]
29
30
  def self.read(root)
30
31
  raise called_in_config unless Nukadoko.legacy_program.nil?
31
32
 
32
33
  legacy_copy = File.join(root, TREE_LEGACY_PROGRAM)
33
- return elsewhere(legacy_copy, legacy: true) || {} if File.exist?(legacy_copy)
34
+ return ProgramShape.read(elsewhere(legacy_copy, legacy: true) || {}, LEGACY_PROGRAM) if File.exist?(legacy_copy)
34
35
 
35
36
  tree_copy = File.join(root, TREE_PROGRAM)
36
- return elsewhere(tree_copy, legacy: false) || {} if File.exist?(tree_copy)
37
+ return ProgramShape.read(elsewhere(tree_copy, legacy: false) || {}, CONFIG) if File.exist?(tree_copy)
37
38
 
38
39
  legacy = File.join(root, LEGACY_PROGRAM)
39
40
  raise moved if File.exist?(legacy)
40
41
 
41
- called(Nukadoko.config_options[:program], CONFIG) || {}
42
+ ProgramShape.read(called(Nukadoko.config_options[:program], CONFIG) || {}, CONFIG)
42
43
  end
43
44
 
44
45
  # Nukadoko.program called from the config would describe a program