nukadoko 0.1.0 → 0.3.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 (159) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +417 -0
  3. data/README.ja.md +62 -7
  4. data/README.md +19 -5
  5. data/exe/nuka +8 -0
  6. data/lib/nukadoko/aria_snapshot.rb +1008 -0
  7. data/lib/nukadoko/aria_tree.rb +150 -0
  8. data/lib/nukadoko/base.rb +294 -0
  9. data/lib/nukadoko/check.rb +669 -22
  10. data/lib/nukadoko/claim_check.rb +177 -0
  11. data/lib/nukadoko/claim_check_run.rb +293 -0
  12. data/lib/nukadoko/claim_line.rb +381 -0
  13. data/lib/nukadoko/claim_rules.rb +519 -0
  14. data/lib/nukadoko/claim_sides.rb +469 -0
  15. data/lib/nukadoko/claims.rb +576 -37
  16. data/lib/nukadoko/cli.rb +814 -22
  17. data/lib/nukadoko/codes.json +947 -0
  18. data/lib/nukadoko/codes.rb +19 -0
  19. data/lib/nukadoko/command.rb +214 -12
  20. data/lib/nukadoko/criteria.rb +172 -0
  21. data/lib/nukadoko/definition.rb +33 -41
  22. data/lib/nukadoko/env_check.rb +88 -0
  23. data/lib/nukadoko/features.rb +80 -10
  24. data/lib/nukadoko/fixtures.rb +329 -11
  25. data/lib/nukadoko/http_recording.rb +68 -0
  26. data/lib/nukadoko/init.rb +129 -0
  27. data/lib/nukadoko/js_json.rb +274 -0
  28. data/lib/nukadoko/js_pattern.rb +531 -0
  29. data/lib/nukadoko/judge.rb +221 -0
  30. data/lib/nukadoko/judgment_file.rb +78 -0
  31. data/lib/nukadoko/judgments.rb +256 -0
  32. data/lib/nukadoko/literal.rb +129 -0
  33. data/lib/nukadoko/measured.rb +105 -0
  34. data/lib/nukadoko/order.rb +84 -0
  35. data/lib/nukadoko/parts.rb +43 -22
  36. data/lib/nukadoko/pattern.rb +73 -3
  37. data/lib/nukadoko/poll.rb +21 -8
  38. data/lib/nukadoko/program.rb +105 -17
  39. data/lib/nukadoko/project_files.rb +17 -0
  40. data/lib/nukadoko/project_fixtures.rb +17 -2
  41. data/lib/nukadoko/project_program.rb +119 -0
  42. data/lib/nukadoko/report_allure.rb +751 -0
  43. data/lib/nukadoko/report_messages.rb +167 -0
  44. data/lib/nukadoko/report_run.rb +169 -0
  45. data/lib/nukadoko/run.rb +898 -91
  46. data/lib/nukadoko/run_record.rb +535 -0
  47. data/lib/nukadoko/scaffold.rb +1015 -0
  48. data/lib/nukadoko/schema.rb +120 -21
  49. data/lib/nukadoko/secrets.rb +375 -0
  50. data/lib/nukadoko/selection.rb +163 -0
  51. data/lib/nukadoko/signature.rb +4 -1
  52. data/lib/nukadoko/skill.rb +36 -0
  53. data/lib/nukadoko/spaces.rb +28 -0
  54. data/lib/nukadoko/step.rb +68 -21
  55. data/lib/nukadoko/summary.rb +662 -0
  56. data/lib/nukadoko/table.rb +34 -2
  57. data/lib/nukadoko/text_fit.rb +179 -0
  58. data/lib/nukadoko/trace.rb +244 -0
  59. data/lib/nukadoko/trace_actions.rb +116 -0
  60. data/lib/nukadoko/tree_passes.rb +122 -0
  61. data/lib/nukadoko/verdict.rb +128 -0
  62. data/lib/nukadoko/version.rb +2 -1
  63. data/lib/nukadoko/vocabulary.rb +6 -0
  64. data/lib/nukadoko/walk.rb +305 -0
  65. data/lib/nukadoko/walk_files.rb +473 -0
  66. data/lib/nukadoko/walk_step.rb +149 -0
  67. data/lib/nukadoko.rb +60 -2
  68. data/llms.txt +297 -0
  69. data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
  70. data/sig/generated/nukadoko/aria_tree.rbs +142 -0
  71. data/sig/generated/nukadoko/base.rbs +142 -0
  72. data/sig/generated/nukadoko/check.rbs +255 -0
  73. data/sig/generated/nukadoko/claim_check.rbs +118 -0
  74. data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
  75. data/sig/generated/nukadoko/claim_line.rbs +158 -0
  76. data/sig/generated/nukadoko/claim_rules.rbs +172 -0
  77. data/sig/generated/nukadoko/claim_sides.rbs +207 -0
  78. data/sig/generated/nukadoko/claims.rbs +208 -0
  79. data/sig/generated/nukadoko/cli.rbs +182 -0
  80. data/sig/generated/nukadoko/codes.rbs +16 -0
  81. data/sig/generated/nukadoko/command.rbs +109 -0
  82. data/sig/generated/nukadoko/criteria.rbs +83 -0
  83. data/sig/generated/nukadoko/definition.rbs +30 -0
  84. data/sig/generated/nukadoko/env_check.rbs +34 -0
  85. data/sig/generated/nukadoko/features.rbs +101 -0
  86. data/sig/generated/nukadoko/fixtures.rbs +175 -0
  87. data/sig/generated/nukadoko/http_recording.rbs +29 -0
  88. data/sig/generated/nukadoko/init.rbs +64 -0
  89. data/sig/generated/nukadoko/js_json.rbs +109 -0
  90. data/sig/generated/nukadoko/js_pattern.rbs +194 -0
  91. data/sig/generated/nukadoko/judge.rbs +64 -0
  92. data/sig/generated/nukadoko/judgment_file.rbs +39 -0
  93. data/sig/generated/nukadoko/judgments.rbs +120 -0
  94. data/sig/generated/nukadoko/literal.rbs +71 -0
  95. data/sig/generated/nukadoko/measured.rbs +42 -0
  96. data/sig/generated/nukadoko/order.rbs +54 -0
  97. data/sig/generated/nukadoko/parts.rbs +35 -0
  98. data/sig/generated/nukadoko/pattern.rbs +62 -0
  99. data/sig/generated/nukadoko/poll.rbs +41 -0
  100. data/sig/generated/nukadoko/program.rbs +101 -0
  101. data/sig/generated/nukadoko/project_files.rbs +19 -0
  102. data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
  103. data/sig/generated/nukadoko/project_program.rbs +55 -0
  104. data/sig/generated/nukadoko/report_allure.rbs +268 -0
  105. data/sig/generated/nukadoko/report_messages.rbs +68 -0
  106. data/sig/generated/nukadoko/report_run.rbs +102 -0
  107. data/sig/generated/nukadoko/run.rbs +264 -0
  108. data/sig/generated/nukadoko/run_record.rbs +175 -0
  109. data/sig/generated/nukadoko/scaffold.rbs +328 -0
  110. data/sig/generated/nukadoko/schema.rbs +155 -0
  111. data/sig/generated/nukadoko/secrets.rbs +172 -0
  112. data/sig/generated/nukadoko/selection.rbs +73 -0
  113. data/sig/generated/nukadoko/signature.rbs +17 -0
  114. data/sig/generated/nukadoko/skill.rbs +20 -0
  115. data/sig/generated/nukadoko/spaces.rbs +31 -0
  116. data/sig/generated/nukadoko/step.rbs +115 -0
  117. data/sig/generated/nukadoko/summary.rbs +204 -0
  118. data/sig/generated/nukadoko/table.rbs +43 -0
  119. data/sig/generated/nukadoko/text_fit.rbs +70 -0
  120. data/sig/generated/nukadoko/trace.rbs +127 -0
  121. data/sig/generated/nukadoko/trace_actions.rbs +40 -0
  122. data/sig/generated/nukadoko/tree_passes.rbs +48 -0
  123. data/sig/generated/nukadoko/verdict.rbs +56 -0
  124. data/sig/generated/nukadoko/version.rbs +5 -0
  125. data/sig/generated/nukadoko/vocabulary.rbs +25 -0
  126. data/sig/generated/nukadoko/walk.rbs +139 -0
  127. data/sig/generated/nukadoko/walk_files.rbs +189 -0
  128. data/sig/generated/nukadoko/walk_step.rbs +50 -0
  129. data/sig/generated/nukadoko.rbs +26 -0
  130. data/sig/nukadoko.rbs +5 -48
  131. data/skills/nukadoko/SKILL.md +120 -0
  132. data/skills/nukadoko/references/checking-steps.md +200 -0
  133. data/skills/nukadoko/references/diagnosing.md +200 -0
  134. data/skills/nukadoko/references/done.md +99 -0
  135. data/skills/nukadoko/references/evidence.md +124 -0
  136. data/skills/nukadoko/references/examples/cli.md +230 -0
  137. data/skills/nukadoko/references/examples/http-api.md +342 -0
  138. data/skills/nukadoko/references/examples/workflow.md +173 -0
  139. data/skills/nukadoko/references/exploring.md +184 -0
  140. data/skills/nukadoko/references/fixtures.md +198 -0
  141. data/skills/nukadoko/references/from-prose.md +51 -0
  142. data/skills/nukadoko/references/judgments.md +95 -0
  143. data/skills/nukadoko/references/maintenance.md +97 -0
  144. data/skills/nukadoko/references/merge-risk.md +74 -0
  145. data/skills/nukadoko/references/parts.md +152 -0
  146. data/skills/nukadoko/references/perturbed-worlds.md +109 -0
  147. data/skills/nukadoko/references/program.md +200 -0
  148. data/skills/nukadoko/references/reviewing.md +200 -0
  149. data/skills/nukadoko/references/running.md +200 -0
  150. data/skills/nukadoko/references/scenario-rules.md +188 -0
  151. data/skills/nukadoko/references/states.md +66 -0
  152. data/skills/nukadoko/references/step-files.md +200 -0
  153. data/skills/nukadoko/references/verdicts.md +200 -0
  154. data/skills/nukadoko/references/writing-steps.md +200 -0
  155. data/skills/nukadoko/references/writing-the-feature.md +165 -0
  156. data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
  157. data/skills/nukadoko/scripts/merge-risk.jq +56 -0
  158. data/skills/nukadoko-verification/SKILL.md +263 -0
  159. metadata +133 -1
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  require "socket"
5
+ require "shellwords"
4
6
  require "net/http"
