Skip to content
Rehearsev1.2.0

Write file scenarios

A file scenario names an input, the command to run, and the output that command writes.

packages/report/weekly.rehearse.yaml
name: The weekly stock report lists every item with its price
input:
file: stock.json
run: node packages/report/report.mjs {{input}} {{output}}/weekly.csv
output:
file: weekly.csv
layout: csv

Its input is a file beside it.

packages/report/stock.json
[
{ "sku": "P-1", "name": "Pen", "price": 3.5, "quantity": 40 },
{ "sku": "N-2", "name": "Notebook", "price": 12, "quantity": 15 },
{ "sku": "I-3", "name": "Ink", "price": 7.25, "quantity": 8 }
]

A comparison groups what changed in the output by column.

Terminal
$ rehearse compare --base main
…
changed without saying so:
The weekly stock report lists every item with its price (packages/report/weekly.rehearse.yaml)
weekly.csv column 3 "price": "3.5" → "3.50" (2 places)
unannounced, by what changed:
weekly.csv column 3 "price" — in 1 scenario

A file scenario tries a command that reads an input and writes an output: a report, an export, a rendered document, a generated map. Rehearse runs it on the base and on the head and compares the outputs by their layout. A CSV is compared cell by cell and grouped by column, so one change in a column reads as one line, however many rows it touches. Above, the head writes prices with two decimals: 3.5 became 3.50 and 12 became 12.00, and 7.25 stayed as it was.

  1. Point the manifest at your file scenarios. List them under scenarios.files.

    rehearse.yaml
    scenarios:
    files: ['**/*.rehearse.yaml']
  2. Name it with what it tries, in plain words. Its id is its path: packages/report/weekly.rehearse.yaml.

  3. Give it an input, or none:

    Input Is
    file: stock.json a file beside the scenario. Its contents are part of the scenario’s definition.
    command: node make-input.mjs {{input}} a command that writes {{input}}, a file in a scratch folder (named input, or name:)
  4. Give it the command to run. It runs in the checkout (or in cwd, relative to the checkout), and writes into {{output}}, a scratch folder of its own.

  5. Say what it writes, and how to read it: output: { file: weekly.csv, layout: csv }. file is relative to {{output}}. A command that always writes to the same place can name any absolute path instead, such as '{{checkout}}/out/report.csv'.

  6. Run it by its path, which is its id.

    Terminal
    $ rehearse run packages/report/weekly.rehearse.yaml
    ✓ The weekly stock report lists every item with its price (packages/report/weekly.rehearse.yaml)
    on …/shop at dc928d1a5199 · world stocked · as owner
    exit 0 · weekly.csv (csv, 3 rows)
Placeholder Environment Is
{{input}} REHEARSE_INPUT the input file
{{output}} REHEARSE_OUTPUT the folder to write the output in
{{here}} the scenario file’s folder
{{checkout}}, {{manifest}} the checkout, and the manifest’s folder
{{world}}, {{person}} the world and the person it runs as
{{token}} REHEARSE_TOKEN that person’s token
{{api_url}} each running service’s URL, by its name

env: sets more variables. timeout: is in seconds (default 120).

A file scenario that calls a service names it: services: [api] brings it up first. A file scenario starts from its world:, else the default world, like any other scenario. If that world adds anything, every service comes up before it runs.

A file scenario fails when input or run exits with anything but 0, takes longer than its timeout, or an output file is missing. The log’s last lines are in the failure. Everything else is a difference, not a failure.

Layout Reads Compared
csv, tsv rows, the first one naming the columns (header: false and columns: [...] when it doesn’t) the columns, the number of rows, then each cell, grouped by column
json a JSON document by path, as a call’s body, with ids and times set aside
lines text, line by line line by line
fixed fixed-width rows: columns: [{ name, start, width }], start counted from 1 each cell, grouped by column
zip each entry, by the layout its glob gives in entries entry by entry

A scenario can write more than one file: give output a list. An output that isn’t text (a PNG, a PDF) is compared by its size and hash.

unordered: true on an output compares its rows (or a JSON output’s arrays) without their order.

A file scenario’s setAside names what never counts as a difference.

packages/report/weekly.rehearse.yaml
setAside:
columns: [generated_at, batch_id] # never compared
values: ['RUN-[0-9]+'] # set aside wherever they appear
keep: [sku] # never set aside

A cell is text, so Rehearse sets aside what it finds inside text: UUIDs, ISO date-times, relative times, and the manifest’s and the scenario’s values. columns are never compared at all. See what is set aside.

A package of files (a handoff, an export, a .docx) is a zip. Compare it entry by entry, each by its own layout.

render/handoff.rehearse.yaml
name: An order renders into its handoff package
input:
file: order.json
run: python3 render/render.py {{input}} {{output}}/handoff.zip
output:
file: handoff.zip
layout: zip
entries:
'*.pdf': skip
'*.csv': csv
'*.json': json

A comparison names the entry inside the zip that changed.

Terminal
$ rehearse compare --base main
0 as on the base · 1 changed without saying so
changed without saying so:
An order renders into its handoff package (render/handoff.rehearse.yaml)
handoff.zip!lines.csv column 3 "price": "3.5" → "3.50" (2 places)

entries maps a glob to a layout: csv, tsv, json, lines, zip or skip. The first glob that matches an entry wins; an entry no glob matches is read as lines. A difference names the entry after !.

zip reads an archive inside the archive with the same entries. A Word file is one: read it as zip, and its parts are compared one by one, with the times inside them set aside. Kurvball’s handoff renderer is compared this way.

packages/modules/handoff/rehearse/handoff.rehearse.yaml
output:
file: handoff.zip
layout: zip
entries:
'**/*.md': lines
'*.json': json
'**/*.docx': zip
'**/*.pdf': skip

An entry inside an inner archive is named with both, such as handoff.zip!brief.docx!word/document.xml.

The stage shows a file scenario’s input and its output: a table of rows, a JSON document, or lines, and a zip entry by entry. In Diff, each cell that changed is marked with what it was on the base. See the stage.