|
| 1 | +# Script format |
| 2 | + |
| 3 | +Name each scenario `<command>_<scenario>.txt` under |
| 4 | +`cmd/layerctl/testdata/script`. |
| 5 | +The runner starts each script in its own temporary directory, available as |
| 6 | +`$WORK`, and puts `layerctl` on `PATH` using the Go test binary. |
| 7 | +Git starts with a test author and committer, fixed dates for both, and no |
| 8 | +system or user config. |
| 9 | +Use `at 2000-01-02T03:04:05Z` to set both dates for subsequent commands |
| 10 | +when a scenario depends on commit timing. |
| 11 | +Choose explicit branch names when they matter to the scenario. |
| 12 | + |
| 13 | +Testscript reads a command per line. |
| 14 | +Use `#` comments for the promise or phases whose purpose needs explaining. |
| 15 | +`exec` runs a program and expects success, and `! exec` expects failure. |
| 16 | +Use `git` for Git commands in the script's working directory. |
| 17 | +Use `! git` when the command should fail. |
| 18 | +Single quotes group arguments and suppress variable expansion. |
| 19 | +Shell pipes, redirections, and substitutions require an explicitly invoked |
| 20 | +shell. |
| 21 | +Use built-in commands such as `cd`, `cp`, `exists`, and `cmp` for fixture |
| 22 | +operations. |
| 23 | +Lines of the form `-- path --` at the end of the script introduce files |
| 24 | +unpacked under `$WORK` before the first command. |
| 25 | + |
| 26 | +Put input files under `give/` and expected output files under `want/`. |
| 27 | +Use `give/repo/` for the initial contents of a Git repository and |
| 28 | +`give/layer/` for its layer manifest, patches, and other layer files. |
| 29 | +Include the patch itself when its contents are part of the setup. |
| 30 | +When a command writes layer files, put the expected manifest and patches |
| 31 | +under `want/layer/`. |
| 32 | +Omit the expected manifest when the command leaves it unchanged. |
| 33 | + |
| 34 | +This fragment checks the path printed for a projection named `demo` that the |
| 35 | +preceding setup created at `$WORK/workspace`. |
| 36 | + |
| 37 | +```text |
| 38 | +exec layerctl projection path demo |
| 39 | +cmpenv stdout $WORK/want/path |
| 40 | +
|
| 41 | +-- want/path -- |
| 42 | +$WORK/workspace |
| 43 | +``` |
| 44 | + |
| 45 | +`cmp` compares file contents and accepts `stdout` and `stderr` from the last |
| 46 | +program as inputs. |
| 47 | +`cmpenv` expands variables in the expected file. |
| 48 | +Use `cp stdout saved` to keep output for a later comparison. |
| 49 | +`stdout` and `stderr` take regular expressions. |
| 50 | +Use `${WORK@R}` outside single-quoted segments to escape regex metacharacters |
| 51 | +in the work directory path. |
| 52 | + |
| 53 | +Run the full script suite or select one script by its filename stem. |
| 54 | + |
| 55 | +```sh |
| 56 | +go test ./cmd/layerctl -run '^TestScript$' |
| 57 | +go test ./cmd/layerctl -run '^TestScript$/^help$' -v |
| 58 | +``` |
| 59 | + |
| 60 | +See the [testscript package documentation](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript) |
| 61 | +for the full command reference. |
0 commit comments