5
7
 
6
8
  # How the program under test starts, once per environment: an environment
@@ -8,35 +10,102 @@ require "net/http"
8
10
  # environment's state never reaches another's scenarios.
9
11
  module Nukadoko
10
12
  module Engine
11
- Environment = Data.define(:name, :conditions, :env)
13
+ Environment = Data.define(
14
+ :name, #: String
15
+ :conditions, #: Hash[String, untyped]
16
+ :env, #: Hash[untyped, untyped]
17
+ :env_files, #: Array[String]
18
+ )
12
19
 
13
20
  class Program
21
+ #: (Hash[Symbol, untyped]) -> void
14
22
  def initialize(options)
15
23
  @options = options
16
24
  end
17
25
 
18
- def environments
19
- declared = @options[:environments]
20
- return [Environment.new("default", {}, {})] unless declared
26
+ # The worlds a run starts: each --env in the order given; without one,
27
+ # defaultEnvironments in its order, else every environment that
28
+ # declares conditions, else the top level under the name default.
29
+ # default is always a name, whether or not the file defines it.
30
+ #: (?Array[String]) -> Array[Environment]
31
+ def environments(selected = [])
32
+ declared = self.declared
33
+ (selected.any? ? selected : default_names).map do |name|
34
+ unless name == "default" || declared.key?(name)
35
+ add = "add environments.#{name} to the program in nukadoko.config.rb"
36
+ fix = if declared.empty? then "Leave out --env or pass --env default (the program in nukadoko.config.rb defines no other environment), or #{add}."
37
+ else "Pass --env with a name it accepts (#{["default", *declared.keys].uniq.sort.join(", ")}), or #{add}."
38
+ end
39
+ raise Refusal.new("unknown-environment", %(Unknown environment "#{name}": not defined in the environments of the program in nukadoko.config.rb), fix: fix)
40
+ end
21
41
 
