-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
phpunit: Clean cache directory before running tests
We've had a number of cases where we had confusing test failures locally due to a bad cache entry from another commit we were working on. Clean the cache before each test run to avoid this issue. Since cleaning the cache directory is a bit more slow than simply checking its existence, I've also gone ahead and switched the logic from setUp (before each test) to setUpBeforeClass (only once). For that to work, the variables have to be static instead. While at it, address a few minor outdated style choices that were inconsistent with other Wikimedia code: * Rename the two variables and our test classes to camelcase. * Remove "phpunit_" prefix. * Rename our base class to end in "TestCase" and mention Less. Change-Id: I980ca88619425848b40899267a217c7f017f9559
- Loading branch information
Showing
5 changed files
with
37 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,40 @@ | ||
<?php | ||
|
||
class phpunit_bootstrap extends PHPUnit\Framework\TestCase { | ||
public $fixtures_dir; | ||
public $cache_dir; | ||
class LessTestCase extends PHPUnit\Framework\TestCase { | ||
protected static $fixturesDir; | ||
protected static $cacheDir; | ||
|
||
public static function getFixtureDir() { | ||
$rootDir = dirname( dirname( __DIR__ ) ); | ||
return $rootDir . '/test/Fixtures'; | ||
} | ||
|
||
public function setUp(): void { | ||
public static function setUpBeforeClass(): void { | ||
$rootDir = dirname( dirname( __DIR__ ) ); | ||
require_once $rootDir . '/lib/Less/Autoloader.php'; | ||
Less_Autoloader::register(); | ||
|
||
$this->fixtures_dir = self::getFixtureDir(); | ||
$this->cache_dir = $rootDir . '/test/phpunit/_cache/'; | ||
$this->checkCacheDirectory(); | ||
$rootDir = dirname( dirname( __DIR__ ) ); | ||
self::$fixturesDir = $rootDir . '/test/Fixtures'; | ||
|
||
self::$cacheDir = $rootDir . '/test/phpunit/_cache/'; | ||
self::checkCacheDirectory(); | ||
// Cleaning the cache dir is only needed once per class | ||
self::cleanCacheDirectory(); | ||
} | ||
|
||
private function checkCacheDirectory() { | ||
if ( !file_exists( $this->cache_dir ) && !mkdir( $this->cache_dir ) ) { | ||
$this->fail( "Could not be create cache dir at " . $this->cache_dir ); | ||
private static function checkCacheDirectory() { | ||
if ( !file_exists( self::$cacheDir ) && !mkdir( self::$cacheDir ) ) { | ||
self::fail( "Could not create cache dir at " . self::$cacheDir ); | ||
} | ||
|
||
if ( !is_writable( self::$cacheDir ) ) { | ||
self::fail( "Cache dir not writable at " . self::$cacheDir ); | ||
} | ||
} | ||
|
||
if ( !is_writable( $this->cache_dir ) ) { | ||
$this->fail( "Cache dir not writable at " . $this->cache_dir ); | ||
private static function cleanCacheDirectory() { | ||
// Clean directory of cache from previous test runs | ||
// on different code versions (incl e.g. any bugs they might contain). | ||
foreach ( scandir( self::$cacheDir ) as $entry ) { | ||
if ( $entry !== '.' && $entry !== '..' ) { | ||
unlink( self::$cacheDir . '/' . $entry ); | ||
} | ||
} | ||
} | ||
} |