Skip to content

Commit 01b647e

Browse files
committed
test: Support child fixtures and enable for lessjs-2.5.3
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
1 parent 739bac9 commit 01b647e

File tree

4 files changed

+135
-51
lines changed

4 files changed

+135
-51
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#colours{color1:#fea;color2:#ffeeaa;color3:rgba(255,238,170,0.1);string:"#fea";/*! but not this type
2+
Note preserved whitespace
3+
*/}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}

test/compare.php

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
subdirectory, and compare it to an eponymous file in the
1313
"css/" subdirectory.
1414
15+
- {{FIXTURE_DIR}}
16+
1517
Default: test/Fixtures/lessjs-2.5.3/
1618
1719
--override By default, the compare tool validates the full upstream
@@ -32,6 +34,8 @@
3234
3335
TEXT;
3436

37+
define( 'FIXTURES', require __DIR__ . '/fixtures.php' );
38+
3539
class LessFixtureDiff {
3640
private int $summaryOK = 0;
3741
private array $summaryFail = [];
@@ -44,15 +48,12 @@ public function cli( $args ) {
4448
if ( $arg === '--override' ) {
4549
$useOverride = true;
4650
} elseif ( strpos( $arg, '--' ) === 0 ) {
47-
print "Error: Invalid option $arg\n\n";
48-
print USAGE;
49-
exit( 1 );
51+
$this->error( "Invalid option $arg" );
5052
} elseif ( $fixtureDir === null ) {
5153
// First non-option argument
5254
$fixtureDir = $arg;
5355
} else {
54-
print "Error: Unexpected argument $arg\n\n";
55-
print USAGE;
56+
$this->error( "Unexpected argument $arg" );
5657
}
5758
}
5859

@@ -62,32 +63,55 @@ public function cli( $args ) {
6263
);
6364
}
6465

