Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions hack/licenses/detector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
)

// checkInSync regenerates the report into a temp file and compares it with
// outFile. An out-of-date file prints a diff and a warning but returns nil.
func (g generator) checkInSync(hackDir, outFile string, deps []byte) error {
tmp, err := os.CreateTemp("", "third-party-licenses-*.md")
if err != nil {
return err
}
target := tmp.Name()
if err := tmp.Close(); err != nil {
return err
}
defer func() { _ = os.Remove(target) }()

_, _ = fmt.Fprintln(g.stderr, "Generating", filepath.Base(outFile)+"...")
if err := g.runDetector(hackDir, deps, target); err != nil {
return err
}

current, err := os.ReadFile(outFile)
if err != nil {
return fmt.Errorf("reading %s: %w", outFile, err)
}
generated, err := os.ReadFile(target)
if err != nil {
return err
}
if !bytes.Equal(current, generated) {
diff := exec.Command("git", "--no-pager", "diff", "--no-index", "--", outFile, target)
diff.Stdout = g.stderr
diff.Stderr = g.stderr
_ = g.runCmd(diff)
_, _ = fmt.Fprintln(
g.stderr,
"warning: THIRD_PARTY_LICENSES.md is out of date; run 'mise run cli:licenses' and commit the result",
)
return nil
}

_, _ = fmt.Fprintln(g.stderr, "THIRD_PARTY_LICENSES.md is up to date.")
return nil
}

func (g generator) runDetector(hackDir string, deps []byte, outPath string) error {
tmpDir, err := os.MkdirTemp("", "go-licence-detector-")
if err != nil {
return err
}
defer func() { _ = os.RemoveAll(tmpDir) }()

cmd := exec.Command("go", "run",
"go.elastic.co/go-licence-detector@"+detectorVersion,
"-rules", filepath.Join(hackDir, "rules.json"),
"-overrides", filepath.Join(hackDir, "overrides.ndjson"),
"-depsTemplate", filepath.Join(hackDir, "third-party-licenses.md.tmpl"),
"-depsOut", outPath,
)
cmd.Dir = tmpDir
cmd.Stdin = bytes.NewReader(deps)
cmd.Stderr = g.stderr
if err := g.runCmd(cmd); err != nil {
return fmt.Errorf("go-licence-detector: %w", err)
}

return normalizeTrailingNewline(outPath)
}

// normalizeTrailingNewline leaves exactly one trailing newline on path.
func normalizeTrailingNewline(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return err
}
normalized := append(bytes.TrimRight(content, "\n"), '\n')
if bytes.Equal(content, normalized) {
return nil
}
return os.WriteFile(path, normalized, 0o644)
}
126 changes: 126 additions & 0 deletions hack/licenses/detector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"strings"
"testing"
)

const (
reportBody = "# Licenses\n"
inSyncMsg = "THIRD_PARTY_LICENSES.md is up to date."
)

func TestNormalizeTrailingNewline(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{name: "no trailing newline", in: "# Licenses", want: reportBody},
{name: "one trailing newline", in: reportBody, want: reportBody},
{name: "many trailing newlines", in: "# Licenses\n\n\n", want: reportBody},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "out.md")
fatalOnErr(t, os.WriteFile(path, []byte(tt.in), 0o644))

fatalOnErr(t, normalizeTrailingNewline(path))

got, err := os.ReadFile(path)
fatalOnErr(t, err)
if string(got) != tt.want {
t.Errorf("content = %q, want %q", got, tt.want)
}
})
}
}

// TestRunDetectorFeedsDepsAndNormalizesOutput checks the detector receives the
// deps stream on stdin and that its output lands at the requested path with
// one trailing newline.
func TestRunDetectorFeedsDepsAndNormalizesOutput(t *testing.T) {
fake := &fakeProcesses{detectorOut: reportBody + "\n"}
g := generator{root: t.TempDir(), runCmd: fake.run, stderr: io.Discard}
deps := []byte("{\n\t\"Path\": \"a.example/a\"\n}\n")
outPath := filepath.Join(t.TempDir(), "THIRD_PARTY_LICENSES.md")

fatalOnErr(t, g.runDetector(filepath.Join(g.root, "hack", "licenses"), deps, outPath))

if !bytes.Equal(fake.detectorIn, deps) {
t.Errorf("detector stdin = %q, want %q", fake.detectorIn, deps)
}
got, err := os.ReadFile(outPath)
fatalOnErr(t, err)
if string(got) != reportBody {
t.Errorf("output = %q, want %q", got, reportBody)
}
}

