Skip to content

Commit f3bc804

Browse files
authored
cli: Test Layerctl workflows with testscript (#4)
Projection recovery and layer capture rely on Git and filesystem state that persists across CLI invocations. Run the Layerctl entrypoint and real Git in disposable workspaces with fixed commit timestamps so scripts can exercise those workflows and check the resulting files and commits. Document how to write fixtures and focus assertions on the behavior each script exercises.
1 parent 3f3d4cb commit f3bc804

14 files changed

Lines changed: 387 additions & 1 deletion

‎.agents/skills/testscript/SKILL.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
name: testscript
3+
description: >-
4+
Choose, write, or review Layerctl end-to-end CLI tests in
5+
cmd/layerctl/testdata/script, including workflows that persist Git state
6+
across commands and user-facing failure or recovery behavior.
7+
---
8+
9+
# Layerctl test scripts
10+
11+
Use a script when the promise depends on the built CLI, such as its
12+
arguments, exit status, output, or state observed by a later invocation.
13+
Use Go package tests when a package API is the relevant boundary or when
14+
the case needs precise control of internal state.
15+
16+
Give each script one user-visible promise.
17+
Keep the manifests, patch inputs, and expected results alongside the commands
18+
that exercise them.
19+
Use real Git and local repositories for sources.
20+
Keep a workflow together when the result depends on earlier invocations.
21+
22+
Assert the full value or state that the promise covers.
23+
Compare a complete output stream when its format is part of the contract.
24+
For Git state, query the relevant tree, ref, or worktree with Git and compare
25+
the result with an independent expectation.
26+
For a rejected command, check the failure and any state the command promises
27+
to preserve.
28+
When stderr includes Git diagnostics or progress, anchor the Layerctl message
29+
you care about and allow variation in unrelated text.
30+
Match the expected values of paths, refs, and other fields that matter to the
31+
scenario.
32+
Use a regex for fields whose literal value can vary within the contract.
33+
34+
Read [Script format](references/format.md) when authoring or running a script
35+
for fixture syntax, assertions, and focused test commands.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.

‎AGENTS.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Layerctl composes Git sources and ordered operations into projection workspaces.
88

99
Go 1.26 and Git are required. Run `go test ./...` and `go build -o /dev/null ./cmd/layerctl` from the repository root. Run `gofmt` on changed Go files. Tests create disposable Git repositories; preserve existing projections and worktrees when debugging. Use `nix flake check` to build and test the Nix package, and `goreleaser check` to validate the release configuration. Run `./scripts/test-release-notes.sh` after changing release-note extraction. `goreleaser release --snapshot --clean` builds release artifacts without publishing them.
1010

11+
When choosing how to cover a CLI behavior or working on a test script,
12+
read [the testscript skill](.agents/skills/testscript/SKILL.md).
13+
1114
## Packaging and releases
1215

1316
`nix/package.nix` holds the reusable `buildGoModule` definition; `flake.nix` exposes it for Linux and macOS on x86-64 and ARM64. Keep the vendored Go dependency hash current. Follow [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) in `CHANGELOG.md`: record notable changes under `Unreleased`, then prepare a dated semantic-version section before tagging. The release workflow reads that section and runs GoReleaser on a `v`-prefixed version tag. Release builds embed the version and full commit in `layerctl --version`. Do not create or push a release tag without a request.

‎cmd/layerctl/script_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
"time"
7+
8+
"github.com/rogpeppe/go-internal/testscript"
9+
)
10+
11+
func TestMain(m *testing.M) {
12+
testscript.Main(m, map[string]func(){
13+
"layerctl": main,
14+
})
15+
}
16+
17+
func TestScript(t *testing.T) {
18+
testscript.Run(t, testscript.Params{
19+
Dir: "testdata/script",
20+
Setup: func(env *testscript.Env) error {
21+
env.Setenv("GIT_CONFIG_NOSYSTEM", "1")
22+
env.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
23+
env.Setenv("GIT_AUTHOR_NAME", "Layerctl Test")
24+
env.Setenv("GIT_AUTHOR_EMAIL", "layerctl@example.test")
25+
env.Setenv("GIT_COMMITTER_NAME", "Layerctl Test")
26+
env.Setenv("GIT_COMMITTER_EMAIL", "layerctl@example.test")
27+
setGitDate(env.Setenv, "2000-01-01T00:00:00Z")
28+
return nil
29+
},
30+
Cmds: map[string]func(*testscript.TestScript, bool, []string){
31+
"git": scriptGit,
32+
"at": scriptAt,
33+
},
34+
})
35+
}
36+
37+
func scriptGit(ts *testscript.TestScript, neg bool, args []string) {
38+
err := ts.Exec("git", args...)
39+
if neg {
40+
if err == nil {
41+
ts.Fatalf("unexpected git command success")
42+
}
43+
return
44+
}
45+
ts.Check(err)
46+
}
47+
48+
func scriptAt(ts *testscript.TestScript, neg bool, args []string) {
49+
if neg || len(args) != 1 {
50+
ts.Fatalf("usage: at <RFC3339 timestamp>")
51+
}
52+
date, err := time.Parse(time.RFC3339, args[0])
53+
ts.Check(err)
54+
setGitDate(ts.Setenv, date.Format(time.RFC3339))
55+
}
56+
57+
func setGitDate(setenv func(key, value string), date string) {
58+
setenv("GIT_AUTHOR_DATE", date)
59+
setenv("GIT_COMMITTER_DATE", date)
60+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Check reports which layer failed during composition.
2+
3+
# Make the script executable so the failure comes from running the operation.
4+
mkdir definition/layers
5+
mv give/layer definition/layers/0001-fail
6+
cd definition
7+
git init -q -b main
8+
chmod 0755 layers/0001-fail/fail.sh
9+
10+
! exec layerctl check
11+
stderr '0001-fail'
12+
stderr 'fail\.sh'
13+
14+
-- give/layer/layer.toml --
15+
[vcs]
16+
commit-msg = "Fail the check"
17+
18+
[[operation]]
19+
[operation.script]
20+
file = "fail.sh"
21+
-- give/layer/fail.sh --
22+
#!/bin/sh
23+
printf '%s: intentional failure\n' "${0##*/}" >&2
24+
exit 23
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exec layerctl --help
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Layer add records a committed edit as a new layer on disk.
2+
3+
# Use SHA-1 to match the expected patch and project through the predecessor.
4+
env GIT_DEFAULT_HASH=sha1
5+
mkdir definition/layers
6+
mv give/layer definition/layers/0001-base
7+
cd definition
8+
git init -q -b main
9+
exec layerctl projection create edit --through 0001-base --worktree $WORK/edit
10+
11+
# Modify an existing file so the new layer records the change as a patch.
12+
cp $WORK/give/edited.txt $WORK/edit/file.txt
13+
git -C $WORK/edit commit -qam 'Edit file'
14+
15+
exec layerctl layer add --from edit 0002-edit
16+
cmp layers/0002-edit/layer.toml $WORK/want/layer/layer.toml
17+
cmp layers/0002-edit/patch $WORK/want/layer/patch
18+
19+
-- give/layer/layer.toml --
20+
[vcs]
21+
commit-msg = "Add file"
22+
23+
[[operation]]
24+
[operation.overlay]
25+
directory = "overlay"
26+
-- give/layer/overlay/file.txt --
27+
original
28+
-- give/edited.txt --
29+
edited
30+
-- want/layer/layer.toml --
31+
[vcs]
32+
commit-msg = """
33+
Edit file
34+
"""
35+
36+
[[operation]]
37+
[operation.patch]
38+
file = "patch"
39+
-- want/layer/patch --
40+
diff --git a/file.txt b/file.txt
41+
index 4b48deed3a433909bfd6b6ab3d4b91348b6af464..7663aa741bec2b1328630882586fb7a2cbc8a255 100644
42+
--- a/file.txt
43+
+++ b/file.txt
44+
@@ -1 +1 @@
45+
-original
46+
+edited
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Refresh records a committed file edit in the layer on disk.
2+
3+
# Use SHA-1 to match the expected patch, then install the starting overlay.
4+
env GIT_DEFAULT_HASH=sha1
5+
mkdir definition/layers
6+
mv give/layer definition/layers/0001-base
7+
cd definition
8+
git init -q -b main
9+
10+
# Refresh captures committed changes, so commit the edit in the projection.
11+
exec layerctl projection create edit --through 0001-base --worktree $WORK/edit
12+
cp $WORK/give/edited.txt $WORK/edit/file.txt
13+
git -C $WORK/edit commit -qam 'Edit file'
14+
15+
# The refreshed manifest keeps the overlay and appends a patch for the edit.
16+
exec layerctl layer refresh --from edit 0001-base
17+
cmp layers/0001-base/layer.toml $WORK/want/layer/layer.toml
18+
cmp layers/0001-base/refresh.patch $WORK/want/layer/refresh.patch
19+
20+
-- give/layer/layer.toml --
21+
[vcs]
22+
commit-msg = "Add file"
23+
24+
[[operation]]
25+
[operation.overlay]
26+
directory = "overlay"
27+
-- give/layer/overlay/file.txt --
28+
original
29+
-- give/edited.txt --
30+
edited
31+
-- want/layer/layer.toml --
32+
[vcs]
33+
commit-msg = """
34+
Edit file
35+
"""
36+
37+
[[operation]]
38+
[operation.overlay]
39+
directory = "overlay"
40+
41+
[[operation]]
42+
[operation.patch]
43+
file = "refresh.patch"
44+
-- want/layer/refresh.patch --
45+
diff --git a/file.txt b/file.txt
46+
index 4b48deed3a433909bfd6b6ab3d4b91348b6af464..7663aa741bec2b1328630882586fb7a2cbc8a255 100644
47+
--- a/file.txt
48+
+++ b/file.txt
49+
@@ -1 +1 @@
50+
-original
51+
+edited
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# A fresh invocation resumes a projection by name and commits the resolution.
2+
3+
# Keep the base commit for Git's three-way apply. Both upstream and
4+
# feature.patch replace the same line from that commit.
5+
env GIT_DEFAULT_HASH=sha1
6+
git -C give/repo init -q -b main
7+
git -C give/repo add file.txt
8+
git -C give/repo commit -qm base
9+
cp give/upstream.txt give/repo/file.txt
10+
git -C give/repo commit -qam upstream
11+
12+
# Install the layer and point its clone operation at this run's source repo.
13+
mkdir definition/layers
14+
mv give/layer definition/layers/0001-feature
15+
cd definition
16+
git init -q -b main
17+
exec sed 's|@WORK@|'$WORK'|g' layers/0001-feature/layer.toml
18+
cp stdout layers/0001-feature/layer.toml
19+
20+
# Creation stops at the conflict and reports how to resume the projection.
21+
! exec layerctl projection create demo --worktree $WORK/workspace
22+
stderr 'resolve it then run layerctl projection continue demo$'
23+
24+
# Resolve the file in the stopped workspace and check the commit from continue.
25+
cp $WORK/give/resolved.txt $WORK/workspace/file.txt
26+
exec layerctl projection continue demo
27+
git -C $WORK/workspace show HEAD:file.txt
28+
cmp stdout $WORK/give/resolved.txt
29+
30+
-- give/repo/file.txt --
31+
base
32+
-- give/layer/layer.toml --
33+
[vcs]
34+
commit-msg = "Apply feature"
35+
36+
[[operation]]
37+
[operation.git-clone]
38+
url = "@WORK@/give/repo"
39+
40+
[[operation]]
41+
[operation.patch]
42+
file = "feature.patch"
43+
-- give/layer/feature.patch --
44+
diff --git a/file.txt b/file.txt
45+
index df967b96a579e45a18b8251732d16804b2e56a55..a7453f07505c42ea8d6fdda75fa91710c81c53d6 100644
46+
--- a/file.txt
47+
+++ b/file.txt
48+
@@ -1 +1 @@
49+
-base
50+
+feature
51+
-- give/upstream.txt --
52+
upstream
53+
-- give/resolved.txt --
54+
resolved
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Deleting a projection refuses to remove a directory that replaced its workspace.
2+
3+
mkdir definition/layers
4+
mv give/layer definition/layers/0001-base
5+
cd definition
6+
git init -q -b main
7+
exec layerctl projection create demo --worktree $WORK/workspace
8+
9+
# The recorded path now points to another directory with a file to preserve.
10+
mv $WORK/workspace $WORK/original-workspace
11+
mkdir $WORK/workspace
12+
cp $WORK/give/keep.txt $WORK/workspace/keep.txt
13+
14+
! exec layerctl projection delete demo
15+
stderr '^layerctl: refuse to delete composition "demo": '
16+
cmp $WORK/workspace/keep.txt $WORK/give/keep.txt
17+
18+
-- give/layer/layer.toml --
19+
[vcs]
20+
commit-msg = "Start projection"
21+
-- give/keep.txt --
22+
Unrelated file

0 commit comments

Comments
 (0)