|
1 | 1 | package command |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path/filepath" |
4 | 8 | "testing" |
5 | 9 |
|
| 10 | + fixtures "github.com/src-d/go-git-fixtures" |
6 | 11 | "github.com/stretchr/testify/require" |
| 12 | + "gopkg.in/src-d/go-git.v4/plumbing" |
7 | 13 | ) |
8 | 14 |
|
9 | 15 | func TestDirectories(t *testing.T) { |
@@ -70,14 +76,14 @@ func TestDirectories(t *testing.T) { |
70 | 76 | path: "file:///siva/path?bare=true", |
71 | 77 | expected: directory{ |
72 | 78 | Path: "/siva/path", |
73 | | - Bare: true, |
| 79 | + Bare: bareOn, |
74 | 80 | }, |
75 | 81 | }, |
76 | 82 | { |
77 | 83 | path: "file:///siva/path?bare=false", |
78 | 84 | expected: directory{ |
79 | 85 | Path: "/siva/path", |
80 | | - Bare: false, |
| 86 | + Bare: bareOff, |
81 | 87 | }, |
82 | 88 | }, |
83 | 89 | { |
@@ -118,7 +124,7 @@ func TestDirectories(t *testing.T) { |
118 | 124 | expected: directory{ |
119 | 125 | Path: "/siva/path", |
120 | 126 | Format: "git", |
121 | | - Bare: false, |
| 127 | + Bare: bareOff, |
122 | 128 | }, |
123 | 129 | }, |
124 | 130 | { |
@@ -147,3 +153,169 @@ func TestDirectories(t *testing.T) { |
147 | 153 | }) |
148 | 154 | } |
149 | 155 | } |
| 156 | + |
| 157 | +func TestDiscoverBare(t *testing.T) { |
| 158 | + defer func() { |
| 159 | + require.NoError(t, fixtures.Clean()) |
| 160 | + }() |
| 161 | + |
| 162 | + tmpDir, err := ioutil.TempDir("", "gitbase") |
| 163 | + require.NoError(t, err) |
| 164 | + defer os.RemoveAll(tmpDir) |
| 165 | + |
| 166 | + emptyDir := filepath.Join(tmpDir, "empty") |
| 167 | + err = os.Mkdir(emptyDir, 0777) |
| 168 | + require.NoError(t, err) |
| 169 | + |
| 170 | + bareDir := filepath.Join(tmpDir, "bare") |
| 171 | + err = os.Mkdir(bareDir, 0777) |
| 172 | + require.NoError(t, err) |
| 173 | + dir := fixtures.ByTag("worktree").One().DotGit().Root() |
| 174 | + err = os.Rename(dir, filepath.Join(bareDir, "repo")) |
| 175 | + require.NoError(t, err) |
| 176 | + |
| 177 | + nonBareDir := filepath.Join(tmpDir, "non_bare") |
| 178 | + err = os.Mkdir(nonBareDir, 0777) |
| 179 | + require.NoError(t, err) |
| 180 | + dir = fixtures.ByTag("worktree").One().Worktree().Root() |
| 181 | + err = os.Rename(dir, filepath.Join(nonBareDir, "repo")) |
| 182 | + require.NoError(t, err) |
| 183 | + |
| 184 | + tests := []struct { |
| 185 | + path string |
| 186 | + bare bareOpt |
| 187 | + expected bool |
| 188 | + err bool |
| 189 | + }{ |
| 190 | + { |
| 191 | + path: "/does/not/exist", |
| 192 | + err: true, |
| 193 | + }, |
| 194 | + { |
| 195 | + path: emptyDir, |
| 196 | + bare: bareAuto, |
| 197 | + expected: false, |
| 198 | + }, |
| 199 | + { |
| 200 | + path: emptyDir, |
| 201 | + bare: bareOn, |
| 202 | + expected: true, |
| 203 | + }, |
| 204 | + { |
| 205 | + path: emptyDir, |
| 206 | + bare: bareOff, |
| 207 | + expected: false, |
| 208 | + }, |
| 209 | + { |
| 210 | + path: bareDir, |
| 211 | + bare: bareAuto, |
| 212 | + expected: true, |
| 213 | + }, |
| 214 | + { |
| 215 | + path: bareDir, |
| 216 | + bare: bareOn, |
| 217 | + expected: true, |
| 218 | + }, |
| 219 | + { |
| 220 | + path: bareDir, |
| 221 | + bare: bareOff, |
| 222 | + expected: false, |
| 223 | + }, |
| 224 | + { |
| 225 | + path: nonBareDir, |
| 226 | + bare: bareAuto, |
| 227 | + expected: false, |
| 228 | + }, |
| 229 | + { |
| 230 | + path: nonBareDir, |
| 231 | + bare: bareOn, |
| 232 | + expected: true, |
| 233 | + }, |
| 234 | + { |
| 235 | + path: nonBareDir, |
| 236 | + bare: bareOff, |
| 237 | + expected: false, |
| 238 | + }, |
| 239 | + } |
| 240 | + |
| 241 | + for _, test := range tests { |
| 242 | + dir := directory{ |
| 243 | + Path: test.path, |
| 244 | + Bare: test.bare, |
| 245 | + } |
| 246 | + |
| 247 | + t.Run(bareTestName(dir, test.err), func(t *testing.T) { |
| 248 | + bare, err := discoverBare(dir) |
| 249 | + if test.err { |
| 250 | + require.Error(t, err) |
| 251 | + return |
| 252 | + } |
| 253 | + |
| 254 | + require.NoError(t, err) |
| 255 | + require.Equal(t, test.expected, bare) |
| 256 | + }) |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +func bareTestName(d directory, err bool) string { |
| 261 | + bare := "" |
| 262 | + switch d.Bare { |
| 263 | + case bareOn: |
| 264 | + bare = "bare" |
| 265 | + case bareOff: |
| 266 | + bare = "non-bare" |
| 267 | + case bareAuto: |
| 268 | + bare = "auto" |
| 269 | + } |
| 270 | + |
| 271 | + if err { |
| 272 | + bare = "error" |
| 273 | + } |
| 274 | + |
| 275 | + return fmt.Sprintf("%s_%s", d.Path, bare) |
| 276 | +} |
| 277 | + |
| 278 | +func TestCache(t *testing.T) { |
| 279 | + require := require.New(t) |
| 280 | + |
| 281 | + tmpDir, err := ioutil.TempDir("", "gitbase") |
| 282 | + require.NoError(err) |
| 283 | + func() { |
| 284 | + require.NoError(os.RemoveAll(tmpDir)) |
| 285 | + }() |
| 286 | + |
| 287 | + server := &Server{ |
| 288 | + CacheSize: 512, |
| 289 | + Format: "siva", |
| 290 | + Bucket: 0, |
| 291 | + LogLevel: "debug", |
| 292 | + Directories: []string{"../../../_testdata"}, |
| 293 | + IndexDir: tmpDir, |
| 294 | + } |
| 295 | + |
| 296 | + err = server.buildDatabase() |
| 297 | + require.NoError(err) |
| 298 | + |
| 299 | + cache := server.sharedCache |
| 300 | + pool := server.pool |
| 301 | + hash := plumbing.NewHash("dbfab055c70379219cbcf422f05316fdf4e1aed3") |
| 302 | + |
| 303 | + _, ok := cache.Get(hash) |
| 304 | + require.False(ok) |
| 305 | + |
| 306 | + repo, err := pool.GetRepo("015da2f4-6d89-7ec8-5ac9-a38329ea875b") |
| 307 | + require.NoError(err) |
| 308 | + |
| 309 | + _, ok = repo.Cache().Get(hash) |
| 310 | + require.False(ok) |
| 311 | + require.Equal(cache, repo.Cache()) |
| 312 | + |
| 313 | + _, err = repo.CommitObject(hash) |
| 314 | + require.NoError(err) |
| 315 | + |
| 316 | + _, ok = cache.Get(hash) |
| 317 | + require.True(ok) |
| 318 | + |
| 319 | + _, ok = repo.Cache().Get(hash) |
| 320 | + require.True(ok) |
| 321 | +} |
0 commit comments