Skip to content

Commit a8351f6

Browse files
committed
Speed up race-enabled tests with isolated process shards
1 parent a644af6 commit a8351f6

19 files changed

Lines changed: 1557 additions & 169 deletions

‎.github/copilot-instructions.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ 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 ./... # tests with race detection
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
1415
```
1516

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.
18+
1619
No Makefile, no code generation, no external linter config. Standard Go toolchain only.
1720

1821
## Project layout

‎.github/workflows/test.yml‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
os: [ubuntu-latest, windows-latest, macos-latest]
17+
include:
18+
- {os: ubuntu-latest, shard: 0, shards: 1, workers: 2}
19+
- {os: windows-latest, shard: 0, shards: 4, workers: 1}
20+
- {os: windows-latest, shard: 1, shards: 4, workers: 1}
21+
- {os: windows-latest, shard: 2, shards: 4, workers: 1}
22+
- {os: windows-latest, shard: 3, shards: 4, workers: 1}
23+
- {os: macos-latest, shard: 0, shards: 4, workers: 1}
24+
- {os: macos-latest, shard: 1, shards: 4, workers: 1}
25+
- {os: macos-latest, shard: 2, shards: 4, workers: 1}
26+
- {os: macos-latest, shard: 3, shards: 4, workers: 1}
27+
name: test (${{ matrix.os }}, shard ${{ matrix.shard }})
1828
runs-on: ${{ matrix.os }}
1929
steps:
2030
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -26,5 +36,39 @@ jobs:
2636
- name: Vet
2737
run: go vet ./...
2838
- name: Test
29-
# The real-Git worktree cases can exceed Go's 10-minute package limit on Windows.
30-
run: go test -race -count=1 -timeout=30m ./...
39+
run: >-
40+
go run ./scripts/testsuite
41+
-workers ${{ matrix.workers }}
42+
-shard ${{ matrix.shard }}
43+
-shards ${{ matrix.shards }}
44+
-output "${{ runner.temp }}/test-results"
45+
- name: Upload test results
46+
if: always()
47+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
48+
with:
49+
name: tests-${{ matrix.os }}-${{ matrix.shard }}
50+
path: ${{ runner.temp }}/test-results
51+
if-no-files-found: error
52+
retention-days: 14
53+
54+
test-complete:
55+
name: Tests complete
56+
if: always()
57+
needs: test
58+
runs-on: ubuntu-latest
59+
steps:
60+
- name: Require successful shards
61+
if: needs.test.result != 'success'
62+
run: |
63+
echo "A required test shard failed, was cancelled, or did not run."
64+
exit 1
65+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
66+
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
67+
with:
68+
go-version-file: go.mod
69+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
70+
with:
71+
pattern: tests-*
72+
path: ${{ runner.temp }}/test-results
73+
- name: Verify full platform inventories
74+
run: go run ./scripts/testsuite -verify "${{ runner.temp }}/test-results"

‎AGENTS.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ 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 ./... # all tests with race detection
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
1415
```
1516

16-
Always run `go vet` before `go test`. CI runs both on every push/PR across ubuntu, windows, and macOS (`test.yml`).
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.
1720

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

@@ -134,7 +137,7 @@ if errors.As(err, &exitErr) { ... }
134137

