nukadoko 0.2.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 (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +126 -0
  3. data/README.ja.md +5 -3
  4. data/README.md +5 -3
  5. data/lib/nukadoko/aria_snapshot.rb +4 -4
  6. data/lib/nukadoko/base.rb +49 -29
  7. data/lib/nukadoko/check.rb +4 -4
  8. data/lib/nukadoko/claim_check.rb +2 -2
  9. data/lib/nukadoko/claim_rules.rb +1 -1
  10. data/lib/nukadoko/claim_sides.rb +469 -0
  11. data/lib/nukadoko/claims.rb +182 -20
  12. data/lib/nukadoko/cli.rb +16 -7
  13. data/lib/nukadoko/codes.json +20 -15
  14. data/lib/nukadoko/criteria.rb +1 -1
  15. data/lib/nukadoko/env_check.rb +1 -3
  16. data/lib/nukadoko/fixtures.rb +53 -21
  17. data/lib/nukadoko/init.rb +0 -1
  18. data/lib/nukadoko/js_json.rb +274 -0
  19. data/lib/nukadoko/js_pattern.rb +531 -0
  20. data/lib/nukadoko/parts.rb +1 -1
  21. data/lib/nukadoko/pattern.rb +62 -2
  22. data/lib/nukadoko/poll.rb +5 -3
  23. data/lib/nukadoko/program.rb +5 -5
  24. data/lib/nukadoko/project_files.rb +17 -0
  25. data/lib/nukadoko/project_program.rb +119 -0
  26. data/lib/nukadoko/run.rb +47 -17
  27. data/lib/nukadoko/run_record.rb +25 -22
  28. data/lib/nukadoko/schema.rb +60 -18
  29. data/lib/nukadoko/secrets.rb +20 -4
  30. data/lib/nukadoko/spaces.rb +28 -0
  31. data/lib/nukadoko/step.rb +7 -1
  32. data/lib/nukadoko/text_fit.rb +1 -1
  33. data/lib/nukadoko/tree_passes.rb +6 -3
  34. data/lib/nukadoko/version.rb +1 -1
  35. data/lib/nukadoko.rb +24 -2
  36. data/llms.txt +12 -11
  37. data/sig/generated/nukadoko/aria_snapshot.rbs +2 -2
  38. data/sig/generated/nukadoko/base.rbs +24 -18
  39. data/sig/generated/nukadoko/check.rbs +1 -1
  40. data/sig/generated/nukadoko/claim_sides.rbs +207 -0
  41. data/sig/generated/nukadoko/claims.rbs +46 -3
  42. data/sig/generated/nukadoko/cli.rbs +1 -1
  43. data/sig/generated/nukadoko/env_check.rbs +0 -4
  44. data/sig/generated/nukadoko/fixtures.rbs +25 -2
  45. data/sig/generated/nukadoko/init.rbs +0 -2
  46. data/sig/generated/nukadoko/js_json.rbs +109 -0
  47. data/sig/generated/nukadoko/js_pattern.rbs +194 -0
  48. data/sig/generated/nukadoko/pattern.rbs +34 -0
  49. data/sig/generated/nukadoko/poll.rbs +3 -2
  50. data/sig/generated/nukadoko/program.rbs +1 -1
  51. data/sig/generated/nukadoko/project_files.rbs +19 -0
  52. data/sig/generated/nukadoko/project_program.rbs +55 -0
  53. data/sig/generated/nukadoko/run.rbs +10 -3
  54. data/sig/generated/nukadoko/run_record.rbs +5 -4
  55. data/sig/generated/nukadoko/schema.rbs +22 -4
  56. data/sig/generated/nukadoko/secrets.rbs +17 -2
  57. data/sig/generated/nukadoko/spaces.rbs +31 -0
  58. data/sig/generated/nukadoko/tree_passes.rbs +2 -2
  59. data/sig/generated/nukadoko.rbs +11 -0
  60. data/skills/nukadoko/SKILL.md +2 -2
  61. data/skills/nukadoko/references/diagnosing.md +1 -1
  62. data/skills/nukadoko/references/examples/http-api.md +19 -16
  63. data/skills/nukadoko/references/fixtures.md +1 -1
  64. data/skills/nukadoko/references/perturbed-worlds.md +8 -6
  65. data/skills/nukadoko/references/program.md +20 -20
  66. data/skills/nukadoko/references/running.md +2 -2
  67. data/skills/nukadoko/references/scenario-rules.md +2 -2
  68. data/skills/nukadoko/references/verdicts.md +3 -3
  69. data/skills/nukadoko/references/writing-steps.md +1 -1
  70. data/skills/nukadoko-verification/SKILL.md +6 -6
  71. metadata +13 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 469ec78d0a9894519a020a4cc29e409a15977d3241b8b2d90563caed7d3b71e2
4
- data.tar.gz: a69b9814e47df79f92c0c435e48bdb5b90a97188b2421a265f891ebd6a1f2aa3
3
+ metadata.gz: 0efeb0fd04363a9a701f5b2f80d6d93eb751c73c40d407f759fcf7429c4c2d75
4
+ data.tar.gz: dbbe4722b0d2f7c00a23bcd63af502bd0e9567f431569c98f767cbbf6a6392de
5
5
  SHA512:
6
- metadata.gz: 9b1c6850c722e5186e8d6f227ef67c9f68368cc61d89706b67ea772a97e77881db3f529a04853b4c65d3eae8ddbccd645bd762f137519ad50369e060e3e579df
7
- data.tar.gz: a667506585b11ce283a364bf910897ab38fe6b274a7979744ab2a970d5f72efbc01ff0502de9bc8aedf2225dec87d6be9f53c5596bca8c005b44ed5c311af614
6
+ metadata.gz: c0c925c96e0dddd666eba72f2ac9f8a430b7143ee97af6a3d2b4055e18b3076025f8b634352468e0a49aa1d428030558f632c4a708de2a73658b308f958ea6e4
7
+ data.tar.gz: 28d4a9a558783d882847b38ef3752cb0e58f7f40dadd27e8a6585d9b5df2c555edb37bfca96f7741aef9e6705558723ef36d4ad83493089ac1e58c09be87d47d
data/CHANGELOG.md CHANGED
@@ -6,6 +6,132 @@ notes come from the section below whose heading starts with `## [<version>]`.
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.3.0] - 2026-11-16
10
+
11
+ ### Changed
12
+
13
+ - **The program moves into the config.** `nukadoko.program.rb` and
14
+ `Nukadoko.program` are gone: the program under test is `program:` in
15
+ `Nukadoko.config`, a lambda that returns the options
16
+ `Nukadoko.program` took, as the TypeScript engine's `program` is. A
17
+ `nuka run --base` compares the config with that lambda cut out, so a
18
+ change to how the program starts is no change to any criterion, and an
19
+ environment's `conditions` and `defaultEnvironments` are compared as
20
+ before. A project root that still holds `nukadoko.program.rb` stops
21
+ every command with `program-file-moved` (exit code 2):
22
+
23
+ ```text
24
+ refused program-file-moved - nukadoko.program.rb is no longer read: the program under test is part of nukadoko.config.rb now, so nukadoko does not start what nukadoko.program.rb describes.
25
+ fix: Move what nukadoko.program.rb describes into nukadoko.config.rb as its program, and delete nukadoko.program.rb; CHANGELOG.md, under "The program moves into the config", shows the move.
26
+ ```
27
+
28
+ `Nukadoko.program` called from the config is refused with
29
+ `config-invalid`. To upgrade, move the options into the config and
30
+ delete `nukadoko.program.rb`. Before:
31
+
32
+ ```ruby
33
+ # nukadoko.program.rb
34
+ Nukadoko.program serve: { command: "bin/rails server -p {port}", ready: "/up" }
35
+ ```
36
+
37
+ After:
38
+
39
+ ```ruby
40
+ # nukadoko.config.rb
41
+ Nukadoko.config(
42
+ program: -> { { serve: { command: "bin/rails server -p {port}", ready: "/up" } } },
43
+ )
44
+ ```
45
+
46
+ Then run `nuka check` until it is green. A `--base` ref from before
47
+ the move needs nothing: the run still reads that ref's program file for
48
+ its base world.
49
+
50
+ ### Fixed
51
+
52
+ - An `is-a "url"` claim takes what the TypeScript engine's WHATWG URL
53
+ parser takes: a host or a path in any script (`http://例え.jp/`), `{`, `}`,
54
+ and `|` in a path or a fragment, and extra slashes after the scheme. It
55
+ refuses a port past 65535 and a label that decodes to no letter, which
56
+ Ruby's URI took. Both engines now judge the same texts the same way.
57
+ - The `text`, `lines`, and `comma-list` views, an `is-a` kind name, and a
58
+ blank expected text trim the spaces JavaScript's trim takes, as the
59
+ TypeScript engine does: a no-break space, an ideographic space, a byte
60
+ order mark, and a line separator. They no longer take a NUL or a next
61
+ line (U+0085). A `comma-list` keeps the empty item after a comma at the
62
+ end, and a docstring that ends in an empty line keeps it in a criterion.
63
+ - A screenshot nukadoko takes (a scenario's `final.png`, a judged view's
64
+ `page.png`, and a `nuka run --step` pause's `page.png`) covers in black
65
+ each element that shows a loaded secret, as text or as a field's value,
66
+ as the TypeScript engine does. When those elements are not found within
67
+ 5 seconds, no screenshot is written. A judged view and a pause show the
68
+ caret.
69
+ - A `--base` run's base and old passes read their env files from head's
70
+ checkout, which holds the ones git ignores, and redact with head's
71
+ secrets as well as the tree's own, as the TypeScript engine does.
72
+ - A `matches` claim reads its pattern as JavaScript's `RegExp` does, as
73
+ the TypeScript engine does, and no longer as a Ruby pattern: `^` and `$`
74
+ match only at the ends of the text, `\s` takes every Unicode space, an
75
+ escaped letter with no meaning is the letter (`\A`, `\h`), a `{` that
76
+ starts no count is a `{`, `[^]` takes any character, a backreference to
77
+ a group that did not match matches nothing, `.` takes one UTF-16 unit,
78
+ and a pattern JavaScript refuses (`(?i)`, `a*+`, `(?#...)`) does not
79
+ build, with V8's reason. A lookbehind Ruby cannot run (one of unbounded
80
+ length) is refused with Ruby's reason.
81
+ - A `has-item` claim on an array whose items are all `false` or `null`,
82
+ none of them of the expected value's type, breaks with the reason that
83
+ no item is of that type, as the TypeScript engine says, instead of
84
+ simply breaking.
85
+ - A step pattern reads its built-in parameter types as the JavaScript
86
+ cucumber-expressions package does, as the TypeScript engine does. A
87
+ `{word}` ends at any Unicode space, so a no-break space no longer joins
88
+ two words. A `{float}` or `{double}` takes the JavaScript pattern and
89
+ reads as `parseFloat` reads it (`1E3` is 1000, a lone sign is NaN). An
90
+ `{int}` past 2**53 is the nearest double, and a `{bigdecimal}` is the
91
+ text as written, not a `BigDecimal`.
92
+ - Args and returns that do not fit their schema fail with zod's words, as
93
+ the TypeScript engine's do: `args validation failed: n: Invalid input:
94
+ expected number, received NaN`. Args refuse a key the schema does not
95
+ declare (`(root): Unrecognized key: "x"`), an `unknown` field may be
96
+ missing, an `integer` past 2**53 is too big, and a step with `returns`
97
+ whose `run` gave back nil says so. An arg that is NaN no longer stops the
98
+ run while its step record is written: JSON has no NaN, so the record
99
+ holds null, as JavaScript writes it.
100
+ - A claim reads its value as the TypeScript engine reads it. A dotted
101
+ `actual` (`body.user.name`) is a path into what observe returned, not
102
+ one key, and a path that finds nothing breaks the claim with where the
103
+ walk stopped (`observe:value.user.name is undefined, since observe:value
104
+ has nothing at user`) instead of failing the step. A view of a value that
105
+ is no text, and an `at` with nothing there, break the claim with the
106
+ TypeScript engine's reason. A claim that compares a value with itself, or
107
+ with one observe received or computed as a boolean, breaks as there.
108
+ - A claim judges a plain JSON copy of each side, as the TypeScript engine
109
+ does: a whole float is the integer JavaScript holds (`2500.0` reads and
110
+ shows as `2500`), an object lists its index keys first, and NaN,
111
+ Infinity, an integer past `Number.MAX_SAFE_INTEGER`, a value of another
112
+ class, a value that holds itself, or one nested past 256 levels leaves
113
+ the claim unjudged with the reason. A step with an unjudged claim fails
114
+ as `claim_unjudged`, as there.
115
+ - `nuka run --no-check` refuses to start when a step's declaration is one
116
+ the TypeScript engine's `defineStep` refuses as its file loads, naming
117
+ the first such file, instead of leaving the step out.
118
+ - An `each` claim reads its array and each item's paths as the TypeScript
119
+ engine does, with the same reasons when a read finds nothing. It breaks
120
+ when a declaration's expected value is one observe received, and when a
121
+ `where` clause implies an item claim whatever the data (`greater-than 10`
122
+ then `at-least 5`), as there. A poll waits on the outcome the record holds.
123
+ - A json view reads its text as JavaScript's `JSON.parse` does and, when
124
+ the text is no JSON, gives V8's reason (`Unexpected token 'a', "a" is not
125
+ valid JSON`, `Expected ',' or '}' after property value in JSON at position
126
+ 6 (line 1 column 7)`) instead of Ruby's. A text Ruby's parser takes and
127
+ JavaScript's refuses, such as one with a comment, is refused.
128
+ - A refusal or a claim reason quotes a value as JavaScript's
129
+ `JSON.stringify` writes it: a literal written `2.0` reads `2`, and
130
+ `1e20` reads `100000000000000000000`, as in the TypeScript engine's
131
+ text. A json view of a text that holds an unpaired surrogate escape
132
+ (`"\ud800"`), which JavaScript reads and a Ruby string cannot hold, is
133
+ refused with that reason instead of the parser's.
134
+
9
135
  ## [0.2.0] - 2026-11-15
10
136
 
11
137
  ### Added
data/README.ja.md CHANGED
@@ -98,7 +98,7 @@ gem が入れる実行ファイルには、`bundle binstubs <gem>` が `bin/<nam
98
98
  `--then` とキャプチャ 1 つと一緒に使うと、claim がその 2 つを比べるので、ステップは書き直さずに使えます。
