Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .agents/skills/testscript/SKILL.md
Original file line number Diff line number Diff line change
@@ -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.
61 changes: 61 additions & 0 deletions .agents/skills/testscript/references/format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Script format

Name each scenario `<command>_<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.
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 60 additions & 0 deletions cmd/layerctl/script_test.go
Original file line number Diff line number Diff line change
@@ -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 <RFC3339 timestamp>")
}
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)
}
24 changes: 24 additions & 0 deletions cmd/layerctl/testdata/script/check_reports_failed_layer.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions cmd/layerctl/testdata/script/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exec layerctl --help
46 changes: 46 additions & 0 deletions cmd/layerctl/testdata/script/layer_add_writes_layer.txt
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions cmd/layerctl/testdata/script/layer_refresh_writes_layer.txt
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions cmd/layerctl/testdata/script/projection_continue_conflict.txt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions cmd/layerctl/testdata/script/projection_path_saved_workspace.txt
Original file line number Diff line number Diff line change
@@ -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
Loading