-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitRepository_test.go
132 lines (115 loc) · 2.53 KB
/
gitRepository_test.go
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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestNewGitRepository(t *testing.T) {
workTree := "/home/marisa/proj/go-wyag"
gitDir := filepath.Join(workTree, ".git")
repo, _ := NewGitRepository(workTree, true)
expectedRepo := &GitRepository{
WorkTree: workTree,
GitDir: gitDir,
}
if repo.GitDir != expectedRepo.GitDir {
assertStrings(t, repo.GitDir, expectedRepo.GitDir)
}
}
func TestComputeRepoPath(t *testing.T) {
workTree := "/home/marisa/proj/go-wyag"
repo, _ := NewGitRepository(workTree, true)
tests := []struct {
name string
repoPath string
want string
}{
{
"one path",
repo.computeGitdirPath("alpha"),
"/home/marisa/proj/go-wyag/.git/alpha",
},
{
"multiple paths",
repo.computeGitdirPath("alpha", "beta"),
"/home/marisa/proj/go-wyag/.git/alpha/beta",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.repoPath != tt.want {
assertStrings(t, tt.repoPath, tt.want)
}
})
}
}
func TestMkdirGitdirPath(t *testing.T) {
workTree := "/home/marisa/proj/go-wyag"
repo, _ := NewGitRepository(workTree, true)
// shouldMkdir = false tests
tests1 := []struct {
name string
paths []string
want string
}{
{
"one path (shouldMkdir = false)",
[]string{"alpha"},
"",
},
{
"multiple paths (shouldMkdir = false)",
[]string{"alpha", "beta"},
"",
},
}
// shouldMkdir = true tests
tests2 := []struct {
name string
paths []string
want string
}{
{
"one path (shouldMkdir = true)",
[]string{"alpha"},
"/home/marisa/proj/go-wyag/.git/alpha",
},
{
"multiple paths (shouldMkdir = false)",
[]string{"alpha", "beta"},
"/home/marisa/proj/go-wyag/.git/alpha/beta",
},
}
for _, tt := range tests1 {
t.Run(tt.name, func(t *testing.T) {
gitDirPath, err := repo.mkdirGitdirPath(false, tt.paths...)
if err != nil {
t.Error(err)
}
if gitDirPath != tt.want {
assertStrings(t, gitDirPath, tt.want)
}
})
}
for _, tt := range tests2 {
t.Run(tt.name, func(t *testing.T) {
gitDirPath, err := repo.mkdirGitdirPath(true, tt.paths...)
if err != nil {
t.Error(err)
}
// Remove .git directory and all its children after
// finishing one test so subsequent testings of
// shouldMkdir = false tests work correctly
defer os.RemoveAll(repo.GitDir)
if gitDirPath != tt.want {
assertStrings(t, gitDirPath, tt.want)
}
})
}
}
func assertStrings(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q but expected %q", got, want)
}
}