Skip to content

Commit

Permalink
tests: update licensescan tests to recognize sigs.k8s.io/yaml license…
Browse files Browse the repository at this point in the history
… change

The Apache license was added in 1.4.0

Signed-off-by: justinsb <justinsb@google.com>
  • Loading branch information
justinsb committed May 1, 2024
1 parent c39dd38 commit 8587794
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions tools/licensescan/licensedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ import (
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"testing"

"sigs.k8s.io/yaml"
)

// Test that the license didn't change for the same module
func TestLicensesForConsistency(t *testing.T) {
files := make(map[string]*moduleInfo)
type moduleVersion struct {
ModuleInfo *moduleInfo
Version string
}

modules := make(map[string][]*moduleVersion)

if err := filepath.Walk("modules", func(p string, info fs.FileInfo, err error) error {
if err != nil {
Expand All @@ -46,30 +53,35 @@ func TestLicensesForConsistency(t *testing.T) {
return fmt.Errorf("error parsing %q: %w", p, err)
}

files[p] = m
modulePath := filepath.Dir(p)
modulePath = strings.TrimPrefix(modulePath, "modules/")
version := filepath.Base(p)
version = strings.TrimSuffix(version, ".yaml")
modules[modulePath] = append(modules[modulePath], &moduleVersion{
ModuleInfo: m,
Version: version,
})
return nil
}); err != nil {
t.Fatalf("error during walk: %v", err)
}

for f1, m1 := range files {
dir := filepath.Dir(f1)
for f2, m2 := range files {
// Only compare pairs once
if f1 >= f2 {
continue
}

if filepath.Dir(f2) != dir {
continue
}
for module, versions := range modules {
sort.Slice(versions, func(i, j int) bool {
return versions[i].Version < versions[j].Version
})
for i := 0; i < len(versions)-1; i++ {
v1 := versions[i]
v2 := versions[i+1]

if m1.License != m2.License {
switch f1 {
case "modules/github.com/klauspost/compress/v1.11.2.yaml":
if v1.ModuleInfo.License != v2.ModuleInfo.License {
switch module + "@" + v1.Version {
case "github.com/klauspost/compress@v1.11.2":
// license changed after v1.11.2
case "sigs.k8s.io/yaml@v1.3.0":
// license changed after v1.3.0
default:
t.Errorf("license mismatch: %v=%v, %v=%v", f1, m1.License, f2, m2.License)
t.Errorf("license mismatch: %v %v=%v, %v=%v", module, v1.Version, v1.ModuleInfo.License, v2.Version, v2.ModuleInfo.License)
}
}

Expand Down

0 comments on commit 8587794

Please sign in to comment.