Skip to content

Commit

Permalink
fix: trim path from deps.json in portable way (#2674)
Browse files Browse the repository at this point in the history
* fix: trim path from deps.json in portable way

Previously, the path trimming regex would leave leading path separator
in place on Windows.

Probably a better long term fix is to a library path operation.

Signed-off-by: Will Murphy <will.murphy@anchore.com>
  • Loading branch information
willmurphyscode committed Feb 29, 2024
1 parent 5ef83fd commit 3ad91f2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/dotnet/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newDotnetDepsPackage(nameVersion string, lib dotnetDepsLibrary, locations .
}

func getDepsJSONFilePrefix(p string) string {
r := regexp.MustCompile(`([^\/]+)\.deps\.json$`)
r := regexp.MustCompile(`([^\\\/]+)\.deps\.json$`)
match := r.FindStringSubmatch(p)
if len(match) > 1 {
return match[1]
Expand Down
40 changes: 40 additions & 0 deletions syft/pkg/cataloger/dotnet/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dotnet

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_getDepsJSONFilePrefix(t *testing.T) {
tests := []struct {
name string
path string
want string
}{
{
name: "windows-style full path",
path: `C:\Code\Projects\My-Project\My.Rest.Project.deps.json`,
want: "My.Rest.Project",
},
{
name: "leading backslash",
path: `\My.Project.deps.json`,
want: "My.Project",
},
{
name: "unix-style path with lots of prefixes",
path: "/my/cool/project/cool-project.deps.json",
want: "cool-project",
},
{
name: "unix-style relative path",
path: "cool-project/my-dotnet-project.deps.json",
want: "my-dotnet-project",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, getDepsJSONFilePrefix(tt.path), "getDepsJSONFilePrefix(%v)", tt.path)
})
}
}

0 comments on commit 3ad91f2

Please sign in to comment.