Skip to content

Commit

Permalink
slicer returns Report to allow future testing
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Feb 7, 2024
1 parent 04c7117 commit 22933d4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cmd/chisel/cmd_cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ func (cmd *cmdCut) Execute(args []string) error {
archives[archiveName] = openArchive
}

return slicer.Run(&slicer.RunOptions{
_, err = slicer.Run(&slicer.RunOptions{
Selection: selection,
Archives: archives,
TargetDir: cmd.RootDir,
})
return err
}

// TODO These need testing, and maybe moving into a common file.
Expand Down
26 changes: 13 additions & 13 deletions internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RunOptions struct {
TargetDir string
}

func Run(options *RunOptions) error {
func Run(options *RunOptions) (*Report, error) {

archives := make(map[string]archive.Archive)
extract := make(map[string]map[string][]deb.ExtractInfo)
Expand Down Expand Up @@ -66,7 +66,7 @@ func Run(options *RunOptions) error {
if !filepath.IsAbs(targetDirAbs) {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot obtain current directory: %w", err)
return nil, fmt.Errorf("cannot obtain current directory: %w", err)
}
targetDirAbs = filepath.Join(dir, targetDir)
}
Expand All @@ -78,10 +78,10 @@ func Run(options *RunOptions) error {
archiveName := release.Packages[slice.Package].Archive
archive := options.Archives[archiveName]
if archive == nil {
return fmt.Errorf("archive %q not defined", archiveName)
return nil, fmt.Errorf("archive %q not defined", archiveName)
}
if !archive.Exists(slice.Package) {
return fmt.Errorf("slice package %q missing from archive", slice.Package)
return nil, fmt.Errorf("slice package %q missing from archive", slice.Package)
}
archives[slice.Package] = archive
extractPackage = make(map[string][]deb.ExtractInfo)
Expand Down Expand Up @@ -140,7 +140,7 @@ func Run(options *RunOptions) error {
}
reader, err := archives[slice.Package].Fetch(slice.Package)
if err != nil {
return err
return nil, err
}
defer reader.Close()
packages[slice.Package] = reader
Expand Down Expand Up @@ -171,7 +171,7 @@ func Run(options *RunOptions) error {
reader.Close()
packages[slice.Package] = nil
if err != nil {
return err
return nil, err
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ func Run(options *RunOptions) error {
tarHeader.Typeflag = tar.TypeSymlink
linkTarget = pathInfo.Info
default:
return fmt.Errorf("internal error: cannot extract path of kind %q", pathInfo.Kind)
return nil, fmt.Errorf("internal error: cannot extract path of kind %q", pathInfo.Kind)
}

info, err := fsutil.Create(&fsutil.CreateOptions{
Expand All @@ -229,11 +229,11 @@ func Run(options *RunOptions) error {
MakeParents: true,
})
if err != nil {
return err
return nil, err
}
err = report.Add(slice, &info)
if err != nil {
return err
return nil, err
}
}
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func Run(options *RunOptions) error {
}
err := scripts.Run(&opts)
if err != nil {
return fmt.Errorf("slice %s: %w", slice, err)
return nil, fmt.Errorf("slice %s: %w", slice, err)
}
}

Expand All @@ -307,7 +307,7 @@ func Run(options *RunOptions) error {
}
}
if err != nil {
return fmt.Errorf("cannot perform 'until' removal: %w", err)
return nil, fmt.Errorf("cannot perform 'until' removal: %w", err)
}
}
}
Expand All @@ -316,11 +316,11 @@ func Run(options *RunOptions) error {
err := os.Remove(realPath)
// The non-empty directory error is caught by IsExist as well.
if err != nil && !os.IsExist(err) {
return fmt.Errorf("cannot perform 'until' removal: %#v", err)
return nil, fmt.Errorf("cannot perform 'until' removal: %#v", err)
}
}

return nil
return report, nil
}

func contains(l []string, s string) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func runSlicerTests(c *C, tests []slicerTest) {
if test.hackopt != nil {
test.hackopt(c, &options)
}
err = slicer.Run(&options)
_, err = slicer.Run(&options)
if test.error == "" {
c.Assert(err, IsNil)
} else {
Expand Down

0 comments on commit 22933d4

Please sign in to comment.