From 01b3f0d95863cb70695828f4224b0ab26a989bd0 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Fri, 20 Nov 2026 16:33:02 -0800 Subject: [PATCH] cli: Test Layerctl workflows with testscript 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. --- .agents/skills/testscript/SKILL.md | 35 +++++++++++ .../skills/testscript/references/format.md | 61 +++++++++++++++++++ AGENTS.md | 3 + cmd/layerctl/script_test.go | 60 ++++++++++++++++++ .../script/check_reports_failed_layer.txt | 24 ++++++++ cmd/layerctl/testdata/script/help.txt | 1 + .../script/layer_add_writes_layer.txt | 46 ++++++++++++++ .../script/layer_refresh_writes_layer.txt | 51 ++++++++++++++++ .../script/projection_continue_conflict.txt | 54 ++++++++++++++++ .../projection_delete_replaced_workspace.txt | 22 +++++++ .../projection_path_saved_workspace.txt | 17 ++++++ go.mod | 6 ++ go.sum | 6 ++ nix/package.nix | 2 +- 14 files changed, 387 insertions(+), 1 deletion(-) create mode 100644 .agents/skills/testscript/SKILL.md create mode 100644 .agents/skills/testscript/references/format.md create mode 100644 cmd/layerctl/script_test.go create mode 100644 cmd/layerctl/testdata/script/check_reports_failed_layer.txt create mode 100644 cmd/layerctl/testdata/script/help.txt create mode 100644 cmd/layerctl/testdata/script/layer_add_writes_layer.txt create mode 100644 cmd/layerctl/testdata/script/layer_refresh_writes_layer.txt create mode 100644 cmd/layerctl/testdata/script/projection_continue_conflict.txt create mode 100644 cmd/layerctl/testdata/script/projection_delete_replaced_workspace.txt create mode 100644 cmd/layerctl/testdata/script/projection_path_saved_workspace.txt diff --git a/.agents/skills/testscript/SKILL.md b/.agents/skills/testscript/SKILL.md new file mode 100644 index 0000000..85aa13b --- /dev/null +++ b/.agents/skills/testscript/SKILL.md @@ -0,0 +1,35 @@ +--- +name: testscript +description: >- + Choose, write, or review Layerctl end-to-end CLI tests in + cmd/layerctl/testdata/script, including workflows that persist Git state + across commands and user-facing failure or recovery behavior. +--- + +# Layerctl test scripts + +Use a script when the promise depends on the built CLI, such as its +arguments, exit status, output, or state observed by a later invocation. +Use Go package tests when a package API is the relevant boundary or when +the case needs precise control of internal state. + +Give each script one user-visible promise. +Keep the manifests, patch inputs, and expected results alongside the commands +that exercise them. +Use real Git and local repositories for sources. +Keep a workflow together when the result depends on earlier invocations. + +Assert the full value or state that the promise covers. +Compare a complete output stream when its format is part of the contract. +For Git state, query the relevant tree, ref, or worktree with Git and compare +the result with an independent expectation. +For a rejected command, check the failure and any state the command promises +to preserve. +When stderr includes Git diagnostics or progress, anchor the Layerctl message +you care about and allow variation in unrelated text. +Match the expected values of paths, refs, and other fields that matter to the +scenario. +Use a regex for fields whose literal value can vary within the contract. + +Read [Script format](references/format.md) when authoring or running a script +for fixture syntax, assertions, and focused test commands. diff --git a/.agents/skills/testscript/references/format.md b/.agents/skills/testscript/references/format.md new file mode 100644 index 0000000..5e86d0f --- /dev/null +++ b/.agents/skills/testscript/references/format.md @@ -0,0 +1,61 @@ +# Script format + +Name each scenario `_.txt` under +`cmd/layerctl/testdata/script`. +The runner starts each script in its own temporary directory, available as +`$WORK`, and puts `layerctl` on `PATH` using the Go test binary. +Git starts with a test author and committer, fixed dates for both, and no +system or user config. +Use `at 2000-01-02T03:04:05Z` to set both dates for subsequent commands +when a scenario depends on commit timing. +Choose explicit branch names when they matter to the scenario. + +Testscript reads a command per line. +Use `#` comments for the promise or phases whose purpose needs explaining. +`exec` runs a program and expects success, and `! exec` expects failure. +Use `git` for Git commands in the script's working directory. +Use `! git` when the command should fail. +Single quotes group arguments and suppress variable expansion. +Shell pipes, redirections, and substitutions require an explicitly invoked +shell. +Use built-in commands such as `cd`, `cp`, `exists`, and `cmp` for fixture +operations. +Lines of the form `-- path --` at the end of the script introduce files +unpacked under `$WORK` before the first command. + +Put input files under `give/` and expected output files under `want/`. +Use `give/repo/` for the initial contents of a Git repository and +`give/layer/` for its layer manifest, patches, and other layer files. +Include the patch itself when its contents are part of the setup. +When a command writes layer files, put the expected manifest and patches +under `want/layer/`. +Omit the expected manifest when the command leaves it unchanged. + +This fragment checks the path printed for a projection named `demo` that the +preceding setup created at `$WORK/workspace`. + +```text +exec layerctl projection path demo +cmpenv stdout $WORK/want/path + +-- want/path -- +$WORK/workspace +``` + +`cmp` compares file contents and accepts `stdout` and `stderr` from the last +program as inputs. +`cmpenv` expands variables in the expected file. +Use `cp stdout saved` to keep output for a later comparison. +`stdout` and `stderr` take regular expressions. +Use `${WORK@R}` outside single-quoted segments to escape regex metacharacters +in the work directory path. + +Run the full script suite or select one script by its filename stem. + +```sh +go test ./cmd/layerctl -run '^TestScript$' +go test ./cmd/layerctl -run '^TestScript$/^help$' -v +``` + +See the [testscript package documentation](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript) +for the full command reference. diff --git a/AGENTS.md b/AGENTS.md index 988c88f..03fe607 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -8,6 +8,9 @@ Layerctl composes Git sources and ordered operations into projection workspaces. 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. +When choosing how to cover a CLI behavior or working on a test script, +read [the testscript skill](.agents/skills/testscript/SKILL.md). + ## Packaging and releases `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. diff --git a/cmd/layerctl/script_test.go b/cmd/layerctl/script_test.go new file mode 100644 index 0000000..c8861fa --- /dev/null +++ b/cmd/layerctl/script_test.go @@ -0,0 +1,60 @@ +package main + +import ( + "os" + "testing" + "time" + + "github.com/rogpeppe/go-internal/testscript" +) + +func TestMain(m *testing.M) { + testscript.Main(m, map[string]func(){ + "layerctl": main, + }) +} + +func TestScript(t *testing.T) { + testscript.Run(t, testscript.Params{ + Dir: "testdata/script", + Setup: func(env *testscript.Env) error { + env.Setenv("GIT_CONFIG_NOSYSTEM", "1") + env.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) + env.Setenv("GIT_AUTHOR_NAME", "Layerctl Test") + env.Setenv("GIT_AUTHOR_EMAIL", "layerctl@example.test") + env.Setenv("GIT_COMMITTER_NAME", "Layerctl Test") + env.Setenv("GIT_COMMITTER_EMAIL", "layerctl@example.test") + setGitDate(env.Setenv, "2000-01-01T00:00:00Z") + return nil + }, + Cmds: map[string]func(*testscript.TestScript, bool, []string){ + "git": scriptGit, + "at": scriptAt, + }, + }) +} + +func scriptGit(ts *testscript.TestScript, neg bool, args []string) { + err := ts.Exec("git", args...) + if neg { + if err == nil { + ts.Fatalf("unexpected git command success") + } + return + } + ts.Check(err) +} + +func scriptAt(ts *testscript.TestScript, neg bool, args []string) { + if neg || len(args) != 1 { + ts.Fatalf("usage: at ") + } + date, err := time.Parse(time.RFC3339, args[0]) + ts.Check(err) + setGitDate(ts.Setenv, date.Format(time.RFC3339)) +} + +func setGitDate(setenv func(key, value string), date string) { + setenv("GIT_AUTHOR_DATE", date) + setenv("GIT_COMMITTER_DATE", date) +} diff --git a/cmd/layerctl/testdata/script/check_reports_failed_layer.txt b/cmd/layerctl/testdata/script/check_reports_failed_layer.txt new file mode 100644 index 0000000..00b6946 --- /dev/null +++ b/cmd/layerctl/testdata/script/check_reports_failed_layer.txt @@ -0,0 +1,24 @@ +# Check reports which layer failed during composition. + +# Make the script executable so the failure comes from running the operation. +mkdir definition/layers +mv give/layer definition/layers/0001-fail +cd definition +git init -q -b main +chmod 0755 layers/0001-fail/fail.sh + +! exec layerctl check +stderr '0001-fail' +stderr 'fail\.sh' + +-- give/layer/layer.toml -- +[vcs] +commit-msg = "Fail the check" + +[[operation]] +[operation.script] +file = "fail.sh" +-- give/layer/fail.sh -- +#!/bin/sh +printf '%s: intentional failure\n' "${0##*/}" >&2 +exit 23 diff --git a/cmd/layerctl/testdata/script/help.txt b/cmd/layerctl/testdata/script/help.txt new file mode 100644 index 0000000..d44b7d9 --- /dev/null +++ b/cmd/layerctl/testdata/script/help.txt @@ -0,0 +1 @@ +exec layerctl --help diff --git a/cmd/layerctl/testdata/script/layer_add_writes_layer.txt b/cmd/layerctl/testdata/script/layer_add_writes_layer.txt new file mode 100644 index 0000000..b06319f --- /dev/null +++ b/cmd/layerctl/testdata/script/layer_add_writes_layer.txt @@ -0,0 +1,46 @@ +# Layer add records a committed edit as a new layer on disk. + +# Use SHA-1 to match the expected patch and project through the predecessor. +env GIT_DEFAULT_HASH=sha1 +mkdir definition/layers +mv give/layer definition/layers/0001-base +cd definition +git init -q -b main +exec layerctl projection create edit --through 0001-base --worktree $WORK/edit + +# Modify an existing file so the new layer records the change as a patch. +cp $WORK/give/edited.txt $WORK/edit/file.txt +git -C $WORK/edit commit -qam 'Edit file' + +exec layerctl layer add --from edit 0002-edit +cmp layers/0002-edit/layer.toml $WORK/want/layer/layer.toml +cmp layers/0002-edit/patch $WORK/want/layer/patch + +-- give/layer/layer.toml -- +[vcs] +commit-msg = "Add file" + +[[operation]] +[operation.overlay] +directory = "overlay" +-- give/layer/overlay/file.txt -- +original +-- give/edited.txt -- +edited +-- want/layer/layer.toml -- +[vcs] +commit-msg = """ +Edit file +""" + +[[operation]] +[operation.patch] +file = "patch" +-- want/layer/patch -- +diff --git a/file.txt b/file.txt +index 4b48deed3a433909bfd6b6ab3d4b91348b6af464..7663aa741bec2b1328630882586fb7a2cbc8a255 100644 +--- a/file.txt ++++ b/file.txt +@@ -1 +1 @@ +-original ++edited diff --git a/cmd/layerctl/testdata/script/layer_refresh_writes_layer.txt b/cmd/layerctl/testdata/script/layer_refresh_writes_layer.txt new file mode 100644 index 0000000..b768afb --- /dev/null +++ b/cmd/layerctl/testdata/script/layer_refresh_writes_layer.txt @@ -0,0 +1,51 @@ +# Refresh records a committed file edit in the layer on disk. + +# Use SHA-1 to match the expected patch, then install the starting overlay. +env GIT_DEFAULT_HASH=sha1 +mkdir definition/layers +mv give/layer definition/layers/0001-base +cd definition +git init -q -b main + +# Refresh captures committed changes, so commit the edit in the projection. +exec layerctl projection create edit --through 0001-base --worktree $WORK/edit +cp $WORK/give/edited.txt $WORK/edit/file.txt +git -C $WORK/edit commit -qam 'Edit file' + +# The refreshed manifest keeps the overlay and appends a patch for the edit. +exec layerctl layer refresh --from edit 0001-base +cmp layers/0001-base/layer.toml $WORK/want/layer/layer.toml +cmp layers/0001-base/refresh.patch $WORK/want/layer/refresh.patch + +-- give/layer/layer.toml -- +[vcs] +commit-msg = "Add file" + +[[operation]] +[operation.overlay] +directory = "overlay" +-- give/layer/overlay/file.txt -- +original +-- give/edited.txt -- +edited +-- want/layer/layer.toml -- +[vcs] +commit-msg = """ +Edit file +""" + +[[operation]] +[operation.overlay] +directory = "overlay" + +[[operation]] +[operation.patch] +file = "refresh.patch" +-- want/layer/refresh.patch -- +diff --git a/file.txt b/file.txt +index 4b48deed3a433909bfd6b6ab3d4b91348b6af464..7663aa741bec2b1328630882586fb7a2cbc8a255 100644 +--- a/file.txt ++++ b/file.txt +@@ -1 +1 @@ +-original ++edited diff --git a/cmd/layerctl/testdata/script/projection_continue_conflict.txt b/cmd/layerctl/testdata/script/projection_continue_conflict.txt new file mode 100644 index 0000000..dceea36 --- /dev/null +++ b/cmd/layerctl/testdata/script/projection_continue_conflict.txt @@ -0,0 +1,54 @@ +# A fresh invocation resumes a projection by name and commits the resolution. + +# Keep the base commit for Git's three-way apply. Both upstream and +# feature.patch replace the same line from that commit. +env GIT_DEFAULT_HASH=sha1 +git -C give/repo init -q -b main +git -C give/repo add file.txt +git -C give/repo commit -qm base +cp give/upstream.txt give/repo/file.txt +git -C give/repo commit -qam upstream + +# Install the layer and point its clone operation at this run's source repo. +mkdir definition/layers +mv give/layer definition/layers/0001-feature +cd definition +git init -q -b main +exec sed 's|@WORK@|'$WORK'|g' layers/0001-feature/layer.toml +cp stdout layers/0001-feature/layer.toml + +# Creation stops at the conflict and reports how to resume the projection. +! exec layerctl projection create demo --worktree $WORK/workspace +stderr 'resolve it then run layerctl projection continue demo$' + +# Resolve the file in the stopped workspace and check the commit from continue. +cp $WORK/give/resolved.txt $WORK/workspace/file.txt +exec layerctl projection continue demo +git -C $WORK/workspace show HEAD:file.txt +cmp stdout $WORK/give/resolved.txt + +-- give/repo/file.txt -- +base +-- give/layer/layer.toml -- +[vcs] +commit-msg = "Apply feature" + +[[operation]] +[operation.git-clone] +url = "@WORK@/give/repo" + +[[operation]] +[operation.patch] +file = "feature.patch" +-- give/layer/feature.patch -- +diff --git a/file.txt b/file.txt +index df967b96a579e45a18b8251732d16804b2e56a55..a7453f07505c42ea8d6fdda75fa91710c81c53d6 100644 +--- a/file.txt ++++ b/file.txt +@@ -1 +1 @@ +-base ++feature +-- give/upstream.txt -- +upstream +-- give/resolved.txt -- +resolved diff --git a/cmd/layerctl/testdata/script/projection_delete_replaced_workspace.txt b/cmd/layerctl/testdata/script/projection_delete_replaced_workspace.txt new file mode 100644 index 0000000..c84c4bb --- /dev/null +++ b/cmd/layerctl/testdata/script/projection_delete_replaced_workspace.txt @@ -0,0 +1,22 @@ +# Deleting a projection refuses to remove a directory that replaced its workspace. + +mkdir definition/layers +mv give/layer definition/layers/0001-base +cd definition +git init -q -b main +exec layerctl projection create demo --worktree $WORK/workspace + +# The recorded path now points to another directory with a file to preserve. +mv $WORK/workspace $WORK/original-workspace +mkdir $WORK/workspace +cp $WORK/give/keep.txt $WORK/workspace/keep.txt + +! exec layerctl projection delete demo +stderr '^layerctl: refuse to delete composition "demo": ' +cmp $WORK/workspace/keep.txt $WORK/give/keep.txt + +-- give/layer/layer.toml -- +[vcs] +commit-msg = "Start projection" +-- give/keep.txt -- +Unrelated file diff --git a/cmd/layerctl/testdata/script/projection_path_saved_workspace.txt b/cmd/layerctl/testdata/script/projection_path_saved_workspace.txt new file mode 100644 index 0000000..c6914d7 --- /dev/null +++ b/cmd/layerctl/testdata/script/projection_path_saved_workspace.txt @@ -0,0 +1,17 @@ +# A new invocation can look up a projection's saved workspace by name. + +# Use a known workspace path so the lookup has an independent expectation. +mkdir definition/layers +mv give/layer definition/layers/0001-base +cd definition +git init -q -b main +exec layerctl projection create demo --worktree $WORK/workspace + +exec layerctl projection path demo +cmpenv stdout $WORK/want/path + +-- give/layer/layer.toml -- +[vcs] +commit-msg = "Start projection" +-- want/path -- +$WORK/workspace diff --git a/go.mod b/go.mod index bc39ac8..eaf1ad8 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,9 @@ go 1.26 require github.com/BurntSushi/toml v1.5.0 require github.com/alecthomas/kong v1.17.0 + +require ( + github.com/rogpeppe/go-internal v1.16.0 + golang.org/x/sys v0.26.0 // indirect + golang.org/x/tools v0.26.0 // indirect +) diff --git a/go.sum b/go.sum index 5707bd0..744bbbb 100644 --- a/go.sum +++ b/go.sum @@ -8,3 +8,9 @@ github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= +github.com/rogpeppe/go-internal v1.16.0 h1:O9DK+vNMDVGLr2BeZqmpLeMjiMNkuXfcqntWbZV6S5g= +github.com/rogpeppe/go-internal v1.16.0/go.mod h1:DrUVZyrJU+txYW5/1kwtXQSMFio52ZOxX7yM1VHvnxs= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= +golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= diff --git a/nix/package.nix b/nix/package.nix index 021a2de..7f36532 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -13,7 +13,7 @@ buildGoModule (finalAttrs: { pname = "layerctl"; version = "0.2.0"; src = source; - vendorHash = "sha256-OK7q/CQpa4gNWIPWHZPyew2Ym8NPkXqSDSzE92gYrKk="; + vendorHash = "sha256-GeW0Iza5TajDQGqCTJat9fn/eSMZSdc8XHduJxmSdyw="; ldflags = [ "-s" "-w" "-X main.version=${finalAttrs.version}" "-X main.commit=${revision}" ]; subPackages = [ "cmd/layerctl" ]; nativeBuildInputs = [ makeWrapper ];