PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/demo/protected/extensions/less/lib/lessphp/tests/test.php

https://bitbucket.org/stratosgear/yii-bootstrap
PHP | 190 lines | 176 code | 8 blank | 6 comment | 7 complexity | 5ecbfbcfea3d8019788792c28b6e935b MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0
  1. #!/usr/bin/env php
  2. <?php
  3. error_reporting(E_ALL);
  4. /**
  5. * Go through all files matching pattern in input directory
  6. * and compile them, then compare them to paired file in
  7. * output directory.
  8. */
  9. $difftool = 'diff -b -B -t -u';
  10. $input = array(
  11. 'dir' => 'inputs',
  12. 'glob' => '*.less',
  13. );
  14. $output = array(
  15. 'dir' => 'outputs',
  16. 'filename' => '%s.css',
  17. );
  18. $prefix = strtr(realpath(dirname(__FILE__)), '\\', '/');
  19. require $prefix.'/../lessc.inc.php';
  20. $compiler = new lessc();
  21. $compiler->importDir = array($input['dir'].'/test-imports');
  22. $fa = 'Fatal Error: ';
  23. if (php_sapi_name() != 'cli') {
  24. exit($fa.$argv[0].' must be run in the command line.');
  25. }
  26. $opts = getopt('hCd::g');
  27. if ($opts === false || isset($opts['h'])) {
  28. echo <<<EOT
  29. Usage: ./test.php [options] [searchstring]
  30. where [options] can be a mix of these:
  31. -h Show this help message and exit.
  32. -d=[difftool] Show the diff of the actual output vs. the reference when a
  33. test fails; uses 'diff -b -B -t -u' by default.
  34. The test is aborted after the first failure report, unless
  35. you also specify the '-g' option ('go on').
  36. -g Continue executing the other tests when a test fails and
  37. option '-d' is active.
  38. -C Regenerate ('compile') the reference output files from the
  39. given inputs.
  40. WARNING: ONLY USE THIS OPTION WHEN YOU HAVE ASCERTAINED
  41. THAT lessphp PROCESSES ALL TESTS CORRECTLY!
  42. The optional [searchstring] is used to filter the input files: only tests
  43. which have filename(s) containing the specified searchstring will be
  44. executed. I.e. the corresponding glob pattern is '*[searchstring]*.less'.
  45. The script EXIT CODE is the number of failed tests (with a maximum of 255),
  46. 0 on success and 1 when this help message is shown. This aids in integrating
  47. this script in larger (user defined) shell test scripts.
  48. Examples of use:
  49. - Test the full test set:
  50. ./test.php
  51. - Run only the mixin tests:
  52. ./test.php mixin
  53. - Use a custom diff tool to show diffs for failing tests
  54. ./test.php -d=meld
  55. EOT;
  56. exit(1);
  57. }
  58. $input['dir'] = $prefix.'/'.$input['dir'];
  59. $output['dir'] = $prefix.'/'.$output['dir'];
  60. if (!is_dir($input['dir']) || !is_dir($output['dir']))
  61. exit($fa." both input and output directories must exist\n");
  62. $exe = array_shift($argv); // remove filename
  63. // get the first non flag as search string
  64. $searchString = null;
  65. foreach ($argv as $a) {
  66. if (strlen($a) > 0 && $a{0} != '-') {
  67. $searchString = $a;
  68. break;
  69. }
  70. }
  71. $tests = array();
  72. $matches = glob($input['dir'].'/'.(!is_null($searchString) ? '*'.$searchString : '' ).$input['glob']);
  73. if ($matches) {
  74. foreach ($matches as $fname) {
  75. extract(pathinfo($fname)); // for $filename, from php 5.2
  76. $tests[] = array(
  77. 'in' => $fname,
  78. 'out' => $output['dir'].'/'.sprintf($output['filename'], $filename),
  79. );
  80. }
  81. }
  82. $count = count($tests);
  83. $compiling = isset($opts["C"]);
  84. $continue_when_test_fails = isset($opts["g"]);
  85. $showDiff = isset($opts["d"]);
  86. if ($showDiff && !empty($opts["d"])) {
  87. $difftool = $opts["d"];
  88. }
  89. echo ($compiling ? "Compiling" : "Running")." $count test".($count == 1 ? '' : 's').":\n";
  90. function dump($msgs, $depth = 1, $prefix=" ") {
  91. if (!is_array($msgs)) $msgs = array($msgs);
  92. foreach ($msgs as $m) {
  93. echo str_repeat($prefix, $depth).' - '.$m."\n";
  94. }
  95. }
  96. $fail_prefix = " ** ";
  97. $fail_count = 0;
  98. $i = 1;
  99. foreach ($tests as $test) {
  100. printf(" [Test %04d/%04d] %s -> %s\n", $i, $count, basename($test['in']), basename($test['out']));
  101. try {
  102. ob_start();
  103. $parsed = trim($compiler->parse(file_get_contents($test['in'])));
  104. ob_end_clean();
  105. } catch (exception $e) {
  106. dump(array(
  107. "Failed to compile input, reason:",
  108. $e->getMessage(),
  109. "Aborting"
  110. ), 1, $fail_prefix);
  111. break;
  112. }
  113. if ($compiling) {
  114. file_put_contents($test['out'], $parsed);
  115. } else {
  116. if (!is_file($test['out'])) {
  117. dump(array(
  118. "Failed to find output file: $test[out]",
  119. "Maybe you forgot to compile tests?",
  120. "Aborting"
  121. ), 1, $fail_prefix);
  122. break;
  123. }
  124. $expected = trim(file_get_contents($test['out']));
  125. // don't care about CRLF vs LF change (DOS/Win vs. UNIX):
  126. $expected = trim(str_replace("\r\n", "\n", $expected));
  127. $parsed = trim(str_replace("\r\n", "\n", $parsed));
  128. if ($expected != $parsed) {
  129. $fail_count++;
  130. if ($showDiff) {
  131. dump("Failed:", 1, $fail_prefix);
  132. $tmp = $test['out'].".tmp";
  133. file_put_contents($tmp, $parsed);
  134. system($difftool.' '.$test['out'].' '.$tmp);
  135. unlink($tmp);
  136. if (!$continue_when_test_fails) {
  137. dump("Aborting");
  138. break;
  139. } else {
  140. echo "===========================================================================\n";
  141. }
  142. } else {
  143. dump("Failed, run with -d flag to view diff", 1, $fail_prefix);
  144. }
  145. } else {
  146. dump("Passed");
  147. }
  148. }
  149. $i++;
  150. }
  151. exit($fail_count > 255 ? 255 : $fail_count);
  152. ?>