Skip to content

Commit

Permalink
Remove space characters in license for cargo. (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy committed Mar 1, 2023
1 parent 0955592 commit d299844
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/deps/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func normalizeLicense(licenseStr string) string {
segs := make(map[string]struct{})
for _, ss := range strings.Split(licenseStr, "/") {
for _, s := range strings.Split(ss, " OR ") {
s = strings.TrimSpace(s)
segs[s] = struct{}{}
}
}
Expand Down
65 changes: 51 additions & 14 deletions pkg/deps/cargo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ license = "Apache-2.0"
Excludes: []deps.Exclude{},
}

report := resolveTmpCargo(t, cargoToml, &config)
report := resolveTmpCargo(t, cargoToml, "", &config)
if len(report.Resolved) != 1 {
t.Error("len(report.Resolved) != 1")
}
Expand All @@ -87,7 +87,7 @@ license = "Apache-2.0"
Excludes: []deps.Exclude{{Name: "foo", Version: "0.0.0"}},
}

report := resolveTmpCargo(t, cargoToml, &config)
report := resolveTmpCargo(t, cargoToml, "", &config)
if len(report.Resolved) != 0 {
t.Error("len(report.Resolved) != 0")
}
Expand Down Expand Up @@ -116,7 +116,7 @@ license = "Apache-2.0"
Excludes: []deps.Exclude{},
}

report := resolveTmpCargo(t, cargoToml, &config)
report := resolveTmpCargo(t, cargoToml, "", &config)
if len(report.Resolved) != 1 {
t.Error("len(report.Resolved) != 1")
}
Expand All @@ -137,6 +137,40 @@ license = "Apache-2.0"
[dependencies]
libc = "0.2.126" # actual license: MIT OR Apache-2.0
bitflags = "1.3.2" # actual license: MIT/Apache-2.0
fnv = "1.0.7" # actual license: Apache-2.0 / MIT
`

cargoLock := `
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foo"
version = "0.0.0"
dependencies = [
"bitflags",
"fnv",
"libc",
]
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
`

config := deps.ConfigDeps{
Expand All @@ -146,26 +180,21 @@ bitflags = "1.3.2" # actual license: MIT/Apache-2.0
Excludes: []deps.Exclude{},
}

report := resolveTmpCargo(t, cargoToml, &config)
if len(report.Resolved) != 3 {
t.Error("len(report.Resolved) != 3")
report := resolveTmpCargo(t, cargoToml, cargoLock, &config)
if len(report.Resolved) != 4 {
t.Error("len(report.Resolved) != 4")
}
for _, result := range report.Resolved {
if result.Dependency == "libc" {
if result.Dependency == "libc" || result.Dependency == "bitflags" || result.Dependency == "fnv" {
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")
t.Errorf("Resolve dependency %s failed", result.Dependency)
}
}
}
}
}

func resolveTmpCargo(t *testing.T, cargoTomlContent string, config *deps.ConfigDeps) *deps.Report {
func resolveTmpCargo(t *testing.T, cargoTomlContent string, cargoLockContent string, config *deps.ConfigDeps) *deps.Report {
dir, err := os.MkdirTemp("", "skywalking-eyes-test-cargo-")
if err != nil {
t.Error("Make temp dir failed", err)
Expand Down Expand Up @@ -194,6 +223,14 @@ func resolveTmpCargo(t *testing.T, cargoTomlContent string, config *deps.ConfigD
return nil
}

if cargoLockContent != "" {
cargoLockFile := filepath.Join(dir, "Cargo.lock")
if err := os.WriteFile(cargoLockFile, []byte(cargoLockContent), 0644); err != nil {
t.Error("Write Cargo.lock failed", err)
return nil
}
}

resolver := new(deps.CargoTomlResolver)

var report deps.Report
Expand Down

0 comments on commit d299844

Please sign in to comment.