-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_local_test.go
More file actions
196 lines (168 loc) · 5.32 KB
/
Copy pathcheck_local_test.go
File metadata and controls
196 lines (168 loc) · 5.32 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"os"
"path/filepath"
"testing"
"time"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/haproxytech/check-commit/v5/junit"
)
func commitFile(t *testing.T, wt *git.Worktree, dir, name, content, message, author string) plumbing.Hash {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if _, err := wt.Add(name); err != nil {
t.Fatal(err)
}
hash, err := wt.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: author,
Email: author + "@example.com",
When: time.Now(),
},
})
if err != nil {
t.Fatal(err)
}
return hash
}
func TestGetLocalCommitDataOnlyFreshCommits(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
repo, err := git.PlainInit(dir, false)
if err != nil {
t.Fatal(err)
}
wt, err := repo.Worktree()
if err != nil {
t.Fatal(err)
}
commitFile(t, wt, dir, "a.txt", "a", "MINOR: base: some base commit message", "alice")
base := commitFile(t, wt, dir, "b.txt", "b", "MINOR: base: another published commit", "alice")
// mark everything up to here as published on origin
err = repo.Storer.SetReference(plumbing.NewHashReference("refs/remotes/origin/main", base))
if err != nil {
t.Fatal(err)
}
// fresh commits by different authors: author-change heuristic would stop here
c1 := commitFile(t, wt, dir, "c.txt", "c", "MINOR: one: first fresh commit", "alice")
c2 := commitFile(t, wt, dir, "d.txt", "d", "MINOR: two: second fresh commit", "bob")
commits, diffs, err := getLocalCommitData(&junit.JunitSuiteDummy{})
if err != nil {
t.Fatal(err)
}
if len(commits) != 2 {
t.Fatalf("expected 2 fresh commits, got %d: %+v", len(commits), commits)
}
if len(diffs) != 2 {
t.Fatalf("expected 2 attributed diffs, got %d: %+v", len(diffs), diffs)
}
byHash := map[string]map[string]string{}
for _, d := range diffs {
byHash[d.Hash] = d.Files
}
if _, ok := byHash[shortHash(c1.String())]["c.txt"]; !ok {
t.Errorf("c.txt should be attributed to %s, got %v", shortHash(c1.String()), byHash)
}
if _, ok := byHash[shortHash(c2.String())]["d.txt"]; !ok {
t.Errorf("d.txt should be attributed to %s, got %v", shortHash(c2.String()), byHash)
}
subjects := map[string]bool{}
for _, c := range commits {
subjects[c.Subject] = true
}
if !subjects["MINOR: one: first fresh commit"] || !subjects["MINOR: two: second fresh commit"] {
t.Fatalf("unexpected subjects: %+v", commits)
}
if subjects["MINOR: base: another published commit"] {
t.Fatal("published commit should not be checked")
}
}
func TestGetLocalCommitDataUpstreamRemotePreferred(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
repo, err := git.PlainInit(dir, false)
if err != nil {
t.Fatal(err)
}
wt, err := repo.Worktree()
if err != nil {
t.Fatal(err)
}
base := commitFile(t, wt, dir, "a.txt", "a", "MINOR: base: some base commit message", "alice")
err = repo.Storer.SetReference(plumbing.NewHashReference("refs/remotes/upstream/main", base))
if err != nil {
t.Fatal(err)
}
// pushed to fork (origin) but not yet in upstream: must still be checked
pushed := commitFile(t, wt, dir, "b.txt", "b", "MINOR: one: pushed to fork only", "alice")
err = repo.Storer.SetReference(plumbing.NewHashReference("refs/remotes/origin/feature", pushed))
if err != nil {
t.Fatal(err)
}
commitFile(t, wt, dir, "c.txt", "c", "MINOR: two: local only commit", "alice")
commits, _, err := getLocalCommitData(&junit.JunitSuiteDummy{})
if err != nil {
t.Fatal(err)
}
if len(commits) != 2 {
t.Fatalf("expected 2 commits ahead of upstream, got %d: %+v", len(commits), commits)
}
subjects := map[string]bool{}
for _, c := range commits {
subjects[c.Subject] = true
}
if !subjects["MINOR: one: pushed to fork only"] || !subjects["MINOR: two: local only commit"] {
t.Fatalf("unexpected subjects: %+v", commits)
}
}
func TestGetLocalCommitDataNothingAheadOfRemote(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
repo, err := git.PlainInit(dir, false)
if err != nil {
t.Fatal(err)
}
wt, err := repo.Worktree()
if err != nil {
t.Fatal(err)
}
head := commitFile(t, wt, dir, "a.txt", "a", "MINOR: base: some base commit message", "alice")
err = repo.Storer.SetReference(plumbing.NewHashReference("refs/remotes/origin/main", head))
if err != nil {
t.Fatal(err)
}
commits, diffs, err := getLocalCommitData(&junit.JunitSuiteDummy{})
if err != nil {
t.Fatal(err)
}
if len(commits) != 0 || len(diffs) != 0 {
t.Fatalf("expected nothing to check, got commits=%+v diffs=%+v", commits, diffs)
}
}
func TestGetLocalCommitDataNoRemotesFallsBackToAuthor(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
repo, err := git.PlainInit(dir, false)
if err != nil {
t.Fatal(err)
}
wt, err := repo.Worktree()
if err != nil {
t.Fatal(err)
}
commitFile(t, wt, dir, "a.txt", "a", "MINOR: base: some base commit message", "alice")
commitFile(t, wt, dir, "b.txt", "b", "MINOR: one: first fresh commit", "bob")
commitFile(t, wt, dir, "c.txt", "c", "MINOR: two: second fresh commit", "bob")
commits, _, err := getLocalCommitData(&junit.JunitSuiteDummy{})
if err != nil {
t.Fatal(err)
}
// no remote refs: author-change heuristic collects bob's commits only
if len(commits) != 2 {
t.Fatalf("expected 2 commits from author heuristic, got %d: %+v", len(commits), commits)
}
}