From 2bcd74d9681689792e6430b595a095aa7fa084d9 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 4 May 2019 21:32:51 +0100 Subject: [PATCH] tests: Use standard utilities for temporary file names Change-Id: Iff28c27990a81b02a92148a88256b9a25518f496 --- tests/parser/ParserTestPrinter.php | 8 ++------ tests/phpunit/MediaWikiTestCase.php | 9 +++++---- tests/phpunit/includes/libs/CSSMinTest.php | 6 +++--- tests/phpunit/mocks/filebackend/MockFileBackend.php | 2 +- tests/phpunit/mocks/filerepo/MockLocalRepo.php | 11 +++++------ 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/tests/parser/ParserTestPrinter.php b/tests/parser/ParserTestPrinter.php index fddee3d28f..34f8cd50d3 100644 --- a/tests/parser/ParserTestPrinter.php +++ b/tests/parser/ParserTestPrinter.php @@ -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; diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index bdf61b320e..ec61c23634 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -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; } diff --git a/tests/phpunit/includes/libs/CSSMinTest.php b/tests/phpunit/includes/libs/CSSMinTest.php index ef333f9420..4c937899db 100644 --- a/tests/phpunit/includes/libs/CSSMinTest.php +++ b/tests/phpunit/includes/libs/CSSMinTest.php @@ -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 ) ); } diff --git a/tests/phpunit/mocks/filebackend/MockFileBackend.php b/tests/phpunit/mocks/filebackend/MockFileBackend.php index 0a0499305a..a1bdbadb53 100644 --- a/tests/phpunit/mocks/filebackend/MockFileBackend.php +++ b/tests/phpunit/mocks/filebackend/MockFileBackend.php @@ -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; } diff --git a/tests/phpunit/mocks/filerepo/MockLocalRepo.php b/tests/phpunit/mocks/filerepo/MockLocalRepo.php index eeaf05a0aa..b2c51ca49d 100644 --- a/tests/phpunit/mocks/filerepo/MockLocalRepo.php +++ b/tests/phpunit/mocks/filerepo/MockLocalRepo.php @@ -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(); } } -- 2.20.1