Add a LESS test suite
Add the LessFileCompilationTest test case class, which represents the validation of a LESS file by compilation. Add the LessTestSuite test suite, which tests all LESS files registered with the ResourceLoader, and use this to rewrite the checkLess.php maintenance script. Bug: 54665 Change-Id: Iedb8dc31e4817d8b4e40b655cf9b8fb092979e90
This commit is contained in:
parent
f10285fc18
commit
13f3e21e9c
5 changed files with 107 additions and 32 deletions
|
|
@ -22,49 +22,32 @@
|
|||
*/
|
||||
|
||||
require_once __DIR__ . '/Maintenance.php';
|
||||
require_once 'PHPUnit/Autoload.php';
|
||||
|
||||
/**
|
||||
* @ingroup Maintenance
|
||||
*/
|
||||
class CheckLess extends Maintenance {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->mDescription = 'Checks LESS files for errors';
|
||||
$this->mDescription = 'Checks LESS files for errors by running the LessTestSuite PHPUnit test suite';
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
$result = false;
|
||||
$resourceLoader = new ResourceLoader();
|
||||
foreach ( $resourceLoader->getModuleNames() as $name ) {
|
||||
/** @var ResourceLoaderFileModule $module */
|
||||
$module = $resourceLoader->getModule( $name );
|
||||
if ( !$module || !$module instanceof ResourceLoaderFileModule ) {
|
||||
continue;
|
||||
}
|
||||
global $IP;
|
||||
|
||||
$hadErrors = false;
|
||||
foreach ( $module->getAllStyleFiles() as $file ) {
|
||||
if ( $module->getStyleSheetLang( $file ) !== 'less' ) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$compiler = ResourceLoader::getLessCompiler();
|
||||
$compiler->compileFile( $file );
|
||||
} catch ( Exception $e ) {
|
||||
if ( !$hadErrors ) {
|
||||
$this->error( "Errors checking module $name:\n" );
|
||||
$hadErrors = true;
|
||||
}
|
||||
$this->error( $e->getMessage() . "\n" );
|
||||
$result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( !$result ) {
|
||||
$this->output( "No errors found\n" );
|
||||
} else {
|
||||
die( 1 );
|
||||
}
|
||||
// NOTE (phuedx, 2014-03-26) wgAutoloadClasses isn't set up
|
||||
// by either of the dependencies at the top of the file, so
|
||||
// require it here.
|
||||
require_once __DIR__ . '/../tests/TestsAutoLoader.php';
|
||||
|
||||
$textUICommand = new PHPUnit_TextUI_Command();
|
||||
$argv = array(
|
||||
"$IP/tests/phpunit/phpunit.php",
|
||||
"$IP/tests/phpunit/suites/LessTestSuite.php"
|
||||
);
|
||||
$textUICommand->run( $argv );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ $wgAutoloadClasses += array(
|
|||
'ResourceLoaderTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
|
||||
'ResourceLoaderFileModuleTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
|
||||
'TestUser' => "$testDir/phpunit/includes/TestUser.php",
|
||||
'LessFileCompilationTest' => "$testDir/phpunit/LessFileCompilationTest.php",
|
||||
|
||||
# tests/phpunit/includes
|
||||
'BlockTest' => "$testDir/phpunit/includes/BlockTest.php",
|
||||
|
|
|
|||
54
tests/phpunit/LessFileCompilationTest.php
Normal file
54
tests/phpunit/LessFileCompilationTest.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Modelled on Sebastian Bergmann's PHPUnit_Extensions_PhptTestCase class.
|
||||
*
|
||||
* @see https://github.com/sebastianbergmann/phpunit/blob/master/src/Extensions/PhptTestCase.php
|
||||
* @author Sam Smith <samsmith@wikimedia.org>
|
||||
*/
|
||||
class LessFileCompilationTest extends MediaWikiTestCase {
|
||||
|
||||
/**
|
||||
* @var string $file
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* @var ResourceLoaderModule The ResourceLoader module that contains
|
||||
* the file
|
||||
*/
|
||||
protected $module;
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @param ResourceLoaderModule $module The ResourceLoader module that
|
||||
* contains the file
|
||||
* @throws PHPUnit_Framework_Exception When the file parameter isn't a
|
||||
* string or readable file
|
||||
*/
|
||||
public function __construct( $file, ResourceLoaderModule $module ) {
|
||||
if ( !is_string( $file ) || !is_file( $file ) || !is_readable( $file ) ) {
|
||||
throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'readable file' );
|
||||
}
|
||||
|
||||
parent::__construct( 'testLessFileCompilation' );
|
||||
|
||||
$this->file = $file;
|
||||
$this->module = $module;
|
||||
}
|
||||
|
||||
public function testLessFileCompilation() {
|
||||
$compiler = ResourceLoader::getLessCompiler();
|
||||
$this->assertNotNull( $compiler->compileFile( $this->file ) );
|
||||
}
|
||||
|
||||
public function getName( $withDataSet = true ) {
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
$moduleName = $this->module->getName();
|
||||
|
||||
return "{$this->file} in the \"{$moduleName}\" module";
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,9 @@
|
|||
<file>suites/ExtensionsTestSuite.php</file>
|
||||
<file>suites/ExtensionsParserTestSuite.php</file>
|
||||
</testsuite>
|
||||
<testsuite name="less">
|
||||
<file>suites/LessTestSuite.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<groups>
|
||||
<exclude>
|
||||
|
|
|
|||
34
tests/phpunit/suites/LessTestSuite.php
Normal file
34
tests/phpunit/suites/LessTestSuite.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @author Sam Smith <samsmith@wikimedia.org>
|
||||
*/
|
||||
class LessTestSuite extends PHPUnit_Framework_TestSuite {
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$resourceLoader = new ResourceLoader();
|
||||
|
||||
foreach ( $resourceLoader->getModuleNames() as $name ) {
|
||||
$module = $resourceLoader->getModule( $name );
|
||||
if ( !$module || !$module instanceof ResourceLoaderFileModule ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $module->getAllStyleFiles() as $styleFile ) {
|
||||
// TODO (phuedx, 2014-03-19) The
|
||||
// ResourceLoaderFileModule class shouldn't
|
||||
// know how to get a file's extension.
|
||||
if ( $module->getStyleSheetLang( $styleFile ) !== 'less' ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->addTest( new LessFileCompilationTest( $styleFile, $module ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function suite() {
|
||||
return new static;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue