github.com/anchore/syft@v1.38.2/syft/format/internal/spdxutil/helpers/document_name_test.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/anchore/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.ImageMetadata{ 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.DirectoryMetadata{Path: "some/path/to/place"}, 38 }, 39 expected: "some/path/to/place", 40 }, 41 { 42 name: "file", 43 srcMetadata: source.Description{ 44 Metadata: source.FileMetadata{Path: "some/path/to/place"}, 45 }, 46 expected: "some/path/to/place", 47 }, 48 { 49 name: "snap", 50 srcMetadata: source.Description{ 51 Name: "some/name", 52 // there is nothing in the snap metadata that indicates a name 53 Metadata: source.SnapMetadata{}, 54 }, 55 expected: "some/name", 56 }, 57 { 58 name: "named", 59 srcMetadata: source.Description{ 60 Name: "some/name", 61 Metadata: source.FileMetadata{Path: "some/path/to/place"}, 62 }, 63 expected: "some/name", 64 }, 65 } 66 for _, test := range tests { 67 t.Run(test.name, func(t *testing.T) { 68 actual := DocumentName(test.srcMetadata) 69 assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("actual name %q", actual)) 70 71 // track each scheme tested (passed or not) 72 tracker.Tested(t, test.srcMetadata.Metadata) 73 }) 74 } 75 }