lawspec 0.21.0 → 0.23.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 (39) hide show
  1. package/CHANGELOG.md +322 -2
  2. package/README.md +3 -3
  3. package/beam-coverage.mjs +160 -0
  4. package/beam-doctor.mjs +306 -0
  5. package/bin/lawspec.mjs +150 -41
  6. package/build.json +359 -101
  7. package/compatibility.json +25 -0
  8. package/core.wasm +2 -2
  9. package/doctor.mjs +29 -2
  10. package/examples/native-payments/csharp/lawspec.json +51 -0
  11. package/examples/native-payments/csharp/src/Domain/Payments.cs +19 -0
  12. package/examples/native-payments/csharp/tests/Native/PaymentGenerators.cs +14 -0
  13. package/examples/native-payments/elixir/lawspec.json +149 -0
  14. package/examples/native-payments/elixir/lib/payments_domain.ex +54 -0
  15. package/examples/native-payments/elixir/test/support/payment_generators.ex +9 -0
  16. package/examples/native-payments/erlang/include/payments_domain.hrl +2 -0
  17. package/examples/native-payments/erlang/lawspec.json +148 -0
  18. package/examples/native-payments/erlang/src/payments_domain.erl +21 -0
  19. package/examples/native-payments/erlang/test/payment_generators.erl +10 -0
  20. package/examples/native-payments/fsharp/lawspec.json +167 -0
  21. package/examples/native-payments/fsharp/src/Native/Types/PaymentsDomain.fs +23 -0
  22. package/examples/native-payments/fsharp/tests/Native/PaymentGenerators.fs +11 -0
  23. package/examples/native-payments/gleam/lawspec.json +149 -0
  24. package/examples/native-payments/gleam/prepare.mjs +48 -0
  25. package/examples/native-payments/gleam/src/payments_domain.gleam +32 -0
  26. package/examples/native-payments/gleam/test/payment_generators.gleam +13 -0
  27. package/examples/specs/collections.lawspec +44 -0
  28. package/examples/specs/composition.lawspec +110 -0
  29. package/examples/specs/contracts.lawspec +124 -0
  30. package/examples/specs/generation.lawspec +11 -0
  31. package/examples/specs/records.lawspec +191 -0
  32. package/examples/specs/records_inner.lawspec +10 -0
  33. package/examples/specs/sequences.lawspec +86 -0
  34. package/failure-database.mjs +46 -0
  35. package/index.d.ts +45 -1
  36. package/native-examples.mjs +7 -2
  37. package/package.json +2 -2
  38. package/templates.mjs +134 -3
  39. package/test-command.mjs +188 -17
