github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/python/pypipurl/pythonpurl_test.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package pypipurl converts a package to a PyPI type PackageURL.
    16  package pypipurl_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/osv-scalibr/extractor/filesystem/language/python/pypipurl"
    23  	"github.com/google/osv-scalibr/purl"
    24  )
    25  
    26  func TestMakePackageURL(t *testing.T) {
    27  	tests := []struct {
    28  		name    string
    29  		version string
    30  		want    *purl.PackageURL
    31  	}{
    32  		{
    33  			name:    "test",
    34  			version: "1.0.0",
    35  			want: &purl.PackageURL{
    36  				Type:    "pypi",
    37  				Name:    "test",
    38  				Version: "1.0.0",
    39  			},
    40  		},
    41  		{
    42  			name:    "test-with-dashes",
    43  			version: "1.0.0",
    44  			want: &purl.PackageURL{
    45  				Type:    "pypi",
    46  				Name:    "test-with-dashes",
    47  				Version: "1.0.0",
    48  			},
    49  		},
    50  		{
    51  			name:    "test_with_underscore",
    52  			version: "1.0.0",
    53  			want: &purl.PackageURL{
    54  				Type:    "pypi",
    55  				Name:    "test-with-underscore",
    56  				Version: "1.0.0",
    57  			},
    58  		},
    59  		{
    60  			name:    "test___with_long__underscore",
    61  			version: "1.0.0",
    62  			want: &purl.PackageURL{
    63  				Type:    "pypi",
    64  				Name:    "test-with-long-underscore",
    65  				Version: "1.0.0",
    66  			},
    67  		},
    68  		{
    69  			name:    "test.with-mixed_symbols",
    70  			version: "1.0.0",
    71  			want: &purl.PackageURL{
    72  				Type:    "pypi",
    73  				Name:    "test-with-mixed-symbols",
    74  				Version: "1.0.0",
    75  			},
    76  		},
    77  		{
    78  			name:    "test.__-with_mixed_.--run",
    79  			version: "1.0.0",
    80  			want: &purl.PackageURL{
    81  				Type:    "pypi",
    82  				Name:    "test-with-mixed-run",
    83  				Version: "1.0.0",
    84  			},
    85  		},
    86  	}
    87  
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			got := pypipurl.MakePackageURL(tt.name, tt.version)
    91  			if diff := cmp.Diff(tt.want, got); diff != "" {
    92  				t.Errorf("MakePackageURL() returned unexpected diff (-want +got):\n%s", diff)
    93  			}
    94  		})
    95  	}
    96  }