Skip to content

Commit 43f8bb8

Browse files
authored
Dependencies check should report unknown licneses (#158)
1 parent 5b7ee17 commit 43f8bb8

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

assets/compatibility/Apache-2.0.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ compatible:
4343
- Unlicense.txt
4444
- HPND.txt
4545
- MulanPSL-2.0.txt
46+
- MIT
4647

4748
incompatible:
4849
- Unknown

pkg/deps/check.go

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package deps
1919

2020
import (
2121
"fmt"
22+
"math"
2223
"path/filepath"
2324
"strings"
2425

@@ -73,34 +74,35 @@ func Check(mainLicenseSpdxID string, config *ConfigDeps) error {
7374
return CheckWithMatrix(mainLicenseSpdxID, &matrix, &report)
7475
}
7576

76-
func CheckWithMatrix(mainLicenseSpdxID string, matrix *CompatibilityMatrix, report *Report) error {
77-
var incompatibleResults []*Result
78-
for _, result := range append(report.Resolved, report.Skipped...) {
79-
compare := func(list []string, spdxID string) bool {
80-
for _, com := range list {
81-
if spdxID == com {
82-
return true
83-
}
84-
}
85-
return false
86-
}
87-
compareAll := func(spdxIDs []string, compare func(spdxID string) bool) bool {
88-
for _, spdxID := range spdxIDs {
89-
if !compare(spdxID) {
90-
return false
91-
}
92-
}
77+
func compare(list []string, spdxID string) bool {
78+
for _, com := range list {
79+
if spdxID == com {
9380
return true
9481
}
95-
compareAny := func(spdxIDs []string, compare func(spdxID string) bool) bool {
96-
for _, spdxID := range spdxIDs {
97-
if compare(spdxID) {
98-
return true
99-
}
100-
}
82+
}
83+
return false
84+
}
85+
func compareAll(spdxIDs []string, compare func(spdxID string) bool) bool {
86+
for _, spdxID := range spdxIDs {
87+
if !compare(spdxID) {
10188
return false
10289
}
90+
}
91+
return true
92+
}
93+
func compareAny(spdxIDs []string, compare func(spdxID string) bool) bool {
94+
for _, spdxID := range spdxIDs {
95+
if compare(spdxID) {
96+
return true
97+
}
98+
}
99+
return false
100+
}
103101

102+
func CheckWithMatrix(mainLicenseSpdxID string, matrix *CompatibilityMatrix, report *Report) error {
103+
var incompatibleResults []*Result
104+
var unknownResults []*Result
105+
for _, result := range append(report.Resolved, report.Skipped...) {
104106
operator, spdxIDs := parseLicenseExpression(result.LicenseSpdxID)
105107

106108
switch operator {
@@ -134,16 +136,34 @@ func CheckWithMatrix(mainLicenseSpdxID string, matrix *CompatibilityMatrix, repo
134136
}
135137
if incompatible := compare(matrix.Incompatible, spdxIDs[0]); incompatible {
136138
incompatibleResults = append(incompatibleResults, result)
139+
continue
137140
}
141+
unknownResults = append(unknownResults, result)
138142
}
139143
}
140144

141-
if len(incompatibleResults) > 0 {
142-
str := ""
145+
if len(incompatibleResults) > 0 || len(unknownResults) > 0 {
146+
dWidth, lWidth := float64(len("Dependency")), float64(len("License"))
143147
for _, r := range incompatibleResults {
144-
str += fmt.Sprintf("\nLicense: %v Dependency: %v", r.LicenseSpdxID, r.Dependency)
148+
dWidth = math.Max(float64(len(r.Dependency)), dWidth)
149+
lWidth = math.Max(float64(len(r.LicenseSpdxID)), lWidth)
145150
}
146-
return fmt.Errorf("the following licenses are incompatible with the main license: %v %v", mainLicenseSpdxID, str)
151+
for _, r := range unknownResults {
152+
dWidth = math.Max(float64(len(r.Dependency)), dWidth)
153+
lWidth = math.Max(float64(len(r.LicenseSpdxID)), lWidth)
154+
}
155+
156+
rowTemplate := fmt.Sprintf("%%-%dv | %%%dv\n", int(dWidth), int(lWidth))
157+
s := fmt.Sprintf(rowTemplate, "Dependency", "License")
158+
s += fmt.Sprintf(rowTemplate, strings.Repeat("-", int(dWidth)), strings.Repeat("-", int(lWidth)))
159+
for _, r := range incompatibleResults {
160+
s += fmt.Sprintf(rowTemplate, r.Dependency, r.LicenseSpdxID)
161+
}
162+
for _, r := range unknownResults {
163+
s += fmt.Sprintf(rowTemplate, r.Dependency, r.LicenseSpdxID)
164+
}
165+
166+
return fmt.Errorf("the following licenses are unknown or incompatible with the main license, please check manually: %v\n%v", mainLicenseSpdxID, s)
147167
}
148168

149169
return nil

pkg/deps/check_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestCheckWithMatrix(t *testing.T) {
7979
},
8080
}); err == nil {
8181
t.Errorf("Should return error")
82-
} else if !strings.Contains(err.Error(), "License: LGPL-2.0 Dependency: Bar") {
82+
} else if !strings.Contains(err.Error(), "Bar | LGPL-2.0") {
8383
t.Errorf("Should return error and contains dependency Bar, now is `%s`", err.Error())
8484
}
8585

@@ -98,7 +98,7 @@ func TestCheckWithMatrix(t *testing.T) {
9898
},
9999
}); err == nil {
100100
t.Errorf("Should return error")
101-
} else if !strings.Contains(err.Error(), "License: Unknown Dependency: Bar") {
101+
} else if !strings.Contains(err.Error(), "Bar | Unknown") {
102102
t.Errorf("Should return error and has dependency Bar, now is `%s`", err.Error())
103103
}
104104

0 commit comments

Comments
 (0)