Skip to content

Commit

Permalink
Fix issue where RelativePath didn't give correct result
Browse files Browse the repository at this point in the history
When the location passed to RelativePath was a file, which is the case
when DPKG resolver asks for files relative to the DB file, then RelativePath
was not working correctly for the unindexed directory

Signed-off-by: adammcclenaghan <[email protected]>
  • Loading branch information
adammcclenaghan committed Oct 21, 2024
1 parent 995b08d commit fb1ca8b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion syft/internal/fileresolver/unindexed_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (u UnindexedDirectory) FilesByMIMEType(types ...string) ([]syftFile.Locatio
// RelativeFileByPath fetches a single file at the given path relative to the layer squash of the given reference.
// This is helpful when attempting to find a file that is in the same layer or lower as another file.
func (u UnindexedDirectory) RelativeFileByPath(l syftFile.Location, p string) *syftFile.Location {
p = path.Clean(path.Join(l.RealPath, p))
p = path.Clean(p)
locs, err := u.filesByPath(true, false, p)
if err != nil || len(locs) == 0 {
return nil
Expand Down
24 changes: 24 additions & 0 deletions syft/internal/fileresolver/unindexed_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,30 @@ func Test_UnindexedDirectoryResolver_FilesByMIMEType(t *testing.T) {
}
}

func Test_UnindexedDirectoryResolver_RelativeFileByPath(t *testing.T) {
cases := []struct {
name string
root string
searchFile string
expected *strset.Set
}{
{
name: "should find nested file from root",
root: "./test-fixtures/image-simple",
searchFile: "target/really/nested/file-3.txt",
expected: strset.New("file-3.txt"),
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
resolver := NewFromUnindexedDirectory(c.root)
rootLoc := file.NewLocation(c.root)
loc := resolver.RelativeFileByPath(rootLoc, c.searchFile)
assert.True(t, c.expected.Has(loc.Coordinates.RealPath), "does not have path %q", loc.RealPath)
})
}
}

func testWithTimeout(t *testing.T, timeout time.Duration, test func(*testing.T)) {
done := make(chan bool)
go func() {
Expand Down

0 comments on commit fb1ca8b

Please sign in to comment.