write_file_test.go
·
95 lines
·
3.1 KB
| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // TestWriteFileCommit creates files (including nested paths) directly on a |
| 13 | // bare repository through git plumbing, without a working tree. |
| 14 | func TestWriteFileCommit(t *testing.T) { |
| 15 | ctx := context.Background() |
| 16 | tmp := t.TempDir() |
| 17 | |
| 18 | repoPath := filepath.Join(tmp, "repo.git") |
| 19 | if err := InitBare(ctx, repoPath, "main"); err != nil { |
| 20 | t.Fatalf("InitBare: %v", err) |
| 21 | } |
| 22 | |
| 23 | work := filepath.Join(tmp, "work") |
| 24 | if err := os.MkdirAll(work, 0o755); err != nil { |
| 25 | t.Fatalf("mkdir: %v", err) |
| 26 | } |
| 27 | run := func(dir string, args ...string) { |
| 28 | t.Helper() |
| 29 | cmd := exec.CommandContext(ctx, "git", args...) |
| 30 | cmd.Dir = dir |
| 31 | if out, err := cmd.CombinedOutput(); err != nil { |
| 32 | t.Fatalf("git %v: %v (%s)", args, err, string(out)) |
| 33 | } |
| 34 | } |
| 35 | run(work, "init", "-b", "main") |
| 36 | run(work, "config", "user.name", "Alice") |
| 37 | run(work, "config", "user.email", "alice@example.com") |
| 38 | if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("# Repo\n"), 0o644); err != nil { |
| 39 | t.Fatalf("write: %v", err) |
| 40 | } |
| 41 | run(work, "add", ".") |
| 42 | run(work, "commit", "-m", "initial") |
| 43 | run(work, "push", repoPath, "main") |
| 44 | |
| 45 | hash, err := WriteFileCommit(ctx, repoPath, "main", "docs/guide.md", []byte("hello\n"), "Add docs/guide.md", "Alice", "alice@example.com") |
| 46 | if err != nil { |
| 47 | t.Fatalf("WriteFileCommit: %v", err) |
| 48 | } |
| 49 | if hash == "" { |
| 50 | t.Fatalf("expected commit hash") |
| 51 | } |
| 52 | |
| 53 | blob, err := ReadBlob(ctx, repoPath, "main", "docs/guide.md") |
| 54 | if err != nil { |
| 55 | t.Fatalf("ReadBlob: %v", err) |
| 56 | } |
| 57 | if string(blob.Data) != "hello\n" { |
| 58 | t.Fatalf("unexpected blob content: %q", string(blob.Data)) |
| 59 | } |
| 60 | |
| 61 | out, err := runGit(ctx, repoPath, 10_000_000_000, "log", "-1", "--format=%s|%an|%ae", "main") |
| 62 | if err != nil { |
| 63 | t.Fatalf("log: %v", err) |
| 64 | } |
| 65 | line := strings.TrimSpace(string(out)) |
| 66 | if line != "Add docs/guide.md|Alice|alice@example.com" { |
| 67 | t.Fatalf("unexpected commit metadata: %s", line) |
| 68 | } |
| 69 | |
| 70 | // A second write replaces the file and keeps history. |
| 71 | if _, err := WriteFileCommit(ctx, repoPath, "main", "docs/guide.md", []byte("hello v2\n"), "Update docs/guide.md", "Bob", "bob@example.com"); err != nil { |
| 72 | t.Fatalf("second WriteFileCommit: %v", err) |
| 73 | } |
| 74 | if n := CountCommits(ctx, repoPath, "main"); n != 3 { |
| 75 | t.Fatalf("expected 3 commits, got %d", n) |
| 76 | } |
| 77 | blob, err = ReadBlob(ctx, repoPath, "main", "docs/guide.md") |
| 78 | if err != nil { |
| 79 | t.Fatalf("ReadBlob v2: %v", err) |
| 80 | } |
| 81 | if string(blob.Data) != "hello v2\n" { |
| 82 | t.Fatalf("unexpected blob content: %q", string(blob.Data)) |
| 83 | } |
| 84 | |
| 85 | // Invalid paths and refs must be rejected. |
| 86 | if _, err := WriteFileCommit(ctx, repoPath, "main", "../evil.txt", []byte("x"), "", "Alice", "alice@example.com"); err == nil { |
| 87 | t.Fatalf("expected invalid path to be rejected") |
| 88 | } |
| 89 | if _, err := WriteFileCommit(ctx, repoPath, "main", ".git/config", []byte("x"), "", "Alice", "alice@example.com"); err == nil { |
| 90 | t.Fatalf("expected .git path to be rejected") |
| 91 | } |
| 92 | if _, err := WriteFileCommit(ctx, repoPath, "no-such-branch", "file.txt", []byte("x"), "", "Alice", "alice@example.com"); err == nil { |
| 93 | t.Fatalf("expected missing branch to be rejected") |
| 94 | } |
| 95 | } |
| 96 |