From ab7501be34032fb9e5c48726a68ae90a917af9eb Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 4 Mar 2026 08:26:56 +0800 Subject: *: Lint --- diff/trees/diff_test.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'diff/trees/diff_test.go') diff --git a/diff/trees/diff_test.go b/diff/trees/diff_test.go index 2fb8540f..1664bdf8 100644 --- a/diff/trees/diff_test.go +++ b/diff/trees/diff_test.go @@ -157,88 +157,112 @@ type diffExpectation struct { func writeTestFile(t *testing.T, path, data string) { t.Helper() - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + + err := os.MkdirAll(filepath.Dir(path), 0o755) + if err != nil { t.Fatalf("create directory for %s: %v", path, err) } - if err := os.WriteFile(path, []byte(data), 0o644); err != nil { + + err = os.WriteFile(path, []byte(data), 0o644) + if err != nil { t.Fatalf("write %s: %v", path, err) } } func openLooseStore(t *testing.T, objectsPath string, algo objectid.Algorithm) *loose.Store { t.Helper() + root, err := os.OpenRoot(objectsPath) if err != nil { t.Fatalf("OpenRoot(%q): %v", objectsPath, err) } + t.Cleanup(func() { _ = root.Close() }) + store, err := loose.New(root, algo) if err != nil { t.Fatalf("loose.New: %v", err) } + t.Cleanup(func() { _ = store.Close() }) + return store } func makeReadTree(t *testing.T, store *loose.Store, algo objectid.Algorithm) func(objectid.ObjectID) (*object.Tree, error) { t.Helper() + return func(id objectid.ObjectID) (*object.Tree, error) { ty, content, err := store.ReadBytesContent(id) if err != nil { return nil, err } + if ty != objecttype.TypeTree { return nil, errors.New("diff/trees test: object is not a tree") } + return object.ParseTree(content, algo) } } func mustReadTree(t *testing.T, readTree func(objectid.ObjectID) (*object.Tree, error), id objectid.ObjectID) *object.Tree { t.Helper() + tree, err := readTree(id) if err != nil { t.Fatalf("read tree %s: %v", id, err) } + return tree } func parseID(t *testing.T, algo objectid.Algorithm, hex string) objectid.ObjectID { t.Helper() + id, err := objectid.ParseHex(algo, hex) if err != nil { t.Fatalf("parse object id %q: %v", hex, err) } + return id } func checkDiffs(t *testing.T, diffs []trees.Entry, expected map[string]diffExpectation) { t.Helper() + got := make(map[string]trees.Entry, len(diffs)) for _, diff := range diffs { path := string(diff.Path) if _, exists := got[path]; exists { t.Fatalf("duplicate diff path %q", path) } + got[path] = diff } + if len(got) != len(expected) { t.Fatalf("diff count = %d, want %d", len(got), len(expected)) } + for path, want := range expected { diff, ok := got[path] if !ok { t.Fatalf("missing diff for %q", path) } + if diff.Kind != want.kind { t.Errorf("%s kind = %v, want %v", path, diff.Kind, want.kind) } + if (diff.Old == nil) != want.oldNil { t.Errorf("%s old nil = %v, want %v", path, diff.Old == nil, want.oldNil) } + if (diff.New == nil) != want.newNil { t.Errorf("%s new nil = %v, want %v", path, diff.New == nil, want.newNil) } + if diff.Kind == trees.EntryKindModified && diff.Old != nil && diff.New != nil && diff.Old.ID == diff.New.ID { t.Errorf("%s modified entry should change IDs", path) } -- cgit v1.3.1-10-gc9f91