diff options
| author | 2026-03-04 08:26:56 +0800 | |
|---|---|---|
| committer | 2026-03-04 08:59:53 +0800 | |
| commit | ab7501be34032fb9e5c48726a68ae90a917af9eb (patch) | |
| tree | 20d005647569befea8133e953c3270e8fd2a2a5b /reachability/integration_test.go | |
| parent | *: gofumpt (diff) | |
| signature | No signature | |
*: Lint
Diffstat (limited to 'reachability/integration_test.go')
| -rw-r--r-- | reachability/integration_test.go | 89 |
1 files changed, 75 insertions, 14 deletions
diff --git a/reachability/integration_test.go b/reachability/integration_test.go index 10668006..079ce5fc 100644 --- a/reachability/integration_test.go +++ b/reachability/integration_test.go @@ -47,8 +47,11 @@ func TestWalkCommitsMatchesGitRevList(t *testing.T) { nil, map[objectid.ObjectID]struct{}{merge: {}}, ) + got := oidSetFromSeq(walk.Seq()) - if err := walk.Err(); err != nil { + + err := walk.Err() + if err != nil { t.Fatalf("walk.Err(): %v", err) } @@ -62,12 +65,17 @@ func TestWalkCommitsMatchesGitRevList(t *testing.T) { nil, map[objectid.ObjectID]struct{}{tag2: {}}, ) + peelGot := oidSetFromSeq(peelWalk.Seq()) - if err := peelWalk.Err(); err != nil { + + err = peelWalk.Err() + if err != nil { t.Fatalf("peelWalk.Err(): %v", err) } + wantWithTags := maps.Clone(want) wantWithTags[tag1] = struct{}{} + wantWithTags[tag2] = struct{}{} if !maps.Equal(peelGot, wantWithTags) { t.Fatalf("tag-root commit walk mismatch:\n got=%v\nwant=%v", sortedOIDStrings(peelGot), sortedOIDStrings(wantWithTags)) @@ -104,8 +112,11 @@ func TestWalkObjectsMatchesGitRevListObjects(t *testing.T) { nil, map[objectid.ObjectID]struct{}{head: {}}, ) + got := oidSetFromSeq(walk.Seq()) - if err := walk.Err(); err != nil { + + err := walk.Err() + if err != nil { t.Fatalf("walk.Err(): %v", err) } @@ -119,10 +130,14 @@ func TestWalkObjectsMatchesGitRevListObjects(t *testing.T) { nil, map[objectid.ObjectID]struct{}{tag: {}}, ) + peelGot := oidSetFromSeq(peelWalk.Seq()) - if err := peelWalk.Err(); err != nil { + + err = peelWalk.Err() + if err != nil { t.Fatalf("peelWalk.Err(): %v", err) } + wantFromTag := gitRevListSet(t, testRepo, true, []objectid.ObjectID{tag}, nil) if !maps.Equal(peelGot, wantFromTag) { t.Fatalf("tag-root object walk mismatch:\n got=%v\nwant=%v", sortedOIDStrings(peelGot), sortedOIDStrings(wantFromTag)) @@ -133,11 +148,16 @@ func TestWalkObjectsMatchesGitRevListObjects(t *testing.T) { map[objectid.ObjectID]struct{}{base: {}}, map[objectid.ObjectID]struct{}{head: {}}, ) + withHave := oidSetFromSeq(walkWithHave.Seq()) - if err := walkWithHave.Err(); err != nil { + + err = walkWithHave.Err() + if err != nil { t.Fatalf("walkWithHave.Err(): %v", err) } - if _, ok := withHave[base]; ok { + + _, ok := withHave[base] + if ok { t.Fatalf("walk output unexpectedly contains have commit %s", base) } }) @@ -170,7 +190,9 @@ func TestIsAncestorMatchesGitMergeBase(t *testing.T) { if err != nil { t.Fatalf("IsAncestor(c1, tag): %v", err) } - if want := gitMergeBaseIsAncestor(t, testRepo, c1, c2); got != want { + + want := gitMergeBaseIsAncestor(t, testRepo, c1, c2) + if got != want { t.Fatalf("IsAncestor(c1, tag)=%v, want %v", got, want) } @@ -178,7 +200,9 @@ func TestIsAncestorMatchesGitMergeBase(t *testing.T) { if err != nil { t.Fatalf("IsAncestor(c3, c2): %v", err) } - if want := gitMergeBaseIsAncestor(t, testRepo, c3, c2); got != want { + + want = gitMergeBaseIsAncestor(t, testRepo, c3, c2) + if got != want { t.Fatalf("IsAncestor(c3, c2)=%v, want %v", got, want) } }) @@ -195,12 +219,15 @@ func TestCheckConnectedMissingObject(t *testing.T) { }) _, treeID, commitID := testRepo.MakeCommit(t, "missing") - if err := os.Remove(looseObjectPath(testRepo.Dir(), treeID)); err != nil { + + err := os.Remove(looseObjectPath(testRepo.Dir(), treeID)) + if err != nil { t.Fatalf("remove tree object: %v", err) } r := openReachabilityFromTestRepo(t, testRepo) - err := r.CheckConnected( + + err = r.CheckConnected( reachability.DomainObjects, nil, map[objectid.ObjectID]struct{}{commitID: {}}, @@ -208,10 +235,12 @@ func TestCheckConnectedMissingObject(t *testing.T) { if err == nil { t.Fatal("expected error") } + var missing *reachability.ErrObjectMissing if !errors.As(err, &missing) { t.Fatalf("expected ErrObjectMissing, got %T (%v)", err, err) } + if missing.OID != treeID { t.Fatalf("missing oid = %s, want %s", missing.OID, treeID) } @@ -246,14 +275,21 @@ func TestWalkOnPackedOnlyRepo(t *testing.T) { nil, map[objectid.ObjectID]struct{}{c2: {}}, ) + got := oidSetFromSeq(walk.Seq()) - if err := walk.Err(); err != nil { + + err := walk.Err() + if err != nil { t.Fatalf("walk.Err(): %v", err) } - if _, ok := got[c2]; !ok { + + _, ok := got[c2] + if !ok { t.Fatalf("walk output missing HEAD commit %s", c2) } - if _, ok := got[c1]; !ok { + + _, ok = got[c1] + if !ok { t.Fatalf("walk output missing parent commit %s", c1) } }) @@ -261,16 +297,19 @@ func TestWalkOnPackedOnlyRepo(t *testing.T) { func openReachabilityFromTestRepo(t *testing.T, testRepo *testgit.TestRepo) *reachability.Reachability { t.Helper() + root, err := os.OpenRoot(testRepo.Dir()) if err != nil { t.Fatalf("os.OpenRoot: %v", err) } + t.Cleanup(func() { _ = root.Close() }) repo, err := repository.Open(root) if err != nil { t.Fatalf("repository.Open: %v", err) } + t.Cleanup(func() { _ = repo.Close() }) return reachability.New(repo.Objects()) @@ -278,10 +317,13 @@ func openReachabilityFromTestRepo(t *testing.T, testRepo *testgit.TestRepo) *rea func oidSetFromSeq(seq func(func(objectid.ObjectID) bool)) map[objectid.ObjectID]struct{} { out := make(map[objectid.ObjectID]struct{}) + seq(func(id objectid.ObjectID) bool { out[id] = struct{}{} + return true }) + return out } @@ -298,9 +340,11 @@ func gitRevListSet( if includeObjects { args = append(args, "--objects") } + for _, want := range wants { args = append(args, want.String()) } + if len(haves) > 0 { args = append(args, "--not") for _, have := range haves { @@ -310,21 +354,28 @@ func gitRevListSet( out := testRepo.Run(t, args...) set := make(map[objectid.ObjectID]struct{}) + for line := range strings.SplitSeq(strings.TrimSpace(out), "\n") { line = strings.TrimSpace(line) if line == "" { continue } + tok := line - if i := strings.IndexByte(tok, ' '); i >= 0 { + + i := strings.IndexByte(tok, ' ') + if i >= 0 { tok = tok[:i] } + id, err := objectid.ParseHex(testRepo.Algorithm(), tok) if err != nil { t.Fatalf("parse rev-list oid %q: %v", tok, err) } + set[id] = struct{}{} } + return set } @@ -332,6 +383,7 @@ func gitMergeBaseIsAncestor(t *testing.T, testRepo *testgit.TestRepo, a, b objec t.Helper() // testgit.Run fatals on non-zero status, so we compare merge-base output. mb := testRepo.Run(t, "merge-base", a.String(), b.String()) + return mb == a.String() } @@ -340,33 +392,40 @@ func sortedOIDStrings(set map[objectid.ObjectID]struct{}) []string { for id := range set { out = append(out, id.String()) } + slices.Sort(out) + return out } func looseObjectPath(repoDir string, id objectid.ObjectID) string { hex := id.String() + return filepath.Join(repoDir, "objects", hex[:2], hex[2:]) } func assertPackedOnly(t *testing.T, repoDir string) { t.Helper() + objectsDir := filepath.Join(repoDir, "objects") entries, err := os.ReadDir(objectsDir) if err != nil { t.Fatalf("ReadDir(objects): %v", err) } + for _, entry := range entries { name := entry.Name() if name == "pack" || name == "info" { continue } + if len(name) == 2 && isHexDirName(name) { subEntries, err := os.ReadDir(filepath.Join(objectsDir, name)) if err != nil { t.Fatalf("ReadDir(objects/%s): %v", name, err) } + if len(subEntries) != 0 { t.Fatalf("found loose objects in %s", filepath.Join(objectsDir, name)) } @@ -378,11 +437,13 @@ func isHexDirName(name string) bool { if len(name) != 2 { return false } + for i := range 2 { c := name[i] if (c < '0' || c > '9') && (c < 'a' || c > 'f') { return false } } + return true } |
