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

     1  package python
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/go-test/deep"
     8  
     9  	"github.com/anchore/syft/syft/file"
    10  	"github.com/anchore/syft/syft/pkg"
    11  )
    12  
    13  func TestParseWheelEggMetadata(t *testing.T) {
    14  	tests := []struct {
    15  		Fixture          string
    16  		ExpectedMetadata parsedData
    17  	}{
    18  		{
    19  			Fixture: "test-fixtures/egg-info/PKG-INFO",
    20  			ExpectedMetadata: parsedData{
    21  				"Apache 2.0",
    22  				file.NewLocation("test-fixtures/egg-info/PKG-INFO"),
    23  				pkg.PythonPackageMetadata{
    24  					Name:                 "requests",
    25  					Version:              "2.22.0",
    26  					Platform:             "UNKNOWN",
    27  					Author:               "Kenneth Reitz",
    28  					AuthorEmail:          "me@kennethreitz.org",
    29  					SitePackagesRootPath: "test-fixtures",
    30  				},
    31  			},
    32  		},
    33  		{
    34  			Fixture: "test-fixtures/dist-info/METADATA",
    35  			ExpectedMetadata: parsedData{
    36  				"BSD License",
    37  				file.NewLocation("test-fixtures/dist-info/METADATA"),
    38  				pkg.PythonPackageMetadata{
    39  					Name:                 "Pygments",
    40  					Version:              "2.6.1",
    41  					Platform:             "any",
    42  					Author:               "Georg Brandl",
    43  					AuthorEmail:          "georg@python.org",
    44  					SitePackagesRootPath: "test-fixtures",
    45  				},
    46  			},
    47  		},
    48  	}
    49  
    50  	for _, test := range tests {
    51  		t.Run(test.Fixture, func(t *testing.T) {
    52  			fixture, err := os.Open(test.Fixture)
    53  			if err != nil {
    54  				t.Fatalf("failed to open fixture: %+v", err)
    55  			}
    56  
    57  			actual, err := parseWheelOrEggMetadata(test.Fixture, fixture)
    58  			if err != nil {
    59  				t.Fatalf("failed to parse: %+v", err)
    60  			}
    61  
    62  			for _, d := range deep.Equal(actual, test.ExpectedMetadata) {
    63  				t.Errorf("diff: %+v", d)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func TestIsRegularEggFile(t *testing.T) {
    70  	cases := []struct {
    71  		path     string
    72  		expected bool
    73  	}{
    74  		{
    75  			"/usr/lib64/python2.6/site-packages/M2Crypto-0.20.2-py2.6.egg-info",
    76  			true,
    77  		},
    78  		{
    79  			"/usr/lib64/python2.6/site-packages/M2Crypto-0.20.2-py2.6.egg-info/PKG-INFO",
    80  			false,
    81  		},
    82  		{
    83  			"/usr/lib64/python2.6/site-packages/M2Crypto-0.20.2-py2.6.dist-info/METADATA",
    84  			false,
    85  		},
    86  	}
    87  
    88  	for _, c := range cases {
    89  		t.Run(c.path, func(t *testing.T) {
    90  			actual := isEggRegularFile(c.path)
    91  
    92  			if actual != c.expected {
    93  				t.Errorf("expected %t but got %t", c.expected, actual)
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestDetermineSitePackagesRootPath(t *testing.T) {
   100  	cases := []struct {
   101  		inputPath string
   102  		expected  string
   103  	}{
   104  		{
   105  			inputPath: "/usr/lib64/python2.6/site-packages/ethtool-0.6-py2.6.egg-info",
   106  			expected:  "/usr/lib64/python2.6/site-packages",
   107  		},
   108  		{
   109  			inputPath: "/usr/lib/python2.7/dist-packages/configobj-5.0.6.egg-info/top_level.txt",
   110  			expected:  "/usr/lib/python2.7/dist-packages",
   111  		},
   112  		{
   113  			inputPath: "/usr/lib/python2.7/dist-packages/six-1.10.0.egg-info/PKG-INFO",
   114  			expected:  "/usr/lib/python2.7/dist-packages",
   115  		},
   116  	}
   117  
   118  	for _, c := range cases {
   119  		t.Run(c.inputPath, func(t *testing.T) {
   120  			actual := determineSitePackagesRootPath(c.inputPath)
   121  
   122  			if actual != c.expected {
   123  				t.Errorf("expected %s but got %s", c.expected, actual)
   124  			}
   125  		})
   126  	}
   127  }
   128  
   129  func TestParseWheelEggMetadataInvalid(t *testing.T) {
   130  	tests := []struct {
   131  		Fixture          string
   132  		ExpectedMetadata parsedData
   133  	}{
   134  		{
   135  			Fixture: "test-fixtures/egg-info/PKG-INFO-INVALID",
   136  			ExpectedMetadata: parsedData{
   137  				"",
   138  				file.Location{},
   139  				pkg.PythonPackageMetadata{
   140  					Name:                 "mxnet",
   141  					Version:              "1.8.0",
   142  					SitePackagesRootPath: "test-fixtures",
   143  				},
   144  			},
   145  		},
   146  	}
   147  
   148  	for _, test := range tests {
   149  		t.Run(test.Fixture, func(t *testing.T) {
   150  			fixture, err := os.Open(test.Fixture)
   151  			if err != nil {
   152  				t.Fatalf("failed to open fixture: %+v", err)
   153  			}
   154  
   155  			actual, err := parseWheelOrEggMetadata(test.Fixture, fixture)
   156  			if err != nil {
   157  				t.Fatalf("failed to parse: %+v", err)
   158  			}
   159  
   160  			for _, d := range deep.Equal(actual, test.ExpectedMetadata) {
   161  				t.Errorf("diff: %+v", d)
   162  			}
   163  		})
   164  	}
   165  }