135138
| Workflow | Trigger | What it does |
136139
|----------|---------|-------------|
137-
| `test.yml` | push to main, PRs | `go vet` + `go test -race -count=1 ./...` on 3 OS matrix |
140+
| `test.yml` | push to main, PRs | `go vet` + full race-enabled process shards on 3 OSes; verified completion gate |
138141
| `release.yml` | `v*` tags | Cross-platform precompiled binaries via `cli/gh-extension-precompile` |
139142
| `docs.yml` | push to main (docs/**) | Builds Astro/Starlight docs, deploys to GitHub Pages |
140143

‎CONTRIBUTING.md‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,47 @@ This produces a `gh-stack` binary in the current directory.
3636
## Test
3737

3838
```sh
39-
# Run all tests with race detection
40-
go test -race -count=1 ./...
39+
# Run static analysis before tests
40+
go vet ./...
41+
42+
# Run all tests with race detection in isolated parallel processes
43+
go run ./scripts/testsuite -workers 4
44+
45+
# The canonical unsharded suite remains supported
46+
go test -race -count=1 -timeout=30m ./...
47+
```
48+
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.
54+
55+
Results are saved in a printed temporary directory, or under the directory supplied
56+
with `-output`. Each run contains JSON event logs and a `report.json` with the test
57+
inventory, assignments, results, runner/toolchain metadata, and build/test timings.
58+
Top-level timings include their subtests; do not add both together.
59+
60+
To run a package or a specific regression:
61+
62+
```sh
63+
go vet ./internal/modify
64+
go run ./scripts/testsuite -workers 4 ./internal/modify
65+
66+
go vet ./cmd
67+
go test -race -count=1 -timeout=30m -run '^TestRebase_Worktrees' ./cmd
4168
```
4269

70+
CI partitions the full suite across Windows and macOS jobs. The `Tests complete`
71+
check verifies that every platform has a complete, consistent set of successful
72+
shards. Missing results, failures, or cancelled shards fail the check. Test
73+
membership comes from Go's test binaries, not the advisory scheduling weights in
74+
`scripts/testsuite/timings.go`; newly added tests are included automatically.
75+
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+
4380
## Vet
4481

4582
```sh
@@ -79,8 +116,8 @@ npm run build # Production build to docs/dist/
79116
1. [Fork][fork] and clone the repository
80117
1. Create a new branch: `git checkout -b my-branch-name`
81118
1. Make your change and add tests
82-
1. Make sure tests pass: `go test -race -count=1 ./...`
83119
1. Make sure vet passes: `go vet ./...`
120+
1. Make sure tests pass: `go run ./scripts/testsuite -workers 4` (or the canonical command above)
84121
1. Push to your fork and [submit a pull request][pr]
85122

86123
Here are a few things you can do that will increase the likelihood of your pull request being accepted:

‎cmd/view_test.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ func TestTimeAgo(t *testing.T) {
4141
}
4242

4343
func TestViewJSON(t *testing.T) {
44-
git.SetOps(&git.MockOps{
44+
original := git.CurrentOps()
45+
t.Cleanup(func() { assert.Same(t, original, git.CurrentOps()) })
46+
restore := git.SetOps(&git.MockOps{
4547
IsAncestorFn: func(ancestor, descendant string) (bool, error) {
4648
return true, nil // all branches are linear
4749
},
4850
})
51+
defer restore()
4952

5053
tests := []struct {
5154
name string
@@ -139,7 +142,9 @@ func TestViewJSON(t *testing.T) {
139142
}
140143

141144
func TestViewJSON_BranchFields(t *testing.T) {
142-
git.SetOps(&git.MockOps{
145+
original := git.CurrentOps()
146+
t.Cleanup(func() { assert.Same(t, original, git.CurrentOps()) })
147+
restore := git.SetOps(&git.MockOps{
143148
IsAncestorFn: func(ancestor, descendant string) (bool, error) {
144149
// feat/02 needs rebase
145150
if descendant == "feat/02" {
@@ -148,6 +153,7 @@ func TestViewJSON_BranchFields(t *testing.T) {
148153
return true, nil
149154
},
150155
})
156+
defer restore()
151157

152158
s := &stack.Stack{
153159
Trunk: stack.BranchRef{Branch: "main", Head: "aaa111"},

‎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.path("rev-parse", "--absolute-git-dir")
127+
return d.directory("--absolute-git-dir")
128128
}
129129

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

‎internal/git/gitops_test.go‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ func writeFile(t *testing.T, dir, name, content string) {
8181
require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(content), 0644))
8282
}
8383

84+
func setupLocalRepo(t *testing.T) string {
85+
t.Helper()
86+
dir := t.TempDir()
87+
gitExec(t, dir, "init", "-q", "-b", "main")
88+
writeFile(t, dir, "init.txt", "hello")
89+
gitExec(t, dir, "add", ".")
90+
gitExec(t, dir, "commit", "-qm", "initial commit")
91+
return dir
92+
}
93+
8494
// withGitDir temporarily sets the git client to operate in dir by changing
8595
// the working directory. Returns a restore function.
8696
func withGitDir(t *testing.T, dir string) func() {
@@ -694,14 +704,13 @@ func TestIntegration_CherryPickQuitLeavesIndexUnmerged(t *testing.T) {
694704

695705
func setupWorktreeRepo(t *testing.T) (*defaultOps, string) {
696706
t.Helper()
697-
_, dir := setupBareAndClone(t)
707+
dir := setupLocalRepo(t)
698708
gitExec(t, dir, "config", "user.name", "Test")
699709
gitExec(t, dir, "config", "user.email", "test@test.com")
700710
gitExec(t, dir, "config", "commit.gpgsign", "false")
701711
gitExec(t, dir, "config", "rerere.enabled", "false")
702712
gitExec(t, dir, "config", "merge.conflictStyle", "merge")
703713
gitExec(t, dir, "config", "rebase.backend", "merge")
704-
t.Cleanup(withGitDir(t, dir))
705714
return &defaultOps{client: &cligit.Client{RepoDir: dir}}, dir
706715
}
707716

@@ -790,15 +799,14 @@ func TestIntegration_WorktreeDirectoriesAndDiscovery(t *testing.T) {
790799
}
791800

792801
func TestIntegration_WorktreePathsPreserveWhitespace(t *testing.T) {
802+
t.Parallel()
793803
for _, name := range []string{"space and \u03bb", " leading and trailing ", "embedded\nand trailing\n", `quotes " and backslash \`} {
794804
t.Run(name, func(t *testing.T) {
795805
if runtime.GOOS == "windows" && (strings.ContainsAny(name, "\n\"\\") || strings.HasSuffix(name, " ")) {
796806
t.Skip("these filename characters are not supported on Windows")
797807
}
798808
root, dir := setupWorktreeRepo(t)
799809
mainPath := filepath.Join(filepath.Dir(dir), "main "+name)
800-
// Windows cannot rename the process's current directory.
801-
require.NoError(t, os.Chdir(filepath.Dir(dir)))
802810
require.NoError(t, os.Rename(dir, mainPath))
803811
root.client.RepoDir = mainPath
804812
linkedPath := filepath.Join(t.TempDir(), name)
@@ -850,12 +858,12 @@ func setupSeparateGitRepo(t *testing.T) (*defaultOps, string, string) {
850858
writeFile(t, dir, "init.txt", "initial")
851859
gitExec(t, dir, "add", ".")
852860
gitExec(t, dir, "commit", "-m", "initial")
853-
t.Cleanup(withGitDir(t, dir))
854861
root := &defaultOps{client: &cligit.Client{RepoDir: dir}}
855862
return root, dir, gitDir
856863
}
857864

858865
func TestIntegration_SeparateGitDirectory(t *testing.T) {
866+
t.Parallel()
859867
root, dir, gitDir := setupSeparateGitRepo(t)
860868
linked, linkedPath := addTestWorktree(t, root, dir, "feature")
861869
for _, scope := range []Ops{root, linked} {
@@ -890,6 +898,7 @@ func TestIntegration_SeparateGitDirectory(t *testing.T) {
890898
}
891899

892900
func TestIntegration_SeparateGitDirectoryBacklink(t *testing.T) {
901+
t.Parallel()
893902
tests := []struct {
894903
name string
895904
relative bool
@@ -948,6 +957,7 @@ func TestIntegration_SeparateGitDirectoryBacklink(t *testing.T) {
948957
}
949958

950959
func TestIntegration_SeparateGitDirectoryMissingBacklink(t *testing.T) {
960+
t.Parallel()
951961
root, dir, gitDir := setupSeparateGitRepo(t)
952962
linked, linkedPath := addTestWorktree(t, root, dir, "feature")
953963
worktrees, err := linked.Worktrees()
@@ -1022,6 +1032,7 @@ func TestIntegration_ForWorktreeRejectsInvalidContext(t *testing.T) {
10221032
}
10231033

10241034
func TestIntegration_ForWorktreeIndependentExecutors(t *testing.T) {
1035+
t.Parallel()
10251036
root, dir := setupWorktreeRepo(t)
10261037
first, _ := addTestWorktree(t, root, dir, "first")
10271038
second, _ := addTestWorktree(t, root, dir, "second")
@@ -1047,7 +1058,41 @@ func TestIntegration_ForWorktreeIndependentExecutors(t *testing.T) {
10471058
assert.Equal(t, "main", gitExec(t, dir, "branch", "--show-current"))
10481059
}
10491060

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+
10501094
func TestIntegration_WorktreeMissingCheckoutDoesNotReportStagedChanges(t *testing.T) {
1095+
t.Parallel()
10511096
root, dir := setupWorktreeRepo(t)
10521097
linked, path := addTestWorktree(t, root, dir, "feature")
10531098
require.NoError(t, os.Rename(path, path+"-moved"))
@@ -1058,6 +1103,7 @@ func TestIntegration_WorktreeMissingCheckoutDoesNotReportStagedChanges(t *testin
10581103
}
10591104

10601105
func TestIntegration_WorktreeStateQueriesRejectChangedScope(t *testing.T) {
1106+
t.Parallel()
10611107
for _, change := range []string{"missing checkout", "removed gitfile", "replaced gitfile"} {
10621108
t.Run(change, func(t *testing.T) {
10631109
root, dir := setupWorktreeRepo(t)
@@ -1096,6 +1142,10 @@ func TestIntegration_WorktreeStateQueriesRejectChangedScope(t *testing.T) {
10961142
child, err := selected.ForWorktree(otherPath)
10971143
require.Error(t, err)
10981144
assert.Nil(t, child)
1145+
_, err = selected.GitDir()
1146+
require.Error(t, err)
1147+
_, err = selected.CommonDir()
1148+
require.Error(t, err)
10991149
require.Error(t, selected.StageAll())
11001150
require.Error(t, selected.ResetHard("HEAD"))
11011151
require.True(t, IsRebaseStartError(selected.Rebase("main", RebaseOpts{})))
@@ -1210,6 +1260,7 @@ func TestIntegration_GitStateQueriesSurfaceFilesystemErrors(t *testing.T) {
12101260
}
12111261

12121262
func TestIntegration_WorktreeRebasePreservesOtherRefsAndConfig(t *testing.T) {
1263+
t.Parallel()
12131264
for _, onto := range []bool{false, true} {
12141265
for _, dates := range []bool{false, true} {
12151266
t.Run(fmt.Sprintf("onto=%t/dates=%t", onto, dates), func(t *testing.T) {
@@ -1612,6 +1663,11 @@ func TestIntegration_WorktreeLocalMutations(t *testing.T) {
16121663
defaultBranch, err := linked.DefaultBranch()
16131664
require.NoError(t, err)
16141665
assert.Equal(t, "main", defaultBranch)
1666+
gitExec(t, dir, "update-ref", "refs/remotes/origin/remote-trunk", "main")
1667+
gitExec(t, dir, "symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/remote-trunk")
1668+
defaultBranch, err = linked.DefaultBranch()
1669+
require.NoError(t, err)
1670+
assert.Equal(t, "remote-trunk", defaultBranch, "a remote HEAD takes precedence over local branch-name fallbacks")
16151671
initial := gitExec(t, dir, "rev-parse", "HEAD")
16161672
writeFile(t, dir, "new.txt", "new main commit\n")
16171673
gitExec(t, dir, "add", ".")

‎internal/git/rebase_start_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func rebaseTestRepo(t *testing.T) string {
1313
t.Helper()
14-
_, clone := setupBareAndClone(t)
14+
clone := setupLocalRepo(t)
1515

1616
gitExec(t, clone, "checkout", "-b", "feature")
1717
writeFile(t, clone, "feature.txt", "feature")

0 commit comments

Comments
 (0)