Skip to content

Commit

Permalink
test: Support child fixtures and enable for lessjs-2.5.3
Browse files Browse the repository at this point in the history
Refactor the way we run fixtures to also consider child fixtures.
We can't simply extend the glob() as-is, because the reason child
fixtures exist is to separate input files that excercise non-default
Parser options.

For example, "compression/" tests the output of the `compress => true`
option.

* Move array of fixture groups from FixtureTest.php to fixtures.php,
  so that compare.php can easily re-use it.

* Make the cssDir and lessDir explicit instead of automatic,
  so that we can easily set it to a subdirectory.

* Add optional 'options' key for custom Parser options.

* Define "strict-units/", and enable in PHPUnit. We pass this!

* Define "compression/", and enable in PHPUnit.
  Add an override because our output is slightly less optimised than
  upstream (we output "0.5px" and "0px", instead of the shorter
  zeroless ".5px" and unitless "0").

  To test live and compare against upstream output, run:
  `test compare.php test/Fixtures/lessjs-2.5.3/css/compression/`

* Define "lessjs-2.5.3/include-path/", and disable in PHPUnit.
  We haven't implemented data-uri() yet.

Change-Id: Iade37bae1728202926026289f3bc7acdfd1ab078
  • Loading branch information
Krinkle committed Mar 5, 2024
1 parent 739bac9 commit 01b647e
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#colours{color1:#fea;color2:#ffeeaa;color3:rgba(255,238,170,0.1);string:"#fea";/*! but not this type
Note preserved whitespace
*/}dimensions{val:0.1px;val:0em;val:4cm;val:0.2;val:5;angles-must-have-unit:0deg;durations-must-have-unit:0s;length-doesnt-have-unit:0px;width:auto\9}@page{marks:none;@top-left-corner{vertical-align:top}@top-left{vertical-align:top}}.shadow^.dom,body^^.shadow{display:done}
78 changes: 51 additions & 27 deletions test/compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
subdirectory, and compare it to an eponymous file in the
"css/" subdirectory.
- {{FIXTURE_DIR}}
Default: test/Fixtures/lessjs-2.5.3/
--override By default, the compare tool validates the full upstream
Expand All @@ -32,6 +34,8 @@
TEXT;

define( 'FIXTURES', require __DIR__ . '/fixtures.php' );

class LessFixtureDiff {
private int $summaryOK = 0;
private array $summaryFail = [];
Expand All @@ -44,15 +48,12 @@ public function cli( $args ) {
if ( $arg === '--override' ) {
$useOverride = true;
} elseif ( strpos( $arg, '--' ) === 0 ) {
print "Error: Invalid option $arg\n\n";
print USAGE;
exit( 1 );
$this->error( "Invalid option $arg" );
} elseif ( $fixtureDir === null ) {
// First non-option argument
$fixtureDir = $arg;
} else {
print "Error: Unexpected argument $arg\n\n";
print USAGE;
$this->error( "Unexpected argument $arg" );
}
}

Expand All @@ -62,32 +63,55 @@ public function cli( $args ) {
);
}

