*/
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 );
}
}
'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",
--- /dev/null
+<?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";
+ }
+}
<file>suites/ExtensionsTestSuite.php</file>
<file>suites/ExtensionsParserTestSuite.php</file>
</testsuite>
+ <testsuite name="less">
+ <file>suites/LessTestSuite.php</file>
+ </testsuite>
</testsuites>
<groups>
<exclude>
--- /dev/null
+<?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;
+ }
+}