turbo-editors/turbo-corepublic Fork 0
v1.0.0
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

release_test.go · 463 lines · 15.8 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 17h ago1// Tests for the release tooling. They live in the module root because that is
2// where the script is, and because a broken release script is only discovered
3// at the worst possible moment otherwise.
4package main
5
6import (
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "testing"
12)
13
14// skipInsideARelease stops a test that runs the release script from running
15// while the release script is running it.
16//
17// The script sets this before `make check`, and `make check` runs this suite.
18// Without the guard the two call each other forever — which is not a test-only
19// hazard: a real release would recurse in exactly the same way.
20func skipInsideARelease(t *testing.T) {
21 t.Helper()
22 if os.Getenv("TURBO_CORE_RELEASING") != "" {
23 t.Skip("running inside a release; not starting another one")
24 }
25}
26
27// readReleaseScript returns the tag script's text.
28func readReleaseScript(t *testing.T) string {
29 t.Helper()
30
31 data, err := os.ReadFile("01-release.tag.sh")
32 if err != nil {
33 t.Fatalf("reading the release script: %v", err)
34 }
35 return string(data)
36}
37
38func TestTheReleaseScriptStopsOnTheFirstFailure(t *testing.T) {
39 // Without this, `git tag` refusing an existing tag is skipped in silence
40 // and the push that follows pushes the *old* one. It happened in turbo-go.
41 if got := readReleaseScript(t); !strings.Contains(got, "set -euo pipefail") {
42 t.Error("the script does not stop on failure")
43 }
44}
45
46func TestTheReleaseScriptChecksBothRefsForTheTag(t *testing.T) {
47 // A tag deleted locally after a failed attempt still exists on origin, and
48 // that state is invisible from the local repository alone.
49 script := readReleaseScript(t)
50
51 for _, want := range []string{"refs/tags/${TAG}", "git ls-remote --tags origin"} {
52 if !strings.Contains(script, want) {
53 t.Errorf("the script never looks for %q", want)
54 }
55 }
56}
57
58func TestTheReleaseScriptRunsTheSuiteBeforePublishing(t *testing.T) {
59 // A version two editors will pin is the wrong place to find out the suite
60 // was red.
61 if got := readReleaseScript(t); !strings.Contains(got, "make --no-print-directory check") {
62 t.Error("the script publishes without running make check")
63 }
64}
65
66func TestTheReleaseScriptRefusesAReplaceDirective(t *testing.T) {
67 // The proxy serves go.mod as written, so a published library carrying a
68 // replace tells every consumer to look for turbo-core in a directory that
69 // does not exist on their machine.
70 if got := readReleaseScript(t); !strings.Contains(got, "replace") {
71 t.Error("the script does not check go.mod for a replace directive")
72 }
73}
74
75func TestThisModuleHasNoReplaceDirective(t *testing.T) {
76 // The check above only helps if it is true today as well.
77 data, err := os.ReadFile("go.mod")
78 if err != nil {
79 t.Fatalf("reading go.mod: %v", err)
80 }
81 for _, line := range strings.Split(string(data), "\n") {
82 if strings.HasPrefix(strings.TrimSpace(line), "replace ") {
83 t.Errorf("go.mod carries %q; a published library must not", line)
84 }
85 }
86}
87
88func TestTheReleaseScriptTagsOnlyAfterPushing(t *testing.T) {
89 // A rejected push must not leave a tag behind pointing at a commit the
90 // remote has never seen.
91 script := readReleaseScript(t)
92
93 push := strings.Index(script, "git push origin \"$(git rev-parse")
94 tag := strings.Index(script, "git tag -a")
95 if push < 0 || tag < 0 {
96 t.Fatal("the script neither pushes nor tags")
97 }
98 if tag < push {
99 t.Error("the script tags before it pushes")
100 }
101}
102
103func TestTheReleaseScriptSaysHowToPointTheEditorsAtTheNewVersion(t *testing.T) {
104 // Publishing is half the job; an editor still pinning the old version and
105 // a replace directive is the state this is designed to get out of.
106 script := readReleaseScript(t)
107
108 for _, want := range []string{"go mod edit -require", "-dropreplace"} {
109 if !strings.Contains(script, want) {
110 t.Errorf("the script never mentions %q", want)
111 }
112 }
113}
114
115func TestMakeVersionReportsSomethingUsable(t *testing.T) {
116 out, err := exec.Command("make", "--no-print-directory", "version").Output()
117 if err != nil {
118 t.Fatalf("make version: %v", err)
119 }
120 if strings.TrimSpace(string(out)) == "" {
121 t.Error("make version printed nothing")
122 }
123}
124
125func TestTheReleaseScriptTagsAndPushesForReal(t *testing.T) {
126 // The whole flow, in a throwaway clone with its own bare remote, so no tag
127 // is ever created in the real repository. Reading the script is not the
128 // same as running it: every guard above was added because one of them was
129 // wrong once.
130 skipInsideARelease(t)
131 if _, err := exec.LookPath("git"); err != nil {
132 t.Skip("git is not available")
133 }
134
135 root := t.TempDir()
136 remote := filepath.Join(root, "remote.git")
137 clone := filepath.Join(root, "clone")
138
139 run(t, root, "git", "init", "--bare", "--initial-branch=main", remote)
140 run(t, root, "git", "clone", remote, clone)
141 copyModuleInto(t, clone)
142 run(t, clone, "git", "config", "user.email", "test@example.test")
143 run(t, clone, "git", "config", "user.name", "Release Test")
144 writeFile(t, filepath.Join(clone, "release.env"), "TAG=v0.0.1-test\nABOUT=\"a throwaway release\"\n")
145
146 out, err := runAllowingFailure(t, clone, "./01-release.tag.sh")
147 if err != nil {
148 t.Fatalf("the release script failed:\n%s", out)
149 }
150 if !strings.Contains(out, "published") {
151 t.Errorf("the script did not report publishing:\n%s", out)
152 }
153
154 tags, _ := runAllowingFailure(t, remote, "git", "tag")
155 if !strings.Contains(tags, "v0.0.1-test") {
156 t.Errorf("the remote has tags %q, want v0.0.1-test", strings.TrimSpace(tags))
157 }
158}
159
160func TestTheReleaseScriptRefusesATagItAlreadyPublished(t *testing.T) {
161 // Moving a published version is not an option: two editors may pin it, and
162 // the proxy caches what it fetched.
163 skipInsideARelease(t)
164 if _, err := exec.LookPath("git"); err != nil {
165 t.Skip("git is not available")
166 }
167
168 root := t.TempDir()
169 remote := filepath.Join(root, "remote.git")
170 clone := filepath.Join(root, "clone")
171
172 run(t, root, "git", "init", "--bare", "--initial-branch=main", remote)
173 run(t, root, "git", "clone", remote, clone)
174 copyModuleInto(t, clone)
175 run(t, clone, "git", "config", "user.email", "test@example.test")
176 run(t, clone, "git", "config", "user.name", "Release Test")
177 writeFile(t, filepath.Join(clone, "release.env"), "TAG=v0.0.1-test\nABOUT=\"a throwaway release\"\n")
178
179 if out, err := runAllowingFailure(t, clone, "./01-release.tag.sh"); err != nil {
180 t.Fatalf("the first release failed:\n%s", out)
181 }
182
183 out, err := runAllowingFailure(t, clone, "./01-release.tag.sh")
184
185 if err == nil {
186 t.Fatalf("the script published the same tag twice:\n%s", out)
187 }
188 if !strings.Contains(out, "already exists") {
189 t.Errorf("the refusal does not say the tag is taken:\n%s", out)
190 }
191}
192
193// copyModuleInto copies the module's source into a directory, so the script can
194// be run against a real checkout without touching this one.
195//
📦 Turbo Core d662ceb k33g 10h ago196// .git is left out because the target has its own, and *.env because a test
197// writes its own release.env — copying this checkout's would release whatever
198// version happens to be in it.
199//
200// The copying is done here rather than by shelling out to cp, which on a
201// network-backed working copy has been seen to write the right number of bytes
202// and the wrong ones: every file in the copy came out NUL-filled, and the
203// tests below then failed on a go build that had nothing to do with them.
🛟 Updated. 28d5985 k33g 17h ago204func copyModuleInto(t *testing.T, target string) {
205 t.Helper()
206
207 entries, err := os.ReadDir(".")
208 if err != nil {
209 t.Fatalf("reading the module: %v", err)
210 }
211 for _, entry := range entries {
212 if entry.Name() == ".git" || strings.HasSuffix(entry.Name(), ".env") {
213 continue
214 }
📦 Turbo Core d662ceb k33g 10h ago215 copyTree(t, entry.Name(), filepath.Join(target, entry.Name()))
216 }
217}
218
219// copyTree copies a file or a directory to a new path.
220//
221// Anything that is neither a regular file nor a directory is skipped: the tool
222// directories beside the source hold symlinks into caches that do not exist in
223// a temporary copy, and the release scripts have no use for them.
224func copyTree(t *testing.T, from, to string) {
225 t.Helper()
226
227 err := filepath.WalkDir(from, func(path string, entry os.DirEntry, err error) error {
228 if err != nil {
229 return err
230 }
231 relative, err := filepath.Rel(from, path)
232 if err != nil {
233 return err
234 }
235 destination := filepath.Join(to, relative)
236
237 if entry.IsDir() {
238 return os.MkdirAll(destination, 0o755)
239 }
240 if !entry.Type().IsRegular() {
241 return nil
242 }
243 info, err := entry.Info()
244 if err != nil {
245 return err
246 }
247 data, err := os.ReadFile(path)
248 if err != nil {
249 return err
250 }
251 if err := os.MkdirAll(filepath.Dir(destination), 0o755); err != nil {
252 return err
253 }
254 // The mode carries the execute bit, without which the scripts these
255 // tests exist to run cannot be run.
256 return os.WriteFile(destination, data, info.Mode().Perm())
257 })
258 if err != nil {
259 t.Fatalf("copying %s: %v", from, err)
🛟 Updated. 28d5985 k33g 17h ago260 }
261}
262
263// run executes a command in a directory, failing the test if it does not
264// succeed.
265func run(t *testing.T, dir string, name string, args ...string) {
266 t.Helper()
267
268 if out, err := runAllowingFailure(t, dir, name, args...); err != nil {
269 t.Fatalf("%s %v: %v\n%s", name, args, err, out)
270 }
271}
272
273// runAllowingFailure executes a command and returns its combined output along
274// with whether it succeeded.
275func runAllowingFailure(t *testing.T, dir string, name string, args ...string) (string, error) {
276 t.Helper()
277
278 command := exec.Command(name, args...)
279 command.Dir = dir
280 out, err := command.CombinedOutput()
281 return string(out), err
282}
283
284// writeFile creates a file, failing the test if it cannot.
285func writeFile(t *testing.T, path, contents string) {
286 t.Helper()
287
288 if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
289 t.Fatalf("writing %s: %v", path, err)
290 }
291}
292
📦 Turbo Core d662ceb k33g 10h ago293// readBuildScript returns the staging script's text.
294func readBuildScript(t *testing.T) string {
🛟 Updated. 28d5985 k33g 17h ago295 t.Helper()
296
📦 Turbo Core d662ceb k33g 10h ago297 data, err := os.ReadFile("02-build-releases.sh")
🛟 Updated. 28d5985 k33g 17h ago298 if err != nil {
📦 Turbo Core d662ceb k33g 10h ago299 t.Fatalf("reading the build script: %v", err)
🛟 Updated. 28d5985 k33g 17h ago300 }
301 return string(data)
302}
303
📦 Turbo Core d662ceb k33g 10h ago304// readWorkflow returns the release workflow's text.
305func readWorkflow(t *testing.T) string {
306 t.Helper()
307
308 data, err := os.ReadFile(filepath.Join(".github", "workflows", "release.yml"))
309 if err != nil {
310 t.Fatalf("reading the release workflow: %v", err)
🛟 Updated. 28d5985 k33g 17h ago311 }
📦 Turbo Core d662ceb k33g 10h ago312 return string(data)
🛟 Updated. 28d5985 k33g 17h ago313}
314
📦 Turbo Core d662ceb k33g 10h ago315func TestTheBuildScriptStopsOnTheFirstFailure(t *testing.T) {
316 // It runs in CI on a tag that is already pushed. A step failing in silence
317 // there publishes a release page for artefacts that were never staged.
318 if got := readBuildScript(t); !strings.Contains(got, "set -euo pipefail") {
🛟 Updated. 28d5985 k33g 17h ago319 t.Error("the script does not stop on failure")
320 }
321}
322
📦 Turbo Core d662ceb k33g 10h ago323func TestTheBuildScriptRefusesAReplaceDirective(t *testing.T) {
324 // 01 checks this too, but CI runs *this* script without ever running 01,
325 // and the proxy serves go.mod as written.
326 if got := readBuildScript(t); !strings.Contains(got, "replace") {
327 t.Error("the script does not check go.mod for a replace directive")
🛟 Updated. 28d5985 k33g 17h ago328 }
329}
330
📦 Turbo Core d662ceb k33g 10h ago331func TestTheBuildScriptRefusesATagThatIsNotAVersion(t *testing.T) {
332 // The proxy will not serve a tag it cannot read as a version, so a typo
333 // here stages perfectly and then fails at every `go get`.
334 skipInsideARelease(t)
🛟 Updated. 28d5985 k33g 17h ago335
📦 Turbo Core d662ceb k33g 10h ago336 dir := t.TempDir()
337 copyModuleInto(t, dir)
338
339 out, err := runAllowingFailure(t, dir, "./02-build-releases.sh", "v0.o.0")
340
341 if err == nil {
342 t.Fatalf("the script staged a release for a tag that is not a version:\n%s", out)
343 }
344 if !strings.Contains(out, "v1.2.3") {
345 t.Errorf("the refusal does not say what a tag should look like:\n%s", out)
🛟 Updated. 28d5985 k33g 17h ago346 }
347}
348
📦 Turbo Core d662ceb k33g 10h ago349func TestTheBuildScriptStagesAnArchiveAndItsChecksum(t *testing.T) {
350 // Reading the script is not the same as running it: the archive is taken
351 // from git, built again once extracted, and checksummed, and each of those
352 // can break on its own.
🛟 Updated. 28d5985 k33g 17h ago353 skipInsideARelease(t)
📦 Turbo Core d662ceb k33g 10h ago354 if _, err := exec.LookPath("git"); err != nil {
355 t.Skip("git is not available")
🛟 Updated. 28d5985 k33g 17h ago356 }
357
358 dir := t.TempDir()
359 copyModuleInto(t, dir)
📦 Turbo Core d662ceb k33g 10h ago360 run(t, dir, "git", "init", "--initial-branch=main")
361 run(t, dir, "git", "config", "user.email", "test@example.test")
362 run(t, dir, "git", "config", "user.name", "Release Test")
363 run(t, dir, "git", "add", ".")
364 run(t, dir, "git", "commit", "-m", "a throwaway commit")
🛟 Updated. 28d5985 k33g 17h ago365
📦 Turbo Core d662ceb k33g 10h ago366 out, err := runAllowingFailure(t, dir, "./02-build-releases.sh", "v0.0.1-test")
🛟 Updated. 28d5985 k33g 17h ago367 if err != nil {
📦 Turbo Core d662ceb k33g 10h ago368 t.Fatalf("staging failed:\n%s", out)
🛟 Updated. 28d5985 k33g 17h ago369 }
📦 Turbo Core d662ceb k33g 10h ago370
371 staged := filepath.Join(dir, "release", "v0.0.1-test")
372 for _, name := range []string{"turbo-core-0.0.1-test.tar.gz", "SHA256SUMS", "README.md"} {
373 if _, err := os.Stat(filepath.Join(staged, name)); err != nil {
374 t.Errorf("%s was not staged: %v", name, err)
375 }
🛟 Updated. 28d5985 k33g 17h ago376 }
📦 Turbo Core d662ceb k33g 10h ago377
378 // A checksum file that does not match what is beside it is worse than
379 // none: it is checked once, by somebody who then trusts it.
380 if _, err := exec.LookPath("sha256sum"); err != nil {
381 t.Skip("sha256sum is not available to verify SHA256SUMS")
🛟 Updated. 28d5985 k33g 17h ago382 }
📦 Turbo Core d662ceb k33g 10h ago383 if out, err := runAllowingFailure(t, staged, "sha256sum", "-c", "SHA256SUMS"); err != nil {
384 t.Errorf("SHA256SUMS does not match the archive:\n%s", out)
🛟 Updated. 28d5985 k33g 17h ago385 }
386}
387
📦 Turbo Core d662ceb k33g 10h ago388func TestTheBuildScriptREADMEInstallsTheModule(t *testing.T) {
389 // The page a person lands on has to say the one line that uses the
390 // library. A release page whose only content is a version number tells
391 // nobody how to depend on it.
392 if got := readBuildScript(t); !strings.Contains(got, "go get ${MODULE}@${TAG}") {
393 t.Error("the staged README never says how to get the module")
394 }
395}
🛟 Updated. 28d5985 k33g 17h ago396
📦 Turbo Core d662ceb k33g 10h ago397func TestTheWorkflowPublishesOnATagPush(t *testing.T) {
398 // The tag push is the trigger: ./01-release.tag.sh ends by pushing one,
399 // and nothing else starts a release.
400 workflow := readWorkflow(t)
🛟 Updated. 28d5985 k33g 17h ago401
📦 Turbo Core d662ceb k33g 10h ago402 for _, want := range []string{"push:", "tags:", `- "v*"`} {
403 if !strings.Contains(workflow, want) {
404 t.Errorf("the workflow never declares %q", want)
405 }
406 }
407 // A workflow with the default read-only token cannot create a release, and
408 // fails at its last step after doing all the work.
409 if !strings.Contains(workflow, "contents: write") {
410 t.Error("the workflow does not ask for contents: write")
411 }
412}
🛟 Updated. 28d5985 k33g 17h ago413
📦 Turbo Core d662ceb k33g 10h ago414func TestTheWorkflowStagesWithTheSameScriptAPersonRuns(t *testing.T) {
415 // A CI job that stages its own way is a second pipeline nobody tests, and
416 // the local one is then only ever exercised by accident.
417 if got := readWorkflow(t); !strings.Contains(got, "./02-build-releases.sh") {
418 t.Error("the workflow does not stage the release with ./02-build-releases.sh")
🛟 Updated. 28d5985 k33g 17h ago419 }
📦 Turbo Core d662ceb k33g 10h ago420}
421
422func TestTheWorkflowAttachesWhatWasStaged(t *testing.T) {
423 // Publishing a release page with no files attached is a silent half-job:
424 // the page exists and the links are not there.
425 workflow := readWorkflow(t)
426
427 for _, want := range []string{"turbo-core-*.tar.gz", "SHA256SUMS", "fail_on_unmatched_files: true"} {
428 if !strings.Contains(workflow, want) {
429 t.Errorf("the workflow never mentions %q", want)
430 }
🛟 Updated. 28d5985 k33g 17h ago431 }
432}
433
📦 Turbo Core d662ceb k33g 10h ago434func TestTheWorkflowLinksToTheDocumentationAtThatTag(t *testing.T) {
435 // A release page is not inside the repository tree, so a relative path
436 // from it 404s — and a link to the branch would rot as the branch moves.
437 workflow := readWorkflow(t)
🛟 Updated. 28d5985 k33g 17h ago438
📦 Turbo Core d662ceb k33g 10h ago439 if !strings.Contains(workflow, "blob/${GITHUB_REF_NAME}") {
440 t.Error("the release notes do not link into the repository at the released tag")
441 }
442 if !strings.Contains(workflow, "/docs/en/README.md") {
443 t.Error("the release notes do not link to the documentation")
🛟 Updated. 28d5985 k33g 17h ago444 }
445}
446
📦 Turbo Core d662ceb k33g 10h ago447func TestTheWorkflowNeedsNoPersonalToken(t *testing.T) {
448 // The release API behind Rickub's /gh shim accepts the job's own
449 // GITHUB_TOKEN and refuses a personal one, so a secret referenced here is
450 // a credential that cannot work and still has to be kept somewhere.
451 if got := readWorkflow(t); strings.Contains(got, "secrets.") {
452 t.Error("the workflow reads a secret; the job's own token is the only credential the release API takes")
453 }
454}
455
456func TestTheWorkflowDoesNotStartAReleaseInsideItself(t *testing.T) {
457 // The suite it runs includes tests that run ./01-release.tag.sh against a
458 // throwaway clone. Locally the script exports this before calling make;
459 // in CI nothing calls the script, so the job has to set it itself.
460 if got := readWorkflow(t); !strings.Contains(got, "TURBO_CORE_RELEASING") {
461 t.Error("the workflow runs the suite without TURBO_CORE_RELEASING set")
🛟 Updated. 28d5985 k33g 17h ago462 }
463}