mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
Ensure SPDXIDs are valid (#955)
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package spdxhelpers
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var expr = regexp.MustCompile("[^a-zA-Z0-9.-]")
|
||||
|
||||
// SPDX spec says SPDXID must be:
|
||||
// "SPDXRef-"[idstring] where [idstring] is a unique string containing letters, numbers, ., and/or -
|
||||
func SanitizeElementID(id string) string {
|
||||
return expr.ReplaceAllString(id, "-")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package spdxhelpers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_SanitizeElementID(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
input: "letters",
|
||||
expected: "letters",
|
||||
},
|
||||
{
|
||||
input: "ssl-client",
|
||||
expected: "ssl-client",
|
||||
},
|
||||
{
|
||||
input: "ssl_client",
|
||||
expected: "ssl-client",
|
||||
},
|
||||
{
|
||||
input: "go-module-sigs.k8s.io/structured-merge-diff/v3",
|
||||
expected: "go-module-sigs.k8s.io-structured-merge-diff-v3",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.input, func(t *testing.T) {
|
||||
actual := SanitizeElementID(test.input)
|
||||
|
||||
assert.Equal(t, test.expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user