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