From 480b6537206747687fbb75a3dff1811efcc6e53e Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Sun, 16 Sep 2018 21:44:42 +0200 Subject: [PATCH] StructureTest::testUnitTestFileNamesEndWithTest() should not shell out Bug: T169005 Change-Id: I17b35f31c3989ca0b9056252866a45434c31a105 --- tests/phpunit/structure/StructureTest.php | 44 ++++++++++------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/tests/phpunit/structure/StructureTest.php b/tests/phpunit/structure/StructureTest.php index 82302b1758..97bed4c7a8 100644 --- a/tests/phpunit/structure/StructureTest.php +++ b/tests/phpunit/structure/StructureTest.php @@ -12,10 +12,9 @@ class StructureTest extends MediaWikiTestCase { * @coversNothing */ public function testUnitTestFileNamesEndWithTest() { - if ( wfIsWindows() ) { - $this->markTestSkipped( 'This test does not work on Windows' ); - } - $rootPath = escapeshellarg( __DIR__ . '/..' ); + // realpath() also normalizes directory separator on windows for prefix compares + $rootPath = realpath( __DIR__ . '/..' ); + $suitesPath = realpath( __DIR__ . '/../suites/' ); $testClassRegex = implode( '|', [ 'ApiFormatTestBase', 'ApiTestCase', @@ -29,26 +28,26 @@ class StructureTest extends MediaWikiTestCase { '\\?PHPUnit\\Framework\\TestCase', 'TestCase', // \PHPUnit\Framework\TestCase with appropriate use statement 'DumpTestCase', + 'SpecialPageTestBase', ] ); - $testClassRegex = "^class .* extends ($testClassRegex)"; - $finder = "find $rootPath -name '*.php' '!' -name '*Test.php'" . - " | xargs grep -El '$testClassRegex|function suite\('"; - - $results = null; - $exitCode = null; - exec( $finder, $results, $exitCode ); + $testClassRegex = "/^class .* extends ($testClassRegex)/m"; - $this->assertEquals( - 0, - $exitCode, - 'Verify find/grep command succeeds.' - ); + $results = $this->recurseFiles( $rootPath ); $results = array_filter( $results, - [ $this, 'filterSuites' ] + function ( $filename ) use ( $testClassRegex, $suitesPath ) { + // Remove testUnitTestFileNamesEndWithTest false positives + if ( strpos( $filename, $suitesPath ) === 0 + || substr( $filename, -8 ) === 'Test.php' + ) { + return false; + } + $contents = file_get_contents( $filename ); + return preg_match( $testClassRegex, $contents ); + } ); - $strip = strlen( $rootPath ) - 1; + $strip = strlen( $rootPath ) + 1; foreach ( $results as $k => $v ) { $results[$k] = substr( $v, $strip ); } @@ -59,12 +58,7 @@ class StructureTest extends MediaWikiTestCase { ); } - /** - * Filter to remove testUnitTestFileNamesEndWithTest false positives. - * @param string $filename - * @return bool - */ - public function filterSuites( $filename ) { - return strpos( $filename, __DIR__ . '/../suites/' ) !== 0; + private function recurseFiles( $dir ) { + return ( new File_Iterator_Facade() )->getFilesAsArray( $dir, [ '.php' ] ); } } -- 2.20.1