github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/python/parse_setup_test.go (about) 1 package python 2 3 import ( 4 "testing" 5 6 "github.com/nextlinux/gosbom/gosbom/artifact" 7 "github.com/nextlinux/gosbom/gosbom/file" 8 "github.com/nextlinux/gosbom/gosbom/pkg" 9 "github.com/nextlinux/gosbom/gosbom/pkg/cataloger/internal/pkgtest" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestParseSetup(t *testing.T) { 14 tests := []struct { 15 fixture string 16 expected []pkg.Package 17 }{ 18 { 19 fixture: "test-fixtures/setup/setup.py", 20 expected: []pkg.Package{ 21 { 22 Name: "pathlib3", 23 Version: "2.2.0", 24 PURL: "pkg:pypi/pathlib3@2.2.0", 25 Language: pkg.Python, 26 Type: pkg.PythonPkg, 27 }, 28 { 29 Name: "mypy", 30 Version: "v0.770", 31 PURL: "pkg:pypi/mypy@v0.770", 32 Language: pkg.Python, 33 Type: pkg.PythonPkg, 34 }, 35 { 36 Name: "mypy1", 37 Version: "v0.770", 38 PURL: "pkg:pypi/mypy1@v0.770", 39 Language: pkg.Python, 40 Type: pkg.PythonPkg, 41 }, 42 { 43 Name: "mypy2", 44 Version: "v0.770", 45 PURL: "pkg:pypi/mypy2@v0.770", 46 Language: pkg.Python, 47 Type: pkg.PythonPkg, 48 }, 49 { 50 Name: "mypy3", 51 Version: "v0.770", 52 PURL: "pkg:pypi/mypy3@v0.770", 53 Language: pkg.Python, 54 Type: pkg.PythonPkg, 55 }, 56 }, 57 }, 58 { 59 // regression... ensure we clean packages names and don't find "%s" as the name 60 fixture: "test-fixtures/setup/dynamic-setup.py", 61 expected: nil, 62 }, 63 } 64 65 for _, tt := range tests { 66 t.Run(tt.fixture, func(t *testing.T) { 67 locations := file.NewLocationSet(file.NewLocation(tt.fixture)) 68 for i := range tt.expected { 69 tt.expected[i].Locations = locations 70 } 71 var expectedRelationships []artifact.Relationship 72 73 pkgtest.TestFileParser(t, tt.fixture, parseSetup, tt.expected, expectedRelationships) 74 }) 75 } 76 77 } 78 79 func Test_hasTemplateDirective(t *testing.T) { 80 81 tests := []struct { 82 input string 83 want bool 84 }{ 85 { 86 input: "foo", 87 want: false, 88 }, 89 { 90 input: "foo %s", 91 want: true, 92 }, 93 { 94 input: "%s", 95 want: true, 96 }, 97 { 98 input: "{f_string}", 99 want: true, 100 }, 101 { 102 input: "{}", // .format() directive 103 want: true, 104 }, 105 } 106 for _, tt := range tests { 107 t.Run(tt.input, func(t *testing.T) { 108 assert.Equal(t, tt.want, hasTemplateDirective(tt.input)) 109 }) 110 } 111 }