Merge "StructureTest::testUnitTestFileNamesEndWithTest() should not shell out"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 19 Sep 2018 17:37:55 +0000 (17:37 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 19 Sep 2018 17:37:55 +0000 (17:37 +0000)
tests/phpunit/structure/StructureTest.php

index 82302b1..97bed4c 100644 (file)
@@ -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' ] );
        }
 }