turbo-editors/turbo-corepublic Fork 0
v1.0.2
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.

📦 Turbo Core d662ceb · on v1.0.2 · k33g · 10h ago
release_test.go · 463 lines · 15.8 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// 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")
	}
}