Skip to content

Commit 18d3c8f

Browse files
authored
Fix PHPUnit relative test-file resolution
1 parent 0761dd8 commit 18d3c8f

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,31 @@ function pg_log($msg) {
342342
file_put_contents($result_file, $msg . "\n", FILE_APPEND);
343343
}
344344
345+
function pg_resolve_selected_test_file($selected_test_file, $test_dir, $runtime_cwd, $plugin_path) {
346+
if ($selected_test_file === '') {
347+
return '';
348+
}
349+
if ($selected_test_file[0] === '/') {
350+
return $selected_test_file;
351+
}
352+
353+
$relative = ltrim($selected_test_file, '/');
354+
$candidates = array(
355+
rtrim($test_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relative,
356+
rtrim($runtime_cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relative,
357+
rtrim($plugin_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relative,
358+
);
359+
$tests_real = realpath($test_dir);
360+
foreach (array_unique($candidates) as $candidate) {
361+
$candidate_real = realpath($candidate);
362+
if ($candidate_real !== false && $tests_real !== false && strpos($candidate_real, rtrim($tests_real, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR) === 0 && is_file($candidate_real)) {
363+
return $candidate_real;
364+
}
365+
}
366+
367+
return $candidates[0];
368+
}
369+
345370
${phpEnvAssignmentFunction("pg_apply_env", "json_encode", "pg_log('NOTICE: skipping invalid bench_env key: ' . var_export($name, true));")}
346371
347372
${phpWpConfigDefineAppenderFunction("pg_append_wp_config_defines", "pg_log('NOTICE: skipping invalid wp_config_defines key: ' . var_export($name, true));")}
@@ -1139,7 +1164,7 @@ try {
11391164
$test_files = wp_codebox_phpunit_discover($directories, $suffixes, $prefixes, $excludes, $configured_files);
11401165
$test_files = pg_filter_changed_test_files($test_files, $changed_test_files_raw, $test_dir);
11411166
if ($selected_test_file !== '') {
1142-
$selected_abs = $selected_test_file[0] === '/' ? $selected_test_file : $test_dir . '/' . ltrim($selected_test_file, '/');
1167+
$selected_abs = pg_resolve_selected_test_file($selected_test_file, $test_dir, $runtime_cwd, $plugin_path);
11431168
if (!in_array($selected_abs, $test_files, true)) {
11441169
$selected_real = realpath($selected_abs);
11451170
$tests_real = realpath($test_dir);

tests/phpunit-project-autoload.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "node:assert/strict"
22
import { execFileSync } from "node:child_process"
3-
import { mkdtempSync, writeFileSync } from "node:fs"
3+
import { mkdirSync, mkdtempSync, realpathSync, writeFileSync } from "node:fs"
44
import { tmpdir } from "node:os"
55
import { join } from "node:path"
66

@@ -65,6 +65,38 @@ echo "ok\n";
6565
assert.equal(execFileSync("php", [scriptPath], { encoding: "utf8" }), "ok\n")
6666
}
6767

68+
function assertSelectedTestFileResolution(source: string): void {
69+
const tempDir = mkdtempSync(join(tmpdir(), "wp-codebox-selected-test-file-"))
70+
const pluginRoot = join(tempDir, "demo-plugin")
71+
const testRoot = join(pluginRoot, "tests")
72+
const nestedTestDir = join(testRoot, "Feature")
73+
const selectedTestFile = join(nestedTestDir, "ExampleTest.php")
74+
const scriptPath = join(tempDir, "assert-selected-test-file.php")
75+
mkdirSync(nestedTestDir, { recursive: true })
76+
writeFileSync(selectedTestFile, "<?php // test\n")
77+
const selectedTestFileReal = realpathSync(selectedTestFile)
78+
79+
const resolverFunction = extractPhpFunction(source, "pg_resolve_selected_test_file")
80+
writeFileSync(scriptPath, `<?php
81+
${resolverFunction}
82+
$plugin_root = ${phpString(pluginRoot)};
83+
$test_root = ${phpString(testRoot)};
84+
$selected_test_file = ${phpString(selectedTestFile)};
85+
$cases = array(
86+
'relative-to-test-root' => pg_resolve_selected_test_file('Feature/ExampleTest.php', $test_root, $plugin_root, $plugin_root),
87+
'relative-to-runtime-root' => pg_resolve_selected_test_file('tests/Feature/ExampleTest.php', $test_root, $plugin_root, $plugin_root),
88+
'absolute-runtime-path' => pg_resolve_selected_test_file($selected_test_file, $test_root, $plugin_root, $plugin_root),
89+
);
90+
echo json_encode($cases);
91+
`)
92+
93+
assert.deepEqual(JSON.parse(execFileSync("php", [scriptPath], { encoding: "utf8" })), {
94+
"relative-to-test-root": selectedTestFileReal,
95+
"relative-to-runtime-root": selectedTestFileReal,
96+
"absolute-runtime-path": selectedTestFile,
97+
})
98+
}
99+
68100
const recipe = buildWordPressPhpunitRecipe({
69101
pluginSlug: "woocommerce",
70102
extra_plugins: [{
@@ -146,6 +178,7 @@ assert.ok(projectModeCode.includes("configured PHPUnit harness autoload file is
146178
assert.ok(projectModeCode.includes("NOTICE:project bootstrap mode continuing without configured PHPUnit harness autoload"))
147179
assert.ok(projectModeCode.includes("$test_root = \"/home/example/public_html/bin/tests/phpunit\";"))
148180
assert.ok(projectModeCode.includes("pg_resolve_test_root"))
181+
assert.ok(projectModeCode.includes("pg_resolve_selected_test_file"))
149182
assert.ok(projectModeCode.includes("function pg_project_bootstrap_real_path"))
150183
assert.ok(projectModeCode.includes("$base_dir = dirname($xml_real);"))
151184
assert.ok(projectModeCode.includes("$bootstrap_real = pg_project_bootstrap_real_path($bootstrap, $phpunit_xml, $from_config);"))
@@ -156,6 +189,7 @@ assert.ok(projectModeCode.includes("' files=' . count($configured_files)"))
156189
assert.equal(projectModeCode.match(/return array\(\$directories, \$suffixes, \$prefixes, \$excludes\);/g)?.length ?? 0, 0)
157190
assert.equal(projectModeCode.match(/return \$return_values\(\);/g)?.length, 3)
158191
assertPhpunitParseConfigFallbacksReturnFiveTuple(projectModeCode, "wp_codebox_phpunit_parse_config", "pg_log")
192+
assertSelectedTestFileResolution(projectModeCode)
159193

160194
let capturedDefaultProjectCode = ""
161195
await runPhpunitCommand({

0 commit comments

Comments
 (0)