package ci
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// leakgate runs leakgate.sh over one file holding text, and says whether it
// passed.
func leakgate(t *testing.T, text string) (bool, string) {
t.Helper()
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "f.go"), []byte(text), 0o644); err != nil {
t.Fatal(err)
}
out, err := exec.Command("bash", "leakgate.sh", dir).CombinedOutput()
return err == nil, string(out)
}
// The gate fails on a banned word or a private record id, wherever it sits on
// the line, and passes the known false positives.
func TestTheLeakGateRefusesWhatMustNotBePublished(t *testing.T) {
for _, c := range []struct{ name, text string }{
{"a banned word", "// a folder some downloader called Lidarr made\n"},
{"a banned word in a fixture path", `{".sluice-held/Artist/01.flac", true},` + "\n"},
{"a banned word beside an allowed one", "var list JsonArray // the torrent's files\n"},
{"a ticket", "// fixed in (#4321)\n"},
{"a worded citation", "// as milestone 635 decided\n"},
{"a short milestone id", "// added in m7-scan-progress\n"},
{"a citation split across comment lines", "// as the rule\n// 12 says\n"},
{"a library measured, split across lines", "// counted on the reference\n// library\n"},
{"an id beside a drawing path", " see rule 9\n"},
{"an id beside Material 3", "// M3's default, as M12 chose\n"},
} {
if passed, out := leakgate(t, c.text); passed {
t.Errorf("%s: passed %q\n%s", c.name, c.text, out)
}
}
}
func TestTheLeakGatePassesItsKnownFalsePositives(t *testing.T) {
text := strings.Join([]string{
"import kotlinx.serialization.json.JsonArray",
``,
`android:pathData="M17,3H7c-1.1,0 -1.99,0.9 -1.99,2L5,21z" />`,
`back: 'M15 5l-7 7 7 7',`,
"97 -322 147 -488 125z m928 -812 c53 -27 39 -34",
"// M3 inactive default is secondaryContainer; drops M3's stop",
`expect(css).toMatch(/--fs-obsidian: #000;[\s\S]*--fs-parchment: #FFF;/);`,
"// a song called the rule of three is fine",
}, "\n") + "\n"
if passed, out := leakgate(t, text); !passed {
t.Errorf("refused a known false positive:\n%s", out)
}
}
// repo makes a git repository whose commits carry the given subjects, and
// returns its path.
func repo(t *testing.T, subjects ...string) string {
t.Helper()
dir := t.TempDir()
git(t, dir, "init", "-q", "-b", "main")
for _, s := range subjects {
commit(t, dir, s)
}
return dir
}
// commit makes one commit with the given subject, changing a file so that it
// is never empty.
func commit(t *testing.T, dir, subject string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, "f"), []byte(subject), 0o644); err != nil {
t.Fatal(err)
}
git(t, dir, "add", "f")
git(t, dir, "commit", "-q", "-m", subject)
}
func git(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", append([]string{"-c", "user.name=t", "-c", "user.email=t@example.com"}, args...)...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
return strings.TrimSpace(string(out))
}
// changelog runs changelog.sh in dir and returns the private and public lists.
func changelog(t *testing.T, dir string) (string, string) {
t.Helper()
script, err := filepath.Abs("changelog.sh")
if err != nil {
t.Fatal(err)
}
out := t.TempDir()
cmd := exec.Command("bash", script, out)
cmd.Dir = dir
if msg, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("changelog.sh: %v\n%s", err, msg)
}
read := func(name string) string {
b, err := os.ReadFile(filepath.Join(out, name))
if err != nil {
t.Fatal(err)
}
return string(b)
}
return read("private.md"), read("public.md")
}
// The first public release has no public release before it, and its changelog
// is the fixed note rather than every commit the project ever made. A v* tag
// from before the public releases began is not a public release, so it does
// not count as one.
func TestTheFirstReleaseIsAFixedNote(t *testing.T) {
dir := repo(t, "library: the first commit")
git(t, dir, "tag", "v2026.10.26.1520")
commit(t, dir, "web: after the forge-only release")
private, public := changelog(t, dir)
if private != public || strings.Contains(public, "web:") || !strings.Contains(public, "first public release") {
t.Errorf("private %q, public %q: want the same fixed note", private, public)
}
}
// A later release lists what changed since the previous one. The public list
// loses its record ids and the commits that only touch CI or tests.
func TestALaterReleaseListsWhatChangedSinceThePrevious(t *testing.T) {
dir := repo(t, "library: before the release")
git(t, dir, "tag", "v2026.12.05.0000")
for _, s := range []string{
"fix(library): a song keeps its title (#4321)",
"web, ci: a page and its lane (rule 12, #77)",
"ci: a faster lane",
"test(web): one test fewer",
"web tests: one more",
"server, tests: a route, as milestone 635 asks",
} {
commit(t, dir, s)
}
private, public := changelog(t, dir)
if strings.Contains(private, "before the release") {
t.Errorf("the private list reaches past the previous release:\n%s", private)
}
if !strings.Contains(private, "(#4321)") || !strings.Contains(private, "- ci: a faster lane") {
t.Errorf("the private list is not as written:\n%s", private)
}
want := "Changes since v2026.12.05.0000:\n\n" +
"- fix(library): a song keeps its title\n" +
"- web, ci: a page and its lane\n" +
"- server, tests: a route, as asks\n"
if public != want {
t.Errorf("public list:\n%s\nwant:\n%s", public, want)
}
}
// A re-run of a release finds its own tag on the commit, and lists from the
// release before it.
func TestARerunListsFromTheReleaseBefore(t *testing.T) {
dir := repo(t, "library: before")
git(t, dir, "tag", "v2026.12.05.0000")
commit(t, dir, "web: after")
git(t, dir, "tag", "v2026.12.06.0000")
_, public := changelog(t, dir)
if !strings.Contains(public, "since v2026.12.05.0000") || !strings.Contains(public, "- web: after") {
t.Errorf("re-run list:\n%s", public)
}
}