// Tests for the release tooling. They live in the module root because that is // where the script is, and because a broken release script is only discovered // at the worst possible moment otherwise. package main import ( "os" "os/exec" "path/filepath" "strings" "testing" ) // skipInsideARelease stops a test that runs the release script from running // while the release script is running it. // // The script sets this before `make check`, and `make check` runs this suite. // Without the guard the two call each other forever — which is not a test-only // hazard: a real release would recurse in exactly the same way. func skipInsideARelease(t *testing.T) { t.Helper() if os.Getenv("TURBO_CORE_RELEASING") != "" { t.Skip("running inside a release; not starting another one") } } // readReleaseScript returns the tag script's text. func readReleaseScript(t *testing.T) string { t.Helper() data, err := os.ReadFile("01-release.tag.sh") if err != nil { t.Fatalf("reading the release script: %v", err) } return string(data) } func TestTheReleaseScriptStopsOnTheFirstFailure(t *testing.T) { // Without this, `git tag` refusing an existing tag is skipped in silence // and the push that follows pushes the *old* one. It happened in turbo-go. if got := readReleaseScript(t); !strings.Contains(got, "set -euo pipefail") { t.Error("the script does not stop on failure") } } func TestTheReleaseScriptChecksBothRefsForTheTag(t *testing.T) { // A tag deleted locally after a failed attempt still exists on origin, and // that state is invisible from the local repository alone. script := readReleaseScript(t) for _, want := range []string{"refs/tags/${TAG}", "git ls-remote --tags origin"} { if !strings.Contains(script, want) { t.Errorf("the script never looks for %q", want) } } } func TestTheReleaseScriptRunsTheSuiteBeforePublishing(t *testing.T) { // A version two editors will pin is the wrong place to find out the suite // was red. if got := readReleaseScript(t); !strings.Contains(got, "make --no-print-directory check") { t.Error("the script publishes without running make check") } } func TestTheReleaseScriptRefusesAReplaceDirective(t *testing.T) { // The proxy serves go.mod as written, so a published library carrying a // replace tells every consumer to look for turbo-core in a directory that // does not exist on their machine. if got := readReleaseScript(t); !strings.Contains(got, "replace") { t.Error("the script does not check go.mod for a replace directive") } } func TestThisModuleHasNoReplaceDirective(t *testing.T) { // The check above only helps if it is true today as well. data, err := os.ReadFile("go.mod") if err != nil { t.Fatalf("reading go.mod: %v", err) } for _, line := range strings.Split(string(data), "\n") { if strings.HasPrefix(strings.TrimSpace(line), "replace ") { t.Errorf("go.mod carries %q; a published library must not", line) } } } func TestTheReleaseScriptTagsOnlyAfterPushing(t *testing.T) { // A rejected push must not leave a tag behind pointing at a commit the // remote has never seen. script := readReleaseScript(t) push := strings.Index(script, "git push origin \"$(git rev-parse") tag := strings.Index(script, "git tag -a") if push < 0 || tag < 0 { t.Fatal("the script neither pushes nor tags") } if tag < push { t.Error("the script tags before it pushes") } } func TestTheReleaseScriptSaysHowToPointTheEditorsAtTheNewVersion(t *testing.T) { // Publishing is half the job; an editor still pinning the old version and // a replace directive is the state this is designed to get out of. script := readReleaseScript(t) for _, want := range []string{"go mod edit -require", "-dropreplace"} { if !strings.Contains(script, want) { t.Errorf("the script never mentions %q", want) } } } func TestMakeVersionReportsSomethingUsable(t *testing.T) { out, err := exec.Command("make", "--no-print-directory", "version").Output() if err != nil { t.Fatalf("make version: %v", err) } if strings.TrimSpace(string(out)) == "" { t.Error("make version printed nothing") } } func TestTheReleaseScriptTagsAndPushesForReal(t *testing.T) { // The whole flow, in a throwaway clone with its own bare remote, so no tag // is ever created in the real repository. Reading the script is not the // same as running it: every guard above was added because one of them was // wrong once. skipInsideARelease(t) if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not available") } root := t.TempDir() remote := filepath.Join(root, "remote.git") clone := filepath.Join(root, "clone") run(t, root, "git", "init", "--bare", "--initial-branch=main", remote) run(t, root, "git", "clone", remote, clone) copyModuleInto(t, clone) run(t, clone, "git", "config", "user.email", "test@example.test") run(t, clone, "git", "config", "user.name", "Release Test") writeFile(t, filepath.Join(clone, "release.env"), "TAG=v0.0.1-test\nABOUT=\"a throwaway release\"\n") out, err := runAllowingFailure(t, clone, "./01-release.tag.sh") if err != nil { t.Fatalf("the release script failed:\n%s", out) } if !strings.Contains(out, "published") { t.Errorf("the script did not report publishing:\n%s", out) } tags, _ := runAllowingFailure(t, remote, "git", "tag") if !strings.Contains(tags, "v0.0.1-test") { t.Errorf("the remote has tags %q, want v0.0.1-test", strings.TrimSpace(tags)) } } func TestTheReleaseScriptRefusesATagItAlreadyPublished(t *testing.T) { // Moving a published version is not an option: two editors may pin it, and // the proxy caches what it fetched. skipInsideARelease(t) if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not available") } root := t.TempDir() remote := filepath.Join(root, "remote.git") clone := filepath.Join(root, "clone") run(t, root, "git", "init", "--bare", "--initial-branch=main", remote) run(t, root, "git", "clone", remote, clone) copyModuleInto(t, clone) run(t, clone, "git", "config", "user.email", "test@example.test") run(t, clone, "git", "config", "user.name", "Release Test") writeFile(t, filepath.Join(clone, "release.env"), "TAG=v0.0.1-test\nABOUT=\"a throwaway release\"\n") if out, err := runAllowingFailure(t, clone, "./01-release.tag.sh"); err != nil { t.Fatalf("the first release failed:\n%s", out) } out, err := runAllowingFailure(t, clone, "./01-release.tag.sh") if err == nil { t.Fatalf("the script published the same tag twice:\n%s", out) } if !strings.Contains(out, "already exists") { t.Errorf("the refusal does not say the tag is taken:\n%s", out) } } // copyModuleInto copies the module's source into a directory, so the script can // be run against a real checkout without touching this one. // // .git is left out because the target has its own, and *.env because a test // writes its own release.env — copying this checkout's would release whatever // version happens to be in it. // // The copying is done here rather than by shelling out to cp, which on a // network-backed working copy has been seen to write the right number of bytes // and the wrong ones: every file in the copy came out NUL-filled, and the // tests below then failed on a go build that had nothing to do with them. func copyModuleInto(t *testing.T, target string) { t.Helper() entries, err := os.ReadDir(".") if err != nil { t.Fatalf("reading the module: %v", err) } for _, entry := range entries { if entry.Name() == ".git" || strings.HasSuffix(entry.Name(), ".env") { continue } copyTree(t, entry.Name(), filepath.Join(target, entry.Name())) } } // copyTree copies a file or a directory to a new path. // // Anything that is neither a regular file nor a directory is skipped: the tool // directories beside the source hold symlinks into caches that do not exist in // a temporary copy, and the release scripts have no use for them. func copyTree(t *testing.T, from, to string) { t.Helper() err := filepath.WalkDir(from, func(path string, entry os.DirEntry, err error) error { if err != nil { return err } relative, err := filepath.Rel(from, path) if err != nil { return err } destination := filepath.Join(to, relative) if entry.IsDir() { return os.MkdirAll(destination, 0o755) } if !entry.Type().IsRegular() { return nil } info, err := entry.Info() if err != nil { return err } data, err := os.ReadFile(path) if err != nil { return err } if err := os.MkdirAll(filepath.Dir(destination), 0o755); err != nil { return err } // The mode carries the execute bit, without which the scripts these // tests exist to run cannot be run. return os.WriteFile(destination, data, info.Mode().Perm()) }) if err != nil { t.Fatalf("copying %s: %v", from, err) } } // run executes a command in a directory, failing the test if it does not // succeed. func run(t *testing.T, dir string, name string, args ...string) { t.Helper() if out, err := runAllowingFailure(t, dir, name, args...); err != nil { t.Fatalf("%s %v: %v\n%s", name, args, err, out) } } // runAllowingFailure executes a command and returns its combined output along // with whether it succeeded. func runAllowingFailure(t *testing.T, dir string, name string, args ...string) (string, error) { t.Helper() command := exec.Command(name, args...) command.Dir = dir out, err := command.CombinedOutput() return string(out), err } // writeFile creates a file, failing the test if it cannot. func writeFile(t *testing.T, path, contents string) { t.Helper() if err := os.WriteFile(path, []byte(contents), 0o644); err != nil { t.Fatalf("writing %s: %v", path, err) } } // readBuildScript returns the staging script's text. func readBuildScript(t *testing.T) string { t.Helper() data, err := os.ReadFile("02-build-releases.sh") if err != nil { t.Fatalf("reading the build script: %v", err) } return string(data) } // readWorkflow returns the release workflow's text. func readWorkflow(t *testing.T) string { t.Helper() data, err := os.ReadFile(filepath.Join(".github", "workflows", "release.yml")) if err != nil { t.Fatalf("reading the release workflow: %v", err) } return string(data) } func TestTheBuildScriptStopsOnTheFirstFailure(t *testing.T) { // It runs in CI on a tag that is already pushed. A step failing in silence // there publishes a release page for artefacts that were never staged. if got := readBuildScript(t); !strings.Contains(got, "set -euo pipefail") { t.Error("the script does not stop on failure") } } func TestTheBuildScriptRefusesAReplaceDirective(t *testing.T) { // 01 checks this too, but CI runs *this* script without ever running 01, // and the proxy serves go.mod as written. if got := readBuildScript(t); !strings.Contains(got, "replace") { t.Error("the script does not check go.mod for a replace directive") } } func TestTheBuildScriptRefusesATagThatIsNotAVersion(t *testing.T) { // The proxy will not serve a tag it cannot read as a version, so a typo // here stages perfectly and then fails at every `go get`. skipInsideARelease(t) dir := t.TempDir() copyModuleInto(t, dir) out, err := runAllowingFailure(t, dir, "./02-build-releases.sh", "v0.o.0") if err == nil { t.Fatalf("the script staged a release for a tag that is not a version:\n%s", out) } if !strings.Contains(out, "v1.2.3") { t.Errorf("the refusal does not say what a tag should look like:\n%s", out) } } func TestTheBuildScriptStagesAnArchiveAndItsChecksum(t *testing.T) { // Reading the script is not the same as running it: the archive is taken // from git, built again once extracted, and checksummed, and each of those // can break on its own. skipInsideARelease(t) if _, err := exec.LookPath("git"); err != nil { t.Skip("git is not available") } dir := t.TempDir() copyModuleInto(t, dir) run(t, dir, "git", "init", "--initial-branch=main") run(t, dir, "git", "config", "user.email", "test@example.test") run(t, dir, "git", "config", "user.name", "Release Test") run(t, dir, "git", "add", ".") run(t, dir, "git", "commit", "-m", "a throwaway commit") out, err := runAllowingFailure(t, dir, "./02-build-releases.sh", "v0.0.1-test") if err != nil { t.Fatalf("staging failed:\n%s", out) } staged := filepath.Join(dir, "release", "v0.0.1-test") for _, name := range []string{"turbo-core-0.0.1-test.tar.gz", "SHA256SUMS", "README.md"} { if _, err := os.Stat(filepath.Join(staged, name)); err != nil { t.Errorf("%s was not staged: %v", name, err) } } // A checksum file that does not match what is beside it is worse than // none: it is checked once, by somebody who then trusts it. if _, err := exec.LookPath("sha256sum"); err != nil { t.Skip("sha256sum is not available to verify SHA256SUMS") } if out, err := runAllowingFailure(t, staged, "sha256sum", "-c", "SHA256SUMS"); err != nil { t.Errorf("SHA256SUMS does not match the archive:\n%s", out) } } func TestTheBuildScriptREADMEInstallsTheModule(t *testing.T) { // The page a person lands on has to say the one line that uses the // library. A release page whose only content is a version number tells // nobody how to depend on it. if got := readBuildScript(t); !strings.Contains(got, "go get ${MODULE}@${TAG}") { t.Error("the staged README never says how to get the module") } } func TestTheWorkflowPublishesOnATagPush(t *testing.T) { // The tag push is the trigger: ./01-release.tag.sh ends by pushing one, // and nothing else starts a release. workflow := readWorkflow(t) for _, want := range []string{"push:", "tags:", `- "v*"`} { if !strings.Contains(workflow, want) { t.Errorf("the workflow never declares %q", want) } } // A workflow with the default read-only token cannot create a release, and // fails at its last step after doing all the work. if !strings.Contains(workflow, "contents: write") { t.Error("the workflow does not ask for contents: write") } } func TestTheWorkflowStagesWithTheSameScriptAPersonRuns(t *testing.T) { // A CI job that stages its own way is a second pipeline nobody tests, and // the local one is then only ever exercised by accident. if got := readWorkflow(t); !strings.Contains(got, "./02-build-releases.sh") { t.Error("the workflow does not stage the release with ./02-build-releases.sh") } } func TestTheWorkflowAttachesWhatWasStaged(t *testing.T) { // Publishing a release page with no files attached is a silent half-job: // the page exists and the links are not there. workflow := readWorkflow(t) for _, want := range []string{"turbo-core-*.tar.gz", "SHA256SUMS", "fail_on_unmatched_files: true"} { if !strings.Contains(workflow, want) { t.Errorf("the workflow never mentions %q", want) } } } func TestTheWorkflowLinksToTheDocumentationAtThatTag(t *testing.T) { // A release page is not inside the repository tree, so a relative path // from it 404s — and a link to the branch would rot as the branch moves. workflow := readWorkflow(t) if !strings.Contains(workflow, "blob/${GITHUB_REF_NAME}") { t.Error("the release notes do not link into the repository at the released tag") } if !strings.Contains(workflow, "/docs/en/README.md") { t.Error("the release notes do not link to the documentation") } } func TestTheWorkflowNeedsNoPersonalToken(t *testing.T) { // The release API behind Rickub's /gh shim accepts the job's own // GITHUB_TOKEN and refuses a personal one, so a secret referenced here is // a credential that cannot work and still has to be kept somewhere. if got := readWorkflow(t); strings.Contains(got, "secrets.") { t.Error("the workflow reads a secret; the job's own token is the only credential the release API takes") } } func TestTheWorkflowDoesNotStartAReleaseInsideItself(t *testing.T) { // The suite it runs includes tests that run ./01-release.tag.sh against a // throwaway clone. Locally the script exports this before calling make; // in CI nothing calls the script, so the job has to set it itself. if got := readWorkflow(t); !strings.Contains(got, "TURBO_CORE_RELEASING") { t.Error("the workflow runs the suite without TURBO_CORE_RELEASING set") } }