github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/common/spdxhelpers/document_name_test.go (about) 1 package spdxhelpers 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/nextlinux/gosbom/gosbom/source" 9 "github.com/scylladb/go-set/strset" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func Test_DocumentName(t *testing.T) { 14 allSchemes := strset.New() 15 for _, s := range source.AllSchemes { 16 allSchemes.Add(string(s)) 17 } 18 testedSchemes := strset.New() 19 20 tests := []struct { 21 name string 22 inputName string 23 srcMetadata source.Metadata 24 expected string 25 }{ 26 { 27 name: "image", 28 inputName: "my-name", 29 srcMetadata: source.Metadata{ 30 Scheme: source.ImageScheme, 31 ImageMetadata: source.ImageMetadata{ 32 UserInput: "image-repo/name:tag", 33 ID: "id", 34 ManifestDigest: "digest", 35 }, 36 }, 37 expected: "image-repo/name:tag", 38 }, 39 { 40 name: "directory", 41 inputName: "my-name", 42 srcMetadata: source.Metadata{ 43 Scheme: source.DirectoryScheme, 44 Path: "some/path/to/place", 45 }, 46 expected: "some/path/to/place", 47 }, 48 { 49 name: "file", 50 inputName: "my-name", 51 srcMetadata: source.Metadata{ 52 Scheme: source.FileScheme, 53 Path: "some/path/to/place", 54 }, 55 expected: "some/path/to/place", 56 }, 57 } 58 for _, test := range tests { 59 t.Run(test.name, func(t *testing.T) { 60 actual := DocumentName(test.srcMetadata) 61 assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("actual name %q", actual)) 62 63 // track each scheme tested (passed or not) 64 testedSchemes.Add(string(test.srcMetadata.Scheme)) 65 }) 66 } 67 68 // assert all possible schemes were under test 69 assert.ElementsMatch(t, allSchemes.List(), testedSchemes.List(), "not all source.Schemes are under test") 70 }