aboutsummaryrefslogtreecommitdiff
path: root/object/commit/append_test.go
blob: 93b5fcd186466955246494551cf9da0b080b5081 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package commit_test

import (
	"bytes"
	"strings"
	"testing"

	"lindenii.org/go/furgit/internal/testgit"
	"lindenii.org/go/furgit/object/commit"
	"lindenii.org/go/furgit/object/id"
	"lindenii.org/go/furgit/object/signature"
	"lindenii.org/go/furgit/object/typ"
)

func TestAppendGitFsck(t *testing.T) {
	t.Parallel()

	for _, objectFormat := range id.SupportedObjectFormats() {
		t.Run(objectFormat.String(), func(t *testing.T) {
			t.Parallel()

			repo, err := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectFormat})
			if err != nil {
				t.Fatalf("NewRepo: %v", err)
			}

			blobID, err := repo.HashObject(t, typ.TypeBlob, strings.NewReader("content\n"))
			if err != nil {
				t.Fatalf("HashObject(blob): %v", err)
			}

			treeID, err := repo.MkTree(t, []testgit.MkTreeEntry{
				{Mode: "100644", Type: typ.TypeBlob, OID: blobID, Name: "file.txt"},
			})
			if err != nil {
				t.Fatalf("MkTree: %v", err)
			}

			commitObject := &commit.Commit{
				Tree: treeID,
				Author: signature.Signature{
					Name:          []byte("Test Author"),
					Email:         []byte("author@example.org"),
					WhenUnix:      1234567890,
					OffsetMinutes: 0,
				},
				Committer: signature.Signature{
					Name:          []byte("Test Committer"),
					Email:         []byte("committer@example.org"),
					WhenUnix:      1234567890,
					OffsetMinutes: 0,
				},
				Message: []byte("subject\n\nbody\n"),
			}

			rawBody, err := commitObject.AppendWithoutHeader(nil)
			if err != nil {
				t.Fatalf("AppendWithoutHeader: %v", err)
			}

			commitID, err := repo.HashObject(t, typ.TypeCommit, bytes.NewReader(rawBody))
			if err != nil {
				t.Fatalf("HashObject(commit): %v", err)
			}

			err = repo.Fsck(t, testgit.FsckOptions{
				Strict:     true,
				NoDangling: true,
			}, commitID)
			if err != nil {
				t.Fatalf("Fsck: %v", err)
			}

			gitTree, err := repo.ShowFormat(t, commitID, "%T")
			if err != nil {
				t.Fatalf("ShowFormat(%%T): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitTree), "\n"), treeID.String(); got != want {
				t.Fatalf("tree = %q, want %q", got, want)
			}

			gitParents, err := repo.ShowFormat(t, commitID, "%P")
			if err != nil {
				t.Fatalf("ShowFormat(%%P): %v", err)
			}

			if got := strings.TrimSuffix(string(gitParents), "\n"); got != "" {
				t.Fatalf("parents = %q, want none", got)
			}

			gitAuthorName, err := repo.ShowFormat(t, commitID, "%an")
			if err != nil {
				t.Fatalf("ShowFormat(%%an): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitAuthorName), "\n"), "Test Author"; got != want {
				t.Fatalf("author name = %q, want %q", got, want)
			}

			gitAuthorEmail, err := repo.ShowFormat(t, commitID, "%ae")
			if err != nil {
				t.Fatalf("ShowFormat(%%ae): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitAuthorEmail), "\n"), "author@example.org"; got != want {
				t.Fatalf("author email = %q, want %q", got, want)
			}

			gitAuthorTime, err := repo.ShowFormat(t, commitID, "%at")
			if err != nil {
				t.Fatalf("ShowFormat(%%at): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitAuthorTime), "\n"), "1234567890"; got != want {
				t.Fatalf("author time = %q, want %q", got, want)
			}

			gitCommitterName, err := repo.ShowFormat(t, commitID, "%cn")
			if err != nil {
				t.Fatalf("ShowFormat(%%cn): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitCommitterName), "\n"), "Test Committer"; got != want {
				t.Fatalf("committer name = %q, want %q", got, want)
			}

			gitCommitterEmail, err := repo.ShowFormat(t, commitID, "%ce")
			if err != nil {
				t.Fatalf("ShowFormat(%%ce): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitCommitterEmail), "\n"), "committer@example.org"; got != want {
				t.Fatalf("committer email = %q, want %q", got, want)
			}

			gitCommitterTime, err := repo.ShowFormat(t, commitID, "%ct")
			if err != nil {
				t.Fatalf("ShowFormat(%%ct): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitCommitterTime), "\n"), "1234567890"; got != want {
				t.Fatalf("committer time = %q, want %q", got, want)
			}

			gitSubject, err := repo.ShowFormat(t, commitID, "%s")
			if err != nil {
				t.Fatalf("ShowFormat(%%s): %v", err)
			}

			if got, want := strings.TrimSuffix(string(gitSubject), "\n"), "subject"; got != want {
				t.Fatalf("subject = %q, want %q", got, want)
			}

			gitBody, err := repo.ShowFormat(t, commitID, "%b")
			if err != nil {
				t.Fatalf("ShowFormat(%%b): %v", err)
			}

			if got, want := bytes.TrimSuffix(gitBody, []byte{'\n'}), []byte("body\n"); !bytes.Equal(got, want) {
				t.Fatalf("body = %q, want %q", got, want)
			}
		})
	}
}