internal/gitutil/commitfile_test.go
38 lines · 1162 bytes
1 symbol in this file
1package gitutil
2
3import "testing"
4
5func TestCommitFileChangeEmptyRepo(t *testing.T) {
6 dir := t.TempDir()
7 if err := InitBare(dir, "main", ""); err != nil {
8 t.Fatal(err)
9 }
10 sha, err := CommitFileChange(dir, "main", "profile/README.md",
11 []byte("# hello\n"), "alice", "alice@example.org", "add about")
12 if err != nil {
13 t.Fatalf("first commit into an empty repository: %v", err)
14 }
15 if sha == "" {
16 t.Fatal("no sha returned")
17 }
18 raw, err := ReadBlob(dir, "main", "profile/README.md", 1<<20)
19 if err != nil {
20 t.Fatalf("reading it back: %v", err)
21 }
22 if string(raw) != "# hello\n" {
23 t.Errorf("read back %q", raw)
24 }
25
26 // The second commit still takes the parented path.
27 if _, err := CommitFileChange(dir, "main", "profile/README.md",
28 []byte("# hello again\n"), "alice", "alice@example.org", "edit"); err != nil {
29 t.Fatalf("second commit: %v", err)
30 }
31
32 // An unknown branch in a repository that has history is a typo, not a
33 // new orphan branch.
34 if _, err := CommitFileChange(dir, "nope", "x.md",
35 []byte("x"), "alice", "alice@example.org", "x"); err == nil {
36 t.Error("committing to an unknown branch of a non-empty repository succeeded")
37 }
38}