package gitutil import ( "bufio" "context" "io" "net" "os" "os/exec" "path/filepath" "strings" "sync" "testing" "time" ) const testSparseDir = "/.tangled/workflows" func TestSparseSync(t *testing.T) { // ignore the developer's git config t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) t.Setenv("GIT_CONFIG_SYSTEM", os.DevNull) t.Setenv("GIT_AUTHOR_NAME", "spindle") t.Setenv("GIT_AUTHOR_EMAIL", "spindle@tangled.org") t.Setenv("GIT_COMMITTER_NAME", "spindle") t.Setenv("GIT_COMMITTER_EMAIL", "spindle@tangled.org") src := t.TempDir() run := func(args ...string) string { t.Helper() cmd := exec.Command("git", append([]string{"-C", src}, args...)...) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("git %v: %v: %s", args, err, out) } return strings.TrimSpace(string(out)) } run("init", "-b", "main") if err := os.MkdirAll(filepath.Join(src, ".tangled", "workflows"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(src, ".tangled", "workflows", "ci.yml"), []byte("when: []\n"), 0o644); err != nil { t.Fatal(err) } run("add", ".") run("commit", "-m", "workflows") rev := run("rev-parse", "HEAD") path := filepath.Join(t.TempDir(), "repo") for _, stage := range []string{"clone", "fetch"} { if err := SparseSync(context.Background(), src, path, rev, testSparseDir); err != nil { t.Fatalf("%s: %v", stage, err) } if _, err := os.Stat(filepath.Join(path, ".tangled", "workflows", "ci.yml")); err != nil { t.Fatalf("%s: sparse file missing from the checkout: %v", stage, err) } } } // hangingKnot keeps the child process alive until the client closes its socket func hangingKnot(t *testing.T) (url string, asked, dropped <-chan struct{}) { t.Helper() ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } t.Cleanup(func() { ln.Close() }) askedCh := make(chan struct{}) droppedCh := make(chan struct{}) var askOnce, dropOnce sync.Once go func() { for { conn, err := ln.Accept() if err != nil { return } go func() { defer conn.Close() br := bufio.NewReader(conn) for { line, err := br.ReadString('\n') if err != nil { return } if strings.TrimSpace(line) == "" { break } } io.WriteString(conn, "HTTP/1.1 200 OK\r\n"+ "Content-Type: application/x-git-upload-pack-advertisement\r\n"+ "Transfer-Encoding: chunked\r\n\r\n") askOnce.Do(func() { close(askedCh) }) // this ends only when the child process dies io.Copy(io.Discard, br) dropOnce.Do(func() { close(droppedCh) }) }() } }() return "http://" + ln.Addr().String(), askedCh, droppedCh } func TestRunCancelKillsRemoteHelper(t *testing.T) { knot, asked, dropped := hangingKnot(t) path := filepath.Join(t.TempDir(), "repo") ctx, cancel := context.WithCancel(context.Background()) defer cancel() done := make(chan error, 1) go func() { done <- Run(ctx, "clone", "--no-checkout", "--depth=1", knot+"/repo.git", path) }() select { case <-asked: case err := <-done: t.Fatalf("clone returned before it reached the knot: %v", err) case <-time.After(30 * time.Second): t.Fatal("git never asked the knot for refs") } cancel() select { case err := <-done: if err == nil { t.Fatal("a cancelled clone reported success") } case <-time.After(15 * time.Second): t.Fatal("Run is still blocked after cancellation: a surviving helper holds the inherited stderr pipe open") } select { case <-dropped: case <-time.After(15 * time.Second): t.Fatal("git-remote-http outlived the cancelled clone") } } func TestSparseSyncCancelReleasesRepoLock(t *testing.T) { knot, asked, _ := hangingKnot(t) path := filepath.Join(t.TempDir(), "repo") ctx, cancel := context.WithCancel(context.Background()) defer cancel() done := make(chan error, 1) go func() { done <- SparseSync(ctx, knot+"/repo.git", path, "HEAD", testSparseDir) }() select { case <-asked: case err := <-done: t.Fatalf("sync returned before it reached the knot: %v", err) case <-time.After(30 * time.Second): t.Fatal("git never asked the knot for refs") } cancel() select { case <-done: case <-time.After(15 * time.Second): t.Fatal("SparseSync is still blocked after cancellation") } // another request must be able to use this repo dead, cancelDead := context.WithCancel(context.Background()) cancelDead() next := make(chan error, 1) go func() { next <- SparseSync(dead, knot+"/repo.git", path, "HEAD", testSparseDir) }() select { case <-next: case <-time.After(15 * time.Second): t.Fatal("a cancelled sync never released the repo lock") } }