Add third-party license notices, generated and embedded in the binary
THIRD-PARTY-LICENSES.md holds the license and notice files of the Go distribution and of the 35 modules that ./cmd/uberbringer links on linux amd64, armv6, armv7 and arm64, as each distribution ships them. tools/genlicenses writes it on go generate from go list -deps, so modules only the tools and tests use stay out. Beside each module's root license it takes NOTICE files and the license files in its linked packages' directories, which cover vendored code in gofiber/utils, klauspost/compress and fasthttp. Each module is listed with its version, so any dependency change needs the file regenerated. The generator fails on a module with no license file or a license it does not recognize. The same text goes to internal/app/uberbringer/licenses_embed.md, which the binary embeds and the new `uberbringer licenses` command prints. The release archives carry the file, and the Docker image keeps it with LICENSE.txt in /usr/share/licenses/uberbringer/. The Makefile's openapi and openapi-check targets become generate and generate-check. They, the CI step and the gate in CLAUDE.md check docs/swagger.json and both license files, and a test fails when a committed copy is out of date. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ee65e72c44
commit
0c88b4ffd5
19 files changed
+4969
-23
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
// `go generate ./...` regenerates THIRD-PARTY-LICENSES.md and the copy the
|
||||
// binary embeds. go generate runs each directive in its own package's
|
||||
// directory, so the directive builds this package as ".".
|
||||
//
|
||||
//go:generate go run .
|
||||
@@ -0,0 +1,129 @@
|
||||
// Command genlicenses writes THIRD-PARTY-LICENSES.md, the licenses and
|
||||
// notices of the third-party software in the uberbringer binary, and the copy
|
||||
// of it that the binary embeds. That software is the Go standard library and
|
||||
// runtime, and every module ./cmd/uberbringer links on the platforms it is
|
||||
// released for. A module's texts are its license and notice files in the
|
||||
// directories of its linked packages and their parents, up to the module
|
||||
// root, so code that a module vendors under a license of its own is covered
|
||||
// too. Modules used only by tools and tests are not linked, so they are left
|
||||
// out. Nothing in the file is written by hand, and a test fails when a
|
||||
// committed copy differs from what this produces.
|
||||
//
|
||||
// go run ./tools/genlicenses (or: go generate ./...)
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The generated files, relative to the repository root. go:embed cannot
|
||||
// reach above the embedding package's directory, so the embedded copy sits
|
||||
// beside that package.
|
||||
const (
|
||||
noticesPath = "THIRD-PARTY-LICENSES.md"
|
||||
embeddedPath = "internal/app/uberbringer/licenses_embed.md"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "genlicenses:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
root, err := moduleRoot()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out, err := generate(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, rel := range []string{noticesPath, embeddedPath} {
|
||||
if err := writeIfChanged(root, rel, out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// generate returns the notices for the binary built from the module at root.
|
||||
func generate(root string) ([]byte, error) {
|
||||
goroot, err := goOutput(root, nil, "env", "GOROOT")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
goroot = strings.TrimSpace(goroot)
|
||||
goFiles, err := licenseFiles(goroot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
goComponent, err := readComponent("Go standard library and runtime", "", goroot, goFiles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
components := []component{goComponent}
|
||||
|
||||
modules, err := linkedModules(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, m := range modules {
|
||||
c, err := readComponent(m.path, m.version, m.dir, m.files)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
components = append(components, c)
|
||||
}
|
||||
return render(components), nil
|
||||
}
|
||||
|
||||
// moduleRoot returns the directory of the main module, which holds go.mod.
|
||||
func moduleRoot() (string, error) {
|
||||
out, err := goOutput("", nil, "list", "-m", "-f", "{{.Dir}}")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(out), nil
|
||||
}
|
||||
|
||||
// goOutput runs the go command in dir, with env added to the environment,
|
||||
// and returns what it writes to standard output.
|
||||
func goOutput(dir string, env []string, args ...string) (string, error) {
|
||||
cmd := exec.Command("go", args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), env...)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("go %s: %w\n%s", strings.Join(args, " "), err, stderr.Bytes())
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
// writeIfChanged writes out to the file rel under root unless it already
|
||||
// holds out, and says which it did.
|
||||
func writeIfChanged(root, rel string, out []byte) error {
|
||||
path := filepath.Join(root, filepath.FromSlash(rel))
|
||||
current, err := os.ReadFile(path)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
if bytes.Equal(current, out) {
|
||||
fmt.Printf("genlicenses: %s is up to date\n", rel)
|
||||
return nil
|
||||
}
|
||||
if err := os.WriteFile(path, out, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("genlicenses: wrote %s\n", rel)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// mainPackage is the binary the notices are for.
|
||||
const mainPackage = "./cmd/uberbringer"
|
||||
|
||||
// platform is a target the binary is built for.
|
||||
type platform struct {
|
||||
goos, goarch, goarm string
|
||||
}
|
||||
|
||||
// platforms are the targets of the release archives in .goreleaser.yaml.
|
||||
// Build constraints can link a module on one platform and not another, so
|
||||
// the notices cover them all.
|
||||
var platforms = []platform{
|
||||
{"linux", "amd64", ""},
|
||||
{"linux", "arm", "6"},
|
||||
{"linux", "arm", "7"},
|
||||
{"linux", "arm64", ""},
|
||||
}
|
||||
|
||||
// module is a third-party module linked into the binary.
|
||||
type module struct {
|
||||
path, version string
|
||||
|
||||
// dir is where the module's files are, in the module cache.
|
||||
dir string
|
||||
|
||||
// files are the license and notice files covering the linked
|
||||
// packages, as slash-separated paths relative to dir, those nearest the
|
||||
// module root first.
|
||||
files []string
|
||||
}
|
||||
|
||||
// listedPackage is the part of a package's `go list -json` record read here.
|
||||
type listedPackage struct {
|
||||
Dir string
|
||||
Standard bool
|
||||
Module *struct {
|
||||
Path string
|
||||
Version string
|
||||
Dir string
|
||||
Main bool
|
||||
}
|
||||
}
|
||||
|
||||
// linkedModules returns, sorted by path, the third-party modules that
|
||||
// mainPackage links on any of the platforms.
|
||||
func linkedModules(root string) ([]module, error) {
|
||||
byPath := make(map[string]*module)
|
||||
found := make(map[string]map[string]bool)
|
||||
names := make(map[string][]string)
|
||||
for _, p := range platforms {
|
||||
packages, err := listDeps(root, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, pkg := range packages {
|
||||
if pkg.Standard || pkg.Module == nil || pkg.Module.Main {
|
||||
continue
|
||||
}
|
||||
if pkg.Module.Dir == "" {
|
||||
return nil, fmt.Errorf("module %s %s has no directory in the module cache", pkg.Module.Path, pkg.Module.Version)
|
||||
}
|
||||
m := byPath[pkg.Module.Path]
|
||||
if m == nil {
|
||||
m = &module{path: pkg.Module.Path, version: pkg.Module.Version, dir: pkg.Module.Dir}
|
||||
byPath[m.path] = m
|
||||
found[m.path] = make(map[string]bool)
|
||||
}
|
||||
files, err := coveringFiles(m.dir, pkg.Dir, names)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("module %s: %w", m.path, err)
|
||||
}
|
||||
for _, f := range files {
|
||||
found[m.path][f] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
modules := make([]module, 0, len(byPath))
|
||||
for _, m := range byPath {
|
||||
m.files = slices.SortedFunc(maps.Keys(found[m.path]), nearestFirst)
|
||||
modules = append(modules, *m)
|
||||
}
|
||||
slices.SortFunc(modules, func(a, b module) int { return strings.Compare(a.path, b.path) })
|
||||
return modules, nil
|
||||
}
|
||||
|
||||
// listDeps returns the packages mainPackage links on platform p.
|
||||
func listDeps(root string, p platform) ([]listedPackage, error) {
|
||||
env := []string{"GOOS=" + p.goos, "GOARCH=" + p.goarch, "GOARM=" + p.goarm, "CGO_ENABLED=0"}
|
||||
out, err := goOutput(root, env, "list", "-deps", "-json=Dir,Standard,Module", mainPackage)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var packages []listedPackage
|
||||
dec := json.NewDecoder(bytes.NewReader([]byte(out)))
|
||||
for dec.More() {
|
||||
var pkg listedPackage
|
||||
if err := dec.Decode(&pkg); err != nil {
|
||||
return nil, fmt.Errorf("reading go list output for %s/%s: %w", p.goos, p.goarch, err)
|
||||
}
|
||||
packages = append(packages, pkg)
|
||||
}
|
||||
return packages, nil
|
||||
}
|
||||
|
||||
// coveringFiles returns the license and notice files in dir and in each of
|
||||
// its parents up to moduleDir, as slash-separated paths relative to
|
||||
// moduleDir. names caches the files found in each directory, by its path.
|
||||
func coveringFiles(moduleDir, dir string, names map[string][]string) ([]string, error) {
|
||||
rel, err := filepath.Rel(moduleDir, dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return nil, fmt.Errorf("package directory %s is outside the module", dir)
|
||||
}
|
||||
var files []string
|
||||
for {
|
||||
abs := filepath.Join(moduleDir, rel)
|
||||
inDir, ok := names[abs]
|
||||
if !ok {
|
||||
inDir, err = licenseFiles(abs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names[abs] = inDir
|
||||
}
|
||||
for _, name := range inDir {
|
||||
files = append(files, path.Join(filepath.ToSlash(rel), name))
|
||||
}
|
||||
if rel == "." {
|
||||
return files, nil
|
||||
}
|
||||
rel = filepath.Dir(rel)
|
||||
}
|
||||
}
|
||||
|
||||
// licenseFiles returns the names of the license and notice files in dir.
|
||||
func licenseFiles(dir string) ([]string, error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var names []string
|
||||
for _, e := range entries {
|
||||
if e.Type().IsRegular() && (isLicenseFile(e.Name()) || isNoticeFile(e.Name())) {
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// isLicenseFile reports whether a file name, in any letter case, is that of
|
||||
// a license: LICENSE, LICENCE, COPYING or UNLICENSE, perhaps with a suffix
|
||||
// such as -MIT, and with no extension or that of a text file.
|
||||
func isLicenseFile(name string) bool {
|
||||
stem := textStem(name)
|
||||
for _, base := range []string{"license", "licence", "copying", "unlicense"} {
|
||||
if stem == base || strings.HasPrefix(stem, base+"-") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// isNoticeFile reports whether a file name, in any letter case, is that of
|
||||
// the NOTICE file whose text the Apache License requires to be passed on.
|
||||
func isNoticeFile(name string) bool {
|
||||
return textStem(name) == "notice"
|
||||
}
|
||||
|
||||
// textStem returns a file name in lower case, without its extension if that
|
||||
// is the extension of a text file.
|
||||
func textStem(name string) string {
|
||||
lower := strings.ToLower(name)
|
||||
switch ext := path.Ext(lower); ext {
|
||||
case ".md", ".txt", ".rst":
|
||||
return strings.TrimSuffix(lower, ext)
|
||||
}
|
||||
return lower
|
||||
}
|
||||
|
||||
// nearestFirst orders slash-separated paths by depth, then by name, so a
|
||||
// module's own license comes before those of code it vendors.
|
||||
func nearestFirst(a, b string) int {
|
||||
if c := cmp.Compare(strings.Count(a, "/"), strings.Count(b, "/")); c != 0 {
|
||||
return c
|
||||
}
|
||||
return strings.Compare(a, b)
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// header opens the notices.
|
||||
const header = `# Third-party licenses
|
||||
|
||||
Überbringer is licensed under the European Union Public Licence v. 1.2
|
||||
(EUPL-1.2). Its text is in LICENSE.txt, which comes with every release.
|
||||
|
||||
The uberbringer binary also contains the Go standard library and runtime and
|
||||
the Go modules listed below. Their license and notice files follow, as each
|
||||
distribution ships them. A file in a subdirectory of a module covers the
|
||||
code in that directory.
|
||||
|
||||
This file is generated by tools/genlicenses (go generate ./...) from the
|
||||
modules linked into the binary. Do not edit it by hand.
|
||||
`
|
||||
|
||||
// component is a part of the binary under licenses of its own.
|
||||
type component struct {
|
||||
name, version string
|
||||
|
||||
// licenses are SPDX identifiers, in the order the files give them.
|
||||
licenses []string
|
||||
|
||||
files []licenseText
|
||||
}
|
||||
|
||||
// licenseText is a license or notice file as its distribution ships it.
|
||||
type licenseText struct {
|
||||
path, body string
|
||||
}
|
||||
|
||||
// readComponent reads the license and notice files, named relative to dir,
|
||||
// that cover a component, and tells its licenses from them.
|
||||
func readComponent(name, version, dir string, files []string) (component, error) {
|
||||
c := component{name: name, version: version}
|
||||
for _, f := range files {
|
||||
b, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(f)))
|
||||
if err != nil {
|
||||
return component{}, fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
body := normalize(string(b))
|
||||
c.files = append(c.files, licenseText{path: f, body: body})
|
||||
if isNoticeFile(path.Base(f)) {
|
||||
continue
|
||||
}
|
||||
ids := classify(body)
|
||||
if len(ids) == 0 {
|
||||
return component{}, fmt.Errorf("%s: %s holds no license that licenseMarkers knows; add its wording there", name, f)
|
||||
}
|
||||
for _, id := range ids {
|
||||
if !slices.Contains(c.licenses, id) {
|
||||
c.licenses = append(c.licenses, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(c.licenses) == 0 {
|
||||
return component{}, fmt.Errorf("%s ships no license file", name)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// normalize gives a file LF line endings and drops the blank lines around
|
||||
// it. Git keeps the generated files with LF endings, so a license file with
|
||||
// CRLF endings would otherwise never match its committed copy.
|
||||
func normalize(s string) string {
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
s = strings.TrimLeft(s, "\n")
|
||||
return strings.TrimRight(s, " \t\n")
|
||||
}
|
||||
|
||||
// licenseMarkers are phrases, in lower case and with single spaces, that
|
||||
// the text of each license holds. "BSD" stands for BSD-2-Clause and
|
||||
// BSD-3-Clause, which classify tells apart.
|
||||
var licenseMarkers = []struct{ id, phrase string }{
|
||||
{"Apache-2.0", "apache license version 2.0"},
|
||||
{"Apache-2.0", "apache license, version 2.0"},
|
||||
{"BSD", "redistribution and use in source and binary forms"},
|
||||
{"ISC", "permission to use, copy, modify, and/or distribute this software for any purpose"},
|
||||
{"MIT", "permission is hereby granted, free of charge, to any person obtaining a copy"},
|
||||
{"MPL-2.0", "mozilla public license version 2.0"},
|
||||
{"MPL-2.0", "mozilla public license, version 2.0"},
|
||||
{"Unlicense", "this is free and unencumbered software released into the public domain"},
|
||||
}
|
||||
|
||||
// bsdThirdClause is in BSD-3-Clause and not in BSD-2-Clause.
|
||||
const bsdThirdClause = "endorse or promote products derived from this software"
|
||||
|
||||
// classify returns the SPDX identifiers of the licenses a license file
|
||||
// holds, in the order they appear, or nil when it holds none it knows. A
|
||||
// file may hold several, such as a module's own license followed by those
|
||||
// of code it includes.
|
||||
func classify(body string) []string {
|
||||
text := strings.Join(strings.Fields(strings.ToLower(body)), " ")
|
||||
type hit struct {
|
||||
at int
|
||||
id string
|
||||
}
|
||||
var hits []hit
|
||||
for _, m := range licenseMarkers {
|
||||
for from := 0; ; {
|
||||
i := strings.Index(text[from:], m.phrase)
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
hits = append(hits, hit{from + i, m.id})
|
||||
from += i + len(m.phrase)
|
||||
}
|
||||
}
|
||||
slices.SortFunc(hits, func(a, b hit) int { return cmp.Compare(a.at, b.at) })
|
||||
|
||||
var ids []string
|
||||
for i, h := range hits {
|
||||
id := h.id
|
||||
if id == "BSD" {
|
||||
end := len(text)
|
||||
if i+1 < len(hits) {
|
||||
end = hits[i+1].at
|
||||
}
|
||||
id = "BSD-2-Clause"
|
||||
if strings.Contains(text[h.at:end], bsdThirdClause) {
|
||||
id = "BSD-3-Clause"
|
||||
}
|
||||
}
|
||||
if !slices.Contains(ids, id) {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// render writes the notices: the header, a table of the components, and
|
||||
// each component's files in full.
|
||||
func render(components []component) []byte {
|
||||
var b strings.Builder
|
||||
b.WriteString(header)
|
||||
b.WriteString("\n| Component | Version | License |\n| --- | --- | --- |\n")
|
||||
for _, c := range components {
|
||||
fmt.Fprintf(&b, "| %s | %s | %s |\n", c.name, c.version, strings.Join(c.licenses, ", "))
|
||||
}
|
||||
for _, c := range components {
|
||||
b.WriteString("\n## " + c.name)
|
||||
if c.version != "" {
|
||||
b.WriteString(" " + c.version)
|
||||
}
|
||||
label := "License"
|
||||
if len(c.licenses) > 1 {
|
||||
label = "Licenses"
|
||||
}
|
||||
fmt.Fprintf(&b, "\n\n%s: %s\n", label, strings.Join(c.licenses, ", "))
|
||||
for _, f := range c.files {
|
||||
fence := fence(f.body)
|
||||
fmt.Fprintf(&b, "\n### %s\n\n%s\n%s\n%s\n", f.path, fence, f.body, fence)
|
||||
}
|
||||
}
|
||||
return []byte(b.String())
|
||||
}
|
||||
|
||||
// fence returns a code fence longer than any run of backticks in body, so
|
||||
// that no text can close its block early.
|
||||
func fence(body string) string {
|
||||
longest, run := 0, 0
|
||||
for _, r := range body {
|
||||
if r != '`' {
|
||||
run = 0
|
||||
continue
|
||||
}
|
||||
run++
|
||||
longest = max(longest, run)
|
||||
}
|
||||
return strings.Repeat("`", max(3, longest+1))
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
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"}},
|
||||
{"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},
|
||||
}
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user