Skip to content

Commit af28053

Browse files
committed
Separate runtime optimizations and harden test runner
1 parent a8351f6 commit af28053

14 files changed

Lines changed: 166 additions & 194 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ Repository operations require Git 2.36+.
1010
go mod download # install deps
1111
go build -o gh-stack . # build
1212
go vet ./... # static analysis. Always run before tests.
13-
go test -race -count=1 -timeout=30m ./... # canonical tests with race detection
14-
go run ./scripts/testsuite -workers 4 # same suite in isolated processes
13+
go run ./scripts/testsuite -workers 4 # full race-enabled suite in isolated processes
1514
```
1615

17-
Run vet before either test command. CI uses the same race-enabled suite on all three OSes, with process-isolated shards and a `Tests complete` manifest/result gate. The runner's JSON artifacts include test inventories and timings; scheduling hints never exclude tests. Do not add `t.Parallel` to tests that change Git mocks, cwd, or environment variables.
16+
Run vet before tests. See [CONTRIBUTING.md](../CONTRIBUTING.md#test) for canonical/targeted commands and runner details. Do not add `t.Parallel` to tests that change Git mocks, cwd, or environment variables.
1817

1918
No Makefile, no code generation, no external linter config. Standard Go toolchain only.
2019

‎.github/workflows/test.yml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ on:
44
push:
55
branches: [main]
66
pull_request:
7-
branches: [main]
87

98
permissions:
109
contents: read

‎AGENTS.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ Repository operations require Git 2.36 or later.
1010
go mod download # install dependencies
1111
go build -o gh-stack . # build (produces ./gh-stack binary)
1212
go vet ./... # static analysis. Run before tests.
13-
go test -race -count=1 -timeout=30m ./... # canonical tests with race detection
14-
go run ./scripts/testsuite -workers 4 # same suite, isolated parallel processes
13+
go run ./scripts/testsuite -workers 4 # full race-enabled suite in isolated processes
1514
```
1615

