|
5 | 5 | "errors"
|
6 | 6 | "os/user"
|
7 | 7 | "path/filepath"
|
| 8 | + "strings" |
8 | 9 | "testing"
|
9 | 10 |
|
10 | 11 | "github.com/github/git-bundle-server/internal/common"
|
@@ -256,3 +257,83 @@ func TestRepos_ReadRepositoryStorage(t *testing.T) {
|
256 | 257 | })
|
257 | 258 | }
|
258 | 259 | }
|
| 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