22
- declared.map { |name, e| Environment.new(name.to_s, (e[:conditions] || {}).transform_keys(&:to_s), e[:env] || {}) }
42
+ environment(name)
43
+ end
23
44
  end
24
45
 
25
- def start(environment, dir, project)
26
- return World.new(@options[:base_url], nil, nil) unless @options[:serve]
46
+ # Whether the config gave a program, so each step names its world.
47
+ #: () -> bool
48
+ def file? = !@options.empty?
49
+
50
+ #: () -> Hash[String, untyped]
51
+ def declared = (@options[:environments] || {}).transform_keys(&:to_s)
52
+
53
+ #: (String) -> Environment
54
+ def environment(name)
55
+ e = declared[name] || {}
56
+ Environment.new(name, (e[:conditions] || {}).transform_keys(&:to_s), e[:env] || {}, Program.env_files(e))
57
+ end
58
+
59
+ # The config's envFiles, then the environment's own: later files win.
60
+ #: (Hash[Symbol, untyped]) -> Array[String]
61
+ def self.env_files(environment) = [*Nukadoko.config_options[:envFiles], *environment[:envFiles]].map(&:to_s)
62
+
63
+ #: () -> Array[String]
64
+ def default_names
65
+ return @options[:defaultEnvironments].map(&:to_s) if @options[:defaultEnvironments]
66
+
67
+ conditioned = declared.select { |_, e| e[:conditions] }.keys
68
+ conditioned.any? ? conditioned : ["default"]
69
+ end
70
+
71
+ # Why a run without --env starts the environments it starts.
72
+ #: () -> String
73
+ def chosen_by
74
+ if @options[:defaultEnvironments] then "defaultEnvironments in nukadoko.config.rb"
75
+ elsif default_names == ["default"] then "no environment declares conditions"
76
+ else "the environments that declare conditions"
77
+ end
78
+ end
27
79
 
