Skip to content

Commit

Permalink
tests: When parser fails, don't show entire diff
Browse files Browse the repository at this point in the history
Some of our fixtures are pretty big, and by using $this->assertSame()
PHPUnit will show the entire diff, including all
missing lines from $expectedCss and new lines which are the Parser
exception.

We have some tests where we store the parser error mesage as css file,
and then we check if the Parser returns expected error messages, but
all of those are below 1KB (to be exact, the biggest error css is
310 bytes).

Therefore for small css files (less than 1KB) we will show the diff,
presenting both the expected CSS and the parser error. For bigger
files (like bootstrap) system should print only the parser exception,
without printing thousands of expected css lines.

Change-Id: I371d7b6b605b74ba3a44e83fb4c81c050649b735
  • Loading branch information
polishdeveloper authored and Krinkle committed Jul 4, 2024
1 parent 64e6f22 commit 8415478
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion test/phpunit/FixturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,27 @@ public function testFixture( $cssFile, $lessFile, $options, $ifSetSkipTestMessag

// Check with standard parser
$parser = new Less_Parser( $options );
$hasError = false;
try {
$parser->registerFunction( '_color', [ __CLASS__, 'FnColor' ] );
$parser->registerFunction( 'add', [ __CLASS__, 'FnAdd' ] );
$parser->registerFunction( 'increment', [ __CLASS__, 'FnIncrement' ] );
$parser->parseFile( $lessFile );
$css = $parser->getCss();
} catch ( Less_Exception_Parser $e ) {
$hasError = true;
$css = $e->getMessage();
}
$css = trim( $css );
$this->assertSame( $expectedCSS, $css, "Standard compiler" );

if ( $hasError && $expectedCSS !== $css && strlen( $expectedCSS ) > 1024 ) {
// If we have a parser exception, show the error as-is instead of a long diff
// with all lines from $expectedCss as missing. We check the length so as to
// still render a diff if this is a test case where we expected a (different) error.
$this->fail( $css );
} else {
$this->assertSame( $expectedCSS, $css, "Standard compiler" );
}

// Check with cache
$optionsWithCache = $options + [
Expand Down

0 comments on commit 8415478

Please sign in to comment.