Skip to content

Commit 91ebb6f

Browse files
Shmulik Kleintiulpin
authored andcommitted
QD-12615 Add tests for .mailmap support in contributors count
# Conflicts: # core/contributors_mailmap_test.go
1 parent 3bce93d commit 91ebb6f

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

core/contributors_mailmap_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2021-2024 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package core
18+
19+
import (
20+
"os"
21+
"os/exec"
22+
"path/filepath"
23+
"testing"
24+
)
25+
26+
func TestGetContributorsWithMailmap(t *testing.T) {
27+
repo := newTestRepo(t)
28+
29+
repo.commit(t, "John Doe", "[email protected]")
30+
repo.commit(t, "John Doe", "[email protected]")
31+
repo.commit(t, "J. Doe", "[email protected]")
32+
repo.commit(t, "Jane Smith", "[email protected]")
33+
34+
if got := len(GetContributors([]string{repo.dir}, -1, false)); got != 4 {
35+
t.Errorf("Without mailmap: expected 4 contributors, got %d", got)
36+
}
37+
38+
repo.writeMailmap(
39+
40+
41+
42+
`,
43+
)
44+
45+
contributors := GetContributors([]string{repo.dir}, -1, false)
46+
if got := len(contributors); got != 2 {
47+
t.Errorf("With mailmap: expected 2 contributors, got %d", got)
48+
}
49+
50+
johnCommits := countCommitsForEmail(contributors, "[email protected]")
51+
if johnCommits != 3 {
52+
t.Errorf("Expected John Doe to have 3 commits, got %d", johnCommits)
53+
}
54+
}
55+
56+
func TestGetContributorsMailmapNameMapping(t *testing.T) {
57+
repo := newTestRepo(t)
58+
59+
repo.commit(t, "bobby", "[email protected]")
60+
repo.commit(t, "Bob", "[email protected]")
61+
repo.writeMailmap(t, "Robert Smith <[email protected]>\n")
62+
63+
contributors := GetContributors([]string{repo.dir}, -1, false)
64+
65+
if contributors[0].Author.Username != "Robert Smith" {
66+
t.Errorf("Expected canonical name 'Robert Smith', got '%s'", contributors[0].Author.Username)
67+
}
68+
if contributors[0].Count != 2 {
69+
t.Errorf("Expected 2 commits, got %d", contributors[0].Count)
70+
}
71+
}
72+
73+
type testRepo struct {
74+
dir string
75+
counter int
76+
}
77+
78+
func newTestRepo(t *testing.T) *testRepo {
79+
dir := t.TempDir()
80+
runGit(t, dir, nil, "init")
81+
return &testRepo{dir: dir}
82+
}
83+
84+
func (r *testRepo) commit(t *testing.T, name, email string) {
85+
r.counter++
86+
file := filepath.Join(r.dir, "file.txt")
87+
os.WriteFile(file, []byte{byte(r.counter)}, 0644)
88+
runGit(t, r.dir, nil, "add", ".")
89+
runGit(
90+
t,
91+
r.dir,
92+
[]string{
93+
"GIT_AUTHOR_NAME=" + name,
94+
"GIT_AUTHOR_EMAIL=" + email,
95+
"GIT_COMMITTER_NAME=" + name,
96+
"GIT_COMMITTER_EMAIL=" + email,
97+
},
98+
"commit",
99+
"-m",
100+
"commit",
101+
)
102+
}
103+
104+
func (r *testRepo) writeMailmap(t *testing.T, content string) {
105+
if err := os.WriteFile(filepath.Join(r.dir, ".mailmap"), []byte(content), 0644); err != nil {
106+
t.Fatal(err)
107+
}
108+
}
109+
110+
func runGit(t *testing.T, dir string, env []string, args ...string) {
111+
cmd := exec.Command("git", args...)
112+
cmd.Dir = dir
113+
defaultEnv := []string{
114+
"GIT_AUTHOR_NAME=Test",
115+
116+
"GIT_COMMITTER_NAME=Test",
117+
118+
}
119+
cmd.Env = append(os.Environ(), append(defaultEnv, env...)...)
120+
if out, err := cmd.CombinedOutput(); err != nil {
121+
t.Fatalf("git %v: %v\n%s", args, err, out)
122+
}
123+
}
124+
125+
func countCommitsForEmail(contributors []contributor, email string) int {
126+
for _, c := range contributors {
127+
if c.Author.Email == email {
128+
return c.Count
129+
}
130+
}
131+
return 0
132+
}

0 commit comments

Comments
 (0)