tests: Use standard utilities for temporary file names
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 4 May 2019 20:32:51 +0000 (21:32 +0100)
committerKrinkle <krinklemail@gmail.com>
Mon, 6 May 2019 17:21:45 +0000 (17:21 +0000)
Change-Id: Iff28c27990a81b02a92148a88256b9a25518f496

tests/parser/ParserTestPrinter.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/libs/CSSMinTest.php
tests/phpunit/mocks/filebackend/MockFileBackend.php
tests/phpunit/mocks/filerepo/MockLocalRepo.php

index fddee3d..34f8cd5 100644 (file)
@@ -168,14 +168,10 @@ class ParserTestPrinter extends TestRecorder {
                        $output = strtr( $output, $pairs );
                }
 
-               # Windows, or at least the fc utility, is retarded
-               $slash = wfIsWindows() ? '\\' : '/';
-               $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
-
-               $infile = "$prefix-$inFileTail";
+               $infile = tempnam( wfTempDir(), "mwParser-$inFileTail" );
                $this->dumpToFile( $input, $infile );
 
-               $outfile = "$prefix-$outFileTail";
+               $outfile = tempnam( wfTempDir(), "mwParser-$outFileTail" );
                $this->dumpToFile( $output, $outfile );
 
                global $wgDiff3;
index bdf61b3..ec61c23 100644 (file)
@@ -499,14 +499,15 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         * @return string Absolute name of the temporary directory
         */
        protected function getNewTempDirectory() {
-               // Starting of with a temporary /file/.
+               // Starting of with a temporary *file*.
                $fileName = $this->getNewTempFile();
 
-               // Converting the temporary /file/ to a /directory/
+               // Converting the temporary file to a *directory*.
                // The following is not atomic, but at least we now have a single place,
-               // where temporary directory creation is bundled and can be improved
+               // where temporary directory creation is bundled and can be improved.
                unlink( $fileName );
-               $this->assertTrue( wfMkdirParents( $fileName ) );
+               // If this fails for some reason, PHP will warn and fail the test.
+               mkdir( $fileName, 0777, /* recursive = */ true );
 
                return $fileName;
        }
index ef333f9..4c93789 100644 (file)
@@ -85,9 +85,9 @@ class CSSMinTest extends MediaWikiTestCase {
         * @covers CSSMin::getMimeType
         */
        public function testGetMimeType( $fileContents, $fileExtension, $expected ) {
-               $fileName = wfTempDir() . DIRECTORY_SEPARATOR . uniqid( 'MW_PHPUnit_CSSMinTest_' ) . '.'
-                       . $fileExtension;
-               $this->addTmpFiles( $fileName );
+               // Automatically removed when it falls out of scope (including if the test fails)
+               $file = TempFSFile::factory( 'PHPUnit_CSSMinTest_', $fileExtension, wfTempDir() );
+               $fileName = $file->getPath();
                file_put_contents( $fileName, $fileContents );
                $this->assertSame( $expected, CSSMin::getMimeType( $fileName ) );
        }
index 0a04993..a1bdbad 100644 (file)
@@ -32,7 +32,7 @@ class MockFileBackend extends MemoryFileBackend {
        protected function doGetLocalCopyMulti( array $params ) {
                $tmpFiles = []; // (path => MockFSFile)
                foreach ( $params['srcs'] as $src ) {
-                       $tmpFiles[$src] = new MockFSFile( wfTempDir() . '/' . wfRandomString( 32 ) );
+                       $tmpFiles[$src] = new MockFSFile( "Fake path for $src" );
                }
                return $tmpFiles;
        }
index eeaf05a..b2c51ca 100644 (file)
@@ -7,17 +7,16 @@
  * @since 1.28
  */
 class MockLocalRepo extends LocalRepo {
-       function getLocalCopy( $virtualUrl ) {
-               return new MockFSFile( wfTempDir() . '/' . wfRandomString( 32 ) );
+       public function getLocalCopy( $virtualUrl ) {
+               return new MockFSFile( "Fake path for $virtualUrl" );
        }
 
-       function getLocalReference( $virtualUrl ) {
-               return new MockFSFile( wfTempDir() . '/' . wfRandomString( 32 ) );
+       public function getLocalReference( $virtualUrl ) {
+               return new MockFSFile( "Fake path for $virtualUrl" );
        }
 
-       function getFileProps( $virtualUrl ) {
+       public function getFileProps( $virtualUrl ) {
                $fsFile = $this->getLocalReference( $virtualUrl );
-
                return $fsFile->getProps();
        }
 }