-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package parlay | ||
|
||
import ( | ||
"fmt" | ||
"github.com/package-url/packageurl-go" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestPurlToEcosystemsRegistry(t *testing.T) { | ||
testCases := []struct { | ||
purlStr string | ||
expected string | ||
}{ | ||
{"pkg:npm/[email protected]", "npmjs.org"}, | ||
{"pkg:golang/github.com/golang/example/hello?go-get=1", "proxy.golang.org"}, | ||
{"pkg:nuget/[email protected]", "nuget.org"}, | ||
{"pkg:hex/[email protected]", "hex.pm"}, | ||
{"pkg:maven/com.google.guava/[email protected]", "repo1.maven.org"}, | ||
{"pkg:pypi/[email protected]", "pypi.org"}, | ||
{"pkg:composer/symfony/[email protected]", "packagist.org"}, | ||
{"pkg:gem/[email protected]", "rubygems.org"}, | ||
{"pkg:cargo/[email protected]", "crates.io"}, | ||
{"pkg:cocoapods/[email protected]", "cocoapod.org"}, | ||
{"pkg:apk/[email protected]", "alpine"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
purl, err := packageurl.FromString(tc.purlStr) | ||
if err != nil { | ||
t.Errorf("Error creating PackageURL: %v", err) | ||
} | ||
got := purlToEcosystemsRegistry(purl) | ||
if got != tc.expected { | ||
t.Errorf("purlToEcosystemsRegistry(%q) = %q; expected %q", tc.purlStr, got, tc.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestPurlToEcosystemsName(t *testing.T) { | ||
testCases := []struct { | ||
purlStr string | ||
expectedName string | ||
}{ | ||
{ | ||
// Test case 1: When the package manager type is "npm" | ||
// and the namespace is not empty, the function should return | ||
// a url encoded string in the form of "<namespace>/<name>" | ||
purlStr: "pkg:npm/my-namespace/my-package", | ||
expectedName: url.QueryEscape("my-namespace/my-package"), | ||
}, | ||
{ | ||
// Test case 2: When the package manager type is "npm" | ||
// and the namespace is empty, the function should return | ||
// the package name as is. | ||
purlStr: "pkg:npm/my-package", | ||
expectedName: "my-package", | ||
}, | ||
{ | ||
// Test case 3: When the package manager type is not "npm" | ||
// and the namespace is not empty, the function should return | ||
// a string in the form of "<namespace>:<name>" | ||
purlStr: "pkg:maven/my-group:my-artifact", | ||
expectedName: "my-group:my-artifact", | ||
}, | ||
{ | ||
// Test case 4: When the package manager type is not "npm" | ||
// and the namespace is empty, the function should return | ||
// the package name as is. | ||
purlStr: "pkg:maven/my-artifact", | ||
expectedName: "my-artifact", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(fmt.Sprintf("Test %v", testCase.purlStr), func(t *testing.T) { | ||
purl, err := packageurl.FromString(testCase.purlStr) | ||
if err != nil { | ||
t.Fatalf("Failed to create PackageURL: %v", err) | ||
} | ||
|
||
result := purlToEcosystemsName(purl) | ||
if result != testCase.expectedName { | ||
t.Errorf("Expected %q, but got %q", testCase.expectedName, result) | ||
} | ||
}) | ||
} | ||
} |