Files
sam 3cf025fb4a
Quality / check (push) Successful in 6m46s
deploy / gate (push) Successful in 4m35s
deploy / archied-image (push) Successful in 2m55s
deploy / agent-image (push) Successful in 12s
deploy / dist-zip (push) Skipped
fix(worktree): mirror fetch follows moved tags and rewritten branches
2026-11-03 10:00:04 +11:00

150 lines
4.2 KiB
Go

package worktree
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing"
"github.com/go-git/go-git/v6/plumbing/object"
)
func TestPruneMirrorsRemovesOnlyIdleUnusedMirrorsInsideRoot(t *testing.T) {
now := time.Now()
old := now.Add(-48 * time.Hour)
tests := []struct {
name string
setup func(t *testing.T, repo, outside string)
remove bool
}{
{name: "idle and alone", remove: true, setup: func(t *testing.T, repo, _ string) {
mirror(t, repo, old)
}},
{name: "a task clone borrows it", setup: func(t *testing.T, repo, _ string) {
mirror(t, repo, old)
mkdir(t, filepath.Join(repo, "issue-1"))
}},
{name: "used recently", setup: func(t *testing.T, repo, _ string) {
mirror(t, repo, now.Add(-time.Hour))
}},
{name: "a clone holds the lock", setup: func(t *testing.T, repo, _ string) {
mirror(t, repo, old)
unlock, err := lockMirror(filepath.Join(repo, mirrorName), false)
if err != nil {
t.Fatal(err)
}
t.Cleanup(unlock)
}},
{name: "symlink to a directory outside the root", setup: func(t *testing.T, repo, outside string) {
mkdir(t, repo)
if err := os.Symlink(outside, filepath.Join(repo, mirrorName)); err != nil {
t.Fatal(err)
}
lock(t, repo, old)
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := t.TempDir()
outside := t.TempDir()
sentinel := filepath.Join(outside, "keep")
if err := os.WriteFile(sentinel, nil, 0o600); err != nil {
t.Fatal(err)
}
repo := filepath.Join(root, "org-x", "owner", "repo")
tt.setup(t, repo, outside)
removed, err := PruneMirrors(context.Background(), root, 24*time.Hour, now)
if err != nil {
t.Fatal(err)
}
_, statErr := os.Lstat(filepath.Join(repo, mirrorName))
if gone := os.IsNotExist(statErr); gone != tt.remove || (len(removed) == 1) != tt.remove {
t.Fatalf("removed %v, mirror gone %v; want removal %v", removed, gone, tt.remove)
}
if _, err := os.Stat(sentinel); err != nil {
t.Fatalf("pruning touched a path outside the root: %v", err)
}
})
}
}
func mirror(t *testing.T, repo string, used time.Time) {
t.Helper()
mkdir(t, filepath.Join(repo, mirrorName, "objects"))
lock(t, repo, used)
}
func lock(t *testing.T, repo string, used time.Time) {
t.Helper()
path := filepath.Join(repo, mirrorName+".lock")
if err := os.WriteFile(path, nil, 0o600); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(path, used, used); err != nil {
t.Fatal(err)
}
}
func mkdir(t *testing.T, dir string) {
t.Helper()
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
}
// A forge may move a tag (a re-cut release, a rolling channel tag); the shared
// mirror must follow it rather than refuse every later fetch.
func TestEnsureMirrorFollowsAMovedTag(t *testing.T) {
// The developer's own git config (commit signing) must not reach the fixture.
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
base := t.TempDir()
upstream, err := git.PlainInit(filepath.Join(base, "o", "r.git"), false)
if err != nil {
t.Fatal(err)
}
commit := func(msg string) plumbing.Hash {
t.Helper()
wt, err := upstream.Worktree()
if err != nil {
t.Fatal(err)
}
h, err := wt.Commit(msg, &git.CommitOptions{AllowEmptyCommits: true, Author: &object.Signature{Name: "t", Email: "t@example.test", When: time.Now()}})
if err != nil {
t.Fatal(err)
}
return h
}
tagAt := func(h plumbing.Hash) {
t.Helper()
_ = upstream.DeleteTag("next")
if _, err := upstream.CreateTag("next", h, nil); err != nil {
t.Fatal(err)
}
}
tagAt(commit("first"))
m := &Manager{WorkDir: t.TempDir(), BaseURL: "file://" + base}
if _, err := m.ensureMirror(context.Background(), "o", "r"); err != nil {
t.Fatal(err)
}
moved := commit("second")
tagAt(moved)
dir, err := m.ensureMirror(context.Background(), "o", "r")
if err != nil {
t.Fatalf("fetch after the tag moved: %v", err)
}
mirrored, err := git.PlainOpen(dir)
if err != nil {
t.Fatal(err)
}
ref, err := mirrored.Tag("next")
if err != nil || ref.Hash() != moved {
t.Fatalf("mirror tag = %v (%v), want %s", ref, err, moved)
}
}