Skip to content
Myr

golden.test.ts

This file

Path

packages/testkit/test/golden.test.ts

262 lines · 8.6 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–262why, one step back
1import { NodeServices } from "@effect/platform-node";
2import { describe, expect, it } from "@effect/vitest";3import { Brand } from "@repo/brand";4import { Cause, ConfigProvider, Effect, Exit, FileSystem } from "effect";56import { git, gitOk } from "../src/git.ts";7import { expectGolden, GoldenDir, normaliseGitOutput } from "../src/golden.ts";89const id = "0123456789abcdef0123456789abcdef01234567";1011const pushOnce = Effect.gen(function* () {12 const fs = yield* FileSystem.FileSystem;13 const root = yield* fs.makeTempDirectoryScoped().pipe(Effect.orDie);14 const remote = `${root}/remote.git`;15 const work = `${root}/work`;16 const hook = `${remote}/hooks/pre-receive`;1718 yield* gitOk(19 root,20 "init",21 "--quiet",22 "--bare",23 "--initial-branch=main",24 remote25 );26 yield* fs27 .writeFileString(28 hook,29 '#!/bin/sh\nwhile read old new ref; do echo "received $new for $ref"; done\n'30 )31 .pipe(Effect.orDie);32 yield* fs.chmod(hook, 0o755).pipe(Effect.orDie);33 yield* gitOk(root, "init", "--quiet", "--initial-branch=main", work);34 yield* fs.writeFileString(`${work}/a.txt`, "a\n").pipe(Effect.orDie);35 yield* gitOk(work, "add", "--all");36 yield* gitOk(work, "commit", "--quiet", "--message", "first");3738 return yield* git(work, "push", remote, "HEAD:refs/heads/main");39}).pipe(Effect.scoped);4041const complaint = (exit: Exit.Exit<void>) =>42 Exit.isFailure(exit) ? String(Cause.squash(exit.cause)) : "";4344const inTempGoldens = <A, E, R>(45 update: string,46 run: (dir: string) => Effect.Effect<A, E, R>47) =>48 Effect.gen(function* () {49 const fs = yield* FileSystem.FileSystem;50 const dir = yield* fs.makeTempDirectoryScoped().pipe(Effect.orDie);5152 return yield* run(`${dir}/golden`).pipe(53 Effect.provideService(GoldenDir, `${dir}/golden`),54 Effect.provide(55 ConfigProvider.layer(56 ConfigProvider.fromEnv({ env: { UPDATE_GOLDEN: update } })57 )58 )59 );60 }).pipe(Effect.scoped);6162describe("git output, normalised", () => {63 it("drops the eight spaces git pads side-band lines with, and any other trailing space", () => {64 expect(65 normaliseGitOutput(66 "remote: StaleBase: main moved \nremote: next: git pull --rebase \nTo here \t\n"67 )68 ).toBe(69 "remote: StaleBase: main moved\nremote: next: git pull --rebase\nTo here\n"70 );71 });7273 it("names temporary folders <tmp>, on Linux and on macOS", () => {74 expect(75 normaliseGitOutput(76 [77 "To /tmp/Ab3dE9/remote.git",78 "To /var/folders/x1/y2z3w4/T/Qq81Zk/remote.git",79 "To /private/var/folders/x1/y2z3w4/T/Qq81Zk/remote.git",80 "fatal: '/private/tmp/kk2/work' is not a git repository",81 ].join("\n")82 )83 ).toBe(84 [85 "To <tmp>/remote.git",86 "To <tmp>/remote.git",87 "To <tmp>/remote.git",88 "fatal: '<tmp>/work' is not a git repository",89 ].join("\n")90 );91 });9293 it("names full object ids <sha>, and leaves short ids and longer hex alone", () => {94 expect(95 normaliseGitOutput(96 `remote: received ${id} for refs/heads/main\n 0123456..89abcde main -> main\nsha256 ${id}0123`97 )98 ).toBe(99 `remote: received <sha> for refs/heads/main\n 0123456..89abcde main -> main\nsha256 ${id}0123`100 );101 });102103 it("names a local server's address <host>, since its port changes every run", () => {104 expect(105 normaliseGitOutput(106 "To http://127.0.0.1:43567/datekit--fix--bob.git\nTo http://localhost:8787/x.git"107 )108 ).toBe("To http://<host>/datekit--fix--bob.git\nTo http://<host>/x.git");109 });110111 it("keeps what a terminal would show of a line rewritten with carriage returns", () => {112 expect(113 normaliseGitOutput(114 "Writing objects: 50% (1/2)\rWriting objects: 100% (2/2), done.\r\nTo x\r\n"115 )116 ).toBe("Writing objects: 100% (2/2), done.\nTo x\n");117 });118119 it("keeps a progress line that ends in a carriage return, as a terminal still shows it", () => {120 expect(121 normaliseGitOutput(122 "Counting objects: 1\rCounting objects: 2\r\r\nTo x\nWriting objects: 50% (1/2)\r"123 )124 ).toBe("Counting objects: 2\nTo x\nWriting objects: 50% (1/2)");125 });126127 it.live(128 "makes two runs of the same push compare equal, and match their golden file",129 () =>130 Effect.gen(function* () {131 const first = yield* pushOnce;132 const second = yield* pushOnce;133134 expect([first.exitCode, second.exitCode]).toStrictEqual([0, 0]);135 expect(first.stderr).toContain(" \n");136 expect(first.stderr).not.toBe(second.stderr);137 expect(normaliseGitOutput(first.stderr)).toBe(138 normaliseGitOutput(second.stderr)139 );140141 yield* expectGolden(142 "push-new-branch",143 normaliseGitOutput(first.stderr)144 );145 }).pipe(Effect.provide(NodeServices.layer)),146 20_000147 );148});149150describe("golden files", () => {151 it.effect("pass when the text matches the file", () =>152 Effect.gen(function* () {153 const fs = yield* FileSystem.FileSystem;154155 yield* inTempGoldens("0", (dir) =>156 Effect.gen(function* () {157 yield* fs.makeDirectory(dir, { recursive: true }).pipe(Effect.orDie);158 yield* fs159 .writeFileString(`${dir}/same.txt`, "To <tmp>/remote.git\n")160 .pipe(Effect.orDie);161162 const exit = yield* Effect.exit(163 expectGolden("same", "To <tmp>/remote.git\n")164 );165166 expect(complaint(exit)).toBe("");167 })168 );169 }).pipe(Effect.provide(NodeServices.layer))170 );171172 it.effect(173 "fail, naming the file and how to accept the change, when the text differs",174 () =>175 Effect.gen(function* () {176 const fs = yield* FileSystem.FileSystem;177178 yield* inTempGoldens("0", (dir) =>179 Effect.gen(function* () {180 yield* fs181 .makeDirectory(dir, { recursive: true })182 .pipe(Effect.orDie);183 yield* fs184 .writeFileString(`${dir}/changed.txt`, "line one\nline two\n")185 .pipe(Effect.orDie);186187 const exit = yield* Effect.exit(188 expectGolden("changed", "line one\nline 2\n")189 );190191 expect(complaint(exit)).toContain(`${dir}/changed.txt`);192 expect(complaint(exit)).toContain("UPDATE_GOLDEN=1");193 expect(complaint(exit)).toContain("- line two");194 expect(complaint(exit)).toContain("+ line 2");195 })196 );197 }).pipe(Effect.provide(NodeServices.layer))198 );199200 it.effect("fail when there is no file yet", () =>201 inTempGoldens("0", () =>202 Effect.gen(function* () {203 const exit = yield* Effect.exit(expectGolden("absent", "text\n"));204205 expect(complaint(exit)).toContain("absent.txt");206 expect(complaint(exit)).toContain("UPDATE_GOLDEN=1");207 })208 ).pipe(Effect.provide(NodeServices.layer))209 );210211 it.effect("are written, folder and all, under UPDATE_GOLDEN=1", () =>212 Effect.gen(function* () {213 const fs = yield* FileSystem.FileSystem;214215 yield* inTempGoldens("1", (dir) =>216 Effect.gen(function* () {217 yield* fs.makeDirectory(dir, { recursive: true }).pipe(Effect.orDie);218 yield* fs219 .writeFileString(`${dir}/rewritten.txt`, "old\n")220 .pipe(Effect.orDie);221222 yield* expectGolden("rewritten", "new\n");223 yield* expectGolden("nested/fresh", "fresh\n");224225 expect(yield* fs.exists(`${dir}/nested/fresh.txt`)).toBeTruthy();226 expect(227 yield* fs.readFileString(`${dir}/rewritten.txt`).pipe(Effect.orDie)228 ).toBe("new\n");229 expect(230 yield* fs231 .readFileString(`${dir}/nested/fresh.txt`)232 .pipe(Effect.orDie)233 ).toBe("fresh\n");234 })235 );236 }).pipe(Effect.provide(NodeServices.layer))237 );238239 it.effect(240 "never spell the product's name: its config folder and name are written as placeholders",241 () =>242 Effect.gen(function* () {243 const fs = yield* FileSystem.FileSystem;244245 yield* inTempGoldens("1", (dir) =>246 Effect.gen(function* () {247 yield* expectGolden(248 "brand",249 `wrote ${Brand.configDir}/project.ts; next: ${Brand.cli} status, then ${Brand.name} lands it\n`250 );251252 expect(yield* fs.exists(`${dir}/brand.txt`)).toBeTruthy();253 expect(254 yield* fs.readFileString(`${dir}/brand.txt`).pipe(Effect.orDie)255 ).toBe(256 "wrote <configDir>/project.ts; next: <brand> status, then <brand> lands it\n"257 );258 })259 );260 }).pipe(Effect.provide(NodeServices.layer))261 );262});