Skip to content

Commit

Permalink
Merge pull request #46 from Octogonapus/fix_tests
Browse files Browse the repository at this point in the history
Fix parsing of some purl types. Fix tests.
  • Loading branch information
sschuberth committed Jun 19, 2023
2 parents 8907843 + 2e2d03f commit 358f100
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 62 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Download test data
run: curl -L https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json
- name: Test go fmt
run: test -z $(go fmt ./...)
- name: Golangci-lint
Expand All @@ -30,4 +28,4 @@ jobs:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
GO111MODULE=off go get github.com/mattn/goveralls
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
.PHONY: test clean lint

test:
curl -L https://raw.githubusercontent.com/package-url/purl-spec/master/test-suite-data.json -o testdata/test-suite-data.json
go test -v -cover ./...

clean:
find . -name "test-suite-data.json" | xargs rm -f

lint:
go get -u golang.org/x/lint/golint
golint -set_exit_status
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ Testing using the normal ``go test`` command. Using ``make test`` will pull the

```
$ make test
curl -L https://raw.githubusercontent.com/package-url/purl-test-suite/master/test-suite-data.json -o testdata/test-suite-data.json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7181 100 7181 0 0 1202 0 0:00:05 0:00:05 --:--:-- 1611
go test -v -cover ./...
=== RUN TestFromStringExamples
--- PASS: TestFromStringExamples (0.00s)
=== RUN TestToStringExamples
--- PASS: TestToStringExamples (0.00s)
=== RUN TestStringer
--- PASS: TestStringer (0.00s)
=== RUN TestQualifiersMapConversion
--- PASS: TestQualifiersMapConversion (0.00s)
PASS
coverage: 94.7% of statements
ok github.com/package-url/packageurl-go 0.002s
coverage: 90.7% of statements
ok github.com/package-url/packageurl-go 0.004s coverage: 90.7% of statements
```
46 changes: 41 additions & 5 deletions packageurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ var (
TypeRPM = "rpm"
// TypeSwift is pkg:swift purl
TypeSwift = "swift"
// TypeHuggingface is pkg:huggingface purl.
TypeHuggingface = "huggingface"
// TypeMLflow is pkg:mlflow purl.
TypeMLFlow = "mlflow"
)

// Qualifier represents a single key=value qualifier in the package url
Expand Down Expand Up @@ -283,7 +287,7 @@ func FromString(purl string) (PackageURL, error) {
remainder = nextSplit[1]

index = strings.LastIndex(remainder, "/")
name := typeAdjustName(purlType, remainder[index+1:])
name := typeAdjustName(purlType, remainder[index+1:], qualifiers)
version := ""

atIndex := strings.Index(name, "@")
Expand All @@ -292,7 +296,7 @@ func FromString(purl string) (PackageURL, error) {
if err != nil {
return PackageURL{}, fmt.Errorf("failed to unescape purl version: %s", err)
}
version = v
version = typeAdjustVersion(purlType, v)

unecapeName, err := url.PathUnescape(name[:atIndex])
if err != nil {
Expand Down Expand Up @@ -342,24 +346,56 @@ func FromString(purl string) (PackageURL, error) {
// See https://github.com/package-url/purl-spec#known-purl-types
func typeAdjustNamespace(purlType, ns string) string {
switch purlType {
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM, TypeRPM:
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM, TypeRPM, TypeComposer:
return strings.ToLower(ns)
}
return ns
}

// Make any purl type-specific adjustments to the parsed name.
// See https://github.com/package-url/purl-spec#known-purl-types
func typeAdjustName(purlType, name string) string {
func typeAdjustName(purlType, name string, qualifiers Qualifiers) string {
quals := qualifiers.Map()
switch purlType {
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM:
case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM, TypeComposer:
return strings.ToLower(name)
case TypePyPi:
return strings.ToLower(strings.ReplaceAll(name, "_", "-"))
case TypeMLFlow:
return adjustMlflowName(name, quals)
}
return name
}

// Make any purl type-specific adjustments to the parsed version.
// See https://github.com/package-url/purl-spec#known-purl-types
func typeAdjustVersion(purlType, version string) string {
switch purlType {
case TypeHuggingface:
return strings.ToLower(version)
}
return version
}

// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#mlflow
func adjustMlflowName(name string, qualifiers map[string]string) string {
if repo, ok := qualifiers["repository_url"]; ok {
if strings.Contains(repo, "azureml") {
// Azure ML is case-sensitive and must be kept as-is
return name
} else if strings.Contains(repo, "databricks") {
// Databricks is case-insensitive and must be lowercased
return strings.ToLower(name)
} else {
// Unknown repository type, keep as-is
return name
}
} else {
// No repository qualifier given, keep as-is
return name
}
}

// validQualifierKey validates a qualifierKey against our QualifierKeyPattern.
func validQualifierKey(key string) bool {
return QualifierKeyPattern.MatchString(key)
Expand Down
Loading

0 comments on commit 358f100

Please sign in to comment.