Skip to content

Commit

Permalink
Merge pull request #456 from buildpacks/fix/glob
Browse files Browse the repository at this point in the history
Slices: Clean pattern before matching
  • Loading branch information
ekcasey authored Nov 3, 2020
2 parents ee48447 + 77ade38 commit 6de73b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions layers/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (f *Factory) createLayerFromSlice(slice Slice, sdir *sliceableDir, layerID
}

func glob(sdir *sliceableDir, pattern string) ([]string, error) {
pattern = filepath.Clean(pattern)
var matches []string
if err := filepath.Walk(sdir.path, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions layers/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,5 +353,43 @@ func testSlices(t *testing.T, when spec.G, it spec.S) {
assertTarEntries(t, sliceLayers[1].TarPath, []*tar.Header{})
})
})

when("the pattern ends in a path separator", func() {
it("matches", func() {
pattern := "some-dir" + string(filepath.Separator)
sliceLayers, err := factory.SliceLayers(dirToSlice, []layers.Slice{
{Paths: []string{pattern}},
})
h.AssertNil(t, err)
h.AssertEq(t, len(sliceLayers), 2)
h.AssertEq(t, sliceLayers[0].ID, "slice-1")
assertTarEntries(t, sliceLayers[0].TarPath, append(parents(t, dirToSlice), []*tar.Header{
{
Name: tarPath(dirToSlice),
Uid: factory.UID,
Gid: factory.GID,
Typeflag: tar.TypeDir,
},
{
Name: tarPath(filepath.Join(dirToSlice, "some-dir")),
Uid: factory.UID,
Gid: factory.GID,
Typeflag: tar.TypeDir,
},
{
Name: tarPath(filepath.Join(dirToSlice, "some-dir", "file.md")),
Uid: factory.UID,
Gid: factory.GID,
Typeflag: tar.TypeReg,
},
{
Name: tarPath(filepath.Join(dirToSlice, "some-dir", "some-file.txt")),
Uid: factory.UID,
Gid: factory.GID,
Typeflag: tar.TypeReg,
},
}...))
})
})
})
}

0 comments on commit 6de73b9

Please sign in to comment.