Something went wrong. Try again.
An unofficial terminal client for Tangled, optimized for humans and agents. forked from aly.codes/tg
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183package gitutil
import ( "bytes" "compress/gzip" "context" "fmt" "io" "os" "os/exec" "path/filepath" "strings" "testing")
func TestMain(m *testing.M) { for _, variable := range []string{ "GIT_CONFIG", "GIT_CONFIG_PARAMETERS", "GIT_CONFIG_SYSTEM", } { if err := os.Unsetenv(variable); err != nil { panic(fmt.Errorf("unset %s: %w", variable, err)) } }
settings := []struct { name string value string }{ {name: "GIT_CONFIG_COUNT", value: "0"}, {name: "GIT_CONFIG_GLOBAL", value: os.DevNull}, {name: "GIT_CONFIG_NOSYSTEM", value: "1"}, } for _, setting := range settings { if err := os.Setenv(setting.name, setting.value); err != nil { panic(fmt.Errorf("set %s: %w", setting.name, err)) } }
os.Exit(m.Run())}
func TestGenerateAndCheckoutPatch(t *testing.T) { ctx := context.Background() tempDir := t.TempDir() originDir := filepath.Join(tempDir, "origin.git") repoDir := filepath.Join(tempDir, "repo") runGit(t, tempDir, "init", "--bare", originDir) runGit(t, tempDir, "clone", originDir, repoDir) runGit(t, repoDir, "config", "user.name", "Test User") runGit(t, repoDir, "config", "user.email", "test@example.com") runGit(t, repoDir, "switch", "-c", "main")
writeTestFile(t, filepath.Join(repoDir, "message.txt"), "base\n") runGit(t, repoDir, "add", "message.txt") runGit(t, repoDir, "commit", "-m", "base") runGit(t, repoDir, "push", "-u", "origin", "main")
runGit(t, repoDir, "switch", "-c", "feature") writeTestFile(t, filepath.Join(repoDir, "message.txt"), "feature\n") runGit(t, repoDir, "add", "message.txt") runGit(t, repoDir, "commit", "-m", "feature") compressedPatch, err := GeneratePatch(ctx, repoDir, "main", "feature") if err != nil { t.Fatalf("GeneratePatch() error = %v", err) } patch := decompressTestPatch(t, compressedPatch)
err = CheckoutPatch(ctx, CheckoutPatchParams{ RepoDir: repoDir, Branch: "review", TargetBranch: "main", Patch: patch, }) if err != nil { t.Fatalf("CheckoutPatch() error = %v", err) } if branch := gitOutputForTest(t, repoDir, "branch", "--show-current"); branch != "review" { t.Fatalf("checked out branch = %q, want review", branch) } contents, err := os.ReadFile(filepath.Join(repoDir, "message.txt")) if err != nil { t.Fatal(err) } if string(contents) != "feature\n" { t.Fatalf("checked out contents = %q, want feature", contents) }}
func TestGeneratePatchPrefersExplicitLocalBaseOverOrigin(t *testing.T) { ctx := context.Background() tempDir := t.TempDir() originDir := filepath.Join(tempDir, "origin.git") repoDir := filepath.Join(tempDir, "repo") runGit(t, tempDir, "init", "--bare", originDir) runGit(t, tempDir, "clone", originDir, repoDir) runGit(t, repoDir, "config", "user.name", "Test User") runGit(t, repoDir, "config", "user.email", "test@example.com") runGit(t, repoDir, "switch", "-c", "main") commitTestFile(t, repoDir, "root.txt", "root\n", "root") runGit(t, repoDir, "push", "-u", "origin", "main") commitTestFile(t, repoDir, "base.txt", "base\n", "local base") runGit(t, repoDir, "switch", "-c", "feature") commitTestFile(t, repoDir, "feature.txt", "feature\n", "feature")
compressedPatch, err := GeneratePatch(ctx, repoDir, "main", "feature") if err != nil { t.Fatalf("GeneratePatch() error = %v", err) } patch := string(decompressTestPatch(t, compressedPatch)) if !strings.Contains(patch, "Subject: [PATCH] feature") { t.Fatalf("patch does not contain only the feature commit:\n%s", patch) } if strings.Contains(patch, "local base") { t.Fatalf("patch unexpectedly contains the local base commit:\n%s", patch) }}
func TestCheckoutPatchRejectsDirtyWorktree(t *testing.T) { repoDir := t.TempDir() runGit(t, repoDir, "init") writeTestFile(t, filepath.Join(repoDir, "untracked.txt"), "dirty\n")
err := CheckoutPatch(context.Background(), CheckoutPatchParams{RepoDir: repoDir}) if err == nil || !strings.Contains(err.Error(), "uncommitted changes") { t.Fatalf("CheckoutPatch() error = %v, want uncommitted changes error", err) }}
func TestValidateBranchDoesNotStreamStdout(t *testing.T) { var stdout bytes.Buffer client := NewClient(&stdout, io.Discard)
if err := client.validateBranch(context.Background(), t.TempDir(), "review"); err != nil { t.Fatalf("validateBranch() error = %v", err) } if stdout.Len() != 0 { t.Fatalf("validateBranch() streamed stdout %q", stdout.String()) }}
func runGit(t *testing.T, dir string, args ...string) { t.Helper() cmd := exec.Command("git", args...) cmd.Dir = dir if output, err := cmd.CombinedOutput(); err != nil { t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, output) }}
func gitOutputForTest(t *testing.T, dir string, args ...string) string { t.Helper() cmd := exec.Command("git", args...) cmd.Dir = dir output, err := cmd.CombinedOutput() if err != nil { t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, output) } return strings.TrimSpace(string(output))}
func writeTestFile(t *testing.T, path, contents string) { t.Helper() if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { t.Fatal(err) }}
func decompressTestPatch(t *testing.T, compressed []byte) []byte { t.Helper() reader, err := gzip.NewReader(bytes.NewReader(compressed)) if err != nil { t.Fatal(err) } defer reader.Close() patch, err := io.ReadAll(reader) if err != nil { t.Fatal(err) } return patch}