Skip to content

Commit 310d250

Browse files
committed
Preserve PHPUnit harness intent across path rewriting
1 parent 9681379 commit 310d250

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

packages/runtime-core/src/recipe-builders.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function buildWordPressPhpunitRecipe(options: WordPressPhpunitRecipeOptio
9494
commandJsonArg("env-json", options.env ?? {}),
9595
commandJsonArg("wp-config-defines-json", options.wpConfigDefines ?? {}),
9696
commandArg("autoload-file", options.autoloadFile ?? "/wp-codebox-vendor/autoload.php"),
97+
commandArg("autoload-file-role", "harness"),
9798
commandArg("project-autoload-file", options.projectAutoloadFile ?? ""),
9899
commandArg("tests-dir", options.testsDir ?? "/wp-codebox-vendor/wp-phpunit/wp-phpunit"),
99100
commandArg("test-root", options.testRoot ?? `${pluginTarget}/tests`),

packages/runtime-playground/src/phpunit-command-handlers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface PhpunitRunCodeOptions {
44
pluginSlug: string
55
cwd: string
66
autoloadFile: string
7+
autoloadFileRole?: "harness"
78
projectAutoloadFile?: string
89
testsDir: string
910
testRoot?: string
@@ -353,6 +354,7 @@ $result_file = ${JSON.stringify(options.resultFile ?? PLUGIN_PHPUNIT_RESULT_FILE
353354
$current_stage = 'preboot';
354355
$pg_stage_output_buffering = false;
355356
$autoload_file = ${JSON.stringify(options.autoloadFile)};
357+
$autoload_file_role = ${JSON.stringify(options.autoloadFileRole ?? "")};
356358
$project_autoload_file = ${JSON.stringify(options.projectAutoloadFile ?? "")};
357359
$tests_dir = ${JSON.stringify(options.testsDir)};
358360
$test_root = ${JSON.stringify(options.testRoot || `/wordpress/wp-content/plugins/${options.pluginSlug}/tests`)};
@@ -1062,7 +1064,7 @@ if ($multisite) {
10621064
}
10631065
10641066
$legacy_project_autoload_file = '';
1065-
if ($bootstrap_mode === 'project' && $project_autoload_file === '' && $autoload_file !== '' && $autoload_file !== '/wp-codebox-vendor/autoload.php') {
1067+
if ($autoload_file_role === '' && $bootstrap_mode === 'project' && $project_autoload_file === '' && $autoload_file !== '' && $autoload_file !== '/wp-codebox-vendor/autoload.php') {
10661068
$legacy_project_autoload_file = $autoload_file;
10671069
}
10681070
$harness_autoload_file = $legacy_project_autoload_file !== '' ? '/wp-codebox-vendor/autoload.php' : $autoload_file;

packages/runtime-playground/src/wordpress-command-runners.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,13 @@ export async function runPhpunitCommand({
909909
const pluginSlug = argValue(args, "plugin-slug")?.trim() || ""
910910
const bootstrapMode = argValue(args, "bootstrap-mode")?.trim() || "managed"
911911
const autoloadFile = argValue(args, "autoload-file")?.trim() || (bootstrapMode === "project" ? "" : "/wp-codebox-vendor/autoload.php")
912+
const autoloadFileRole = argValue(args, "autoload-file-role")?.trim() === "harness" ? "harness" : undefined
912913
const resultFile = PLUGIN_PHPUNIT_RESULT_FILE
913914
const code = explicitCode ? await phpCodeFromArgs(args, "wordpress.phpunit") : normalizePhpCode(phpunitRunCode({
914915
pluginSlug,
915916
cwd: argValue(args, "cwd")?.trim() || `/wordpress/wp-content/plugins/${pluginSlug}`,
916917
autoloadFile,
918+
autoloadFileRole,
917919
projectAutoloadFile: argValue(args, "project-autoload-file")?.trim() || "",
918920
testsDir: argValue(args, "tests-dir")?.trim() || "/wp-codebox-vendor/wp-phpunit/wp-phpunit",
919921
testRoot: argValue(args, "test-root")?.trim() || `/wordpress/wp-content/plugins/${pluginSlug}/tests`,

tests/phpunit-project-autoload.test.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,12 @@ assert.deepEqual(recipe.workflow.steps[0].args.filter((arg) => arg.includes("aut
201201
"autoload-file=/wp-codebox-vendor/autoload.php",
202202
`project-autoload-file=${woocommerceAutoload}`,
203203
])
204+
assert.ok(recipe.workflow.steps[0].args.includes("autoload-file-role=harness"), "modern PHPUnit recipes explicitly preserve harness autoload intent")
204205
assert.deepEqual(rewriteInputMountPathArgs(recipe.workflow.steps[0].args, recipeInputMountPathMap(recipe)).filter((arg) => arg.includes("autoload-file=")), [
205206
"autoload-file=/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php",
206207
`project-autoload-file=${woocommerceAutoload}`,
207208
])
209+
assert.ok(rewriteInputMountPathArgs(recipe.workflow.steps[0].args, recipeInputMountPathMap(recipe)).includes("autoload-file-role=harness"), "CLI path canonicalization preserves autoload intent")
208210
assert.ok(recipe.workflow.steps[0].args.includes("cwd=/home/example/public_html"))
209211
assert.ok(recipe.workflow.steps[0].args.includes("test-root=/home/example/public_html/bin/tests/phpunit"))
210212
assert.ok(recipe.workflow.steps[0].args.includes("phpunit-xml=/home/example/public_html/bin/tests/phpunit/phpunit.xml.dist"))
@@ -236,6 +238,8 @@ assert.ok(projectBootstrapIndex > bootIndex)
236238
assert.ok(projectAutoloadIndex > projectBootstrapIndex)
237239
assert.ok(projectModeCode.includes("'autoload_required' => $bootstrap_mode !== 'project' || $harness_autoload_file !== ''"))
238240
assert.ok(projectModeCode.includes("$legacy_project_autoload_file = $autoload_file"))
241+
assert.ok(projectModeCode.includes('$autoload_file_role = "";'), "direct callers without an explicit role retain the legacy compatibility path")
242+
assert.ok(projectModeCode.includes("if ($autoload_file_role === '' && $bootstrap_mode === 'project'"))
239243
assert.ok(projectModeCode.includes("configured PHPUnit harness autoload file is not readable"))
240244
assert.ok(projectModeCode.includes("NOTICE:project bootstrap mode continuing without configured PHPUnit harness autoload"))
241245
assert.ok(projectModeCode.includes("$test_root = \"/home/example/public_html/bin/tests/phpunit\";"))
@@ -262,12 +266,47 @@ assert.ok(verifyHarnessIndex > 0, "verify_harness stage must be present")
262266
assert.ok(projectModeTestsuiteIndex > verifyHarnessIndex, "harness verification must precede TestSuite construction")
263267
assertProjectBootstrapHarnessGuard(projectModeCode)
264268

265-
let capturedDefaultProjectCode = ""
269+
const canonicalHarnessProjectModeCode = phpunitRunCode({
270+
pluginSlug: "woocommerce",
271+
cwd: "/wordpress/wp-content/plugins/woocommerce",
272+
autoloadFile: "/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php",
273+
autoloadFileRole: "harness",
274+
projectAutoloadFile: woocommerceAutoload,
275+
testsDir: "/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/wp-phpunit/wp-phpunit",
276+
testRoot: "/home/example/public_html/bin/tests/phpunit",
277+
phpunitXml: "/wordpress/wp-content/plugins/woocommerce/phpunit.xml.dist",
278+
selectedTestFile: "",
279+
changedTestFiles: [],
280+
phpunitArgs: [],
281+
env: {},
282+
wpConfigDefines: {},
283+
dependencyMounts: [],
284+
bootstrapFiles: [],
285+
bootstrapMode: "project",
286+
projectBootstrap: "tests/legacy/bootstrap.php",
287+
multisite: false,
288+
})
289+
assert.ok(canonicalHarnessProjectModeCode.includes('$autoload_file_role = "harness";'))
290+
assert.ok(canonicalHarnessProjectModeCode.includes('$harness_autoload_file = $legacy_project_autoload_file !== \'\' ? \'/wp-codebox-vendor/autoload.php\' : $autoload_file;'))
291+
const canonicalHarnessResolution = canonicalHarnessProjectModeCode.match(/\$legacy_project_autoload_file = '';[\s\S]*?\$harness_autoload_file = [^;]+;/)?.[0]
292+
assert.ok(canonicalHarnessResolution, "generated project-mode code must resolve harness autoload intent")
293+
const canonicalHarnessProbe = join(mkdtempSync(join(tmpdir(), "wp-codebox-canonical-harness-")), "probe.php")
294+
writeFileSync(canonicalHarnessProbe, `<?php
295+
$bootstrap_mode = 'project';
296+
$autoload_file = '/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php';
297+
$autoload_file_role = 'harness';
298+
$project_autoload_file = ${phpString(woocommerceAutoload)};
299+
${canonicalHarnessResolution}
300+
echo json_encode(array($legacy_project_autoload_file, $harness_autoload_file));
301+
`)
302+
assert.deepEqual(JSON.parse(execFileSync("php", [canonicalHarnessProbe], { encoding: "utf8" })), ["", "/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php"], "a canonical staged harness path remains the harness in project mode")
303+
304+
let capturedCanonicalHarnessCode = ""
266305
await runPhpunitCommand({
267306
artifactRoot: mkdtempSync(join(tmpdir(), "wp-codebox-phpunit-artifacts-")),
268307
mounts: [],
269308
runPlaygroundCommand: async (_command, _server, input) => {
270-
capturedDefaultProjectCode = input.code
309+
capturedCanonicalHarnessCode = input.code
271310
return { text: "ok", exitCode: 0 }
272311
},
273312
server: { playground: {} } as never,
@@ -276,13 +315,15 @@ await runPhpunitCommand({
276315
args: [
277316
"plugin-slug=ai-provider-for-openai",
278317
"bootstrap-mode=project",
318+
"autoload-file=/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php",
319+
"autoload-file-role=harness",
279320
"phpunit-xml=phpunit.xml.dist",
280321
"test-file=tests/unit/Models/OpenAiEmbeddingGenerationModelTest.php",
281322
],
282323
},
283324
})
284-
assert.ok(capturedDefaultProjectCode.includes('$autoload_file = "";'))
285-
assert.ok(capturedDefaultProjectCode.includes("NOTICE:project bootstrap mode continuing without configured PHPUnit harness autoload"))
325+
assert.ok(capturedCanonicalHarnessCode.includes('$autoload_file = "/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php";'))
326+
assert.ok(capturedCanonicalHarnessCode.includes('$autoload_file_role = "harness";'))
286327

287328
const coreModeCode = corePhpunitRunCode({
288329
coreRoot: "/wordpress",

0 commit comments

Comments
 (0)