#!/usr/bin/env php 'inputs', 'glob' => '*.less', ); $output = array( 'dir' => 'outputs', 'filename' => '%s.css', ); $prefix = strtr(realpath(dirname(__FILE__)), '\\', '/'); require $prefix.'/../lessc.inc.php'; $compiler = new lessc(); $compiler->importDir = array($input['dir'].'/test-imports'); $fa = 'Fatal Error: '; if (php_sapi_name() != 'cli') { exit($fa.$argv[0].' must be run in the command line.'); } $opts = getopt('hCd::g'); if ($opts === false || isset($opts['h'])) { echo << 0 && $a{0} != '-') { $searchString = $a; break; } } $tests = array(); $matches = glob($input['dir'].'/'.(!is_null($searchString) ? '*'.$searchString : '' ).$input['glob']); if ($matches) { foreach ($matches as $fname) { extract(pathinfo($fname)); // for $filename, from php 5.2 $tests[] = array( 'in' => $fname, 'out' => $output['dir'].'/'.sprintf($output['filename'], $filename), ); } } $count = count($tests); $compiling = isset($opts["C"]); $continue_when_test_fails = isset($opts["g"]); $showDiff = isset($opts["d"]); if ($showDiff && !empty($opts["d"])) { $difftool = $opts["d"]; } echo ($compiling ? "Compiling" : "Running")." $count test".($count == 1 ? '' : 's').":\n"; function dump($msgs, $depth = 1, $prefix=" ") { if (!is_array($msgs)) $msgs = array($msgs); foreach ($msgs as $m) { echo str_repeat($prefix, $depth).' - '.$m."\n"; } } $fail_prefix = " ** "; $fail_count = 0; $i = 1; foreach ($tests as $test) { printf(" [Test %04d/%04d] %s -> %s\n", $i, $count, basename($test['in']), basename($test['out'])); try { ob_start(); $parsed = trim($compiler->parse(file_get_contents($test['in']))); ob_end_clean(); } catch (exception $e) { dump(array( "Failed to compile input, reason:", $e->getMessage(), "Aborting" ), 1, $fail_prefix); break; } if ($compiling) { file_put_contents($test['out'], $parsed); } else { if (!is_file($test['out'])) { dump(array( "Failed to find output file: $test[out]", "Maybe you forgot to compile tests?", "Aborting" ), 1, $fail_prefix); break; } $expected = trim(file_get_contents($test['out'])); // don't care about CRLF vs LF change (DOS/Win vs. UNIX): $expected = trim(str_replace("\r\n", "\n", $expected)); $parsed = trim(str_replace("\r\n", "\n", $parsed)); if ($expected != $parsed) { $fail_count++; if ($showDiff) { dump("Failed:", 1, $fail_prefix); $tmp = $test['out'].".tmp"; file_put_contents($tmp, $parsed); system($difftool.' '.$test['out'].' '.$tmp); unlink($tmp); if (!$continue_when_test_fails) { dump("Aborting"); break; } else { echo "===========================================================================\n"; } } else { dump("Failed, run with -d flag to view diff", 1, $fail_prefix); } } else { dump("Passed"); } } $i++; } exit($fail_count > 255 ? 255 : $fail_count); ?>