Skip to content

Commit

Permalink
better error reporting for report
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Feb 26, 2024
1 parent 1554233 commit b5bf664
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
13 changes: 9 additions & 4 deletions internal/slicer/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 21 additions & 21 deletions internal/slicer/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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},
},
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -109,7 +109,7 @@ var reportTests = []struct {
}},
}, {
summary: "Several entries",
sliceAndInfo: []sliceAndInfo{
add: []sliceAndInfo{
{info: sampleDir, slice: oneSlice},
{info: sampleFile, slice: otherSlice},
},
Expand All @@ -130,7 +130,7 @@ var reportTests = []struct {
}},
}, {
summary: "Same path, identical files",
sliceAndInfo: []sliceAndInfo{
add: []sliceAndInfo{
{info: sampleFile, slice: oneSlice},
{info: sampleFile, slice: oneSlice},
},
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 != "" {
Expand Down

0 comments on commit b5bf664

Please sign in to comment.