99
99
  - ブラウザの起動設定は `nukadoko.config.rb` に `Nukadoko.config(browser: { executablePath: "..." })` と書きます。`page` を使うステップは、シナリオごとに自分のブラウザコンテキストを持ちます。ステップの記録には、ページが送ったリクエストとステップのトレースが残ります。
100
100
  - `scratch` を使うステップは `scratch.dir` で新しいディレクトリを得ます。シナリオが終わると消えます。`scratch.copy(source)` は、プロジェクトのファイルかディレクトリの写しを入れた新しいディレクトリを返します。`args:` キーワードを書かない本体は、args なしで呼ばれます。`Nukadoko.run_command(command, args, cwd:, env:, input:, timeout:, redact:)` はプログラムを実行し、`{ exitCode:, signal:, stdout:, stderr: }` を返します。ステップの記録には実行したプロセスが残り、`redact:` に渡した値は `{{secret.NAME}}` になります。
101
- - `Nukadoko.config(envFiles: [".env"])` は env ファイルを `env` フィクスチャに読み込みます。`nukadoko.program.rb` の環境は、その後ろに自分の `envFiles:` を足せます。git が追跡していないファイルと HEAD と違うファイルは秘密の出どころです。その 4 文字以上の値は、ステップの記録、`http.jsonl`、`nuka do` の出力で `{{secret.NAME}}` に置き換わります。ステップが表示したものは、秘密を伏せて stderr に出ます。`secrets: { public: [...], redact: [...] }` で、そのようなキーを平文のままにしたり、追跡しているファイルのキーを伏せたりできます。`nuka check` は、無いファイル、読み飛ばした行、どのファイルにもない `secrets` のキーを警告し、値は表示しません。
101
+ - `Nukadoko.config(envFiles: [".env"])` は env ファイルを `env` フィクスチャに読み込みます。config の `program:` の環境は、その後ろに自分の `envFiles:` を足せます。git が追跡していないファイルと HEAD と違うファイルは秘密の出どころです。その 4 文字以上の値は、ステップの記録、`http.jsonl`、`nuka do` の出力で `{{secret.NAME}}` に置き換わります。ステップが表示したものは、秘密を伏せて stderr に出ます。`secrets: { public: [...], redact: [...] }` で、そのようなキーを平文のままにしたり、追跡しているファイルのキーを伏せたりできます。`nuka check` は、無いファイル、読み飛ばした行、どのファイルにもない `secrets` のキーを警告し、値は表示しません。
102
102
  - プロジェクトのフィクスチャは `nukadoko.config.rb` に書きます。ブロックは値を `use` に渡し、その後ろが後片付けです。例: `Nukadoko.fixture(:store) { |use| use.(Store.new) }`。`scope: :process` にすると、環境ごとの実行全体で 1 つの値を使います。
