Skip to content
quarry Artifact [94346be17f]
Login

Artifact [94346be17f]

Artifact 94346be17f058fadafc8b82fd7a16eafdb3b3168642aa2737d562c4127802002:


package legal

import (
	"encoding/json"
	"os"
	"os/exec"
	"strings"
	"testing"
)

// The inventory must match what the module graph actually contains. A
// licence page that has silently stopped listing a dependency asserts a
// completeness it no longer has, which is worse than having no page.
// Regenerate with `make licenses`.
func TestInventoryIsCurrent(t *testing.T) {
	// From the module root: "./..." means the whole build graph there,
	// and only this package's own dependencies from here.
	cmd := exec.Command("go", "run", "./cmd/licensegen")
	cmd.Dir = "../.."
	out, err := cmd.Output()
	if err != nil {
		t.Skipf("cannot regenerate (no module cache?): %v", err)
	}
	var want, got []Notice
	if err := json.Unmarshal(out, &want); err != nil {
		t.Fatal(err)
	}
	got, err = Notices()
	if err != nil {
		t.Fatal(err)
	}
	if len(got) != len(want) {
		t.Fatalf("inventory lists %d modules, the build graph has %d — run `make licenses`",
			len(got), len(want))
	}
	for i := range want {
		if got[i].Module != want[i].Module || got[i].Version != want[i].Version {
			t.Fatalf("entry %d is %s@%s, want %s@%s — run `make licenses`",
				i, got[i].Module, got[i].Version, want[i].Module, want[i].Version)
		}
	}
}

// Every dependency must carry a licence, and one that does not must say
// so rather than be quietly omitted.
func TestEveryModuleHasALicence(t *testing.T) {
	notices, err := Notices()
	if err != nil {
		t.Fatal(err)
	}
	if len(notices) == 0 {
		t.Fatal("the inventory is empty")
	}
	for _, n := range notices {
		if n.Module == "" || n.Version == "" {
			t.Errorf("entry with no module or version: %+v", n)
		}
		if strings.HasPrefix(n.License, "unknown") {
			t.Errorf("%s ships no licence file; it must be resolved, not shipped as unknown", n.Module)
		}
		if n.Text == "" {
			t.Errorf("%s has no licence text; Apache-2.0 and MIT both require reproducing it", n.Module)
		}
	}
}

// go:embed cannot reach outside its own package, so the licence text is
// copied here. A copy that can drift from the file that actually governs
// is exactly the kind of thing nobody notices, so it is asserted rather
// than trusted.
func TestEmbeddedLicenceMatchesTheRepository(t *testing.T) {
	root, err := os.ReadFile("../../LICENSE")
	if err != nil {
		t.Fatalf("no LICENSE at the repository root: %v", err)
	}
	if Quarry != string(root) {
		t.Error("internal/legal/LICENSE has drifted from ./LICENSE — copy it across")
	}
	if !strings.Contains(Quarry, "BSD 2-Clause") {
		t.Errorf("the embedded licence is not the BSD 2-Clause text")
	}
}