Skip to content

Commit f112462

Browse files
committed
repo: make method to write all repositories public
Rename the 'core.RepositoryProvider' function 'writeRouteFile()' to 'WriteAllRoutes()' to make it accessible from other packages. The ability to make "bulk" changes to the repository registry and commit all at once will be useful in future patches, such as rebuilding the route list based on the repositories on disk. Signed-off-by: Victoria Dye <[email protected]>
1 parent 4c638f2 commit f112462

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

internal/core/repo.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Repository struct {
2121
type RepositoryProvider interface {
2222
CreateRepository(ctx context.Context, route string) (*Repository, error)
2323
GetRepositories(ctx context.Context) (map[string]Repository, error)
24+
WriteAllRoutes(ctx context.Context, repos map[string]Repository) error
2425
ReadRepositoryStorage(ctx context.Context) (map[string]Repository, error)
2526
RemoveRoute(ctx context.Context, route string) error
2627
}
@@ -80,7 +81,7 @@ func (r *repoProvider) CreateRepository(ctx context.Context, route string) (*Rep
8081

8182
repos[route] = repo
8283

83-
err = r.writeRouteFile(repos)
84+
err = r.WriteAllRoutes(ctx, repos)
8485
if err != nil {
8586
return nil, fmt.Errorf("warning: failed to write route file")
8687
}
@@ -104,23 +105,22 @@ func (r *repoProvider) RemoveRoute(ctx context.Context, route string) error {
104105

105106
delete(repos, route)
106107

107-
return r.writeRouteFile(repos)
108+
return r.WriteAllRoutes(ctx, repos)
108109
}
109110

110-
func (r *repoProvider) writeRouteFile(repos map[string]Repository) error {
111+
func (r *repoProvider) WriteAllRoutes(ctx context.Context, repos map[string]Repository) error {
111112
user, err := r.user.CurrentUser()
112113
if err != nil {
113114
return err
114115
}
115116
routefile := filepath.Join(bundleroot(user), "routes")
116117

117118
contents := ""
118-
119119
for routes := range repos {
120120
contents = contents + routes + "\n"
121121
}
122122

123-
return os.WriteFile(routefile, []byte(contents), 0o600)
123+
return r.fileSystem.WriteFile(routefile, []byte(contents))
124124
}
125125

126126
func (r *repoProvider) GetRepositories(ctx context.Context) (map[string]Repository, error) {

internal/core/repo_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"os/user"
77
"path/filepath"
8+
"strings"
89
"testing"
910

1011
"github.com/github/git-bundle-server/internal/common"
@@ -256,3 +257,83 @@ func TestRepos_ReadRepositoryStorage(t *testing.T) {
256257
})
257258
}
258259
}
260+
261+
var writeAllRoutesTests = []struct {
262+
title string
263+
repos map[string]core.Repository
264+
expectedFile []string
265+
}{
266+
{
267+
"empty repo map",
268+
map[string]core.Repository{},
269+
[]string{""},
270+
},
271+
{
272+
"single repo",
273+
map[string]core.Repository{
274+
"test/route": {Route: "test/route"},
275+
},
276+
[]string{
277+
"test/route",
278+
},
279+
},
280+
{
281+
"multiple repos",
282+
map[string]core.Repository{
283+
"test/route": {Route: "test/route"},
284+
"another/repo": {Route: "another/repo"},
285+
},
286+
[]string{
287+
"test/route",
288+
"another/repo",
289+
},
290+
},
291+
}
292+
293+
func TestRepos_WriteAllRoutes(t *testing.T) {
294+
testLogger := &MockTraceLogger{}
295+
testFileSystem := &MockFileSystem{}
296+
testUser := &user.User{
297+
Uid: "123",
298+
Username: "testuser",
299+
HomeDir: "/my/test/dir",
300+
}
301+
testUserProvider := &MockUserProvider{}
302+
testUserProvider.On("CurrentUser").Return(testUser, nil)
303+
repoProvider := core.NewRepositoryProvider(testLogger, testUserProvider, testFileSystem, nil)
304+
305+
for _, tt := range writeAllRoutesTests {
306+
t.Run(tt.title, func(t *testing.T) {
307+
var actualFilename string
308+
var actualFileBytes []byte
309+
310+
testFileSystem.On("WriteFile",
311+
mock.MatchedBy(func(filename string) bool {
312+
actualFilename = filename
313+
return true
314+
}),
315+
mock.MatchedBy(func(fileBytes any) bool {
316+
// Save off value and always match
317+
actualFileBytes = fileBytes.([]byte)
318+
return true
319+
}),
320+
).Return(nil).Once()
321+
322+
err := repoProvider.WriteAllRoutes(context.Background(), tt.repos)
323+
assert.Nil(t, err)
324+
mock.AssertExpectationsForObjects(t, testUserProvider, testFileSystem)
325+
326+
// Check filename
327+
expectedFilename := filepath.Clean("/my/test/dir/git-bundle-server/routes")
328+
assert.Equal(t, expectedFilename, actualFilename)
329+
330+
// Check routes file contents
331+
fileLines := strings.Split(strings.TrimSpace(string(actualFileBytes)), "\n")
332+
333+
assert.ElementsMatch(t, tt.expectedFile, fileLines)
334+
335+
// Reset mocks
336+
testFileSystem.Mock = mock.Mock{}
337+
})
338+
}
339+
}

0 commit comments

Comments
 (0)