103
103
  - `parts [CreateTodo, CompleteById]` で、ステップが呼べるステップを宣言します。`call.(CreateTodo, title: args.title)` で 1 つを実行し、args と返り値を確かめます。パターンのない part も書けます。呼び出しは、呼んだステップの記録の `calls` に残ります。`nuka check` は、`parts` の循環と、`mutates false` なのに `mutates true` の part を持つステップを報告します。
104
104
  - `poll timeout_ms: 3000` は、すべての claim が 2 回続けて成り立つか時間切れになるまで、100 ミリ秒おきに `observe` を読み直します。`settled:` には、一度なったら変わらない状態を書きます。2 回続けてその状態なら、壊れた claim を待つのをやめます。読んだ結果はすべてステップの記録に残ります。
@@ -107,10 +107,12 @@ gem が入れる実行ファイルには、`bundle binstubs <gem>` が `bin/<nam
107
107
  - 契約が宣言していない返り値のキーは、zod のオブジェクトと同じく捨てられます。ステップの記録の `returns_dropped` に、捨てたキーが残ります。
108
108
  - `nuka sig` は `sig/nukadoko/steps.rbs` を書きます。Steep を使うと、宣言していない args のキーを読む本体や、形のちがう値を返す本体がエラーになります。
109
109
 
110
- テストするプログラムは `nukadoko.program.rb` に書きます。
110
+ テスト対象のプログラムは `nukadoko.config.rb` の `program:` に書きます。プログラムの起動のしかたを返すラムダです。
111
111
 
112
112
  ```ruby
