Skip to content

Commit 4ad2a8c

Browse files
committed
repo: test GetRepositories()
Create a unit test setup for the GetRepositories() method, to ensure that it works correctly with mocks. For this method, we only need to mock FileSystem.ReadFileLines() once, so we can have our tests store exactly one list of lines. Create a fake user.User struct to provide the HomeDir value. The only test implemented right now is the empty file. Signed-off-by: Derrick Stolee <[email protected]>
1 parent e951576 commit 4ad2a8c

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

internal/core/repo_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package core_test
2+
3+
import (
4+
"os/user"
5+
"testing"
6+
7+
"github.com/github/git-bundle-server/internal/core"
8+
. "github.com/github/git-bundle-server/internal/testhelpers"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/mock"
11+
)
12+
13+
var getRepositoriesTests = []struct {
14+
title string
15+
16+
// Expected values
17+
readFileLines Pair[[]string, error]
18+
19+
// Expected output
20+
expectedRepos []core.Repository
21+
expectedErr bool
22+
}{
23+
{
24+
"empty file, empty list",
25+
NewPair[[]string, error]([]string{}, nil),
26+
[]core.Repository{},
27+
false,
28+
},
29+
}
30+
31+
func TestRepos_GetRepositories(t *testing.T) {
32+
testFileSystem := &MockFileSystem{}
33+
testUser := &user.User{
34+
Uid: "123",
35+
Username: "testuser",
36+
HomeDir: "/my/test/dir",
37+
}
38+
39+
for _, tt := range getRepositoriesTests {
40+
t.Run(tt.title, func(t *testing.T) {
41+
testFileSystem.On("UserHomeDir").Return("~", nil)
42+
testFileSystem.On("ReadFileLines",
43+
mock.AnythingOfType("string"),
44+
).Return(tt.readFileLines.First, tt.readFileLines.Second).Once()
45+
46+
actual, err := core.GetRepositories(testUser, testFileSystem)
47+
48+
if tt.expectedErr {
49+
assert.NotNil(t, err, "Expected error")
50+
assert.Nil(t, actual, "Expected nil list")
51+
} else {
52+
assert.Nil(t, err, "Expected success")
53+
assert.NotNil(t, actual, "Expected non-nil list")
54+
assert.Equal(t, len(tt.expectedRepos), len(actual), "Length mismatch")
55+
for _, repo := range tt.expectedRepos {
56+
a := actual[repo.Route]
57+
58+
assert.Equal(t, repo.Route, a.Route)
59+
assert.Equal(t, repo.RepoDir, a.RepoDir)
60+
assert.Equal(t, repo.WebDir, a.WebDir)
61+
}
62+
}
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)