Skip to content

Commit

Permalink
fix: use TrimPrefix for relative paths (#145)
Browse files Browse the repository at this point in the history
Use strings.TrimPrefix for relative paths instead of strings.TrimLeft,
as the latter takes in a cutset instead of a prefix substring.
  • Loading branch information
rebornplusplus authored Jul 4, 2024
1 parent 46f8089 commit 504ad63
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func Run(options *RunOptions) (*Report, error) {
return nil
}

relPath := filepath.Clean("/" + strings.TrimLeft(o.Path, targetDir))
relPath := filepath.Clean("/" + strings.TrimPrefix(o.Path, targetDir))
if o.Mode.IsDir() {
relPath = relPath + "/"
}
Expand Down
30 changes: 30 additions & 0 deletions internal/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,36 @@ var slicerTests = []slicerTest{{
},
filesystem: map[string]string{},
report: map[string]string{},
}, {
summary: "Relative paths are properly trimmed during extraction",
slices: []setup.SliceKey{{"test-package", "myslice"}},
pkgs: map[string][]byte{
"test-package": testutil.MustMakeDeb([]testutil.TarEntry{
// This particular path starting with "/foo" is chosen to test for
// a particular bug; which appeared due to the usage of
// strings.TrimLeft() instead strings.TrimPrefix() to determine a
// relative path. Since TrimLeft takes in a cutset instead of a
// prefix, the desired relative path was not produced.
// See https://github.com/canonical/chisel/pull/145.
testutil.Dir(0755, "./foo-bar/"),
}),
},
hackopt: func(c *C, opts *slicer.RunOptions) {
opts.TargetDir = filepath.Join(filepath.Clean(opts.TargetDir), "foo")
err := os.Mkdir(opts.TargetDir, 0755)
c.Assert(err, IsNil)
},
release: map[string]string{
"slices/mydir/test-package.yaml": `
package: test-package
slices:
myslice:
contents:
/foo-bar/:
mutate: |
content.list("/foo-bar/")
`,
},
}}

var defaultChiselYaml = `
Expand Down

0 comments on commit 504ad63

Please sign in to comment.