Skip to content

Commit

Permalink
Normalize license for cargo. (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy committed Feb 28, 2023
1 parent de4e45e commit 0955592
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
21 changes: 21 additions & 0 deletions pkg/deps/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"

"github.com/apache/skywalking-eyes/internal/logger"
"github.com/apache/skywalking-eyes/pkg/license"
Expand Down Expand Up @@ -75,6 +77,10 @@ func (resolver *CargoTomlResolver) Resolve(cargoTomlFile string, config *ConfigD
return err
}

for i := range metadata.Packages {
metadata.Packages[i].License = normalizeLicense(metadata.Packages[i].License)
}

logger.Log.Debugln("Package size:", len(metadata.Packages))

return resolver.ResolvePackages(metadata.Packages, config, report)
Expand Down Expand Up @@ -156,3 +162,18 @@ func (resolver *CargoTomlResolver) ResolvePackageLicense(config *ConfigDeps, pkg

return nil
}

func normalizeLicense(licenseStr string) string {
segs := make(map[string]struct{})
for _, ss := range strings.Split(licenseStr, "/") {
for _, s := range strings.Split(ss, " OR ") {
segs[s] = struct{}{}
}
}
var items []string
for seg := range segs {
items = append(items, seg)
}
sort.Strings(items)
return strings.Join(items, " OR ")
}
19 changes: 13 additions & 6 deletions pkg/deps/cargo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
package deps_test

import (
"github.com/apache/skywalking-eyes/internal/logger"
"github.com/apache/skywalking-eyes/pkg/deps"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/apache/skywalking-eyes/internal/logger"
"github.com/apache/skywalking-eyes/pkg/deps"
)

func TestCanResolveCargo(t *testing.T) {
Expand Down Expand Up @@ -134,7 +135,8 @@ edition = "2021"
license = "Apache-2.0"
[dependencies]
libc = "0.2.126"
libc = "0.2.126" # actual license: MIT OR Apache-2.0
bitflags = "1.3.2" # actual license: MIT/Apache-2.0
`

config := deps.ConfigDeps{
Expand All @@ -145,12 +147,17 @@ libc = "0.2.126"
}

report := resolveTmpCargo(t, cargoToml, &config)
if len(report.Resolved) != 2 {
t.Error("len(report.Resolved) != 2")
if len(report.Resolved) != 3 {
t.Error("len(report.Resolved) != 3")
}
for _, result := range report.Resolved {
if result.Dependency == "libc" {
if result.LicenseSpdxID != "MIT OR Apache-2.0" || result.LicenseContent == "" {
if result.LicenseSpdxID != "Apache-2.0 OR MIT" || result.LicenseContent == "" {
t.Error("Resolve dependency libc failed")
}
}
if result.Dependency == "bitflags" {
if result.LicenseSpdxID != "Apache-2.0 OR MIT" || result.LicenseContent == "" {
t.Error("Resolve dependency libc failed")
}
}
Expand Down

0 comments on commit 0955592

Please sign in to comment.