113
- Nukadoko.program serve: { command: "bin/rails server -p {port}", ready: "/up" }
113
+ Nukadoko.config(
114
+ program: -> { { serve: { command: "bin/rails server -p {port}", ready: "/up" } } },
115
+ )
114
116
  ```
115
117
 
116
118
  ## 開発
data/README.md CHANGED
@@ -53,7 +53,7 @@ end
53
53
  - `nuka scaffold <name> [--pattern <p>] [--then] [--description <text>] [--replace]` writes `features/steps/<name>.rb`, a step whose body raises `not implemented` until someone writes it. `nuka scaffold --feature <path>` writes one for each line no step matches, its pattern read from the line. `--command <entry point>` writes a step that runs the program under Ruby in a scratch directory and returns `exitCode`, `stdout`, and `stderr` (`--cwd <key>` runs it in the directory an args key holds, and `--argv <key>` splits one capture into its arguments). For an executable a gem installs, `bundle binstubs <gem>` writes `bin/<name>`, an entry point `--command` can run. `--request "<METHOD> <path>"` writes a step that sends one request and returns `status` and `body` (`--body <key>` sends that args key). `--from <step>.<key>` fills an args key from an earlier step's returns; with `--then` and one capture, the claim compares the two, so the step needs no edit.
54
54
  - Browser launch options go in `nukadoko.config.rb`, as `Nukadoko.config(browser: { executablePath: "..." })`. A step that names `page` gets a browser context of its own for each scenario; each step record holds the requests the page sent and a trace of the step.
55
55
  - A step that names `scratch` gets `scratch.dir`, a fresh directory that is removed when the scenario ends, and `scratch.copy(source)`, one that holds a copy of a file or directory of the project. A body that names no `args:` keyword is called without them. `Nukadoko.run_command(command, args, cwd:, env:, input:, timeout:, redact:)` runs a program and returns `{ exitCode:, signal:, stdout:, stderr: }`; the step record lists each run, with each `redact:` value the step holds written as `{{secret.NAME}}`.
56
- - `Nukadoko.config(envFiles: [".env"])` loads env files into the `env` fixture, and an environment in `nukadoko.program.rb` adds its own `envFiles:` after them. A file git does not track, or one that differs from HEAD, is a secret source: each of its values four characters or longer is written as `{{secret.NAME}}` in every step record, `http.jsonl`, and what `nuka do` prints, and what a step prints reaches stderr with them redacted. `secrets: { public: [...], redact: [...] }` keeps one such key plain, or redacts one key of a tracked file. `nuka check` warns about a missing file, a line it skipped, and a `secrets` key no file defines, and never prints a value.
56
+ - `Nukadoko.config(envFiles: [".env"])` loads env files into the `env` fixture, and an environment of the config's `program:` adds its own `envFiles:` after them. A file git does not track, or one that differs from HEAD, is a secret source: each of its values four characters or longer is written as `{{secret.NAME}}` in every step record, `http.jsonl`, and what `nuka do` prints, and what a step prints reaches stderr with them redacted. `secrets: { public: [...], redact: [...] }` keeps one such key plain, or redacts one key of a tracked file. `nuka check` warns about a missing file, a line it skipped, and a `secrets` key no file defines, and never prints a value.
57
57
  - A project fixture goes in `nukadoko.config.rb`. The block calls `use` with the value, and what follows is the teardown: `Nukadoko.fixture(:store) { |use| use.(Store.new) }`. `scope: :process` keeps one value for the whole run of an environment.
58
58
  - `parts [CreateTodo, CompleteById]` declares the steps a step may run, and `call.(CreateTodo, title: args.title)` runs one, with its args and returns checked. A part needs no pattern. Each call is kept under `calls` in the calling step's record. `nuka check` reports a cycle in `parts` and a `mutates false` step whose part declares `mutates true`.
59
59
  - `poll timeout_ms: 3000` reads `observe` again, 100 ms apart, until every claim holds on two readings in a row or the time runs out. `settled:` names a state that cannot change once reached, so two settled readings in a row stop the wait for a claim that broke. Every reading is kept in the step record.
@@ -62,10 +62,12 @@ end
62
62
  - A returned key the contract does not declare is dropped, as a zod object drops it, and the step record names it under `returns_dropped`.
63
63
  - `nuka sig` writes `sig/nukadoko/steps.rbs`. With Steep, a body that reads an undeclared args key or returns the wrong shape is an error.
64
64
 
65
- The program under test is described in `nukadoko.program.rb`:
65
+ The program under test is described by `program:` in `nukadoko.config.rb`, a lambda that returns how the program starts:
66
66
 
67
67
  ```ruby