17-
Always run `go vet` before `go test` or the parallel test runner. CI runs vet and the complete race-enabled suite on every push/PR across ubuntu, windows, and macOS (`test.yml`). Windows and macOS use process-isolated shards; `Tests complete` checks the combined manifests and results. No tests are excluded to shard the suite.
18-
19-
The parallel runner discovers tests from compiled binaries, retains ordinary `go test` support, and writes JSON timing/result artifacts to the printed temporary directory (`-output` selects a parent). Its `-workers` limit bounds test processes; children use `-test.parallel=1`. Scheduling weights in `scripts/testsuite/timings.go` affect balance only, never test membership.
16+
Always run `go vet` before tests. See [CONTRIBUTING.md](CONTRIBUTING.md#test) for the alternative canonical `go test` command, targeted tests, and timing artifacts. CI runs the full platform-applicable suite with race detection on Linux, macOS, and Windows, with a cross-shard completion gate.
2017

2118
There is no Makefile, linter config, or code generation step. The standard Go toolchain is all that's needed.
2219

‎CONTRIBUTING.md‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,31 @@ This produces a `gh-stack` binary in the current directory.
3535

3636
## Test
3737

38+
Run static analysis first, then choose **one** full-suite command.
39+
3840
```sh
39-
# Run static analysis before tests
4041
go vet ./...
42+
```
4143

42-
# Run all tests with race detection in isolated parallel processes
44+
For process-isolated parallel execution:
45+
46+
```sh
4347
go run ./scripts/testsuite -workers 4
48+
```
49+
50+
Alternatively, use the canonical Go command:
4451

45-
# The canonical unsharded suite remains supported
52+
```sh
4653
go test -race -count=1 -timeout=30m ./...
4754
```
4855

49-
The parallel runner uses the same tests on every platform, including native Windows
50-
regressions. It compiles race-enabled test binaries once and isolates process-wide
51-
Git mocks, environment changes, and working directories. `-workers` bounds process
52-
concurrency; each child uses `-test.parallel=1` to avoid multiplying that limit.
53-
Choose a smaller worker count on constrained machines.
56+
The parallel runner executes the complete platform-applicable suite, including
57+
native Windows regressions. It compiles race-enabled test binaries once and
58+
isolates Git mocks, environment changes, and working directories between workers.
59+
Tests within a worker still need to restore their state. `-workers` bounds process
60+
concurrency; each child uses `-test.parallel=1`. Choose fewer workers on constrained
61+
machines. External deadlines also cover initialization and `TestMain`, with a
62+
five-second grace period for native timeout diagnostics and race-runtime shutdown.
5463

5564
Results are saved in a printed temporary directory, or under the directory supplied
5665
with `-output`. Each run contains JSON event logs and a `report.json` with the test
@@ -73,10 +82,6 @@ shards. Missing results, failures, or cancelled shards fail the check. Test
7382
membership comes from Go's test binaries, not the advisory scheduling weights in
7483
`scripts/testsuite/timings.go`; newly added tests are included automatically.
7584

76-
The former `TestDistributedModify_AllActions/<action>/<layout>` cases now run as
77-
`TestDistributedModify_{Rename,Insert,Drop,FoldDown,FoldUp,Reorder,RenameInsertAndDrop}/<layout>`.
78-
Both occupied and mixed layouts and their assertions are retained.
79-
8085
## Vet
8186

8287
```sh

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,10 @@ export GH_STACK_THEME=light && gh stack view
770770
| 7 | Rebase already in progress |
771771
| 8 | Stack is locked by another process |
772772

773+
## Contributing
774+
775+
See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and testing instructions.
776+
773777
## License
774778

775779
This project is licensed under the terms of the MIT open source license. Please refer to the [LICENSE](LICENSE) file for the full terms.

‎internal/git/gitops.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func CurrentOps() Ops {
124124
// --- defaultOps method implementations ---
125125

126126
func (d *defaultOps) GitDir() (string, error) {
127-
return d.directory("--absolute-git-dir")
127+
return d.path("rev-parse", "--absolute-git-dir")
128128
}
129129

130130
func (d *defaultOps) RootDir() (string, error) {

‎internal/git/gitops_test.go‎

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,39 +1058,6 @@ func TestIntegration_ForWorktreeIndependentExecutors(t *testing.T) {
10581058
assert.Equal(t, "main", gitExec(t, dir, "branch", "--show-current"))
10591059
}
10601060

1061-
func TestIntegration_WorktreeDirectoriesReuseOnlyCurrentValidation(t *testing.T) {
1062-
root, dir := setupWorktreeRepo(t)
1063-
linked, path := addTestWorktree(t, root, dir, "feature")
1064-
tracePath := filepath.Join(t.TempDir(), "trace.jsonl")
1065-
t.Setenv("GIT_TRACE2_EVENT", tracePath)
1066-
common, err := linked.CommonDir()
1067-
require.NoError(t, err)
1068-
assert.Equal(t, canonicalGitTestPath(t, filepath.Join(dir, ".git")), common)
1069-
gitDir, err := linked.GitDir()
1070-
require.NoError(t, err)
1071-
assert.NotEqual(t, common, gitDir)
1072-
requireWorktree(t, linked, path)
1073-
trace, err := os.ReadFile(tracePath)
1074-
require.NoError(t, err)
1075-
probes := 0
1076-
for _, line := range strings.Split(strings.TrimSpace(string(trace)), "\n") {
1077-
var event struct {
1078-
Event string
1079-
Argv []string
1080-
}
1081-
require.NoError(t, json.Unmarshal([]byte(line), &event))
1082-
if event.Event == "start" && slices.Contains(event.Argv, "rev-parse") {
1083-
probes++
1084-
}
1085-
}
1086-
assert.Equal(t, 8, probes, "directory queries reuse current validation; selecting a scope still validates both directories")
1087-
require.NoError(t, os.Rename(path, path+"-moved"))
1088-
_, err = linked.CommonDir()
1089-
require.Error(t, err, "the previous validation must not be cached across calls")
1090-
_, err = linked.GitDir()
1091-
require.Error(t, err)
1092-
}
1093-
10941061
func TestIntegration_WorktreeMissingCheckoutDoesNotReportStagedChanges(t *testing.T) {
10951062
t.Parallel()
10961063
root, dir := setupWorktreeRepo(t)

‎internal/git/worktree.go‎

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,81 +32,42 @@ func (d *defaultOps) path(args ...string) (string, error) {
3232
}
3333

3434
func (d *defaultOps) CommonDir() (string, error) {
35-
return d.directory("--git-common-dir")
35+
return d.path("rev-parse", "--path-format=absolute", "--git-common-dir")
3636
}
3737

38-
type validatedGitClient struct {
39-
client *cligit.Client
40-
gitDir string
41-
commonDir string
42-
}
43-
44-
func (d *defaultOps) inspectGitClient() (validatedGitClient, error) {
38+
func (d *defaultOps) gitClient() (*cligit.Client, error) {
4539
c := d.client
4640
if c == nil {
4741
c = client
4842
}
49-
validated := validatedGitClient{client: c}
5043
for _, directory := range []struct {
5144
option string
5245
info os.FileInfo
53-
path *string
5446
}{
55-
{"--absolute-git-dir", d.gitDir, &validated.gitDir},
56-
{"--git-common-dir", d.commonDir, &validated.commonDir},
47+
{"--absolute-git-dir", d.gitDir},
48+
{"--git-common-dir", d.commonDir},
5749
} {
5850
if directory.info == nil {
5951
continue
6052
}
6153
cmd, err := c.Command(context.Background(), "rev-parse", "--path-format=absolute", directory.option)
6254
if err != nil {
63-
return validatedGitClient{}, err
55+
return nil, err
6456
}
6557
d.configureCommand(cmd)
6658
out, err := cmd.Output()
6759
if err != nil {
68-
return validatedGitClient{}, fmt.Errorf("inspecting worktree %q: %w", c.RepoDir, err)
60+
return nil, fmt.Errorf("inspecting worktree %q: %w", c.RepoDir, err)
6961
}
70-
path := strings.TrimSuffix(string(out), "\n")
71-
info, err := os.Stat(path)
62+
info, err := os.Stat(strings.TrimSuffix(string(out), "\n"))
7263
if err != nil {
73-
return validatedGitClient{}, fmt.Errorf("inspecting worktree %q: %w", c.RepoDir, err)
64+
return nil, fmt.Errorf("inspecting worktree %q: %w", c.RepoDir, err)
7465
}
7566
if !os.SameFile(directory.info, info) {
76-
return validatedGitClient{}, fmt.Errorf("worktree %q no longer refers to the selected Git directory; rediscover worktrees before continuing", c.RepoDir)
67+
return nil, fmt.Errorf("worktree %q no longer refers to the selected Git directory; rediscover worktrees before continuing", c.RepoDir)
7768
}
78-
*directory.path = path
7969
}
80-
return validated, nil
81-
}
82-
83-
func (d *defaultOps) gitClient() (*cligit.Client, error) {
84-
validated, err := d.inspectGitClient()
85-
return validated.client, err
86-
}
87-
88-
func (d *defaultOps) directory(option string) (string, error) {
89-
validated, err := d.inspectGitClient()
90-
if err != nil {
91-
return "", err
92-
}
93-
// Reuse only paths validated by this call, never across commands.
94-
if option == "--absolute-git-dir" && validated.gitDir != "" {
95-
return validated.gitDir, nil
96-
}
97-
if option == "--git-common-dir" && validated.commonDir != "" {
98-
return validated.commonDir, nil
99-
}
100-
cmd, err := validated.client.Command(context.Background(), "rev-parse", "--path-format=absolute", option)
101-
if err != nil {
102-
return "", err
103-
}
104-
d.configureCommand(cmd)
105-
out, err := cmd.Output()
106-
if err != nil {
107-
return "", err
108-
}
109-
return strings.TrimSuffix(string(out), "\n"), nil
70+
return c, nil
11071
}
11172

11273
// ForWorktree resolves relative paths against this receiver's execution
@@ -115,17 +76,13 @@ func (d *defaultOps) ForWorktree(path string) (Ops, error) {
11576
if path == "" {
11677
return nil, errors.New("worktree path must not be empty")
11778
}
118-
validated, err := d.inspectGitClient()
79+
c, err := d.gitClient()
11980
if err != nil {
12081
return nil, err
12182
}
122-
c := validated.client
123-
common := validated.commonDir
124-
if common == "" {
125-
common, err = d.CommonDir()
126-
if err != nil {
127-
return nil, fmt.Errorf("locating the source repository: %w", err)
128-
}
83+
common, err := d.CommonDir()
84+
if err != nil {
85+
return nil, fmt.Errorf("locating the source repository: %w", err)
12986
}
13087
expectedCommon, err := os.Stat(common)
13188
if err != nil {

‎internal/worktree/context.go‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,46 +200,41 @@ func CheckClean(ops git.Ops, path string) error {
200200
}
201201

202202
func (c *Context) Preflight(branches []string) error {
203-
_, err := c.preflight(branches)
204-
return err
205-
}
206-
207-
func (c *Context) preflight(branches []string) (map[string]git.Ops, error) {
208203
checked := make(map[string]bool)
209-
executors := make(map[string]git.Ops, len(branches))
210204
for _, branch := range branches {
211205
ops, err := c.Ops(branch)
212206
if err != nil {
213-
return nil, err
207+
return err
214208
}
215209
location := c.Location(branch)
216210
if !checked[location.Path] {
217211
if err := CheckClean(ops, location.Path); err != nil {
218-
return nil, err
212+
return err
219213
}
220214
checked[location.Path] = true
221215
}
222216
if !SamePath(location.Path, c.Origin.Path) {
223217
current, err := ops.CurrentBranch()
224218
if err != nil {
225-
return nil, fmt.Errorf("checking current branch in worktree %s: %w", location.Path, err)
219+
return fmt.Errorf("checking current branch in worktree %s: %w", location.Path, err)
226220
}
227221
if current != branch {
228-
return nil, fmt.Errorf("worktree %s no longer has branch %s checked out", location.Path, branch)
222+
return fmt.Errorf("worktree %s no longer has branch %s checked out", location.Path, branch)
229223
}
230224
}
231-
executors[branch] = ops
232225
}
233-
return executors, nil
226+
return nil
234227
}
235228

236229
// Prepare may switch the initiating worktree, but never a different worktree.
237230
func (c *Context) Prepare(branch string) (git.Ops, error) {
238-
executors, err := c.preflight([]string{branch})
231+
if err := c.Preflight([]string{branch}); err != nil {
232+
return nil, err
233+
}
234+
ops, err := c.Ops(branch)
239235
if err != nil {
240236
return nil, err
241237
}
242-
ops := executors[branch]
243238
current, err := ops.CurrentBranch()
244239
if err != nil {
245240
return nil, err

‎internal/worktree/context_test.go‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -248,40 +248,3 @@ func TestStartRejectsCommitsAfterSnapshot(t *testing.T) {
248248
assert.Empty(t, ctx.Pending)
249249
assert.Empty(t, ctx.Touched)
250250
}
251-
252-
func TestPrepareReusesOnlyTheCurrentPreflightExecutor(t *testing.T) {
253-
common := t.TempDir()
254-
current := "main"
255-
resolutions := 0
256-
scoped := &git.MockOps{
257-
CommonDirFn: func() (string, error) { return common, nil },
258-
GitDirFn: func() (string, error) { return common, nil },
259-
CurrentBranchFn: func() (string, error) { return current, nil },
260-
CheckoutBranchFn: func(branch string) error {
261-
current = branch
262-
return nil
263-
},
264-
}
265-
restore := git.SetOps(&git.MockOps{
266-
CommonDirFn: func() (string, error) { return common, nil },
267-
ForWorktreeFn: func(string) (git.Ops, error) {
268-
resolutions++
269-
return scoped, nil
270-
},
271-
})
272-
defer restore()
273-
ctx := &Context{Origin: Location{Path: common}}
274-
for i := 1; i <= 2; i++ {
275-
ops, err := ctx.Prepare("feature")
276-
require.NoError(t, err)
277-
assert.Same(t, scoped, ops)
278-
assert.Equal(t, i, resolutions, "resolve once per preparation, not once per context")
279-
assert.Equal(t, "feature", current)
280-
}
281-
scoped.HasUncommittedChangesFn = func() (bool, error) { return true, nil }
282-
ops, err := ctx.Prepare("other")
283-
require.ErrorContains(t, err, "uncommitted changes")
284-
assert.Nil(t, ops)
285-
assert.Equal(t, 3, resolutions)
286-
assert.Equal(t, "feature", current)
287-
}

0 commit comments

Comments
 (0)