From b5bf664e4b2e6a84e481b43916ff67b5de545111 Mon Sep 17 00:00:00 2001 From: Alberto Carretero Date: Mon, 26 Feb 2024 12:07:43 +0100 Subject: [PATCH] better error reporting for report --- internal/slicer/report.go | 13 +++++++---- internal/slicer/report_test.go | 42 +++++++++++++++++----------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/internal/slicer/report.go b/internal/slicer/report.go index b283c90d..673bfd20 100644 --- a/internal/slicer/report.go +++ b/internal/slicer/report.go @@ -39,14 +39,19 @@ func NewReport(root string) *Report { func (r *Report) Add(slice *setup.Slice, info *fsutil.Info) error { if !strings.HasPrefix(info.Path, r.Root) { - return fmt.Errorf("internal error: cannot add path %q outside out root %q", info.Path, r.Root) + return fmt.Errorf("internal error: cannot add path %q outside of root %q", info.Path, r.Root) } relPath := filepath.Clean("/" + strings.TrimPrefix(info.Path, r.Root)) if entry, ok := r.Entries[relPath]; ok { - if info.Mode != entry.Mode || info.Link != entry.Link || - info.Size != entry.Size || info.Hash != entry.Hash { - return fmt.Errorf("internal error: cannot add conflicting data for path %q", relPath) + if info.Mode != entry.Mode { + return fmt.Errorf("internal error: path %q reported twice with diverging mode: %q != %q", relPath, info.Mode, entry.Mode) + } else if info.Link != entry.Link { + return fmt.Errorf("internal error: path %q reported twice with diverging link: %q != %q", relPath, info.Link, entry.Link) + } else if info.Size != entry.Size { + return fmt.Errorf("internal error: path %q reported twice with diverging size: %d != %d", relPath, info.Size, entry.Size) + } else if info.Hash != entry.Hash { + return fmt.Errorf("internal error: path %q reported twice with diverging hash: %q != %q", relPath, info.Hash, entry.Hash) } entry.Slices[slice] = true r.Entries[relPath] = entry diff --git a/internal/slicer/report_test.go b/internal/slicer/report_test.go index 347dcc65..2ec6b59b 100644 --- a/internal/slicer/report_test.go +++ b/internal/slicer/report_test.go @@ -54,15 +54,15 @@ type sliceAndInfo struct { } var reportTests = []struct { - summary string - sliceAndInfo []sliceAndInfo + summary string + add []sliceAndInfo // indexed by path. expected map[string]slicer.ReportEntry - // error after processing the last sliceAndInfo item. + // error after adding the last [sliceAndInfo]. err string }{{ - summary: "Regular directory", - sliceAndInfo: []sliceAndInfo{{info: sampleDir, slice: oneSlice}}, + summary: "Regular directory", + add: []sliceAndInfo{{info: sampleDir, slice: oneSlice}}, expected: map[string]slicer.ReportEntry{ "/exampleDir": { Path: "/exampleDir", @@ -72,7 +72,7 @@ var reportTests = []struct { }}, }, { summary: "Regular directory added by several slices", - sliceAndInfo: []sliceAndInfo{ + add: []sliceAndInfo{ {info: sampleDir, slice: oneSlice}, {info: sampleDir, slice: otherSlice}, }, @@ -84,8 +84,8 @@ var reportTests = []struct { Link: "", }}, }, { - summary: "Regular file", - sliceAndInfo: []sliceAndInfo{{info: sampleFile, slice: oneSlice}}, + summary: "Regular file", + add: []sliceAndInfo{{info: sampleFile, slice: oneSlice}}, expected: map[string]slicer.ReportEntry{ "/exampleFile": { Path: "/exampleFile", @@ -96,8 +96,8 @@ var reportTests = []struct { Link: "", }}, }, { - summary: "Regular file link", - sliceAndInfo: []sliceAndInfo{{info: sampleLink, slice: oneSlice}}, + summary: "Regular file link", + add: []sliceAndInfo{{info: sampleLink, slice: oneSlice}}, expected: map[string]slicer.ReportEntry{ "/exampleLink": { Path: "/exampleLink", @@ -109,7 +109,7 @@ var reportTests = []struct { }}, }, { summary: "Several entries", - sliceAndInfo: []sliceAndInfo{ + add: []sliceAndInfo{ {info: sampleDir, slice: oneSlice}, {info: sampleFile, slice: otherSlice}, }, @@ -130,7 +130,7 @@ var reportTests = []struct { }}, }, { summary: "Same path, identical files", - sliceAndInfo: []sliceAndInfo{ + add: []sliceAndInfo{ {info: sampleFile, slice: oneSlice}, {info: sampleFile, slice: oneSlice}, }, @@ -145,7 +145,7 @@ var reportTests = []struct { }}, }, { summary: "Error for same path distinct mode", - sliceAndInfo: []sliceAndInfo{ + add: []sliceAndInfo{ {info: sampleFile, slice: oneSlice}, {info: fsutil.Info{ Path: sampleFile.Path, @@ -155,10 +155,10 @@ var reportTests = []struct { Link: sampleFile.Link, }, slice: oneSlice}, }, - err: `internal error: cannot add conflicting data for path "/exampleFile"`, + err: `internal error: path "/exampleFile" reported twice with diverging mode: "----------" != "-rwxrwxrwx"`, }, { summary: "Error for same path distinct hash", - sliceAndInfo: []sliceAndInfo{ + add: []sliceAndInfo{ {info: sampleFile, slice: oneSlice}, {info: fsutil.Info{ Path: sampleFile.Path, @@ -168,10 +168,10 @@ var reportTests = []struct { Link: sampleFile.Link, }, slice: oneSlice}, }, - err: `internal error: cannot add conflicting data for path "/exampleFile"`, + err: `internal error: path "/exampleFile" reported twice with diverging hash: "distinct hash" != "exampleFile_hash"`, }, { summary: "Error for same path distinct size", - sliceAndInfo: []sliceAndInfo{ + add: []sliceAndInfo{ {info: sampleFile, slice: oneSlice}, {info: fsutil.Info{ Path: sampleFile.Path, @@ -181,10 +181,10 @@ var reportTests = []struct { Link: sampleFile.Link, }, slice: oneSlice}, }, - err: `internal error: cannot add conflicting data for path "/exampleFile"`, + err: `internal error: path "/exampleFile" reported twice with diverging size: 0 != 5678`, }, { summary: "Error for same path distinct link", - sliceAndInfo: []sliceAndInfo{ + add: []sliceAndInfo{ {info: sampleFile, slice: oneSlice}, {info: fsutil.Info{ Path: sampleFile.Path, @@ -194,14 +194,14 @@ var reportTests = []struct { Link: "distinct link", }, slice: oneSlice}, }, - err: `internal error: cannot add conflicting data for path "/exampleFile"`, + err: `internal error: path "/exampleFile" reported twice with diverging link: "distinct link" != ""`, }} func (s *S) TestReportAdd(c *C) { for _, test := range reportTests { report := slicer.NewReport("/root/") var err error - for _, si := range test.sliceAndInfo { + for _, si := range test.add { err = report.Add(si.slice, &si.info) } if test.err != "" {