Skip to content

Commit de58f24

Browse files
committed
repo: add more GetRepositories() tests, fix bug
Add a few more cases to TestRepos_GetRepositories() including an error from the filesystem, a single repo case, and multiple repositories. Be sure to match the expected output of ReadFileLines() which is to trim the newlines. In the multiple repositories case, I added an empty line to check the case of skipping newlines. In an earlier version of this commit, I had thought there was a bug, but it was due to me including newlines in the mocked output. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 4ad2a8c commit de58f24

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

internal/core/repo_test.go

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core_test
22

33
import (
4+
"errors"
45
"os/user"
56
"testing"
67

@@ -26,6 +27,59 @@ var getRepositoriesTests = []struct {
2627
[]core.Repository{},
2728
false,
2829
},
30+
{
31+
"error from filesystem",
32+
NewPair([]string{}, errors.New("error")),
33+
[]core.Repository{},
34+
true,
35+
},
36+
{
37+
"one repository",
38+
NewPair[[]string, error]([]string{
39+
"git/git",
40+
}, nil),
41+
[]core.Repository{
42+
{
43+
Route: "git/git",
44+
RepoDir: "/my/test/dir/git-bundle-server/git/git/git",
45+
WebDir: "/my/test/dir/git-bundle-server/www/git/git",
46+
},
47+
},
48+
false,
49+
},
50+
{
51+
"multiple repositories",
52+
NewPair[[]string, error]([]string{
53+
"git/git",
54+
"github/github",
55+
"org with spaces/repo with spaces",
56+
"", // Skips empty lines.
57+
"three/deep/repo",
58+
}, nil),
59+
[]core.Repository{
60+
{
61+
Route: "git/git",
62+
RepoDir: "/my/test/dir/git-bundle-server/git/git/git",
63+
WebDir: "/my/test/dir/git-bundle-server/www/git/git",
64+
},
65+
{
66+
Route: "github/github",
67+
RepoDir: "/my/test/dir/git-bundle-server/git/github/github",
68+
WebDir: "/my/test/dir/git-bundle-server/www/github/github",
69+
},
70+
{
71+
Route: "org with spaces/repo with spaces",
72+
RepoDir: "/my/test/dir/git-bundle-server/git/org with spaces/repo with spaces",
73+
WebDir: "/my/test/dir/git-bundle-server/www/org with spaces/repo with spaces",
74+
},
75+
{
76+
Route: "three/deep/repo",
77+
RepoDir: "/my/test/dir/git-bundle-server/git/three/deep/repo",
78+
WebDir: "/my/test/dir/git-bundle-server/www/three/deep/repo",
79+
},
80+
},
81+
false,
82+
},
2983
}
3084

3185
func TestRepos_GetRepositories(t *testing.T) {
@@ -38,7 +92,7 @@ func TestRepos_GetRepositories(t *testing.T) {
3892

3993
for _, tt := range getRepositoriesTests {
4094
t.Run(tt.title, func(t *testing.T) {
41-
testFileSystem.On("UserHomeDir").Return("~", nil)
95+
testFileSystem.On("UserHomeDir").Return("/my/test/dir", nil)
4296
testFileSystem.On("ReadFileLines",
4397
mock.AnythingOfType("string"),
4498
).Return(tt.readFileLines.First, tt.readFileLines.Second).Once()

0 commit comments

Comments
 (0)