28
- port = TCPServer.open("127.0.0.1", 0) { _1.addr[1] }
80
+ #: (Environment, String, String) -> World
81
+ def start(environment, dir, project)
82
+ port = if @options[:serve]
83
+ server = TCPServer.new("127.0.0.1", 0)
84
+ server.addr[1].tap { server.close }
85
+ end
29
86
  fill = ->(text) { placeholders(text.to_s, port, dir, environment) }
30
- env = (@options[:env] || {}).merge(environment.env).to_h { |k, v| [k.to_s, fill.(v)] }
31
- log = File.open(File.join(dir, "program.log"), "w")
32
- pid = Process.spawn(env, fill.(@options[:serve][:command]), out: log, err: log, pgroup: true, chdir: project)
33
- world = World.new("http://127.0.0.1:#{port}", pid, log)
87
+ # The world's env is what its program and the steps' env fixture get.
88
+ env = (@options[:env] || {}).merge(environment.env).to_h { |k, v| [k.to_s, fill.(v)] }.freeze
89
+ prefix = Trace.new_prefix
90
+ unless @options[:serve]
91
+ return World.new(base_url: @options[:base_url], pid: nil, log: nil, env: env, trace_prefix: prefix, log_path: nil, commands: [], timings: {})
92
+ end
93
+
94
+ log_path = File.join(dir, "program.log")
95
+ log = File.open(log_path, "w")
96
+ command = fill.(@options[:serve][:command])
97
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
98
+ pid = Process.spawn(env, command, out: log, err: log, pgroup: true, chdir: project)
99
+ world = World.new(base_url: "http://localhost:#{port}", pid: pid, log: log, env: env, trace_prefix: prefix, log_path: log_path,
100
+ commands: [{ phase: "serve", argv: Shellwords.split(command) }], timings: {})
34
101
  world.wait_ready(@options[:serve][:ready])