// TestCheckInSync pins current check-mode behaviour: an out-of-date file
// prints a warning and still returns nil (see PR Notes). A missing file is
// an error.
func TestCheckInSync(t *testing.T) {
tests := []struct {
name string
current *string
detector string
wantErr bool
wantStderr string
}{
{
name: "in sync",
current: new(reportBody),
detector: reportBody,
wantStderr: inSyncMsg,
},
{
name: "trailing newline drift is in sync",
current: new(reportBody),
detector: "# Licenses\n\n\n",
wantStderr: inSyncMsg,
},
{
name: "out of date warns and returns nil",
current: new("# Old\n"),
detector: "# New\n",
wantStderr: "warning: THIRD_PARTY_LICENSES.md is out of date",
},
{
name: "missing file is an error",
current: nil,
detector: reportBody,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
outFile := filepath.Join(dir, "THIRD_PARTY_LICENSES.md")
if tt.current != nil {
fatalOnErr(t, os.WriteFile(outFile, []byte(*tt.current), 0o644))
}
fake := &fakeProcesses{detectorOut: tt.detector}
var stderr bytes.Buffer
g := generator{root: dir, runCmd: fake.run, stderr: &stderr}

err := g.checkInSync(filepath.Join(dir, "hack", "licenses"), outFile, []byte("{}\n"))

if tt.wantErr {
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("err = %v, want missing-file error", err)
}
return
}
fatalOnErr(t, err)
if !strings.Contains(stderr.String(), tt.wantStderr) {
t.Errorf("stderr = %q, want it to contain %q", stderr.String(), tt.wantStderr)
}
})
}
}
80 changes: 80 additions & 0 deletions hack/licenses/fake_runner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"
)

// fakeProcesses stands in for the go and git subprocesses so tests run
// without a toolchain or network. go list output is keyed by "GOOS/GOARCH";
// unlisted targets produce no output. The detector writes detectorOut to its
// -depsOut path and records the stdin it was given.
type fakeProcesses struct {
listOut map[string]string
listErr map[string]error
detectorOut string
detectorIn []byte
}

func (f *fakeProcesses) run(cmd *exec.Cmd) error {
switch {
case cmd.Args[0] == "git":
return nil
case cmd.Args[1] == "list":
key := envValue(cmd.Env, "GOOS") + "/" + envValue(cmd.Env, "GOARCH")
if err := f.listErr[key]; err != nil {
return err
}
_, err := io.WriteString(cmd.Stdout, f.listOut[key])
return err
default:
in, err := io.ReadAll(cmd.Stdin)
if err != nil {
return err
}
f.detectorIn = in
out := argValue(cmd.Args, "-depsOut")
if out == "" {
return fmt.Errorf("detector invoked without -depsOut")
}
return os.WriteFile(out, []byte(f.detectorOut), 0o644)
}
}

// envValue returns the last value for key, matching exec's override order.
func envValue(env []string, key string) string {
val := ""
for _, kv := range env {
if k, v, ok := strings.Cut(kv, "="); ok && k == key {
val = v
}
}
return val
}

// argValue returns the argument following flag, or "" if absent.
func argValue(args []string, flag string) string {
for i := 0; i+1 < len(args); i++ {
if args[i] == flag {
return args[i+1]
}
}
return ""
}

// lines joins its arguments with newlines and appends a trailing newline.
func lines(parts ...string) string {
return strings.Join(parts, "\n") + "\n"
}

// fatalOnErr fails the test when err is non-nil.
func fatalOnErr(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
Loading
Loading