github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/common/spdxhelpers/document_name_test.go (about) 1 package spdxhelpers 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/anchore/syft/syft/internal/sourcemetadata" 11 "github.com/anchore/syft/syft/source" 12 ) 13 14 func Test_DocumentName(t *testing.T) { 15 16 tracker := sourcemetadata.NewCompletionTester(t) 17 18 tests := []struct { 19 name string 20 srcMetadata source.Description 21 expected string 22 }{ 23 { 24 name: "image", 25 srcMetadata: source.Description{ 26 Metadata: source.StereoscopeImageSourceMetadata{ 27 UserInput: "image-repo/name:tag", 28 ID: "id", 29 ManifestDigest: "digest", 30 }, 31 }, 32 expected: "image-repo/name:tag", 33 }, 34 { 35 name: "directory", 36 srcMetadata: source.Description{ 37 Metadata: source.DirectorySourceMetadata{Path: "some/path/to/place"}, 38 }, 39 expected: "some/path/to/place", 40 }, 41 { 42 name: "file", 43 srcMetadata: source.Description{ 44 Metadata: source.FileSourceMetadata{Path: "some/path/to/place"}, 45 }, 46 expected: "some/path/to/place", 47 }, 48 { 49 name: "named", 50 srcMetadata: source.Description{ 51 Name: "some/name", 52 Metadata: source.FileSourceMetadata{Path: "some/path/to/place"}, 53 }, 54 expected: "some/name", 55 }, 56 } 57 for _, test := range tests { 58 t.Run(test.name, func(t *testing.T) { 59 actual := DocumentName(test.srcMetadata) 60 assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("actual name %q", actual)) 61 62 // track each scheme tested (passed or not) 63 tracker.Tested(t, test.srcMetadata.Metadata) 64 }) 65 } 66 }