102
+ world.timings[:serve_ms] = Request.since(started)
35
103
  world
36
104
  end
37
105
 
38
106
  private
39
107
 
108
+ #: (String, Integer?, String, Environment) -> String
40
109
  def placeholders(text, port, dir, environment)
41
110
  text.gsub("{port}", port.to_s).gsub("{dir}", dir).gsub(/\{condition\.([\w.-]+)\}/) do
42
111
  environment.conditions.fetch(Regexp.last_match(1), "").to_s
@@ -44,7 +113,21 @@ module Nukadoko
44
113
  end
45
114
  end
46
115
 
47
- World = Data.define(:base_url, :pid, :log) do
116
+ # A started program: where it answers, its env, the prefix of every
117
+ # trace id its steps carry, and its log. `timings` gains stop_ms at its stop.
118
+ World = Data.define(
119
+ :base_url, #: String
120
+ :pid, #: Integer?
121
+ :log, #: File?
122
+ :env, #: Hash[String, String]
123
+ :trace_prefix, #: String
124
+ :log_path, #: String?
125
+ :commands, #: Array[Hash[Symbol, untyped]]
126
+ :timings, #: Hash[Symbol, Integer]
127
+ )
128
+
129
+ class World
130
+ #: (String) -> void
48
131
  def wait_ready(path)
49
132
  uri = URI.join(base_url, path)
50
133
  deadline = Time.now + 30
@@ -58,12 +141,17 @@ module Nukadoko
58
141
  end
59
142
  end
60
143
 
144
+ #: () -> void
61
145
  def stop
62
- Process.kill("TERM", -pid) if pid
63
- Process.wait(pid) if pid
64
- log&.close
65
- rescue Errno::ESRCH, Errno::ECHILD
146
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
147
+ begin
148
+ Process.kill("TERM", -pid) if pid
149
+ Process.wait(pid) if pid
150
+ rescue Errno::ESRCH, Errno::ECHILD
151
+ nil
152
+ end
66
153
  log&.close
154
+ timings[:stop_ms] = Request.since(started) if pid
67
155
  end
68
156
  end
69
157
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The names of the files a project keeps nukadoko's settings in, one place
5
+ # for every part of the engine that reads, writes, or names them.
6
+ module Nukadoko
7
+ module Engine
8
+ CONFIG = "nukadoko.config.rb"
9
+ # The program file of releases before the program moved into the
10
+ # config. A project may no longer hold one; a `--base` ref may.
11
+ LEGACY_PROGRAM = "nukadoko.program.rb"
12
+ # What a `--base` run writes into a tree for the program it runs:
13
+ # another config whose program the tree runs, or a ref's legacy file.
14
+ TREE_PROGRAM = "nukadoko.tree-program.rb"
15
+ TREE_LEGACY_PROGRAM = "nukadoko.tree-legacy-program.rb"
16
+ end
17
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rbs_inline: enabled
2
3
 
3
4
  # Fixtures a project declares in nukadoko.config.rb. A fixture block gets
4
5
  # `use` and calls it with the value; whatever follows that call is the
@@ -8,29 +9,42 @@
8
9
  # Nukadoko.fixture(:store) { |use| use.(Signup::Store.new) }
9
10
  # Nukadoko.fixture(:db, scope: :process) { |use, env:| db = connect(env); use.(db); db.close }
10
11
  module Nukadoko
11
- Fixture = Data.define(:name, :scope, :block) do
12
+ Fixture = Data.define(
13
+ :name, #: Symbol
14
+ :scope, #: Symbol
15
+ :block, #: Proc
16
+ )
17
+
18
+ class Fixture
19
+ #: () -> Array[Symbol]
12
20
  def dependencies = block.parameters.filter_map { |kind, n| n if kind == :keyreq }
