|
| 1 | +package core_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os/user" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/github/git-bundle-server/internal/core" |
| 9 | + . "github.com/github/git-bundle-server/internal/testhelpers" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/mock" |
| 12 | +) |
| 13 | + |
| 14 | +var getRepositoriesTests = []struct { |
| 15 | + title string |
| 16 | + |
| 17 | + // Expected values |
| 18 | + readFileLines Pair[[]string, error] |
| 19 | + |
| 20 | + // Expected output |
| 21 | + expectedRepos []core.Repository |
| 22 | + expectedErr bool |
| 23 | +}{ |
| 24 | + { |
| 25 | + "empty file, empty list", |
| 26 | + NewPair[[]string, error]([]string{}, nil), |
| 27 | + []core.Repository{}, |
| 28 | + false, |
| 29 | + }, |
| 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 | + }, |
| 83 | +} |
| 84 | + |
| 85 | +func TestRepos_GetRepositories(t *testing.T) { |
| 86 | + testFileSystem := &MockFileSystem{} |
| 87 | + testUser := &user.User{ |
| 88 | + Uid: "123", |
| 89 | + Username: "testuser", |
| 90 | + HomeDir: "/my/test/dir", |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range getRepositoriesTests { |
| 94 | + t.Run(tt.title, func(t *testing.T) { |
| 95 | + testFileSystem.On("UserHomeDir").Return("/my/test/dir", nil) |
| 96 | + testFileSystem.On("ReadFileLines", |
| 97 | + mock.AnythingOfType("string"), |
| 98 | + ).Return(tt.readFileLines.First, tt.readFileLines.Second).Once() |
| 99 | + |
| 100 | + actual, err := core.GetRepositories(testUser, testFileSystem) |
| 101 | + |
| 102 | + if tt.expectedErr { |
| 103 | + assert.NotNil(t, err, "Expected error") |
| 104 | + assert.Nil(t, actual, "Expected nil list") |
| 105 | + } else { |
| 106 | + assert.Nil(t, err, "Expected success") |
| 107 | + assert.NotNil(t, actual, "Expected non-nil list") |
| 108 | + assert.Equal(t, len(tt.expectedRepos), len(actual), "Length mismatch") |
| 109 | + for _, repo := range tt.expectedRepos { |
| 110 | + a := actual[repo.Route] |
| 111 | + |
| 112 | + assert.Equal(t, repo.Route, a.Route) |
| 113 | + assert.Equal(t, repo.RepoDir, a.RepoDir) |
| 114 | + assert.Equal(t, repo.WebDir, a.WebDir) |
| 115 | + } |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments