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

🛟 Updated. 28d5985 · on main · k33g · 2h ago
release_test.go · 378 lines · 12.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
// 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 those hold
// the release token — a test has no use for it, and copying a secret into a
// temporary directory is how it ends up somewhere nobody looks.
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
		}
		run(t, ".", "cp", "-r", entry.Name(), target)
	}
}

// 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)
	}
}

// readPublishScript returns the release-page script's text.
func readPublishScript(t *testing.T) string {
	t.Helper()

	data, err := os.ReadFile("02-release.publish.sh")
	if err != nil {
		t.Fatalf("reading the publish script: %v", err)
	}
	return string(data)
}

func TestThePublishScriptBuildsItsJSONWithJq(t *testing.T) {
	// Hand-written JSON in a heredoc breaks silently the day ABOUT contains a
	// quote, a newline or a backtick — and the release notes are exactly where
	// somebody puts a backtick.
	script := readPublishScript(t)

	if !strings.Contains(script, "jq -n") {
		t.Error("the script builds its payload by hand rather than with jq")
	}
	// Comment lines are skipped: the script's own comment explains why it
	// avoids that idiom, and a grep over the whole file matches the
	// explanation. This test failed on its own prose before it was narrowed.
	for _, line := range codeLines(script) {
		if strings.Contains(line, `read -r -d ''`) {
			t.Errorf("the script uses the read -r -d '' idiom, which always exits non-zero: %s", line)
		}
	}
}

func TestThePublishScriptStopsOnTheFirstFailure(t *testing.T) {
	// Safe here precisely because it does not use that idiom.
	if got := readPublishScript(t); !strings.Contains(got, "set -euo pipefail") {
		t.Error("the script does not stop on failure")
	}
}

func TestThePublishScriptTellsUnreachableFromAbsent(t *testing.T) {
	// git ls-remote exits 2 when a ref is absent and 128 when it cannot reach
	// the remote. Conflating them refuses a good release from any machine with
	// no key loaded — which is how this was found.
	script := readPublishScript(t)

	if !strings.Contains(script, "lookup=$?") {
		t.Error("the script does not look at ls-remote's exit code")
	}
	for _, want := range []string{"is not on origin", "cannot reach origin"} {
		if !strings.Contains(script, want) {
			t.Errorf("the script never says %q", want)
		}
	}
}

func TestThePublishScriptTellsTheHTTPStatusesApart(t *testing.T) {
	// "❌ something went wrong" plus the API's raw JSON leaves the reader to
	// guess which of four quite different problems this is.
	script := readPublishScript(t)

	for _, status := range []string{"201", "409", "401", "404"} {
		if !strings.Contains(script, status) {
			t.Errorf("the script does not handle HTTP %s", status)
		}
	}
}

func TestThePublishScriptDryRunSendsNothing(t *testing.T) {
	// The only way to exercise this script here: the real call publishes on
	// somebody's behalf, which a test may not do.
	skipInsideARelease(t)
	if _, err := exec.LookPath("jq"); err != nil {
		t.Skip("jq is not installed")
	}

	dir := t.TempDir()
	copyModuleInto(t, dir)
	writeFile(t, filepath.Join(dir, "release.env"),
		"TAG=\"v0.0.1-test\"\nABOUT=\"notes with a \\\" quote\"\nOWNER=\"turbo-editors\"\nREPO=\"turbo-core\"\n")
	writeFile(t, filepath.Join(dir, "turbo-core.token.env"), "TOKEN=not-a-real-token\n")

	out, err := runAllowingFailure(t, dir, "./02-release.publish.sh", "--dry-run")

	if err != nil {
		t.Fatalf("the dry run failed:\n%s", out)
	}
	if !strings.Contains(out, "nothing was sent") {
		t.Errorf("the dry run does not say it sent nothing:\n%s", out)
	}
	if !strings.Contains(out, "POST https://codeberg.org/api/v1/repos/turbo-editors/turbo-core/releases") {
		t.Errorf("the dry run does not show the request it would make:\n%s", out)
	}
	// The quote in ABOUT must have survived as data rather than breaking the
	// JSON, which is the whole reason jq is in there.
	if !strings.Contains(out, `notes with a \" quote`) {
		t.Errorf("the quote in the notes was not escaped:\n%s", out)
	}
}

func TestThePublishScriptRefusesWithoutAToken(t *testing.T) {
	skipInsideARelease(t)

	dir := t.TempDir()
	copyModuleInto(t, dir)
	writeFile(t, filepath.Join(dir, "release.env"),
		"TAG=\"v0.0.1-test\"\nABOUT=\"notes\"\nOWNER=\"turbo-editors\"\nREPO=\"turbo-core\"\n")

	out, err := runAllowingFailure(t, dir, "./02-release.publish.sh", "--dry-run")

	if err == nil {
		t.Fatalf("the script ran without a token:\n%s", out)
	}
	if !strings.Contains(out, "TOKEN is not set") {
		t.Errorf("the refusal does not say the token is missing:\n%s", out)
	}
}

func TestThePublishScriptLinksToTheDocumentationAtThatTag(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.
	script := readPublishScript(t)

	if !strings.Contains(script, "/src/tag/${TAG}/docs/") {
		t.Error("the release notes do not link to the documentation at the released tag")
	}
}

// codeLines returns the lines of a shell script that are not comments.
func codeLines(script string) []string {
	var out []string
	for _, line := range strings.Split(script, "\n") {
		if trimmed := strings.TrimSpace(line); trimmed != "" && !strings.HasPrefix(trimmed, "#") {
			out = append(out, line)
		}
	}
	return out
}