13
21
  end
14
22
 
23
+ #: (Symbol | String, ?scope: :scenario | :process) { (^(untyped) -> void, **untyped) -> void } -> Fixture
15
24
  def self.fixture(name, scope: :scenario, &block)
16
25
  raise ArgumentError, "scope is :scenario or :process, not #{scope.inspect}" unless %i[scenario process].include?(scope)
17
26
 
18
27
  project_fixtures[name.to_sym] = Fixture.new(name.to_sym, scope, block)
19
28
  end
20
29
 
30
+ #: () -> Hash[Symbol, Fixture]
21
31
  def self.project_fixtures = (@project_fixtures ||= {})
22
32
 
23
33
  module Engine
24
34
  # One scope's built fixtures, torn down in reverse build order.
25
35
  class FixtureCache
36
+ #: () -> void
26
37
  def initialize
27
38
  @values = {}
28
39
  @fibers = []
29
40
  end
30
41
 
42
+ #: (Symbol) -> bool
31
43
  def key?(name) = @values.key?(name)
44
+ #: (Symbol) -> untyped
32
45
  def [](name) = @values.fetch(name)
33
46
 
47
+ #: (Fixture, Hash[Symbol, untyped]) -> untyped
34
48
  def build(fixture, deps)
35
49
  fiber = Fiber.new do
36
50
  fixture.block.call(->(value) { Fiber.yield(value) }, **deps)
@@ -44,8 +58,9 @@ module Nukadoko
44
58
  end
45
59
 
46
60
  # Every teardown runs; the first failure is raised after the rest.
61
+ #: () -> void
47
62
  def teardown
48
- failure = nil
63
+ failure = nil #: StandardError?
49
64
  @fibers.reverse_each do |fiber|
50
65
  fiber.resume if fiber.alive?
