Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions packages/runtime-playground/src/php-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ${runtimeEnvPhp(spec, args)}
${secretEnvPhp(spec)}
${componentManifestPhp(spec)}
require_once '/wordpress/wp-load.php';
${failureDiagnosticFile ? phpFailureDiagnosticCompletionPhp() : ""}
${recipeActivePluginBootstrapPhp(spec, args)}
${wpCliBridge ? `putenv(${JSON.stringify(`WP_CODEBOX_TERMINAL_ACTION_URL=${wpCliBridge.url}`)});
putenv(${JSON.stringify(`WP_CODEBOX_TERMINAL_ACTION_TOKEN=${wpCliBridge.token}`)});
Expand All @@ -45,15 +46,46 @@ ${command.body}`
}

function phpFailureDiagnosticFilePhp(path: string): string {
return `register_shutdown_function(static function (): void {
$wp_codebox_failure = error_get_last();
if (!is_array($wp_codebox_failure) || !in_array($wp_codebox_failure['type'] ?? 0, array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR), true)) {
return `$wp_codebox_bootstrap_complete = false;
$wp_codebox_bootstrap_buffer_level = ob_get_level();
ob_start();
register_shutdown_function(static function () use (&$wp_codebox_bootstrap_complete, $wp_codebox_bootstrap_buffer_level): void {
if ($wp_codebox_bootstrap_complete) {
return;
}
@file_put_contents(${JSON.stringify(path)}, 'STAGE_FATAL:bootstrap:' . (string) ($wp_codebox_failure['message'] ?? '') . ' at ' . (string) ($wp_codebox_failure['file'] ?? '') . ':' . (int) ($wp_codebox_failure['line'] ?? 0) . "\\n", FILE_APPEND);
$wp_codebox_bootstrap_output = '';
while (ob_get_level() > $wp_codebox_bootstrap_buffer_level) {
$wp_codebox_bootstrap_chunk = ob_get_clean();
if ($wp_codebox_bootstrap_chunk !== false) {
$wp_codebox_bootstrap_output = $wp_codebox_bootstrap_chunk . $wp_codebox_bootstrap_output;
}
}
$wp_codebox_failure = error_get_last();
if (is_array($wp_codebox_failure) && in_array($wp_codebox_failure['type'] ?? 0, array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR), true)) {
$wp_codebox_bootstrap_diagnostic = 'STAGE_FATAL:bootstrap:' . (string) ($wp_codebox_failure['message'] ?? '') . ' at ' . (string) ($wp_codebox_failure['file'] ?? '') . ':' . (int) ($wp_codebox_failure['line'] ?? 0);
} else {
$wp_codebox_bootstrap_detail = trim((string) preg_replace('/\\s+/', ' ', strip_tags($wp_codebox_bootstrap_output)));
if ($wp_codebox_bootstrap_detail === '') {
$wp_codebox_bootstrap_detail = 'WordPress bootstrap terminated before completion without emitting output';
}
$wp_codebox_bootstrap_diagnostic = 'STAGE_DIE:bootstrap:' . substr($wp_codebox_bootstrap_detail, 0, 16384);
}
@file_put_contents(${JSON.stringify(path)}, $wp_codebox_bootstrap_diagnostic . "\\n", FILE_APPEND);
});`
}

function phpFailureDiagnosticCompletionPhp(): string {
return `$wp_codebox_bootstrap_complete = true;
$wp_codebox_bootstrap_output = '';
while (ob_get_level() > $wp_codebox_bootstrap_buffer_level) {
$wp_codebox_bootstrap_chunk = ob_get_clean();
if ($wp_codebox_bootstrap_chunk !== false) {
$wp_codebox_bootstrap_output = $wp_codebox_bootstrap_chunk . $wp_codebox_bootstrap_output;
}
}
echo $wp_codebox_bootstrap_output;`
}

export function splitLeadingStrictTypesDeclare(code: string): { strictTypesDeclare: string; body: string } {
const normalized = normalizePhpCode(code)
const match = normalized.match(/^<\?php\s*(declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;)\s*/i)
Expand Down
20 changes: 18 additions & 2 deletions tests/playground-phpunit-bootstrap-failure.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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")

try {
await writeFile(fatalMuPlugin, `<?php\ntrigger_error('PHPUnit bootstrap fixture token: ${secret}', E_USER_ERROR);\n`)
Expand Down Expand Up @@ -49,13 +50,28 @@ try {
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))
} finally {
await rm(root, { recursive: true, force: true })
}

async function runFailedRecipe(): Promise<RecipeRunOutput> {
async function runFailedRecipe(outputPath = artifactsPath): Promise<RecipeRunOutput> {
try {
const result = await execFileAsync(process.execPath, ["packages/cli/dist/index.js", "recipe-run", "--recipe", recipePath, "--artifacts", artifactsPath, "--json"], {
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,
Expand Down