From 13f3e21e9c7ddcef0d59982aeb3bb4b9e240e5f4 Mon Sep 17 00:00:00 2001 From: Sam Smith Date: Mon, 17 Mar 2014 13:24:52 +0000 Subject: [PATCH] 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 --- maintenance/checkLess.php | 47 +++++++------------- tests/TestsAutoLoader.php | 1 + tests/phpunit/LessFileCompilationTest.php | 54 +++++++++++++++++++++++ tests/phpunit/suite.xml | 3 ++ tests/phpunit/suites/LessTestSuite.php | 34 ++++++++++++++ 5 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 tests/phpunit/LessFileCompilationTest.php create mode 100644 tests/phpunit/suites/LessTestSuite.php diff --git a/maintenance/checkLess.php b/maintenance/checkLess.php index d02d8a7b5c..f81285f061 100644 --- a/maintenance/checkLess.php +++ b/maintenance/checkLess.php @@ -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; + + // 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'; - $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 ); - } + $textUICommand = new PHPUnit_TextUI_Command(); + $argv = array( + "$IP/tests/phpunit/phpunit.php", + "$IP/tests/phpunit/suites/LessTestSuite.php" + ); + $textUICommand->run( $argv ); } } diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 7a048bf17a..29c326927f 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -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", diff --git a/tests/phpunit/LessFileCompilationTest.php b/tests/phpunit/LessFileCompilationTest.php new file mode 100644 index 0000000000..f67fe02f3f --- /dev/null +++ b/tests/phpunit/LessFileCompilationTest.php @@ -0,0 +1,54 @@ + + */ +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"; + } +} diff --git a/tests/phpunit/suite.xml b/tests/phpunit/suite.xml index 3e76ff8943..a0c532e539 100644 --- a/tests/phpunit/suite.xml +++ b/tests/phpunit/suite.xml @@ -39,6 +39,9 @@ suites/ExtensionsTestSuite.php suites/ExtensionsParserTestSuite.php + + suites/LessTestSuite.php + diff --git a/tests/phpunit/suites/LessTestSuite.php b/tests/phpunit/suites/LessTestSuite.php new file mode 100644 index 0000000000..26a784adca --- /dev/null +++ b/tests/phpunit/suites/LessTestSuite.php @@ -0,0 +1,34 @@ + + */ +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; + } +} -- 2.20.1