51
66
  rescue StandardError => e
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+ require "open3"
6
+ require "prism"
7
+ require "rbconfig"
8
+
9
+ # A tree's program under test: the `program` lambda of its
10
+ # nukadoko.config.rb, called once. A config with no program has none, and a
11
+ # run starts no world for it.
12
+ #
13
+ # The program goes with the code and the criteria with the config, so a
14
+ # `nuka run --base` tree can hold one side's config and the other side's
15
+ # program. The run writes that program beside the tree's config: the other
16
+ # side's config as nukadoko.tree-program.rb, or a ref's program file from
17
+ # before the program moved into the config as
18
+ # nukadoko.tree-legacy-program.rb. A project's own program file is refused,
19
+ # with the move to make. Boundary: reading the program; starting it is
20
+ # Program's.
21
+ module Nukadoko
22
+ module Engine
23
+ module ProjectProgram
24
+ LIB = File.expand_path("..", __dir__.to_s)
25
+ UPGRADING = 'CHANGELOG.md, under "The program moves into the config"'
26
+
27
+ # The program of the tree at `root`, whose config is already loaded.
28
+ #: (String) -> Hash[Symbol, untyped]
29
+ def self.read(root)
30
+ raise called_in_config unless Nukadoko.legacy_program.nil?
31
+
32
+ legacy_copy = File.join(root, TREE_LEGACY_PROGRAM)
33
+ return elsewhere(legacy_copy, legacy: true) || {} if File.exist?(legacy_copy)
34
+
35
+ tree_copy = File.join(root, TREE_PROGRAM)
36
+ return elsewhere(tree_copy, legacy: false) || {} if File.exist?(tree_copy)
37
+
38
+ legacy = File.join(root, LEGACY_PROGRAM)
39
+ raise moved if File.exist?(legacy)
40
+
41
+ called(Nukadoko.config_options[:program], CONFIG) || {}
42
+ end
43
+
44
+ # Nukadoko.program called from the config would describe a program
45
+ # nothing reads, so it is refused rather than left unstarted.
46
+ #: () -> Refusal
47
+ def self.called_in_config
48
+ Refusal.new("config-invalid",
49
+ "Invalid config at #{CONFIG}: Nukadoko.program is no longer read: the program under test is the program: lambda of Nukadoko.config now",
50
+ fix: "Move the options Nukadoko.program took into #{CONFIG} as program: -> { { ... } }; #{UPGRADING}, shows the move.")
51
+ end
52
+
53
+ #: () -> Refusal
54
+ def self.moved
55
+ Refusal.new("program-file-moved",
56
+ "#{LEGACY_PROGRAM} is no longer read: the program under test is part of #{CONFIG} now, " \
57
+ "so nukadoko does not start what #{LEGACY_PROGRAM} describes.",
58
+ fix: "Move what #{LEGACY_PROGRAM} describes into #{CONFIG} as its program, and delete #{LEGACY_PROGRAM}; #{UPGRADING}, shows the move.")
59
+ end
60
+
61
+ # What a config's `program` returns, or nil when it has none.
62
+ #: (untyped, String) -> Hash[Symbol, untyped]?
63
+ def self.called(program, name)
64
+ return nil if program.nil?
65
+ unless program.respond_to?(:call)
66
+ raise Refusal.new("config-invalid", "Invalid config at #{name}: program: must be a lambda that returns the program",
67
+ fix: "Write program in #{name} as program: -> { { serve: { command: ..., ready: ... } } }.")
68
+ end
69
+
70
+ value = program.call
71
+ return value if value.is_a?(Hash)
72
+
73
+ raise Refusal.new("config-invalid", "Invalid program at #{name}: the program lambda returned #{value.class}, not a Hash of the program's options",
74
+ fix: "Return the program's options as a Hash from program in #{name}.")
75
+ end
76
+
77
+ # The program `file` gives, read in a process of its own, since
78
+ # loading it here would replace this tree's config and fixtures: a
79
+ # config's program, or the options a legacy file passes to
80
+ # Nukadoko.program. Raises a Refusal naming the file when it does not
81
+ # load.
82
+ #: (String, legacy: bool) -> Hash[Symbol, untyped]?
83
+ def self.elsewhere(file, legacy:)
84
+ read = legacy ? "Nukadoko.legacy_program" : "Nukadoko::Engine::ProjectProgram.called(Nukadoko.config_options[:program], ARGV[1])"
85
+ script = "require \"nukadoko\"; load ARGV[0]; puts JSON.generate(#{read})"
86
+ out, err, status = Open3.capture3(RbConfig.ruby, "-I", LIB, "-rjson", "-e", script, file, File.basename(file), chdir: File.dirname(file))
87
+ unless status.success?
88
+ raise Refusal.new("config-invalid", "Could not load #{File.basename(file)}: #{err.lines.first.to_s.strip}",
89
+ fix: "Correct #{File.basename(file)} as the message says.")
90
+ end
91
+
92
+ JSON.parse(out, symbolize_names: true)
93
+ end
94
+
95
+ # The text of a config with its program lambda cut out, so a change to
96
+ # the program alone leaves the config's mapping as it was: the
97
+ # program goes with the code, and a `--base` run compares only what
98
+ # decides how the criteria are read. A config with no program, or
99
+ # with more than one `program:` key, is read whole.
100
+ #: (String) -> String
101
+ def self.mapping_text(text)
102
+ found = [] #: Array[Prism::Node]
103
+ walk = lambda do |node|
104
+ key = node.key if node.is_a?(Prism::AssocNode)
105
+ if key.is_a?(Prism::SymbolNode) && key.unescaped == "program"
106
+ found << node.value
107
+ end
108
+ node.compact_child_nodes.each { walk.(_1) }
109
+ end
110
+ walk.(Prism.parse(text).value)
111
+ return text unless found.size == 1
112
+
113
+ location = found.first.location
114
+ bytes = text.b
115
+ "#{bytes.byteslice(0, location.start_offset)}#{bytes.byteslice(location.end_offset..)}".force_encoding(text.encoding)
116
+ end
117
+ end
118
+ end
119
+ end