Skip to content

Commit 12659d3

Browse files
committed
Fix PHPUnit cache writes through readonly mounts
1 parent 490502f commit 12659d3

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ function ${options.relativeFunctionName}(string $path, string $${options.rootPar
274274

275275
function phpunitArgsPhp(functionName: string, logFunction: string): string {
276276
return `function ${functionName}(array $argv) {
277-
$arguments = array('colors' => 'never', 'testdox' => true, 'verbose' => false, 'extensions' => array());
277+
// PHPUnit otherwise writes .phpunit.result.cache relative to the mounted
278+
// plugin working directory. Readonly mounts must not acquire that side effect.
279+
$arguments = array('colors' => 'never', 'testdox' => true, 'verbose' => false, 'extensions' => array(), 'cacheResult' => false);
278280
$args = array_slice($argv, 1);
279281
for ($i = 0; $i < count($args); $i++) {
280282
$arg = $args[$i];
@@ -300,6 +302,18 @@ function phpunitArgsPhp(functionName: string, logFunction: string): string {
300302
$arguments['verbose'] = true;
301303
continue;
302304
}
305+
if ($arg === '--cache-result-file' && isset($args[$i + 1]) && $args[$i + 1] !== '') {
306+
$arguments['cacheResult'] = true;
307+
$arguments['cacheResultFile'] = $args[++$i];
308+
${logFunction}('NOTICE:phpunit result cache redirected to: ' . $arguments['cacheResultFile']);
309+
continue;
310+
}
311+
if (strpos($arg, '--cache-result-file=') === 0 && substr($arg, strlen('--cache-result-file=')) !== '') {
312+
$arguments['cacheResult'] = true;
313+
$arguments['cacheResultFile'] = substr($arg, strlen('--cache-result-file='));
314+
${logFunction}('NOTICE:phpunit result cache redirected to: ' . $arguments['cacheResultFile']);
315+
continue;
316+
}
303317
}
304318
return $arguments;
305319
}`

tests/phpunit-project-autoload.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,22 @@ const managedModeCode = phpunitRunCode({
321321

322322
assert.ok(managedModeCode.includes("configured PHPUnit harness autoload file is not readable"))
323323

324+
const phpunitArgsFunction = extractPhpFunction(managedModeCode, "wp_codebox_phpunit_args")
325+
const phpunitArgsProbe = join(mkdtempSync(join(tmpdir(), "wp-codebox-phpunit-cache-args-")), "probe.php")
326+
writeFileSync(phpunitArgsProbe, `<?php
327+
function pg_log($message) {}
328+
${phpunitArgsFunction}
329+
echo json_encode(array(
330+
'default' => wp_codebox_phpunit_args(array('phpunit')),
331+
'redirected' => wp_codebox_phpunit_args(array('phpunit', '--cache-result-file=/tmp/wp-codebox-phpunit.result.cache')),
332+
));
333+
`)
334+
const phpunitArgs = JSON.parse(execFileSync("php", [phpunitArgsProbe], { encoding: "utf8" })) as {
335+
default: Record<string, unknown>
336+
redirected: Record<string, unknown>
337+
}
338+
assert.equal(phpunitArgs.default.cacheResult, false, "PHPUnit result caching must be disabled when no sandbox cache path is requested")
339+
assert.equal(phpunitArgs.redirected.cacheResult, true)
340+
assert.equal(phpunitArgs.redirected.cacheResultFile, "/tmp/wp-codebox-phpunit.result.cache")
341+
324342
console.log("phpunit project autoload ok")

0 commit comments

Comments
 (0)