Add a LESS test suite
authorSam Smith <git@samsmith.io>
Mon, 17 Mar 2014 13:24:52 +0000 (13:24 +0000)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Tue, 1 Apr 2014 03:02:53 +0000 (23:02 -0400)
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
tests/TestsAutoLoader.php
tests/phpunit/LessFileCompilationTest.php [new file with mode: 0644]
tests/phpunit/suite.xml
tests/phpunit/suites/LessTestSuite.php [new file with mode: 0644]

index d02d8a7..f81285f 100644 (file)
  */
 
 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 );
        }
 }
 
index 7a048bf..29c3269 100644 (file)
@@ -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 (file)
index 0000000..f67fe02
--- /dev/null
@@ -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";
+       }
+}
index 3e76ff8..a0c532e 100644 (file)
@@ -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>
diff --git a/tests/phpunit/suites/LessTestSuite.php b/tests/phpunit/suites/LessTestSuite.php
new file mode 100644 (file)
index 0000000..26a784a
--- /dev/null
@@ -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;
+       }
+}