package/CHANGELOG.md CHANGED
@@ -6,6 +6,324 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.23.0] - 2026-10-12
10
+
11
+ ### Added
12
+
13
+ - F# and C# targets (`--target fsharp`, `--target csharp`) on .NET 10, with
14
+ xunit.v3 on Microsoft.Testing.Platform and `dotnet test`. F# gets a runtime
15
+ written for it: records and unions in LawSpec's order, exact integers,
16
+ persistent collections, `Result` for typed failures, abilities as
17
+ interfaces, `Task` for async, typed actors and supervisors, single-use
18
+ session ends, mailboxes, a virtual clock, remote evaluation and the secure
19
+ network on the shared wire format, and post-quantum crypto from .NET with
20
+ BouncyCastle where .NET lacks it. Properties run on FsCheck 3.4.0, driven
21
+ from the xUnit facts: LawSpec's refinement-directed generators are FsCheck
22
+ generators of shrink trees, so shrinks stay inside refinements, and FsCheck
23
+ runs, sizes, shrinks and replays (`LAWSPEC_SEED`) the cases; generator
24
+ bindings return FsCheck Arbitraries. Models, linearizability, scenarios and
25
+ the whole harness run around it. C# runs over it: the program's model is a generated F#
26
+ project, and adapters, entries, the embedding API and tests are C#, with
27
+ `LawSpec.CSharp` as the compatibility layer. Definitions whose values all
28
+ have native types run typed, over the program's own F# types with checked
29
+ .NET integers, and each spec handler is a native class with `Result`
30
+ try-forms and its state to read or restore (typed handlers keep it native).
31
+ Native bindings, `lawspec test`, `lawspec doctor`, the native payments
32
+ example and `lawspec-dev bench kafka` cover both targets. Every acceptance
33
+ suite passes on both, with its mutants rejected, and the tutorial lessons
34
+ have F# and C# implementations. Text in a Set or a KeyVal orders by code
35
+ point on .NET as everywhere: F#'s own string order (by UTF-16 unit) is
36
+ never observed, as items, keys, entries or a comparison. See the F# and C#
37
+ setup guides.
38
+ - The collections suite checks that text keys and items order by code point,
39
+ with a character outside the Basic Multilingual Plane beside a fullwidth
40
+ letter, on every target.
41
+ - `lawspec evidence` says which tested laws ran against stand-ins. It reads
42
+ an environment attestation, a versioned JSON document any tool may write
43
+ for one run: the environment, the run's id and times, and for each
44
+ dependency its endpoint, whether it was reached, and whether it was the
45
+ real dependency or a stand-in (with the stand-in's identity, version,
46
+ declared conformance level and optional history digest). A dependency is
47
+ matched by name to an ability whose production handler a law runs under
48
+ (`unit::Name` or `unit::ability::Name`) or to an adapter or bound native
49
+ function the law calls. Each such law whose tests ran gets a line, and
50
+ `--json` the fields `environment`, `environmentRun` and `against`; an
51
+ `ENVIRONMENT` section lists the dependencies. It is reporting only: no
52
+ status changes and no law means anything different.
53
+ - `lawspec evidence --attestation <file>` and `lawspec test --attestation
54
+ <file>`, or `attestation` in `lawspec.json`, name the attestation. `lawspec
55
+ test` keeps it with the run's results in `.lawspec/results`, so a later
56
+ `lawspec evidence` shows the attested run each law last passed in; a law it
57
+ touches runs again when its dependencies are wired differently.
58
+ - The compiler API takes an `attestation` in any request: the result's
59
+ `evidence` carries the fields above and `attestation` lists each dependency
60
+ with the obligations it touched. A malformed attestation, or an unknown
61
+ format or version, is a diagnostic with code `attestation`.
62
+ - Checked definitions and stateful spec handlers run as typed native code on
63
+ all eight targets wherever every value they touch has a typed form
64
+ (Bool, Unit, the bounded integers, Integer, Text, Bytes, List, Maybe,
65
+ Either, KeyVal, Set and data built from them); anything else runs on the
66
+ dynamic values, as before. A definition that performs an operation of
67
+ another ability, or calls one that does, runs typed too: it takes the
68
+ handler context first and performs through the handler in scope, so
69
+ LawTwin's Kafka client handlers (`kafkaProducer`, `kafkaConsumer`,
70
+ `kafkaTransactions`) run typed, and 829 of the contract's 865 definitions
71
+ do (only the IntSize and UIntPtr helpers stay dynamic). Python represents
72
+ data as tuples, lists as shared cons cells that know their length and
73
+ KeyVal and Set as persistent AVL trees, and turns self tail calls into
74
+ loops; JavaScript and TypeScript use classes with generated TypeScript
75
+ types, and a number for every integer that fits a safe integer, a BigInt
76
+ only beyond; Haskell uses strict data types, `Data.Map.Strict` and
77
+ `Data.Set`, and threads a handler's state through an `MVar`. Definitions that `fail with E`, `raise` and `prelude.attempt`, field
78
+ access and record update, bounded arithmetic, recursion on integers and
79
+ the sequence helpers run typed, and so does a spec handler of several
80
+ abilities with a computed start; a typed failure is the same panic or
81
+ exception the dynamic code raises, so each catches the other's.
82
+ `lawspec-dev bench <name> --typed` lists what stays dynamic, and why. Lists are
83
+ persistent cons lists that share their tails, KeyVal and Set persistent AVL
84
+ trees in the portable order, Integer stays in a machine word until it
85
+ outgrows it, data with fields is shared by reference, and a definition's
86
+ self tail calls become a loop. A handler's state is never copied: an
87
+ operation costs what it changes. Values convert directly between the typed
88
+ forms and each target's native types at a handler's interface, and through
89
+ the dynamic value where typed and dynamic code meet.
90
+ - Embedding entry points: a stateful spec handler can be made outside any law
91
+ and called from native code, `Handler::start()` in Rust,
92
+ `Start<Handler>()` in Go, the no-argument constructor in Java and Kotlin,
93
+ `Handler()` in Python, `new Handler()` in JavaScript and TypeScript and
94
+ the handler's function over a symbol context in Haskell. Its operations apply one at a time, so it can
95
+ be shared between threads. Each operation that fails with `E` also has a
96
+ form that returns the failure as a value: `try_op` gives `Result<T, E>` in
97
+ Rust, `TryOp` gives `(T, error)` with a `*LawSpecRaised[E]` in Go, `tryOp`
98
+ an `Outcome<T, E>` in Java and Kotlin, `Right` or `Left` in Python,
99
+ JavaScript and TypeScript, and `Either E T` in Haskell. A handler's state
100
+ can be read and replaced (`state`, `set_state` and their typed forms;
101
+ `<h>Snapshot` and `<h>Restore` in Haskell), and the state's type is
102
+ public. The new guide "Embed a generated model" shows how.
103
+ - `lawspec-dev bench <name>` runs a benchmark of generated code on all eight
104
+ targets. `bench/kafka` drives LawTwin's Kafka reference model (a
105
+ copy under its MIT licence, re-expressed with failing operations, one
106
+ handler for three abilities, fields and bounded arithmetic) through its
107
+ `cluster` handler: a million produces over 100 partitions, a consumer
108
+ group of ten fetching and committing with a rebalance, and ten thousand
109
+ transactions. Every definition of the model and the handler run typed.
110
+ It runs the whole workload at 0.53 (Rust), 1.03 (Go), 0.78 (Java) and 0.93
111
+ (Kotlin) million operations a second, holding about 190 to 270 MiB for the
112
+ million records, where 0.21.0's generated Rust managed 9 operations a
113
+ second, and 1.5 GiB, at two thousand. Python runs it at 118 thousand
114
+ operations a second (it ran 1,100 on the dynamic values, at twenty
115
+ thousand), JavaScript at 942 thousand and TypeScript at 964 thousand
116
+ (7,300 before) and Haskell at 1.83 million (57 before, at two thousand).
117
+ `lawspec-dev bench kafka --deterministic` generates the contract on each
118
+ target with and without a cache, cold and warm, and by `lawspec-core`, and
119
+ fails unless every run gives the same bytes.
120
+ - Fields: `b.balance` reads a field of a single-constructor value, and
121
+ `b with balance = v, owner = o` copies it with fields replaced.
122
+ - Handlers of several abilities: `handler reference for Broker, Admin with
123
+ state s :: S start e is ... end` answers every operation of each from one
124
+ state. A law that runs it for one of its abilities runs it for the others
125
+ it uses, made once per case.
126
+ - Sets of handlers: `handlers reference is cluster, client end`. Every law of
127
+ an ability a set covers runs under all of its handlers at once,
128
+ `` Broker: law [reference] ``, and so does every law that uses one of its
129
+ abilities.
130
+ - `ability Broker with Cluster is ... end`: native handlers made with a
131
+ resource. Each case acquires one `Cluster` and makes every such native
132
+ handler with it, so native handlers of one case share it.
133
+ - Ability operations may fail: `produce :: Batch -> Int64 fails with
134
+ KafkaError`. Spec handler clauses `raise`; native handlers raise the
135
+ runtime's `Fail`. `prelude.attempt` works in definitions.
136
+ - Handler clauses: a start value may call definitions and the prelude
137
+ (`start prelude.keyValOf []`); a tail `match` or `if` may update the state
138
+ in its branches; `op by f` makes a clause of a definition that gives
139
+ `Pair result next`.
140
+ - Sequences: `++`, `prelude.slice`, `take`, `drop`, `at`, `range`, `octets`,
141
+ `bytesOf`, `utf8`, `textOf` and `join` over `Text`, `Bytes` and `List`.
142
+ - Bounded arithmetic: `prelude.wrapping`, `prelude.saturating` and
143
+ `prelude.bounded` put an integer result into the bounded type the context
144
+ wants.
145
+ - Recursion on integers: a recursive call may move an integer argument
146
+ strictly toward a bound.
147
+ - The totality checker knows what guards say about calls, fields and lengths,
148
+ that a call lies in its result type's range, that lengths are not negative,
149
+ and the bounds of `prelude.rem` and `prelude.quot`.
150
+ - A text refined by a regex, directly or through a checked definition, is
151
+ generated from texts the regex matches instead of filtered from any text,
152
+ also in laws that mix it with record or list inputs.
153
+ - On Python, a unit named like another unit's package (`shop.tax` beside
154
+ `shop.tax.rates`) is the package's `__init__.py`.
155
+ - New acceptance suites `records`, `sequences` and `composition`.
156
+ - Erlang, Elixir and Gleam run the language gaps on their dynamic path:
157
+ handlers of several abilities and sets of handlers, operations that `fail
158
+ with`, natives made with a resource, computed starts and state updated in
159
+ branches, field access and record update, the sequence, `Bytes` and `Text`
160
+ operations, bounded arithmetic, and regex-shaped generation of refined text
161
+ fields. A handler of several abilities gives one native interface per
162
+ ability over its one state; each operation that fails has a try form
163
+ (`{ok, V}` or `{error, E}`, a `Result` in Gleam), and a spec handler with
164
+ state has `<handler>_state` and `<handler>_set_state`. The `records`,
165
+ `sequences`, `composition` and `contracts` suites run on all eleven
166
+ targets, and a `beam-embedding` suite checks the embedding forms.
167
+ - Vetting on Rice's Tax's review page: `make assess` records every pending
168
+ row, `make tax` serves the page on which each comment, page, decision and
169
+ reference is judged beside the code or page it explains, and `make audit`
170
+ says what is left. `lawspec-dev tax` runs the Rice's Tax checkout named by
171
+ `RICESTAX_HOME`; see the new how-to guide, Vet the canonical material.
172
+
173
+ ### Changed
174
+
175
+ - `canon.yaml` names the canon version its vetting files were written with,
176
+ 0.2.1, which both `make canon` and Rice's Tax require. Code reshaped in
177
+ 0.20.1 for canon's Haskell grammar has its original forms back, since canon
178
+ 0.2.1 reads them.
179
+ - Definitions that are the same up to their names, such as an import's
180
+ copies, share one generated body: Rust aliases the copies' functions, and
181
+ Java and Go delegate to it. Go packages hold only the bodies they reach,
182
+ and the native entry points of typed definitions convert their values
183
+ directly. LawTwin's Kafka contract generates 23,606 lines of
184
+ `lawspec_definitions.rs` instead of 83,157, 27,439 lines of Java
185
+ definitions instead of 80,444, 27,309 of Kotlin and Java instead of 87,537,
186
+ and 37,900 lines of Go definitions over its four packages instead of
187
+ 113,462.
188
+ - Generated Rust builds its data schema once: `lawspec_schema::schema()`
189
+ returns `&'static ls::Schema`.
190
+ - `lawspec-dev ci` runs its steps in parallel: `--jobs N` at a time
191
+ (`LAWSPEC_CI_JOBS`, half the processors by default), and `--jobs 1` runs
192
+ them one at a time as before. Each step declares the steps it follows and
193
+ what it holds (its target's `.integration` environment, its acceptance
194
+ project, the repository's `.stack-work`, Stack's snapshot database, the
195
+ runtime crate's Cargo target, a timing class for real-time suites), so
196
+ parallel steps never share what one of them changes, and every step still
197
+ runs after a failure. A line is printed as each step finishes, a terminal
198
+ shows the running steps, the summary names failures in step order, and
199
+ Ctrl-C stops every running step's process group. Cached steps are skipped
200
+ exactly as before. See Contribute to LawSpec.
201
+ - The npm package's `prepack` replaces `README.md` and `CHANGELOG.md` in
202
+ `npm/` by rename, and leaves identical copies alone, so concurrent packs
203
+ never read a half-written copy.
204
+ - `package-smoke` allows half an hour for exporting the examples, and
205
+ `lawspec-dev ci` runs it alone.
206
+
207
+ ### Fixed
208
+
209
+ - The predicates npm test makes its scratch project under `.artifacts`, so a
210
+ `generate --check` running beside it in parallel CI no longer sees it.
211
+ - `tools/bootstrap-integration.mjs` installs the crypto packages the crypto
212
+ and distribution suites need (`@noble/post-quantum` from the JavaScript and
213
+ TypeScript locks, `cryptography` for Python), so a new `.integration`
214
+ passes them.
215
+
216
+ - The acceptance harness strips ANSI escape sequences before it decides
217
+ whether a mutant failed to compile, so a coloured toolchain (Gleam under
218
+ `FORCE_COLOR`) no longer hides a compile failure behind its colour codes.
219
+ - Generation is deterministic: the on-disk cache is keyed by the build of
220
+ the compiler that wrote it (its executable's path, size and time) as well
221
+ as its version, so a rebuilt compiler no longer reads another build's
222
+ entries.
223
+ - Generated names are stable under unrelated edits: the typed functions a
224
+ handler's clauses call, and its start, are named from the definitions and
225
+ the handler (with a suffix from the digest of what they name where one is
226
+ needed), not from their position among the others.
227
+ - Generated code builds without warnings, or with targeted allow attributes,
228
+ on every target, also as a dependency of another project.
229
+ - A TypeScript type with several type arguments no longer has a trailing
230
+ comma.
231
+ - A spec handler clause that fails no longer poisons the handler on Rust:
232
+ later operations see the state as the failure left it.
233
+ - A field's refinement may call a checked definition of its unit
234
+ (`label :: (v :: Text where validName v)`).
235
+ - A record update or a rebuild proves a field's regex refinement, which the
236
+ copied field already met; text tests and the sequence helpers are facts
237
+ the checker knows.
238
+ - A definition that says it `fails with E` but never raises may be a
239
+ handler clause (`op by f`).
240
+ - A spec handler named like its ability (`counter` for `Counter`) is
241
+ `CounterSpec` where the two would share a name.
242
+ - A record whose field a regex constrains has boundary witnesses that match
243
+ the regex.
244
+ - Bounded arithmetic used by an import's definitions resolves in the
245
+ importing unit, packages included.
246
+ - Checking scales with a program's size: LawTwin's import of GitHub's whole
247
+ REST description (2,108 types, 1,232 operations, 87 laws) checks in
248
+ minutes, where it did not finish in 30. Field access is typed without
249
+ building its match, registries, indexes, audits and unit elaboration are
250
+ made once per program, and sets and maps replace quadratic list scans.
251
+ `lawspec-dev bench compile` times a scaled model. Field access and record
252
+ update are Core's own projection and update, read directly by validation,
253
+ evaluation and the totality audits, instead of a match binding every field
254
+ of the record: the scaled model of 100, 200 and 400 types checks in 22.5,
255
+ 46.9 and 129.9 seconds instead of 33.8, 95.5 and 340.2 (unoptimized
256
+ builds).
257
+ - Java and Kotlin generation accepts a field named `record`, `sealed`,
258
+ `permits` or `var`, which Java restricts only as type names.
259
+ - Prelude helpers such as `prelude.isRight` work in an ability's laws and in
260
+ handler clauses.
261
+ - A numeric literal in a `match` or `if` branch, or a list, takes its type
262
+ from the other branches or items.
263
+
264
+ ## [0.22.0] - 2026-10-10
265
+
266
+ ### Added
267
+
268
+ - Erlang, Elixir and Gleam targets share a BEAM runtime and generate native
269
+ data types, adapters and checked definitions. Tests use PropEr with EUnit,
270
+ StreamData with ExUnit, and qcheck with gleeunit, including each framework's
271
+ own generation and shrinking.
272
+ - BEAM ability handlers, typed failures, asynchronous tasks, workflow policies
273
+ and built-in abilities, with native type, function, handler and generator
274
+ bindings. Native payment examples demonstrate adopting and removing
275
+ generated tests in an existing application.
276
+ - BEAM stateful models, collection models, opaque handles, consistency
277
+ models, scenarios, typed sessions and mailboxes, and actors with OTP
278
+ supervision. Stateful checks have individual identities for CLI selection,
279
+ reports, caching and replay.
280
+ - Secure BEAM distribution uses the shared wire format and cryptographic
281
+ seed keys. Projects importing `lawspec.crypto` or `lawspec.network` build a
282
+ small C/OpenSSL NIF before compilation. The bridge requires OTP 29,
283
+ OpenSSL 3.5 or later matching OTP's OpenSSL major version, and a Unix C
284
+ toolchain on macOS, Linux or WSL. Projects without those imports have no
285
+ native crypto build step.
286
+ - BEAM setup guides, toolchain checks, offline dependencies, JUnit reporting,
287
+ coverage, and failure replay from the exact shrunk inputs.
288
+ - BEAM harness strategies, generated-case coverage and labels, targeted
289
+ search, repeats and flaky retries, skips and known failures, random law
290
+ order, parallel execution and benchmarks in the native test frameworks.
291
+ - BEAM matcher, table and description examples, with recorded values read
292
+ from the shared project folder and updated by `lawspec test --update-recorded`.
293
+ - BEAM per-case and shared resources use dedicated owner processes for
294
+ acquisition, reset and release. Harness timeouts cancel test workers and
295
+ join their asynchronous children; cleanup has a separate bounded allowance.
296
+ Cleanup failures fail the run, including under retries and known failures.
297
+
298
+ ### Fixed
299
+
300
+ - Recorded values use one typed format across targets, including exact
301
+ numbers, floats, bytes, raw text, presence and nested data. Identity labels
302
+ are local to each recording. Existing extended scalar recordings may need
303
+ updating with `lawspec test --update-recorded`.
304
+ - Generated TypeScript, Python, Go and Kotlin product types retain their type
305
+ parameters when their sole constructor fixes a parameter's type.
306
+ - Generated Haskell constructors and codecs apply fixed type parameters to
307
+ their fields, including nested data.
308
+ - Erlang EUnit output retains deeper failure diagnostics, including native
309
+ generator identities used by acceptance checks.
310
+ - CI logs include exit statuses, and acceptance logs retain completed stages
311
+ when a runner exits unexpectedly.
312
+ - Failure databases and reports keep separate project roots apart, including
313
+ projects using the same target. Replaying or repairing one project's
314
+ failure cannot clear another project's counterexample.
315
+ - CLI JUnit output retains cases from flat Node reports and nested native
316
+ suites.
317
+ - BEAM JUnit output includes intentionally skipped laws and their reasons;
318
+ skips remain separate from evidence that a test executed. Scheduled cases
319
+ keep their declaring unit, so identical test names in different units
320
+ remain distinct.
321
+ - BEAM generators follow scalar and constructor-field refinements, including
322
+ named symbol identities, minimum lengths and bounds depending on earlier
323
+ fields. Native shrinking preserves these constraints.
324
+ - Generated BEAM sets and maps keep their portable order and distinct keys;
325
+ native values with unsorted or duplicate entries fail validation.
326
+
9
327
  ## [0.21.0] - 2026-10-07
10
328
 
11
329
  ### Added
@@ -309,7 +627,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
309
627
  requirements the tests verify), `canonical_decisions.yaml` (the design
310
628
  decisions, each cited as `ref:KEY`) and `canonical_exemptions.yaml` sit at
311
629
  the root, and `canonical_vetting/` holds every comment, page, decision and
312
- reference as pending with owner review deferred until after the 0.21 refactor.
630
+ reference as pending with owner review deferred until 1.0.
313
631
  - `make canon` and `lawspec-dev canon` run canon over the repository, and the
314
632
  complete check runs it as its `canon` step. Set `CANON_HOME` to a canon
315
633
  checkout built with `stack build`; without one the step is skipped with a
@@ -1217,7 +1535,9 @@ A maintenance release. The language, generated code and API are unchanged from
1217
1535
  machine-sized bindings reject an architecture mismatch. Rust adapters take
1218
1536
  owned values.
1219
1537
 
1220
- [Unreleased]: https://github.com/brain-fuel/lawspec/compare/v0.21.0...HEAD
1538
+ [Unreleased]: https://github.com/brain-fuel/lawspec/compare/v0.23.0...HEAD
1539
+ [0.23.0]: https://github.com/brain-fuel/lawspec/compare/v0.22.0...v0.23.0
1540
+ [0.22.0]: https://github.com/brain-fuel/lawspec/compare/v0.21.0...v0.22.0
1221
1541
  [0.21.0]: https://github.com/brain-fuel/lawspec/compare/v0.20.1...v0.21.0
1222
1542
  [0.20.1]: https://github.com/brain-fuel/lawspec/compare/v0.20.0...v0.20.1
1223
1543
  [0.20.0]: https://github.com/brain-fuel/lawspec/compare/v0.19.1...v0.20.0
package/README.md CHANGED
@@ -30,7 +30,7 @@ Node 22+ and the selected target's build tools are required. No Haskell
30
30
  toolchain is needed. The reference platforms are macOS and Linux.
31
31
 
32
32
  ```sh
33
- npm install --save-dev lawspec@0.21.0
33
+ npm install --save-dev lawspec@0.23.0
34
34
  npx lawspec --version
35
35
  ```
36
36
 
@@ -40,8 +40,8 @@ LawSpec can create `package.json` and its test script:
40
40
  ```sh
41
41
  mkdir lawspec-example
42
42
  cd lawspec-example
43
- npm exec --package=lawspec@0.21.0 -- lawspec init --target javascript
44
- npm install --save-dev lawspec@0.21.0
43
+ npm exec --package=lawspec@0.23.0 -- lawspec init --target javascript
44
+ npm install --save-dev lawspec@0.23.0
45
45
  npx lawspec check
46
46
  npx lawspec explain 'example.atoi_codec::itoa and then atoi yields a'
47
47
  npx lawspec doctor
@@ -0,0 +1,160 @@
1
+ // Generated from templates/npm/beam-coverage.mjs by lawspec-dev generate. Do not edit.
2
+ import {execFile} from "node:child_process";
3
+ import {createHash} from "node:crypto";
4
+ import {mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile} from "node:fs/promises";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+ import {promisify} from "node:util";
8
+
9
+ const exec = promisify(execFile);
10
+ export function beamCoverageRequirement(target, report) {
11
+ if (target.language === "gleam" && Number(report?.versions?.gleam?.split(".")[1] ?? 0) < 19)
12
+ return {tool: "Gleam 1.19 for source coverage", install: "upgrade Gleam to 1.19 so native counters refer to the original source lines"};
13
+ if (target.language === "elixir" && report?.coverage?.tool !== "Mix.Tasks.Test.Coverage")
14
+ return {tool: "Mix.Tasks.Test.Coverage", install: "set test_coverage[:tool] to Mix.Tasks.Test.Coverage in mix.exs"};
15
+ return null;
16
+ }
17
+
18
+ const collector = String.raw`#!/usr/bin/env escript
19
+ -mode(compile).
20
+ main([Input, Output]) ->
21
+ try
22
+ {ok, Bytes} = file:read_file(Input),
23
+ #{<<"runs">> := Runs, <<"directory">> := Directory} = json:decode(Bytes),
24
+ {ok, _} = cover:start(),
25
+ [ok = cover:import(text(maps:get(<<"file">>, Run))) || Run <- Runs],
26
+ Beams = lists:foldl(fun(Run, Acc) -> maps:merge(Acc, maps:get(<<"beams">>, Run, #{})) end, #{}, Runs),
27
+ Modules = [analyse(Module, text(Directory), Beams) || Module <- lists:sort(cover:imported_modules())],
28
+ true = Modules =/= [],
29
+ ok = file:write_file(Output, json:encode(Modules)),
30
+ cover:stop()
31
+ catch Class:Reason:Stack ->
32
+ io:format(standard_error, "Cannot collect BEAM coverage: ~tp:~tp~n~tp~n", [Class, Reason, Stack]), halt(1)
33
+ end.
34
+
35
+ analyse(Module, Directory, Beams) ->
36
+ {ok, Calls} = cover:analyse(Module, calls, line),
37
+ Lines = lists:foldl(fun
38
+ ({{_, Line}, Hits}, Acc) when Line > 0 -> maps:update_with(Line, fun(N) -> N + Hits end, Hits, Acc);
39
+ (_, Acc) -> Acc
40
+ end, #{}, Calls),
41
+ Name = atom_to_binary(Module),
42
+ Rows = [#{line => Line, hits => Hits} || {Line, Hits} <- lists:sort(maps:to_list(Lines))],
43
+ case maps:find(Name, Beams) of
44
+ {ok, Beam} -> #{name => Name, lines => Rows, source => source(Module, text(Beam))};
45
+ error ->
46
+ File = binary_to_list(binary:encode_hex(crypto:hash(sha256, Name), lowercase)) ++ ".html",
47
+ {ok, _} = cover:analyse_to_file(Module, [html, {outfile, filename:join(Directory, File)}]),
48
+ #{name => Name, lines => Rows, report => unicode:characters_to_binary(File)}
49
+ end.
50
+
51
+ source(Module, Beam) ->
52
+ {ok, {Module, [{compile_info, Info}]}} = beam_lib:chunks(Beam, [compile_info]),
53
+ Source = case proplists:get_value(source, Info) of
54
+ undefined ->
55
+ {ok, {Module, [{abstract_code, {raw_abstract_v1, Forms}}]}} = beam_lib:chunks(Beam, [abstract_code]),
56
+ [File | _] = [F || {attribute, _, file, {F, _}} <- Forms], File;
57
+ File -> File
58
+ end,
59
+ unicode:characters_to_binary(Source).
60
+ text(Value) -> unicode:characters_to_list(Value).
61
+ `;
62
+
63
+ const escape = (value) => String(value).replace(/[&<>"']/g, (c) =>
64
+ ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
65
+ const totals = (lines) => ({covered: lines.filter(({hits}) => hits > 0).length, total: lines.length});
66
+ const percentage = ({covered, total}) => total ? `${(100 * covered / total).toFixed(1)}%` : "no executable lines";
67
+ const page = (title, body) => `<!doctype html><html lang="en"><meta charset="utf-8"><title>${escape(title)}</title>
68
+ <style>body{font:16px system-ui;margin:2rem;color:#222}table{border-collapse:collapse;width:100%}td,th{padding:.3rem .7rem;text-align:left;border-bottom:1px solid #ddd}pre{margin:0;tab-size:4}.hit{background:#e4f5e9}.miss{background:#ffe6e6}.line,.count{width:4rem;text-align:right;color:#555}a{color:#174da8}</style>
69
+ <h1>${escape(title)}</h1>${body}</html>\n`;
70
+
71
+ // Only exports explicitly returned by this run's invocations are imported.
72
+ // A missing export is an error even if the native runner exited successfully.
73
+ export async function collectBeamCoverage({root, directory, runs}) {
74
+ if (!runs.length) return null;
75
+ root = path.resolve(root);
76
+ directory = path.resolve(directory);
77
+ const temporary = await mkdtemp(path.join(os.tmpdir(), "lawspec-beam-coverage-"));
78
+ try {
79
+ const inputs = [];
80
+ const seen = new Set();
81
+ let rebarDirectories;
82
+ for (const run of runs) {
83
+ if (!run.run || seen.has(run.run) || path.basename(run.file) !== `${run.run}.coverdata`)
84
+ throw new Error("Invalid or duplicate BEAM coverage invocation");
85
+ seen.add(run.run);
86
+ const info = await stat(run.file).catch(() => null);
87
+ if (!info?.isFile() || !info.size) throw new Error(`Native runner did not export coverage for ${run.run}: ${run.file}`);
88
+ let beams;
89
+ if (run.tool === "rebar3" && !rebarDirectories) {
90
+ // Before the first compile Rebar only knows dependency ebin paths.
91
+ // Ask after the test run so fresh and relocated applications are found.
92
+ const result = await exec("rebar3", ["as", "test", "path", "--ebin", "--separator", "\n"], {cwd: root});
93
+ rebarDirectories = result.stdout.trim().split("\n").filter(Boolean);
94
+ if (!rebarDirectories.length || !rebarDirectories.every((item) => path.isAbsolute(item)))
95
+ throw new Error("Cannot resolve Rebar's application beam directories after coverage");
96
+ }
97
+ const directories = run.tool === "rebar3" ? rebarDirectories
98
+ : run.beamDirectories ?? (run.beamDirectory ? [run.beamDirectory] : []);
99
+ for (const directory of directories) {
100
+ const entries = (await readdir(directory)).filter((name) => name.endsWith(".beam"));
101
+ beams = {...beams, ...Object.fromEntries(entries.map((name) => [name.slice(0, -5), path.resolve(directory, name)]))};
102
+ }
103
+ if (run.metadata) {
104
+ const metadata = JSON.parse(await readFile(run.metadata, "utf8"));
105
+ if (metadata.run !== run.run || metadata.file !== run.file || !metadata.modules?.length)
106
+ throw new Error(`Gleam coverage metadata does not match invocation ${run.run}`);
107
+ beams = Object.fromEntries(metadata.modules.map(({name, beam}) => [name, beam]));
108
+ }
109
+ inputs.push({file: path.resolve(run.file), ...(beams ? {beams} : {})});
110
+ }
111
+ // Assemble all pages off to the side. A failed import or source mapping
112
+ // cannot leave an apparently complete index behind.
113
+ const pages = path.join(temporary, "modules");
114
+ await mkdir(pages);
115
+ const input = path.join(temporary, "input.json"), output = path.join(temporary, "output.json");
116
+ const script = path.join(temporary, "collect.escript");
117
+ await writeFile(script, collector);
118
+ await writeFile(input, JSON.stringify({runs: inputs, directory: pages}));
119
+ await exec("escript", [script, input, output], {cwd: root, maxBuffer: 8 * 1024 * 1024});
120
+ const modules = JSON.parse(await readFile(output, "utf8"));
121
+ for (const module of modules) {
122
+ module.summary = totals(module.lines);
123
+ if (!module.source) continue;
124
+ let source = path.resolve(root, module.source);
125
+ const text = await readFile(source, "utf8");
126
+ // Gleam copies foreign Erlang sources into its build tree. Prefer the
127
+ // original when it is byte-for-byte the source that was compiled.
128
+ const marker = `${path.sep}_gleam_artefacts${path.sep}`;
129
+ if (source.includes(marker)) {
130
+ const original = path.join(root, "src", source.split(marker).at(-1));
131
+ if (await readFile(original, "utf8").catch(() => null) === text) source = original;
132
+ }
133
+ module.source = path.relative(root, source);
134
+ const sourceLines = text.split(/\r?\n/);
135
+ if (module.lines.some(({line}) => line > sourceLines.length))
136
+ throw new Error(`Coverage lines do not match source ${module.source}`);
137
+ const hits = new Map(module.lines.map(({line, hits}) => [line, hits]));
138
+ module.report = createHash("sha256").update(module.name).digest("hex") + ".html";
139
+ const rows = sourceLines.map((line, index) => {
140
+ const count = hits.get(index + 1);
141
+ return `<tr class="${count === undefined ? "" : count ? "hit" : "miss"}" id="L${index + 1}"><td class="line">${index + 1}</td><td class="count">${count ?? ""}</td><td><pre>${escape(line)}</pre></td></tr>`;
142
+ }).join("\n");
143
+ await writeFile(path.join(pages, module.report), page(module.name,
144
+ `<p>${escape(module.source)} — ${percentage(module.summary)}</p><table>${rows}</table>`));
145
+ }
146
+ const summary = modules.reduce((sum, module) => ({covered: sum.covered + module.summary.covered,
147
+ total: sum.total + module.summary.total}), {covered: 0, total: 0});
148
+ const result = {version: 1, root, runs: runs.map(({run}) => run), summary, modules};
149
+ const rows = modules.map((module) => `<tr><td><a href="modules/${module.report}">${escape(module.name)}</a></td><td>${module.summary.covered}/${module.summary.total}</td><td>${percentage(module.summary)}</td></tr>`).join("\n");
150
+ await mkdir(path.join(directory, "modules"), {recursive: true});
151
+ for (const module of modules)
152
+ await writeFile(path.join(directory, "modules", module.report), await readFile(path.join(pages, module.report)));
153
+ await writeFile(path.join(directory, "coverage.json"), JSON.stringify(result, null, 2) + "\n");
154
+ await writeFile(path.join(directory, "index.html"), page("BEAM line coverage",
155
+ `<p>${summary.covered}/${summary.total} executable lines covered (${percentage(summary)}); ${runs.length} native run(s).</p><table><thead><tr><th>Module</th><th>Lines</th><th>Coverage</th></tr></thead><tbody>${rows}</tbody></table>`));
156
+ return result;
157
+ } finally {
158
+ await rm(temporary, {recursive: true, force: true});
159
+ }
160
+ }