public function compare( string $fixtureDir, bool $useOverride ): void {
$fixtureDir = rtrim( $fixtureDir, '/' );
$cssDir = "$fixtureDir/css";
$overrideDir = "$fixtureDir/override";
if ( !is_dir( $cssDir ) ) {
// Check because glob() tolerances non-existence
print "Error: Missing directory $cssDir\n\n";
print USAGE;
exit( 1 );
private function error( $message ) {
print "Error: $message\n\n";
print preg_replace_callback(
'/^(.*){{FIXTURE_DIR}}$/m',
static function ( $matches ) {
$prefix = $matches[1];
return $prefix . implode( "\n$prefix", array_keys( FIXTURES ) );
},
USAGE
);
exit( 1 );
}

/**
* @param string $fixtureDir Fixture group name as defined in test/fixtures.php,
* or path to a fixture directory,
* or path to a fixture css/less subdirectory.
* @return array|null
*/
private function getFixture( string $fixtureDir ): ?array {
foreach ( FIXTURES as $group => $fixture ) {
if ( $fixtureDir === $group
|| realpath( $fixtureDir ) === realpath( $fixture['cssDir'] )
|| realpath( $fixtureDir ) === realpath( $fixture['lessDir'] )
|| realpath( $fixtureDir ) === realpath( $fixture['cssDir'] . "/.." )
) {
return $fixture;
}
}
if ( $useOverride && !is_dir( $overrideDir ) ) {
print "Error: Missing directory $overrideDir\n\n";
print USAGE;
exit( 1 );
return null;
}

public function compare( string $fixtureDir, bool $useOverride ): void {
$fixture = $this->getFixture( $fixtureDir );
if ( !$fixture ) {
$this->error( "Undefined fixture $fixtureDir" );
}
$group = basename( $fixtureDir );
$cssDir = $fixture['cssDir'];
$lessDir = $fixture['lessDir'];
$overrideDir = $useOverride ? ( $fixture['overrideDir'] ?? null ) : null;
$options = $fixture['options'] ?? [];
foreach ( glob( "$cssDir/*.css" ) as $cssFile ) {
// From /Fixtures/lessjs/css/something.css
// into /Fixtures/lessjs/less/name.less
$name = basename( $cssFile, '.css' );
$lessFile = dirname( dirname( $cssFile ) ) . '/less/' . $name . '.less';
$overrideFile = dirname( dirname( $cssFile ) ) . '/override/' . $name . '.css';
if ( $useOverride && file_exists( $overrideFile ) ) {
$lessFile = "$lessDir/$name.less";
$overrideFile = $overrideDir ? "$overrideDir/$name.css" : null;
if ( $overrideFile && file_exists( $overrideFile ) ) {
$cssFile = $overrideFile;
}
$this->handleFixture( $cssFile, $lessFile );
$this->handleFixture( $cssFile, $lessFile, $options );
}

// Create a simple to understand summary at the end,
Expand All @@ -105,11 +129,11 @@ private function addToSummary( string $line ) {
$this->summary .= $line . "\n";
}

public function handleFixture( $cssFile, $lessFile ) {
public function handleFixture( $cssFile, $lessFile, $options ) {
$expectedCSS = trim( file_get_contents( $cssFile ) );

// Check with standard parser
$parser = new Less_Parser();
$parser = new Less_Parser( $options );
try {
$parser->parseFile( $lessFile );
$css = $parser->getCss();
Expand Down
51 changes: 51 additions & 0 deletions test/fixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

// Used by phpunit/FixturesTest.php and test/compare.php

$fixtureDir = __DIR__ . '/Fixtures';

return [
'less.php' => [
'lessDir' => "$fixtureDir/less.php/less",
'cssDir' => "$fixtureDir/less.php/css",
],
'bug-reports' => [
'lessDir' => "$fixtureDir/bug-reports/less",
'cssDir' => "$fixtureDir/bug-reports/css",
],

// Upstream fixtures and parser options are declared
// at https://github.com/less/less.js/blob/v2.5.3/test/index.js#L17

'lessjs-2.5.3' => [
'lessDir' => "$fixtureDir/lessjs-2.5.3/less",
'cssDir' => "$fixtureDir/lessjs-2.5.3/css",
'overrideDir' => "$fixtureDir/lessjs-2.5.3/override",
],
'lessjs-2.5.3/compression' => [
'lessDir' => "$fixtureDir/lessjs-2.5.3/less/compression",
'cssDir' => "$fixtureDir/lessjs-2.5.3/css/compression",
'overrideDir' => "$fixtureDir/lessjs-2.5.3/override/compression",
'options' => [
'compress' => true,
],
],
'lessjs-2.5.3/strict-units' => [
'lessDir' => "$fixtureDir/lessjs-2.5.3/less/strict-units",
'cssDir' => "$fixtureDir/lessjs-2.5.3/css/strict-units",
'options' => [
'strictUnits' => true,
],
],
'lessjs-2.5.3/include-path' => [
'lessDir' => "$fixtureDir/lessjs-2.5.3/less/include-path",
'cssDir' => "$fixtureDir/lessjs-2.5.3/css/include-path",
'overrideDir' => "$fixtureDir/lessjs-2.5.3/override/include-path",
'options' => [
'import_dirs' => [
"$fixtureDir/lessjs-2.5.3/data" => '',
"$fixtureDir/lessjs-2.5.3/less/import" => '',
],
],
],
];
54 changes: 30 additions & 24 deletions test/phpunit/FixturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,29 @@ class phpunit_FixturesTest extends phpunit_bootstrap {
'mixins-guards' => true, // T352867
'urls' => true, // T353147
'variables' => true, // T352830, T352866
]
],
'lessjs-2.5.3/include-path' => [
'include-path' => true, // T353147, data-uri()
],
];

public static function provideFixtures() {
foreach ( [
'less.php',
'bug-reports',
'lessjs-2.5.3',
] as $group ) {
$outputDir = self::getFixtureDir() . "/$group/css";
if ( !is_dir( $outputDir ) ) {
foreach ( (
require __DIR__ . '/../fixtures.php'
) as $group => $fixture ) {
$cssDir = $fixture['cssDir'];
$lessDir = $fixture['lessDir'];
$overrideDir = $fixture['overrideDir'] ?? null;
$options = $fixture['options'] ?? [];
if ( !is_dir( $cssDir ) ) {
// Check because glob() tolerances non-existence
throw new RuntimeException( "Directory missing: $outputDir" );
throw new RuntimeException( "Directory missing: $cssDir" );
}
foreach ( glob( "$outputDir/*.css" ) as $cssFile ) {
foreach ( glob( "$cssDir/*.css" ) as $cssFile ) {
$name = basename( $cssFile, '.css' );
// From /Fixtures/lessjs/css/something.css
// into /Fixtures/lessjs/less/name.less
$lessFile = dirname( dirname( $cssFile ) ) . '/less/' . $name . '.less';
$overrideFile = dirname( dirname( $cssFile ) ) . '/override/' . $name . '.css';
if ( file_exists( $overrideFile ) ) {
$lessFile = "$lessDir/$name.less";
$overrideFile = $overrideDir ? "$overrideDir/$name.css" : null;
if ( $overrideFile && file_exists( $overrideFile ) ) {
if ( file_get_contents( $overrideFile ) === file_get_contents( $cssFile ) ) {
print "WARNING: Redundant override for $overrideFile\n";
}
Expand All @@ -68,19 +70,19 @@ public static function provideFixtures() {
if ( self::KNOWN_FAILURE[ $group ][ $name ] ?? false ) {
continue;
}
yield "Fixtures/$group $name" => [ $cssFile, $lessFile ];
yield "Fixtures/$group $name" => [ $cssFile, $lessFile, $options ];
}
}
}

/**
* @dataProvider provideFixtures
*/
public function testFixture( $cssFile, $lessFile ) {
public function testFixture( $cssFile, $lessFile, $options ) {
$expectedCSS = trim( file_get_contents( $cssFile ) );

// Check with standard parser
$parser = new Less_Parser();
$parser = new Less_Parser( $options );
try {
$parser->registerFunction( '_color', [ __CLASS__, 'FnColor' ] );
$parser->registerFunction( 'add', [ __CLASS__, 'FnAdd' ] );
Expand All @@ -94,13 +96,17 @@ public function testFixture( $cssFile, $lessFile ) {
$this->assertSame( $expectedCSS, $css, "Standard compiler" );

// Check with cache
$options = [ 'cache_dir' => $this->cache_dir,
'functions' => [ '_color' => [ __CLASS__, 'FnColor' ],
'add' => [ __CLASS__, 'FnAdd' ],
'increment' => [ __CLASS__, 'FnIncrement' ] ] ];
$optionsWithCache = $options + [
'cache_dir' => $this->cache_dir,
'functions' => [
'_color' => [ __CLASS__, 'FnColor' ],
'add' => [ __CLASS__, 'FnAdd' ],
'increment' => [ __CLASS__, 'FnIncrement' ],
],
];
$files = [ $lessFile => '' ];
try {
$cacheFile = Less_Cache::Regen( $files, $options );
$cacheFile = Less_Cache::Regen( $files, $optionsWithCache );
$css = file_get_contents( $this->cache_dir . '/' . $cacheFile );
} catch ( Less_Exception_Parser $e ) {
$css = $e->getMessage();
Expand All @@ -110,7 +116,7 @@ public function testFixture( $cssFile, $lessFile ) {

// Check using the cached data
try {
$cacheFile = Less_Cache::Get( $files, $options );
$cacheFile = Less_Cache::Get( $files, $optionsWithCache );
$css = file_get_contents( $this->cache_dir . '/' . $cacheFile );
} catch ( Less_Exception_Parser $e ) {
$css = $e->getMessage();
Expand Down

0 comments on commit 01b647e

Please sign in to comment.