65-
public function compare( string $fixtureDir, bool $useOverride ): void {
66-
$fixtureDir = rtrim( $fixtureDir, '/' );
67-
$cssDir = "$fixtureDir/css";
68-
$overrideDir = "$fixtureDir/override";
69-
if ( !is_dir( $cssDir ) ) {
70-
// Check because glob() tolerances non-existence
71-
print "Error: Missing directory $cssDir\n\n";
72-
print USAGE;
73-
exit( 1 );
66+
private function error( $message ) {
67+
print "Error: $message\n\n";
68+
print preg_replace_callback(
69+
'/^(.*){{FIXTURE_DIR}}$/m',
70+
static function ( $matches ) {
71+
$prefix = $matches[1];
72+
return $prefix . implode( "\n$prefix", array_keys( FIXTURES ) );
73+
},
74+
USAGE
75+
);
76+
exit( 1 );
77+
}
78+
79+
/**
80+
* @param string $fixtureDir Fixture group name as defined in test/fixtures.php,
81+
* or path to a fixture directory,
82+
* or path to a fixture css/less subdirectory.
83+
* @return array|null
84+
*/
85+
private function getFixture( string $fixtureDir ): ?array {
86+
foreach ( FIXTURES as $group => $fixture ) {
87+
if ( $fixtureDir === $group
88+
|| realpath( $fixtureDir ) === realpath( $fixture['cssDir'] )
89+
|| realpath( $fixtureDir ) === realpath( $fixture['lessDir'] )
90+
|| realpath( $fixtureDir ) === realpath( $fixture['cssDir'] . "/.." )
91+
) {
92+
return $fixture;
93+
}
7494
}
75-
if ( $useOverride && !is_dir( $overrideDir ) ) {
76-
print "Error: Missing directory $overrideDir\n\n";
77-
print USAGE;
78-
exit( 1 );
95+
return null;
96+
}
97+
98+
public function compare( string $fixtureDir, bool $useOverride ): void {
99+
$fixture = $this->getFixture( $fixtureDir );
100+
if ( !$fixture ) {
101+
$this->error( "Undefined fixture $fixtureDir" );
79102
}
80-
$group = basename( $fixtureDir );
103+
$cssDir = $fixture['cssDir'];
104+
$lessDir = $fixture['lessDir'];
105+
$overrideDir = $useOverride ? ( $fixture['overrideDir'] ?? null ) : null;
106+
$options = $fixture['options'] ?? [];
81107
foreach ( glob( "$cssDir/*.css" ) as $cssFile ) {
82-
// From /Fixtures/lessjs/css/something.css
83-
// into /Fixtures/lessjs/less/name.less
84108
$name = basename( $cssFile, '.css' );
85-
$lessFile = dirname( dirname( $cssFile ) ) . '/less/' . $name . '.less';
86-
$overrideFile = dirname( dirname( $cssFile ) ) . '/override/' . $name . '.css';
87-
if ( $useOverride && file_exists( $overrideFile ) ) {
109+
$lessFile = "$lessDir/$name.less";
110+
$overrideFile = $overrideDir ? "$overrideDir/$name.css" : null;
111+
if ( $overrideFile && file_exists( $overrideFile ) ) {
88112
$cssFile = $overrideFile;
89113
}
90-
$this->handleFixture( $cssFile, $lessFile );
114+
$this->handleFixture( $cssFile, $lessFile, $options );
91115
}
92116

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

108-
public function handleFixture( $cssFile, $lessFile ) {
132+
public function handleFixture( $cssFile, $lessFile, $options ) {
109133
$expectedCSS = trim( file_get_contents( $cssFile ) );
110134

111135
// Check with standard parser
112-
$parser = new Less_Parser();
136+
$parser = new Less_Parser( $options );
113137
try {
114138
$parser->parseFile( $lessFile );
115139
$css = $parser->getCss();

test/fixtures.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
// Used by phpunit/FixturesTest.php and test/compare.php
4+
5+
$fixtureDir = __DIR__ . '/Fixtures';
6+
7+
return [
8+
'less.php' => [
9+
'lessDir' => "$fixtureDir/less.php/less",
10+
'cssDir' => "$fixtureDir/less.php/css",
11+
],
12+
'bug-reports' => [
13+
'lessDir' => "$fixtureDir/bug-reports/less",
14+
'cssDir' => "$fixtureDir/bug-reports/css",
15+
],
16+
17+
// Upstream fixtures and parser options are declared
18+
// at https://github.com/less/less.js/blob/v2.5.3/test/index.js#L17
19+
20+
'lessjs-2.5.3' => [
21+
'lessDir' => "$fixtureDir/lessjs-2.5.3/less",
22+
'cssDir' => "$fixtureDir/lessjs-2.5.3/css",
23+
'overrideDir' => "$fixtureDir/lessjs-2.5.3/override",
24+
],
25+
'lessjs-2.5.3/compression' => [
26+
'lessDir' => "$fixtureDir/lessjs-2.5.3/less/compression",
27+
'cssDir' => "$fixtureDir/lessjs-2.5.3/css/compression",
28+
'overrideDir' => "$fixtureDir/lessjs-2.5.3/override/compression",
29+
'options' => [
30+
'compress' => true,
31+
],
32+
],
33+
'lessjs-2.5.3/strict-units' => [
34+
'lessDir' => "$fixtureDir/lessjs-2.5.3/less/strict-units",
35+
'cssDir' => "$fixtureDir/lessjs-2.5.3/css/strict-units",
36+
'options' => [
37+
'strictUnits' => true,
38+
],
39+
],
40+
'lessjs-2.5.3/include-path' => [
41+
'lessDir' => "$fixtureDir/lessjs-2.5.3/less/include-path",
42+
'cssDir' => "$fixtureDir/lessjs-2.5.3/css/include-path",
43+
'overrideDir' => "$fixtureDir/lessjs-2.5.3/override/include-path",
44+
'options' => [
45+
'import_dirs' => [
46+
"$fixtureDir/lessjs-2.5.3/data" => '',
47+
"$fixtureDir/lessjs-2.5.3/less/import" => '',
48+
],
49+
],
50+
],
51+
];

test/phpunit/FixturesTest.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,29 @@ class phpunit_FixturesTest extends phpunit_bootstrap {
3939
'mixins-guards' => true, // T352867
4040
'urls' => true, // T353147
4141
'variables' => true, // T352830, T352866
42-
]
42+
],
43+
'lessjs-2.5.3/include-path' => [
44+
'include-path' => true, // T353147, data-uri()
45+
],
4346
];
4447

4548
public static function provideFixtures() {
46-
foreach ( [
47-
'less.php',
48-
'bug-reports',
49-
'lessjs-2.5.3',
50-
] as $group ) {
51-
$outputDir = self::getFixtureDir() . "/$group/css";
52-
if ( !is_dir( $outputDir ) ) {
49+
foreach ( (
50+
require __DIR__ . '/../fixtures.php'
51+
) as $group => $fixture ) {
52+
$cssDir = $fixture['cssDir'];
53+
$lessDir = $fixture['lessDir'];
54+
$overrideDir = $fixture['overrideDir'] ?? null;
55+
$options = $fixture['options'] ?? [];
56+
if ( !is_dir( $cssDir ) ) {
5357
// Check because glob() tolerances non-existence
54-
throw new RuntimeException( "Directory missing: $outputDir" );
58+
throw new RuntimeException( "Directory missing: $cssDir" );
5559
}
56-
foreach ( glob( "$outputDir/*.css" ) as $cssFile ) {
60+
foreach ( glob( "$cssDir/*.css" ) as $cssFile ) {
5761
$name = basename( $cssFile, '.css' );
58-
// From /Fixtures/lessjs/css/something.css
59-
// into /Fixtures/lessjs/less/name.less
60-
$lessFile = dirname( dirname( $cssFile ) ) . '/less/' . $name . '.less';
61-
$overrideFile = dirname( dirname( $cssFile ) ) . '/override/' . $name . '.css';
62-
if ( file_exists( $overrideFile ) ) {
62+
$lessFile = "$lessDir/$name.less";
63+
$overrideFile = $overrideDir ? "$overrideDir/$name.css" : null;
64+
if ( $overrideFile && file_exists( $overrideFile ) ) {
6365
if ( file_get_contents( $overrideFile ) === file_get_contents( $cssFile ) ) {
6466
print "WARNING: Redundant override for $overrideFile\n";
6567
}
@@ -68,19 +70,19 @@ public static function provideFixtures() {
6870
if ( self::KNOWN_FAILURE[ $group ][ $name ] ?? false ) {
6971
continue;
7072
}
71-
yield "Fixtures/$group $name" => [ $cssFile, $lessFile ];
73+
yield "Fixtures/$group $name" => [ $cssFile, $lessFile, $options ];
7274
}
7375
}
7476
}
7577

7678
/**
7779
* @dataProvider provideFixtures
7880
*/
79-
public function testFixture( $cssFile, $lessFile ) {
81+
public function testFixture( $cssFile, $lessFile, $options ) {
8082
$expectedCSS = trim( file_get_contents( $cssFile ) );
8183

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

9698
// Check with cache
97-
$options = [ 'cache_dir' => $this->cache_dir,
98-
'functions' => [ '_color' => [ __CLASS__, 'FnColor' ],
99-
'add' => [ __CLASS__, 'FnAdd' ],
100-
'increment' => [ __CLASS__, 'FnIncrement' ] ] ];
99+
$optionsWithCache = $options + [
100+
'cache_dir' => $this->cache_dir,
101+
'functions' => [
102+
'_color' => [ __CLASS__, 'FnColor' ],
103+
'add' => [ __CLASS__, 'FnAdd' ],
104+
'increment' => [ __CLASS__, 'FnIncrement' ],
105+
],
106+
];
101107
$files = [ $lessFile => '' ];
102108
try {
103-
$cacheFile = Less_Cache::Regen( $files, $options );
109+
$cacheFile = Less_Cache::Regen( $files, $optionsWithCache );
104110
$css = file_get_contents( $this->cache_dir . '/' . $cacheFile );
105111
} catch ( Less_Exception_Parser $e ) {
106112
$css = $e->getMessage();
@@ -110,7 +116,7 @@ public function testFixture( $cssFile, $lessFile ) {
110116

111117
// Check using the cached data
112118
try {
113-
$cacheFile = Less_Cache::Get( $files, $options );
119+
$cacheFile = Less_Cache::Get( $files, $optionsWithCache );
114120
$css = file_get_contents( $this->cache_dir . '/' . $cacheFile );
115121
} catch ( Less_Exception_Parser $e ) {
116122
$css = $e->getMessage();

0 commit comments

Comments
 (0)