68
- Nukadoko.program serve: { command: "bin/rails server -p {port}", ready: "/up" }
68
+ Nukadoko.config(
69
+ program: -> { { serve: { command: "bin/rails server -p {port}", ready: "/up" } } },
70
+ )
69
71
  ```
70
72
 
71
73
  ## Development
@@ -36,8 +36,8 @@ module Nukadoko
36
36
  module AriaSnapshot
37
37
  FRAGMENT = "fragment"
38
38
  # JavaScript's \s, which the TypeScript engine's texts are cut by.
39
- SPACE = "\\s\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff"
40
- WS = /[#{SPACE}]/
39
+ SPACE = Spaces::CLASS #: String
40
+ WS = Spaces::SPACE #: Regexp
41
41
  # YAML's indicators: a plain scalar may not start with one, since
42
42
  # YAML reads it as structure.
43
43
  PLAIN_START_REFUSED = [",", "[", "]", "{", "}", "&", "*", "!", "|", ">", "%", "@", "`"].freeze
@@ -66,10 +66,10 @@ module Nukadoko
66
66
  def problem_at(line, message) = Problem.new("line #{line} #{message}")
67
67
 
68
68
  #: (String) -> String
69
- def trim(text) = text.sub(/\A#{WS}+/, "").sub(/#{WS}+\z/, "")
69
+ def trim(text) = Spaces.trim(text)
70
70
 
71
71
  #: (String) -> String
72
- def trim_end(text) = text.sub(/#{WS}+\z/, "")
72
+ def trim_end(text) = Spaces.trim_end(text)
73
73
 
74
74
  #: (String?) -> bool
75
75
  def space?(char) = !char.nil? && char.match?(/\A#{WS}\z/)
data/lib/nukadoko/base.rb CHANGED
@@ -13,14 +13,19 @@ require "rbconfig"
13
13
  #
14
14
  # - base: the ref's code with head's features/ and config, so a new or
15
15
  # changed criterion runs where its behaviour should be absent. The ref's
16
- # program file starts it, or head's when the ref has none.
16
+ # program starts it, or head's when the ref has none: the program goes
17
+ # with the code, so the run writes the ref's config beside head's as
18
+ # nukadoko.tree-program.rb (or the ref's program file from before the
19
+ # program moved into the config as nukadoko.tree-legacy-program.rb).
17
20
  # - old: head's working tree with the ref's features/ and config, so an
18
21
  # unchanged criterion over a changed mapping runs under the steps it was
19
- # approved with.
22
+ # approved with. Head's config goes beside it as nukadoko.tree-program.rb,
23
+ # so head's program starts it.
20
24
  #
21
25
  # The mapping is every file under features/ but a .feature file, the config
22
- # file's bytes, and the program file's conditions and defaultEnvironments,
23
- # which decide where a criterion is required. Every git command runs with
26
+ # file's bytes with its program lambda cut out, and the program's
27
+ # conditions and defaultEnvironments, which decide where a criterion is
28
+ # required. Every git command runs with
24
29
  # hooks off, since a checkout's hook is the project's code and not the
25
30
  # run's. Both trees are removed when the run ends.
26
31
  module Nukadoko
@@ -28,8 +33,6 @@ module Nukadoko
28
33
  class Base
29
34
  NO_HOOKS = ["-c", "core.hooksPath=/dev/null"].freeze
30
35
  FEATURES = "features"
31
- CONFIG = "nukadoko.config.rb"
32
- PROGRAM = "nukadoko.program.rb"
33
36
  STATE = ".nukadoko"
34
37
 
35
38
  attr_reader :ref #: String
@@ -133,7 +136,9 @@ module Nukadoko
133
136
 
134
137
  head_files = working_tree_files
135
138
  ref_files = Base.git(@base_top, "ls-files", "-z", strip: false).first.split("\0").reject(&:empty?)
136
- ref_program = File.exist?(File.join(base_project, PROGRAM)) ? read_program(base_project) : nil
139
+ ref_program = read_program(base_project)
140
+ ref_config = File.join(base_project, CONFIG)
141
+ ref_config_text = File.exist?(ref_config) ? File.read(ref_config) : nil
137
142
  @mapping = compare_mapping(head_files, ref_files, base_project, head_program, ref_program)
138
143
 
139
144
  wanted = selected.map { @changes[_1] }
@@ -141,9 +146,9 @@ module Nukadoko
141
146
  old_needed = wanted.any? { _1&.dig(:change) == "unchanged" && (@mapping[:changed] || _1[:retagged] || _1[:renamed_from]) }
142
147
  write_old_tree(head_files, ref_files) if old_needed
143
148
  if base_needed
144
- overlay_base_tree(head_files, ref_files)
149
+ overlay_base_tree(head_files, ref_files, ref_program && ref_config_text)
145
150
  @base_root = base_project
146
- @program_from_head = !File.exist?(File.join(@base_top, in_repo(PROGRAM))) && File.exist?(File.join(@project, PROGRAM))
151
+ @program_from_head = ref_program.nil? && !(head_program || {}).empty?
147
152
  else
148
153
  @worktree = false
149
154
  Base.git(@top, "worktree", "remove", "--force", "--force", @base_top)
@@ -153,7 +158,7 @@ module Nukadoko
153
158
  end
154
159
 
155
160
  # The environments the old world runs in, with the conditions the ref's
156
- # program file gives each; nil when the ref has no program file, so
161
+ # program gives each; nil when the ref has no program, so
157
162
  # head's environments are the old world's too.
158
163
  #: (Hash[Symbol, untyped]?, Array[String]) -> Array[Environment]?
159
164
  def old_environments_of(program, envs)
@@ -204,18 +209,21 @@ module Nukadoko
204
209
  .to_h { [_1.delete_prefix("#{dir}/"), hash_of(File.join(top, _1))] }
205
210
  end
206
211
 
207
- # The ref's program file read in a process of its own, since loading
208
- # it here would replace head's: its conditions and defaultEnvironments.
209
- #: (String) -> Hash[Symbol, untyped]
212
+ # The ref's program: its program file from before the program moved
213
+ # into the config, or its config's program, read in a process of its
214
+ # own (ProjectProgram.elsewhere). nil when the ref has neither.
215
+ #: (String) -> Hash[Symbol, untyped]?
210
216
  def read_program(root)
211
- script = 'require "nukadoko"; load ARGV[0]; o = Nukadoko.program_options; ' \
212
- 'puts JSON.generate(environments: (o[:environments] || {}).to_h { |k, v| [k, { conditions: v[:conditions] }] }, defaultEnvironments: o[:defaultEnvironments])'
213
- out, err, status = Open3.capture3(RbConfig.ruby, "-I", File.expand_path("..", __dir__.to_s), "-rjson", "-e", script, File.join(root, PROGRAM), chdir: root)
214
- return { problem: err.lines.first.to_s.strip } unless status.success?
215
-
216
- program = JSON.parse(out, symbolize_names: true)
217
- program[:environments] = program[:environments].transform_keys(&:to_s)
218
- program
217
+ legacy = File.join(root, LEGACY_PROGRAM)
218
+ config = File.join(root, CONFIG)
219
+ program = if File.exist?(legacy) then ProjectProgram.elsewhere(legacy, legacy: true) || {}
220
+ elsif File.exist?(config) then ProjectProgram.elsewhere(config, legacy: false)
221
+ end
222
+ return nil if program.nil?
223
+
224
+ { environments: (program[:environments] || {}).transform_keys(&:to_s), defaultEnvironments: program[:defaultEnvironments] }
225
+ rescue Refusal => e
226
+ { problem: e.message }
219
227
  end
220
228
 
221
229
  #: (Hash[Symbol, untyped]?) -> Hash[Symbol, untyped]
@@ -229,7 +237,7 @@ module Nukadoko
229
237
  head = mapping_files(head_files, @top)
230
238
  ref = mapping_files(ref_files, @base_top)
231
239
  files = (head.keys | ref.keys).select { head[_1] != ref[_1] }.sort
232
- config_hash = ->(root) { File.exist?(File.join(root, CONFIG)) ? hash_of(File.join(root, CONFIG)) : nil }
240
+ config_hash = ->(root) { File.exist?(File.join(root, CONFIG)) ? Digest::SHA256.hexdigest(ProjectProgram.mapping_text(File.read(File.join(root, CONFIG)))) : nil }
233
241
  config = config_hash.(@project) != config_hash.(base_project)
234
242
  after = program_mapping(head_program && { environments: (head_program[:environments] || {}).transform_keys(&:to_s), defaultEnvironments: head_program[:defaultEnvironments] })
235
243
  if ref_program&.dig(:problem)
@@ -252,22 +260,34 @@ module Nukadoko
252
260
  File.symlink?(from) ? File.symlink(File.readlink(from), to) : FileUtils.cp(from, to)
253
261
  end
254
262
 
255
- # The ref's checkout loses the ref's mapping and gains head's; the
256
- # ref's program file stays, or head's comes in when the ref has none.
257
- #: (Array[String], Array[String]) -> void
258
- def overlay_base_tree(head_files, ref_files)
263
+ # The ref's checkout loses the ref's mapping and gains head's. The
264
+ # ref's program stays: its program file under the tree's own name, or
265
+ # its config `ref_config` beside head's. When the ref has no program,
266
+ # head's config brings head's.
267
+ #: (Array[String], Array[String], String?) -> void
268
+ def overlay_base_tree(head_files, ref_files, ref_config)
259
269
  ref_files.select { mapping?(_1) }.each { FileUtils.rm_f(File.join(@base_top, _1)) }
260
270
  head_files.select { mapping?(_1) }.each { copy(File.join(@top, _1), File.join(@base_top, _1)) }
261
- program = in_repo(PROGRAM)
262
- copy(File.join(@top, program), File.join(@base_top, program)) if !ref_files.include?(program) && head_files.include?(program)
271
+ legacy = File.join(@base_top, in_repo(LEGACY_PROGRAM))
272
+ if File.exist?(legacy)
273
+ FileUtils.mv(legacy, File.join(@base_top, in_repo(TREE_LEGACY_PROGRAM)))
274
+ elsif ref_config
275
+ File.write(File.join(@base_top, in_repo(TREE_PROGRAM)), ref_config)
276
+ end
263
277
  end
264
278
 
265
279
  # Head's working tree with the ref's mapping in place of its own,
266
- # written before the base checkout is overlaid.
280
+ # written before the base checkout is overlaid. Head's program stays:
281
+ # head's config goes beside the ref's, or a file that names no
282
+ # program when head has no config.
267
283
  #: (Array[String], Array[String]) -> void
268
284
  def write_old_tree(head_files, ref_files)
269
285
  head_files.reject { mapping?(_1) }.each { copy(File.join(@top, _1), File.join(@old_top, _1)) }
270
286
  ref_files.select { mapping?(_1) }.each { copy(File.join(@base_top, _1), File.join(@old_top, _1)) }
287
+ head_config = File.join(@project, CONFIG)
288
+ tree_program = File.join(@old_top, in_repo(TREE_PROGRAM))
289
+ FileUtils.mkdir_p(File.dirname(tree_program))
290
+ File.write(tree_program, File.exist?(head_config) ? File.read(head_config) : "# Head has no config, so no program.\n")
271
291
  end
272
292
  end
273
293
  end
@@ -44,7 +44,7 @@ module Nukadoko
44
44
  # `environments` is the selection a run without --env starts, for
45
45
  # condition-unmet; nil when --env named the selection. `problems` are
46
46
  # what the feature loader found that runs nothing.
47
- # `config` holds what was found in the config and the program file before
47
+ # `config` holds what was found in the config and its program before
48
48
  # any step loaded, such as an env file that is missing.
49
49
  #: (Array[singleton(Step)], Array[Scenario], ?features: bool, ?full: bool, ?conditions: Array[String]?, ?environments: Array[Environment]?, ?problems: Array[FeatureProblem], ?config: Array[Finding]) -> void
50
50
  def initialize(steps, scenarios, features: true, full: false, conditions: nil, environments: nil, problems: [], config: [])
@@ -342,7 +342,7 @@ module Nukadoko
342
342
  fixtures = Nukadoko.project_fixtures
343
343
  return if fixtures.empty?
344
344
 
345
- config = "nukadoko.config.rb"
345
+ config = CONFIG
346
346
  state = Hash.new(:white)
347
347
  visit = lambda do |name, stack|
348
348
  state[name] = :gray
@@ -467,7 +467,7 @@ module Nukadoko
467
467
  if name.nil?
468
468
  boolean = tag.match(/\A@with:([A-Za-z0-9_.-]+)=(true|false)\z/)
469
469
  message = if boolean
470
- "#{tag} names a boolean as text, which reads two ways: write #{boolean[2] == "true" ? "@with:" : "@without:"}#{boolean[1]}, which reads the boolean the program file declares"
470
+ "#{tag} names a boolean as text, which reads two ways: write #{boolean[2] == "true" ? "@with:" : "@without:"}#{boolean[1]}, which reads the boolean the program declares"
471
471
  else
472
472
  "#{tag} does not read as @with:<condition>, @with:<condition>=<value>, or @without:<condition>, so it names no condition"
473
473
  end
@@ -475,7 +475,7 @@ module Nukadoko
475
475
  elsif conditions && !conditions.include?(name)
476
476
  on, off = tag.start_with?("@without:") ? %w[off on] : %w[on off]
477
477
  findings << Finding.new("error", "condition-unknown", uri, line, nil,
478
- %(#{tag} names the condition "#{name}", and no environment in nukadoko.program.rb declares it under conditions. ) +
478
+ %(#{tag} names the condition "#{name}", and no environment in nukadoko.config.rb declares it under conditions. ) +
479
479
  "If #{name} was removed and its #{on} side kept, remove the tag, so the scenario holds everywhere; if its #{off} side was kept, delete the scenario.",
480
480
  Codes.fix("condition-unknown"), nil)
481
481
  end
@@ -131,9 +131,9 @@ module Nukadoko
131
131
  expected = entry[:expected]
132
132
  name = if expected.is_a?(Hash) then expected[:literal].is_a?(String) ? expected[:literal] : nil
133
133
  elsif [nil, "text"].include?(entry[:expected_view]) && step.args[expected]&.type == :string && captures[expected].is_a?(String)
134
- entry[:expected_view] == "text" ? captures[expected].rstrip : captures[expected]
134
+ entry[:expected_view] == "text" ? Spaces.trim_end(captures[expected]) : captures[expected]
135
135
  end
136
- name.nil? || name.strip.empty? ? nil : name
136
+ name.nil? || Spaces.blank?(name) ? nil : name
137
137
  end
138
138
 
139
139
  # The segments a claim reads before its view, as the run reads them:
@@ -60,7 +60,7 @@ module Nukadoko
60
60
 
61
61
  #: (untyped) -> String
62
62
  def describe(value)
63
- value.is_a?(Symbol) ? JSON.generate(value.to_s) : JSON.generate(value)
63
+ JsJson.generate(value)
64
64
  rescue StandardError
65
65
  value.inspect
66
66
  end