diff --git a/pkg/deps/cargo.go b/pkg/deps/cargo.go index e0c1a59..5d900cd 100644 --- a/pkg/deps/cargo.go +++ b/pkg/deps/cargo.go @@ -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" @@ -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) @@ -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 ") +} diff --git a/pkg/deps/cargo_test.go b/pkg/deps/cargo_test.go index db401a3..469d32d 100644 --- a/pkg/deps/cargo_test.go +++ b/pkg/deps/cargo_test.go @@ -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) { @@ -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{ @@ -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") } }