Skip to content

Commit

Permalink
Starting adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garethr committed Apr 23, 2023
1 parent 3c9384c commit 9ef2cf7
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions pkg/parlay/package_test.go
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)
}
})
}
}

0 comments on commit 9ef2cf7

Please sign in to comment.