-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplayground-phpunit-bootstrap-failure.integration.test.ts
More file actions
120 lines (108 loc) · 5.93 KB
/
Copy pathplayground-phpunit-bootstrap-failure.integration.test.ts
File metadata and controls
120 lines (108 loc) · 5.93 KB
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
import assert from "node:assert/strict"
import { execFile } from "node:child_process"
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { promisify } from "node:util"
const execFileAsync = promisify(execFile)
const secret = "sk-abcdefghijklmnopqrstuvwxyz"
const root = await mkdtemp(join(tmpdir(), "wp-codebox-phpunit-bootstrap-failure-"))
const fatalMuPlugin = join(root, "fatal-bootstrap.php")
const recipePath = join(root, "recipe.json")
const artifactsPath = join(root, "artifacts")
const exitArtifactsPath = join(root, "exit-artifacts")
const parseArtifactsPath = join(root, "parse-artifacts")
try {
await writeFile(fatalMuPlugin, `<?php\ntrigger_error('PHPUnit bootstrap fixture token: ${secret}', E_USER_ERROR);\n`)
await writeFile(recipePath, `${JSON.stringify({
schema: "wp-codebox/workspace-recipe/v1",
runtime: { backend: "wordpress-playground", wp: "6.5", blueprint: { steps: [] } },
inputs: {
mounts: [{
source: fatalMuPlugin,
target: "/wordpress/wp-content/mu-plugins/wp-codebox-bootstrap-failure.php",
mode: "readonly",
}],
},
workflow: {
steps: [{ command: "wordpress.phpunit", args: ["plugin-slug=bootstrap-failure-fixture"] }],
},
})}\n`)
const output = await runFailedRecipe()
assert.equal(output.success, false, JSON.stringify(output))
const message = output.error?.message ?? ""
assert.match(message, /wordpress\.phpunit crashed before producing a structured response/)
assert.match(message, /failureClassification=runtime-worker-failure/)
assert.match(message, /wordpress\.phpunit structured diagnostics/)
assert.match(message, /PHPUnit bootstrap fixture token: \[redacted\]/)
assert.doesNotMatch(message, new RegExp(secret))
const latest = JSON.parse(await readFile(join(artifactsPath, "latest-runtime.json"), "utf8")) as { paths?: { runtimeDirectory?: string } }
const runtimeDirectory = latest.paths?.runtimeDirectory
assert.ok(runtimeDirectory, "failed recipe must retain a runtime artifact directory")
const artifactDirectory = join(artifactsPath, runtimeDirectory)
const diagnostic = await readFile(join(artifactDirectory, "files", "phpunit", ".pg-test-result.txt"), "utf8")
const commandLog = await readFile(join(artifactDirectory, "logs", "commands.log"), "utf8")
assert.match(diagnostic, /STAGE_FATAL:bootstrap:PHPUnit bootstrap fixture token: \[redacted\]/)
assert.doesNotMatch(diagnostic, new RegExp(secret))
assert.match(commandLog, /wordpress\.phpunit structured diagnostics/)
assert.match(commandLog, /PHPUnit bootstrap fixture token: \[redacted\]/)
assert.doesNotMatch(commandLog, new RegExp(secret))
await writeFile(fatalMuPlugin, `<?php\necho 'PHPUnit bootstrap exit fixture token: ${secret}';\nexit(1);\n`)
const exitOutput = await runFailedRecipe(exitArtifactsPath)
assert.equal(exitOutput.success, false, JSON.stringify(exitOutput))
const exitMessage = exitOutput.error?.message ?? ""
assert.match(exitMessage, /wordpress\.phpunit structured diagnostics/)
assert.match(exitMessage, /PHPUnit bootstrap exit fixture token: \[redacted\]/)
assert.doesNotMatch(exitMessage, new RegExp(secret))
const exitLatest = JSON.parse(await readFile(join(exitArtifactsPath, "latest-runtime.json"), "utf8")) as { paths?: { runtimeDirectory?: string } }
const exitRuntimeDirectory = exitLatest.paths?.runtimeDirectory
assert.ok(exitRuntimeDirectory, "terminated recipe must retain a runtime artifact directory")
const exitDiagnostic = await readFile(join(exitArtifactsPath, exitRuntimeDirectory, "files", "phpunit", ".pg-test-result.txt"), "utf8")
assert.match(exitDiagnostic, /STAGE_DIE:bootstrap:PHPUnit bootstrap exit fixture token: \[redacted\]/)
assert.doesNotMatch(exitDiagnostic, new RegExp(secret))
await writeFile(recipePath, `${JSON.stringify({
schema: "wp-codebox/workspace-recipe/v1",
runtime: { backend: "wordpress-playground", wp: "6.5", blueprint: { steps: [] } },
inputs: { mounts: [] },
workflow: {
steps: [{ command: "wordpress.phpunit", args: ["plugin-slug=bootstrap-failure-fixture", "code=<?php function malformed("] }],
},
})}\n`)
const parseOutput = await runFailedRecipe(parseArtifactsPath)
assert.equal(parseOutput.success, false, JSON.stringify(parseOutput))
const parseMessage = parseOutput.error?.message ?? ""
assert.match(parseMessage, /wordpress\.phpunit structured diagnostics/)
assert.match(parseMessage, /ParseError/)
const parseLatest = JSON.parse(await readFile(join(parseArtifactsPath, "latest-runtime.json"), "utf8")) as { paths?: { runtimeDirectory?: string } }
const parseRuntimeDirectory = parseLatest.paths?.runtimeDirectory
assert.ok(parseRuntimeDirectory, "malformed recipe must retain a runtime artifact directory")
const parseDiagnostic = await readFile(join(parseArtifactsPath, parseRuntimeDirectory, "files", "phpunit", ".pg-test-result.txt"), "utf8")
assert.match(parseDiagnostic, /STAGE_FAIL:bootstrap:ParseError/)
} finally {
await rm(root, { recursive: true, force: true })
}
async function runFailedRecipe(outputPath = artifactsPath): Promise<RecipeRunOutput> {
try {
const result = await execFileAsync(process.execPath, ["packages/cli/dist/index.js", "recipe-run", "--recipe", recipePath, "--artifacts", outputPath, "--json"], {
cwd: process.cwd(),
timeout: 300_000,
maxBuffer: 2 * 1024 * 1024,
})
return recipeRunOutput(result.stdout)
} catch (error) {
const output = recipeRunOutput(error && typeof error === "object" && "stdout" in error ? error.stdout : undefined)
if (output) {
return output
}
throw error
}
}
interface RecipeRunOutput {
success?: boolean
error?: { message?: string }
}
function recipeRunOutput(value: unknown): RecipeRunOutput {
assert.equal(typeof value, "string", "recipe-run must return JSON output")
return JSON.parse(value) as RecipeRunOutput
}
console.log("playground phpunit bootstrap failure integration ok")