github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/python/parse_setup_test.go (about)

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