|
7 | 7 | "os/exec" |
8 | 8 | "path/filepath" |
9 | 9 | "slices" |
| 10 | + "strconv" |
10 | 11 | "strings" |
11 | 12 | "testing" |
12 | 13 |
|
@@ -277,6 +278,87 @@ func Test_DefaultGitter_GetTreeHash(t *testing.T) { |
277 | 278 | } |
278 | 279 | } |
279 | 280 |
|
| 281 | +func Test_DefaultGitter_GetHashesBatch(t *testing.T) { |
| 282 | + repo := t.TempDir() |
| 283 | + runGit(t, repo, nil, "init", "-q") |
| 284 | + runGit(t, repo, nil, "config", "user.email", "test@example.com") |
| 285 | + runGit(t, repo, nil, "config", "user.name", "Test") |
| 286 | + commitAt(t, repo, "a.txt", "a\n", "c1", "2020-01-01T00:00:00Z") |
| 287 | + runGit(t, repo, nil, "tag", "v1.0.0") |
| 288 | + commitAt(t, repo, "a.txt", "a\nb\n", "c2", "2020-01-02T00:00:00Z") |
| 289 | + runGit(t, repo, nil, "tag", "v2.0.0") |
| 290 | + |
| 291 | + g, err := gitsemver.NewDefaultGitter("git", nil) |
| 292 | + if err != nil { |
| 293 | + t.Fatal(err) |
| 294 | + } |
| 295 | + dg, ok := g.(gitsemver.DefaultGitter) |
| 296 | + if !ok { |
| 297 | + t.Fatalf("expected DefaultGitter, got %T", g) |
| 298 | + } |
| 299 | + tags := []string{"v2.0.0", "v1.0.0"} |
| 300 | + batched, err := dg.GetHashesBatch(repo, tags) |
| 301 | + if err != nil { |
| 302 | + t.Fatal(err) |
| 303 | + } |
| 304 | + if len(batched) != len(tags) { |
| 305 | + t.Fatalf("expected %d hashes, got %d", len(tags), len(batched)) |
| 306 | + } |
| 307 | + for i, tag := range tags { |
| 308 | + commit, tree, err := dg.GetHashes(repo, tag) |
| 309 | + if err != nil { |
| 310 | + t.Fatal(err) |
| 311 | + } |
| 312 | + if batched[i].Tag != tag || batched[i].Commit != commit || batched[i].Tree != tree { |
| 313 | + t.Fatalf("unexpected hashes for %q: %+v", tag, batched[i]) |
| 314 | + } |
| 315 | + } |
| 316 | +} |
| 317 | + |
| 318 | +func Test_DefaultGitter_GetHashesBatch_ChunksAndPreservesOrder(t *testing.T) { |
| 319 | + repo := t.TempDir() |
| 320 | + runGit(t, repo, nil, "init", "-q") |
| 321 | + runGit(t, repo, nil, "config", "user.email", "test@example.com") |
| 322 | + runGit(t, repo, nil, "config", "user.name", "Test") |
| 323 | + commitAt(t, repo, "a.txt", "a\n", "c1", "2020-01-01T00:00:00Z") |
| 324 | + |
| 325 | + const count = 130 |
| 326 | + tags := make([]string, 0, count) |
| 327 | + for i := 0; i < count; i++ { |
| 328 | + tag := "t" + strconv.Itoa(i) |
| 329 | + tags = append(tags, tag) |
| 330 | + runGit(t, repo, nil, "tag", tag) |
| 331 | + } |
| 332 | + |
| 333 | + g, err := gitsemver.NewDefaultGitter("git", nil) |
| 334 | + if err != nil { |
| 335 | + t.Fatal(err) |
| 336 | + } |
| 337 | + dg, ok := g.(gitsemver.DefaultGitter) |
| 338 | + if !ok { |
| 339 | + t.Fatalf("expected DefaultGitter, got %T", g) |
| 340 | + } |
| 341 | + wantCommit, wantTree, err := dg.GetHashes(repo, tags[0]) |
| 342 | + if err != nil { |
| 343 | + t.Fatal(err) |
| 344 | + } |
| 345 | + batched, err := dg.GetHashesBatch(repo, tags) |
| 346 | + if err != nil { |
| 347 | + t.Fatal(err) |
| 348 | + } |
| 349 | + if len(batched) != len(tags) { |
| 350 | + t.Fatalf("expected %d hashes, got %d", len(tags), len(batched)) |
| 351 | + } |
| 352 | + for i := range tags { |
| 353 | + if batched[i].Tag != tags[i] { |
| 354 | + t.Fatalf("expected tag %q at index %d, got %q", tags[i], i, batched[i].Tag) |
| 355 | + } |
| 356 | + if batched[i].Commit != wantCommit || batched[i].Tree != wantTree { |
| 357 | + t.Fatalf("unexpected hashes for %q: %+v", tags[i], batched[i]) |
| 358 | + } |
| 359 | + } |
| 360 | +} |
| 361 | + |
280 | 362 | func Test_DefaultGitter_GetClosestTag(t *testing.T) { |
281 | 363 | dg, err := gitsemver.NewDefaultGitter("git", nil) |
282 | 364 | if err != nil { |
|
0 commit comments