mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-06 03:59:09 +08:00
test: specify release hardening contracts
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
"@effect/vitest": "workspace:^",
|
||||
"@types/node": "^26.6.2",
|
||||
"typescript": "^6.0.3",
|
||||
"vitest": "5.0.1"
|
||||
"vitest": "5.0.1",
|
||||
"yaml": "^2.9.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,14 @@ const appLayer = (options: {
|
||||
|
||||
const run = (args: ReadonlyArray<string>) => Command.runWith(cli, { version: "0.0.0", renderErrors: false })(args)
|
||||
|
||||
const mutationNames = (calls: ReturnType<typeof makeCalls>) =>
|
||||
callNames(calls).filter((name) =>
|
||||
name.startsWith("git.") ||
|
||||
name.startsWith("github.") ||
|
||||
name === "pnpm.applyVersions" ||
|
||||
name === "pnpm.stagePublish"
|
||||
)
|
||||
|
||||
describe("release run", () => {
|
||||
it.effect("opens the version PR and stages nothing while intents are pending", () =>
|
||||
Effect.gen(function*() {
|
||||
@@ -149,4 +157,82 @@ describe("release run", () => {
|
||||
const error = yield* Effect.flip(run(["run"]).pipe(Effect.provide(layer)))
|
||||
assert.notStrictEqual((error as { _tag: string })._tag, "ReleaseError")
|
||||
}))
|
||||
|
||||
it.effect("rejects expected Version when routing chooses Stage without mutating anything", () =>
|
||||
Effect.gen(function*() {
|
||||
const { calls, layer } = appLayer({
|
||||
plan: emptyPlan,
|
||||
published: new Set(["@effect/vitest@4.0.0-rc.117"]),
|
||||
staged: []
|
||||
})
|
||||
const error = yield* Effect.flip(
|
||||
run(["run", "--tag", "rc", "--expect", "Version"]).pipe(Effect.provide(layer))
|
||||
)
|
||||
|
||||
assert.strictEqual((error as { _tag: string })._tag, "ReleaseError")
|
||||
assert.include((error as Error).message, "Version")
|
||||
assert.include((error as Error).message, "Stage")
|
||||
assert.deepStrictEqual(mutationNames(calls), [])
|
||||
}))
|
||||
|
||||
it.effect("rejects expected Stage when routing chooses Version without mutating anything", () =>
|
||||
Effect.gen(function*() {
|
||||
const { calls, layer } = appLayer({ plan: rcPlan, published: new Set() })
|
||||
const error = yield* Effect.flip(
|
||||
run(["run", "--tag", "rc", "--expect", "Stage"]).pipe(Effect.provide(layer))
|
||||
)
|
||||
|
||||
assert.strictEqual((error as { _tag: string })._tag, "ReleaseError")
|
||||
assert.include((error as Error).message, "Stage")
|
||||
assert.include((error as Error).message, "Version")
|
||||
assert.deepStrictEqual(mutationNames(calls), [])
|
||||
}))
|
||||
|
||||
it.effect("rejects expected Stage when routing chooses Idle without mutating anything", () =>
|
||||
Effect.gen(function*() {
|
||||
const { calls, layer } = appLayer({
|
||||
plan: emptyPlan,
|
||||
published: new Set(["effect@4.0.0-rc.117", "@effect/vitest@4.0.0-rc.117"]),
|
||||
staged: []
|
||||
})
|
||||
const error = yield* Effect.flip(
|
||||
run(["run", "--tag", "rc", "--expect", "Stage"]).pipe(Effect.provide(layer))
|
||||
)
|
||||
|
||||
assert.strictEqual((error as { _tag: string })._tag, "ReleaseError")
|
||||
assert.include((error as Error).message, "Stage")
|
||||
assert.include((error as Error).message, "Idle")
|
||||
assert.deepStrictEqual(mutationNames(calls), [])
|
||||
}))
|
||||
|
||||
it.effect("runs when the expected Version route matches", () =>
|
||||
Effect.gen(function*() {
|
||||
const { calls, layer } = appLayer({ plan: rcPlan, published: new Set() })
|
||||
yield* run(["run", "--tag", "rc", "--expect", "Version"]).pipe(Effect.provide(layer))
|
||||
|
||||
assert.strictEqual(callsTo(calls, "github.createPullRequest").length, 1)
|
||||
assert.deepStrictEqual(callsTo(calls, "pnpm.stagePublish"), [])
|
||||
}))
|
||||
|
||||
it.effect("runs when the expected Stage route matches", () =>
|
||||
Effect.gen(function*() {
|
||||
const { calls, layer } = appLayer({ plan: emptyPlan, published: new Set(), staged: [] })
|
||||
yield* run(["run", "--tag", "rc", "--expect", "Stage"]).pipe(Effect.provide(layer))
|
||||
|
||||
assert.strictEqual(callsTo(calls, "pnpm.stagePublish").length, 1)
|
||||
assert.deepStrictEqual(callsTo(calls, "github.createPullRequest"), [])
|
||||
}))
|
||||
|
||||
it.effect("prints the version PR title on a dry run without touching git or GitHub", () =>
|
||||
Effect.gen(function*() {
|
||||
const { calls, layer } = appLayer({ plan: rcPlan, published: new Set() })
|
||||
yield* run(["run", "--tag", "rc", "--dry-run"]).pipe(Effect.provide(layer))
|
||||
|
||||
assert.include((yield* TestConsole.logLines).join("\n"), "Version Packages (rc)")
|
||||
assert.deepStrictEqual(
|
||||
callNames(calls).filter((name) => name.startsWith("git.") || name.startsWith("github.")),
|
||||
[]
|
||||
)
|
||||
assert.deepStrictEqual(callsTo(calls, "pnpm.applyVersions"), [])
|
||||
}))
|
||||
})
|
||||
|
||||
@@ -119,8 +119,33 @@ describe("Pnpm layer", () => {
|
||||
}).pipe(Effect.provide(Pnpm.layer.pipe(
|
||||
Layer.provide(commandDependencies([{
|
||||
stdout: JSON.stringify({
|
||||
effect: { version: "4.0.0-rc.118", stageId: "stage-effect" },
|
||||
vitest: { name: "@effect/vitest", version: "4.0.0-rc.118" }
|
||||
effect: {
|
||||
id: "effect@4.0.0-rc.118",
|
||||
name: "effect",
|
||||
version: "4.0.0-rc.118",
|
||||
size: 2438912,
|
||||
unpackedSize: 11319222,
|
||||
shasum: "339deaedfc5fe1431a76352f0c493ec890ceec7d",
|
||||
integrity: "sha512-real-stage-output",
|
||||
filename: "effect-4.0.0-rc.118.tgz",
|
||||
files: [{ path: "package.json", size: 4321, mode: 420 }],
|
||||
entryCount: 1570,
|
||||
bundled: [],
|
||||
stageId: "stage-effect"
|
||||
},
|
||||
vitest: {
|
||||
id: "@effect/vitest@4.0.0-rc.118",
|
||||
name: "@effect/vitest",
|
||||
version: "4.0.0-rc.118",
|
||||
size: 12345,
|
||||
unpackedSize: 45678,
|
||||
shasum: "6f1ed002ab5595859014ebf0951522d9f7ee292e",
|
||||
integrity: "sha512-real-stage-output",
|
||||
filename: "effect-vitest-4.0.0-rc.118.tgz",
|
||||
files: [{ path: "package.json", size: 987, mode: 420 }],
|
||||
entryCount: 42,
|
||||
bundled: []
|
||||
}
|
||||
})
|
||||
}], commands))
|
||||
)))
|
||||
@@ -145,6 +170,16 @@ describe("Pnpm layer", () => {
|
||||
{ stdout: ReleasePlan.NO_PENDING_CHANGES + "\n" }
|
||||
], commands)))))
|
||||
})
|
||||
|
||||
it.effect("maps a warning-prefixed no-pending response to no applied versions", () => {
|
||||
const commands: Array<ChildProcess.StandardCommand> = []
|
||||
return Effect.gen(function*() {
|
||||
const pnpm = yield* Pnpm
|
||||
assert.deepStrictEqual(yield* pnpm.applyVersions, [])
|
||||
}).pipe(Effect.provide(Pnpm.layer.pipe(Layer.provide(commandDependencies([{
|
||||
stdout: `WARN The current working tree has uncommitted changes\n${ReleasePlan.NO_PENDING_CHANGES}\n`
|
||||
}], commands)))))
|
||||
})
|
||||
})
|
||||
|
||||
describe("Git layer", () => {
|
||||
@@ -352,6 +387,30 @@ describe("Registry layer", () => {
|
||||
Effect.provideService(Console.Console, testConsole)
|
||||
)
|
||||
})
|
||||
|
||||
it.effect("fails closed when the stage queue stops before its reported total", () => {
|
||||
const client = HttpClient.make((request) =>
|
||||
Effect.succeed(HttpClientResponse.fromWeb(
|
||||
request,
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
items: [{ id: "stage-0", packageName: "effect", version: "4.0.0-rc.118" }],
|
||||
total: 2
|
||||
}),
|
||||
{ status: 200, headers: { "content-type": "application/json" } }
|
||||
)
|
||||
))
|
||||
)
|
||||
return Effect.gen(function*() {
|
||||
const registry = yield* Registry
|
||||
const error = yield* Effect.flip(registry.listStaged)
|
||||
assert.strictEqual(error._tag, "ReleaseError")
|
||||
assert.include(error.message, "2")
|
||||
}).pipe(
|
||||
Effect.provide(Registry.layer.pipe(Layer.provide(Layer.succeed(HttpClient.HttpClient, client)))),
|
||||
Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { NPM_STAGE_TOKEN: "stage-token" } })))
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Workspace layer", () => {
|
||||
@@ -361,7 +420,6 @@ describe("Workspace layer", () => {
|
||||
const output = JSON.stringify([
|
||||
{ path: root, private: true },
|
||||
{ name: "effect", version: "4.0.0-rc.118", path: `${root}/packages/effect` },
|
||||
{ name: "missing-version", path: `${root}/packages/missing` },
|
||||
{ name: "@effect/release", version: "0.0.0", path: `${root}/packages/tools/release`, private: true }
|
||||
])
|
||||
return Effect.gen(function*() {
|
||||
@@ -373,6 +431,21 @@ describe("Workspace layer", () => {
|
||||
assert.deepStrictEqual(commands[0].args, ["-r", "ls", "--depth", "-1", "--json"])
|
||||
}).pipe(Effect.provide(Workspace.layer.pipe(Layer.provide(commandDependencies([{ stdout: output }], commands)))))
|
||||
})
|
||||
|
||||
it.effect("rejects a named workspace package without a version", () => {
|
||||
const commands: Array<ChildProcess.StandardCommand> = []
|
||||
const root = globalThis.process.cwd()
|
||||
const output = JSON.stringify([
|
||||
{ path: root, private: true },
|
||||
{ name: "missing-version", path: `${root}/packages/missing` }
|
||||
])
|
||||
return Effect.gen(function*() {
|
||||
const workspace = yield* Workspace
|
||||
const error = yield* Effect.flip(workspace.packages)
|
||||
assert.strictEqual(error._tag, "ReleaseError")
|
||||
assert.include(error.message, "missing-version")
|
||||
}).pipe(Effect.provide(Workspace.layer.pipe(Layer.provide(commandDependencies([{ stdout: output }], commands)))))
|
||||
})
|
||||
})
|
||||
|
||||
describe("Process.findWorkspaceRoot", () => {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import { assert, describe, it } from "@effect/vitest"
|
||||
import { readFile } from "node:fs/promises"
|
||||
import { parse } from "yaml"
|
||||
|
||||
interface Step {
|
||||
readonly name?: string
|
||||
readonly uses?: string
|
||||
readonly if?: string
|
||||
readonly run?: string
|
||||
readonly env?: Record<string, unknown>
|
||||
readonly with?: Record<string, unknown>
|
||||
}
|
||||
|
||||
interface Job {
|
||||
readonly if?: string
|
||||
readonly env?: Record<string, unknown>
|
||||
readonly permissions?: Record<string, string>
|
||||
readonly steps?: ReadonlyArray<Step>
|
||||
}
|
||||
|
||||
interface Workflow {
|
||||
readonly env?: Record<string, unknown>
|
||||
readonly permissions?: Record<string, string>
|
||||
readonly jobs: Record<string, Job>
|
||||
}
|
||||
|
||||
const workflowPath = new URL("../../../../.github/workflows/release.yml", import.meta.url)
|
||||
|
||||
const secretReferences = (value: unknown, path: ReadonlyArray<string> = []): Array<readonly [string, string]> => {
|
||||
if (typeof value === "string") {
|
||||
return value.includes("secrets.") ? [[path.join("."), value]] : []
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.flatMap((item, index) => secretReferences(item, [...path, String(index)]))
|
||||
}
|
||||
if (value !== null && typeof value === "object") {
|
||||
return Object.entries(value).flatMap(([key, item]) => secretReferences(item, [...path, key]))
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
describe("release workflow", () => {
|
||||
it("isolates credentials and permissions by route", async () => {
|
||||
const workflow = parse(await readFile(workflowPath, "utf8")) as Workflow
|
||||
const route = workflow.jobs.route
|
||||
const version = workflow.jobs.version
|
||||
const stage = workflow.jobs.stage
|
||||
|
||||
assert.deepStrictEqual(Object.keys(workflow.jobs).sort(), ["route", "stage", "version"])
|
||||
assert.exists(route, "route job")
|
||||
assert.exists(version, "version job")
|
||||
assert.exists(stage, "stage job")
|
||||
assert.deepStrictEqual(workflow.permissions, {})
|
||||
|
||||
for (const [name, job] of Object.entries(workflow.jobs)) {
|
||||
assert.deepStrictEqual(secretReferences(job.env, ["jobs", name, "env"]), [])
|
||||
const install = job.steps?.find((step) => step.uses === "./.github/actions/setup")
|
||||
assert.exists(install, `${name} install step`)
|
||||
assert.isUndefined(install.env, `${name} install step must not inherit step secrets`)
|
||||
|
||||
for (const checkout of job.steps?.filter((step) => step.uses?.startsWith("actions/checkout@")) ?? []) {
|
||||
assert.strictEqual(checkout.with?.["persist-credentials"], false, `${name} checkout persists credentials`)
|
||||
assert.notProperty(checkout.with ?? {}, "token", `${name} checkout receives a token`)
|
||||
}
|
||||
}
|
||||
assert.deepStrictEqual(secretReferences(workflow.env, ["env"]), [])
|
||||
|
||||
assert.deepStrictEqual(route.permissions, { contents: "read" })
|
||||
assert.deepStrictEqual(version.permissions, { contents: "write", "pull-requests": "write" })
|
||||
assert.deepStrictEqual(stage.permissions, { contents: "read", "id-token": "write" })
|
||||
|
||||
const allSecrets = secretReferences(workflow)
|
||||
const patReferences = allSecrets.filter(([, value]) => value.includes("CHANGESET_GITHUB_TOKEN"))
|
||||
assert.lengthOf(patReferences, 1)
|
||||
assert.match(patReferences[0][0], /^jobs\.version\.steps\.\d+\.env\.GH_TOKEN$/)
|
||||
const stageTokenReferences = allSecrets.filter(([, value]) => value.includes("NPM_STAGE_TOKEN"))
|
||||
assert.lengthOf(stageTokenReferences, 1)
|
||||
assert.match(stageTokenReferences[0][0], /^jobs\.stage\.steps\.\d+\.env\.NPM_STAGE_TOKEN$/)
|
||||
assert.lengthOf(allSecrets, 2, "only the route-specific run steps may receive secrets")
|
||||
|
||||
const versionStep = version.steps?.find((step) => step.env?.GH_TOKEN !== undefined)
|
||||
assert.exists(versionStep)
|
||||
assert.include(versionStep.run, "gh auth setup-git")
|
||||
assert.include(versionStep.run, "release run --tag rc --expect Version")
|
||||
assert.notProperty(version.permissions ?? {}, "id-token")
|
||||
|
||||
const stageStep = stage.steps?.find((step) => step.run?.includes("release run"))
|
||||
assert.exists(stageStep)
|
||||
assert.include(stageStep.run, "release run --tag rc --expect Stage")
|
||||
assert.deepStrictEqual(
|
||||
secretReferences(stage, ["jobs", "stage"]).filter(([, value]) => value.includes("CHANGESET_GITHUB_TOKEN")),
|
||||
[]
|
||||
)
|
||||
})
|
||||
|
||||
it("keeps build preparation on the Stage route only", async () => {
|
||||
const workflow = parse(await readFile(workflowPath, "utf8")) as Workflow
|
||||
const stage = workflow.jobs.stage
|
||||
assert.exists(stage)
|
||||
assert.include(stage.if, "needs.route.outputs.tag == 'Stage'")
|
||||
|
||||
const buildCommands = ["scripts/set-strip-internal.mjs", "pnpm codemod", "pnpm build"]
|
||||
for (const command of buildCommands) {
|
||||
const owners = Object.entries(workflow.jobs).filter(([, job]) =>
|
||||
job.steps?.some((step) => step.run?.includes(command))
|
||||
).map(([name]) => name)
|
||||
assert.deepStrictEqual(owners, ["stage"], `${command} must only run in the stage job`)
|
||||
}
|
||||
})
|
||||
})
|
||||
Generated
+3
@@ -933,6 +933,9 @@ importers:
|
||||
vitest:
|
||||
specifier: 5.0.1
|
||||
version: 5.0.1(@edge-runtime/vm@5.0.0)(@opentelemetry/api@1.9.1)(@types/node@26.6.2)(@vitest/coverage-v8@5.0.1)(happy-dom@20.14.5)(jsdom@30.0.1)(vite@8.3.0(@types/node@26.6.2)(esbuild@0.28.2)(terser@5.51.2)(tsx@4.23.13)(yaml@2.9.1))
|
||||
yaml:
|
||||
specifier: ^2.9.1
|
||||
version: 2.9.1
|
||||
|
||||
packages/tools/utils:
|
||||
dependencies:
|
||||
|
||||
Reference in New Issue
Block a user