forked from muja/goconfig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
repository_test.go
159 lines (144 loc) · 3.42 KB
/
repository_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
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
package goconfig
import (
"path/filepath"
"testing"
testspace "github.com/Jiu2015/gotestspace"
"github.com/stretchr/testify/assert"
)
var repoTestSpace testspace.Space
func initRepoTestSpace(t *testing.T) {
var err error
repoTestSpace, err = testspace.Create(
testspace.WithPathOption("testspace-*"),
testspace.WithShellOption(`
git config --global core.abbrev 10 &&
git config --global init.defaultBranch main &&
git init --bare repo.git &&
git clone repo.git workdir &&
(
cd workdir &&
mkdir a &&
printf "A\n" >a/a.txt &&
git add a &&
test_tick &&
git commit -m "A" &&
mkdir a/b &&
printf "B\n" >a/b/b.txt &&
git add a/b &&
test_tick &&
git commit -m "B" &&
mkdir a/b/c &&
printf "C\n" >a/b/c/c.txt &&
git add a/b/c &&
test_tick &&
git commit -m "C" &&
git push -u origin HEAD
) &&
(
cd workdir &&
git worktree add -b topic1 ../topic1 &&
cd ../topic1 &&
printf "topic1\n" >topic1.txt &&
git add topic1.txt &&
test_tick &&
git commit -m "topic1"
) &&
(
cd workdir &&
git worktree add -b topic2 ../topic2
)
`),
)
assert.Nil(t, err)
}
func TestWithSubtest(t *testing.T) {
// Setup
initRepoTestSpace(t)
t.Run("T=1", subTestRepositoryIsBare)
t.Run("T=2", subTestRepositoryGitPath)
// Tear-down
repoTestSpace.Cleanup()
}
func subTestRepositoryIsBare(t *testing.T) {
var (
repo *Repository
err error
)
for _, tc := range []struct {
Path string
IsBare bool
}{
{"workdir", false},
{"workdir/a/b/c", false},
{"workdir/a/b/c/non-exist/dir", false},
{"repo.git", true},
{"repo.git/refs", true},
{"repo.git/refs/non-exist/dir", true},
{"topic1/a/b/c", false},
{"topic2/a/b/c", false},
} {
repo, err = FindRepository(repoTestSpace.GetPath(tc.Path))
if assert.Nil(t, err) {
assert.Equal(t,
tc.IsBare,
repo.IsBare(),
"Repository at '%s' is a bare? expect: %v, actual: %v",
tc.Path,
tc.IsBare,
repo.IsBare(),
)
assert.Equal(t,
tc.IsBare,
repo.Config().GetBool("core.bare", false),
"Repository at '%s' is a bare? expect: %v, config: %v",
tc.Path,
tc.IsBare,
repo.Config().GetBool("core.bare", false),
)
}
}
}
func subTestRepositoryGitPath(t *testing.T) {
var (
repo *Repository
err error
)
baseDir := repoTestSpace.GetPath("")
baseDir, _ = filepath.EvalSymlinks(baseDir)
getRelDir := func(dir string) string {
dir, _ = filepath.EvalSymlinks(dir)
dir, _ = filepath.Rel(baseDir, dir)
return dir
}
for _, tc := range []struct {
Path string
GitDir string
CommonDir string
WorkDir string
}{
{"workdir", "workdir/.git", "workdir/.git", "workdir"},
{"workdir/a/b/c", "workdir/.git", "workdir/.git", "workdir"},
{"workdir/a/b/c/non-exist/dir", "workdir/.git", "workdir/.git", "workdir"},
{"repo.git", "repo.git", "repo.git", ""},
{"repo.git/refs", "repo.git", "repo.git", ""},
{"repo.git/refs/non-exist/dir", "repo.git", "repo.git", ""},
{"topic1/a/b/c", "workdir/.git/worktrees/topic1", "workdir/.git", "topic1"},
{"topic2/a", "workdir/.git/worktrees/topic2", "workdir/.git", "topic2"},
} {
repo, err = FindRepository(repoTestSpace.GetPath(tc.Path))
if assert.Nil(t, err) {
assert.Equal(t,
tc.GitDir,
getRelDir(repo.GitDir()),
)
assert.Equal(t,
tc.CommonDir,
getRelDir(repo.GitCommonDir()),
)
assert.Equal(t,
tc.WorkDir,
getRelDir(repo.WorkDir()),
)
}
}
}