Files
uberbringer/tools/genlicenses/notices_test.go
T
57_WolveandClaude Opus 5.5 341353f702 Read the license files of the SQLite driver's modules
The license notices stopped on two files of the modules that
modernc.org/sqlite brings, which the daemon will link:

- modernc.org/sqlite ships LICENSE-SQLITE, SQLite's dedication to the
  public domain, whose wording the generator did not know. It now reads
  as "public domain", the term modernc's own notices use.
- modernc.org/memory ships LICENSE-LOGO, which holds only the address
  of the image its logo.png comes from. No package links that image, so
  LICENSE-LOGO no longer counts as a license file.

Nothing links these modules yet, so the notices do not change here.

Part of stage 1d of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:39:48 -06:00

207 lines
6.9 KiB
Go

package main
import (
"bytes"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
)
const (
mitText = `Copyright (c) 2020 A. Author
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal`
bsd2Text = `Copyright (c) 2020 A. Author
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice.
2. Redistributions in binary form must reproduce the above copyright notice.`
bsd3Text = bsd2Text + `
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.`
apacheText = ` Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/`
)
func TestClassify(t *testing.T) {
tests := []struct {
name string
body string
want []string
}{
{"MIT", mitText, []string{"MIT"}},
{"MIT on one line", strings.Join(strings.Fields(mitText), " "), []string{"MIT"}},
{"BSD-2-Clause", bsd2Text, []string{"BSD-2-Clause"}},
{"BSD-3-Clause", bsd3Text, []string{"BSD-3-Clause"}},
{"Apache-2.0 in full", apacheText, []string{"Apache-2.0"}},
{"Apache-2.0 by reference", `Licensed under the Apache License, Version 2.0 (the "License");`, []string{"Apache-2.0"}},
{"ISC", "Permission to use, copy, modify, and/or distribute this software for any\npurpose with or without fee is hereby granted", []string{"ISC"}},
{"MPL-2.0", "Mozilla Public License Version 2.0\n==================================", []string{"MPL-2.0"}},
{"Unlicense", "This is free and unencumbered software released into the public domain.", []string{"Unlicense"}},
{"SQLite's dedication", "SQLite Is Public Domain\n\nAll of the code and documentation in SQLite has been dedicated to the public\ndomain by the authors.", []string{"public domain"}},
{"in the order given", mitText + "\n\n" + apacheText, []string{"MIT", "Apache-2.0"}},
{"each named once", mitText + "\n\n" + bsd3Text + "\n\n" + mitText, []string{"MIT", "BSD-3-Clause"}},
{"BSD clauses read up to the next license", bsd2Text + "\n\n" + apacheText + "\n\n" + bsd3Text, []string{"BSD-2-Clause", "Apache-2.0", "BSD-3-Clause"}},
{"unknown", "All rights reserved.", nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := classify(tt.body); !slices.Equal(got, tt.want) {
t.Errorf("classify = %q, want %q", got, tt.want)
}
})
}
}
func TestFileNames(t *testing.T) {
tests := []struct {
name string
license, notice bool
}{
{"LICENSE", true, false},
{"License.md", true, false},
{"LICENCE.txt", true, false},
{"LICENSE-MIT", true, false},
{"LICENSE-APACHE-2.0.txt", true, false},
{"COPYING", true, false},
{"UNLICENSE", true, false},
{"NOTICE", false, true},
{"notice.txt", false, true},
{"license.go", false, false},
{"licenses_test.go", false, false},
{"README.md", false, false},
{"PATENTS", false, false},
{"LICENSE-LOGO", false, false},
{"license-logo.txt", false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLicenseFile(tt.name); got != tt.license {
t.Errorf("isLicenseFile = %v, want %v", got, tt.license)
}
if got := isNoticeFile(tt.name); got != tt.notice {
t.Errorf("isNoticeFile = %v, want %v", got, tt.notice)
}
})
}
}
func TestNormalize(t *testing.T) {
got := normalize("\n\nCopyright\r\n\r\n indented\r\n\r\n")
if want := "Copyright\n\n indented"; got != want {
t.Errorf("normalize = %q, want %q", got, want)
}
}
func TestRenderFencesBackticks(t *testing.T) {
body := "Run ```go build``` and ````that````."
out := string(render([]component{{
name: "example.com/m", version: "v1.0.0", licenses: []string{"MIT"},
files: []licenseText{{path: "LICENSE", body: body}},
}}))
if want := "\n`````\n" + body + "\n`````\n"; !strings.Contains(out, want) {
t.Errorf("the text is not fenced by five backticks:\n%s", out)
}
}
func TestReadComponent(t *testing.T) {
dir := t.TempDir()
write := func(name, body string) {
t.Helper()
path := filepath.Join(dir, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
write("LICENSE", apacheText)
write("NOTICE", "Copyright 2020 A. Author")
write("vendored/LICENSE", mitText)
write("odd/LICENSE", "All rights reserved.")
t.Run("licenses from every license file", func(t *testing.T) {
c, err := readComponent("m", "v1.0.0", dir, []string{"LICENSE", "NOTICE", "vendored/LICENSE"})
if err != nil {
t.Fatal(err)
}
if want := []string{"Apache-2.0", "MIT"}; !slices.Equal(c.licenses, want) {
t.Errorf("licenses = %q, want %q", c.licenses, want)
}
if len(c.files) != 3 {
t.Errorf("got %d files, want 3", len(c.files))
}
})
t.Run("unknown license", func(t *testing.T) {
if _, err := readComponent("m", "v1.0.0", dir, []string{"LICENSE", "odd/LICENSE"}); err == nil {
t.Error("readComponent took a file with no license it knows")
}
})
t.Run("notice alone", func(t *testing.T) {
if _, err := readComponent("m", "v1.0.0", dir, []string{"NOTICE"}); err == nil {
t.Error("readComponent took a module with no license file")
}
})
}
// generated runs the generator once for the tests that need its output,
// which lists the linked modules with the go command.
var generated = sync.OnceValues(func() ([]byte, error) {
root, err := moduleRoot()
if err != nil {
return nil, err
}
return generate(root)
})
// TestCommittedNoticesUpToDate fails when either committed copy of the
// notices differs from what the linked modules produce: regenerate with
// `go generate ./...`.
func TestCommittedNoticesUpToDate(t *testing.T) {
out, err := generated()
if err != nil {
t.Fatal(err)
}
root, err := moduleRoot()
if err != nil {
t.Fatal(err)
}
for _, rel := range []string{noticesPath, embeddedPath} {
committed, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(rel)))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(committed, out) {
t.Errorf("%s is out of date; run: go generate ./...", rel)
}
}
}
// Modules that only the tools and the tests use are not linked into the
// binary, so they have no notices.
func TestNoticesLeaveOutUnlinkedModules(t *testing.T) {
out, err := generated()
if err != nil {
t.Fatal(err)
}
for _, path := range []string{"golang.org/x/tools", "golang.org/x/vuln", "honnef.co/go/tools", "github.com/invopop/jsonschema"} {
if bytes.Contains(out, []byte("\n## "+path+" ")) {
t.Errorf("the notices include %s", path)
}
}
if !bytes.Contains(out, []byte("\n## github.com/gofiber/fiber/v3 ")) {
t.Error("the notices lack github.com/gofiber/fiber/v3")
}
}