11import assert from "node:assert/strict"
22import { execFileSync } from "node:child_process"
3- import { mkdtempSync , writeFileSync } from "node:fs"
3+ import { mkdirSync , mkdtempSync , realpathSync , writeFileSync } from "node:fs"
44import { tmpdir } from "node:os"
55import { 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+
68100const recipe = buildWordPressPhpunitRecipe ( {
69101 pluginSlug : "woocommerce" ,
70102 extra_plugins : [ {
@@ -146,6 +178,7 @@ assert.ok(projectModeCode.includes("configured PHPUnit harness autoload file is
146178assert . ok ( projectModeCode . includes ( "NOTICE:project bootstrap mode continuing without configured PHPUnit harness autoload" ) )
147179assert . ok ( projectModeCode . includes ( "$test_root = \"/home/example/public_html/bin/tests/phpunit\";" ) )
148180assert . ok ( projectModeCode . includes ( "pg_resolve_test_root" ) )
181+ assert . ok ( projectModeCode . includes ( "pg_resolve_selected_test_file" ) )
149182assert . ok ( projectModeCode . includes ( "function pg_project_bootstrap_real_path" ) )
150183assert . ok ( projectModeCode . includes ( "$base_dir = dirname($xml_real);" ) )
151184assert . 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)"))
156189assert . equal ( projectModeCode . match ( / r e t u r n a r r a y \( \$ d i r e c t o r i e s , \$ s u f f i x e s , \$ p r e f i x e s , \$ e x c l u d e s \) ; / g) ?. length ?? 0 , 0 )
157190assert . equal ( projectModeCode . match ( / r e t u r n \$ r e t u r n _ v a l u e s \( \) ; / g) ?. length , 3 )
158191assertPhpunitParseConfigFallbacksReturnFiveTuple ( projectModeCode , "wp_codebox_phpunit_parse_config" , "pg_log" )
192+ assertSelectedTestFileResolution ( projectModeCode )
159193
160194let capturedDefaultProjectCode = ""
161195await runPhpunitCommand ( {
0 commit comments