PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/dev/tests/js/JsTestDriver/run_js_tests.php

https://bitbucket.org/crismablanco/magento
PHP | 245 lines | 181 code | 30 blank | 34 comment | 20 complexity | 9b1426f8e427205ee39a4afe5ab30c8f MD5 | raw file
  1. <?php
  2. /**
  3. * This script executes all Magento JavaScript unit tests.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. define('RELATIVE_APP_ROOT', '../../../..');
  9. require __DIR__ . '/../../../../app/autoload.php';
  10. $userConfig = normalize('jsTestDriver.php');
  11. $defaultConfig = normalize('jsTestDriver.php.dist');
  12. $configFile = file_exists($userConfig) ? $userConfig : $defaultConfig;
  13. $config = require $configFile;
  14. if (isset($config['JsTestDriver'])) {
  15. $jsTestDriver = $config['JsTestDriver'];
  16. } else {
  17. echo "Value for the 'JsTestDriver' configuration parameter is not specified." . PHP_EOL;
  18. showUsage();
  19. }
  20. if (!file_exists($jsTestDriver)) {
  21. reportError('JsTestDriver jar file does not exist: ' . $jsTestDriver);
  22. }
  23. if (isset($config['Browser'])) {
  24. $browser = $config['Browser'];
  25. } else {
  26. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  27. $browser = 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe';
  28. } elseif (PHP_OS === 'Darwin') {
  29. $browser = '/Applications/Firefox.app/Contents/MacOS/firefox';
  30. } else {
  31. $browser = exec('which firefox');
  32. }
  33. }
  34. if (!file_exists($browser)) {
  35. reportError('Browser executable not found: ' . $browser);
  36. }
  37. $server = isset($config['server']) ? $config['server'] : "http://localhost:9876";
  38. $port = substr(strrchr($server, ':'), 1);
  39. $proxies = isset($config['proxy']) ? $config['proxy'] : [];
  40. $testFilesPath = isset($config['test']) ? $config['test'] : [];
  41. $testFiles = listFiles($testFilesPath);
  42. $loadFilesPath = isset($config['load']) ? $config['load'] : [];
  43. $loadFiles = listFiles($loadFilesPath);
  44. if (empty($loadFiles)) {
  45. reportError('Could not find any files to load.');
  46. }
  47. $serveFilesPath = isset($config['serve']) ? $config['serve'] : [];
  48. $serveFiles = listFiles($serveFilesPath);
  49. $sortedFiles = [];
  50. $fileOrder = normalize('jsTestDriverOrder.php');
  51. if (file_exists($fileOrder)) {
  52. $loadOrder = require $fileOrder;
  53. foreach ($loadOrder as $file) {
  54. $sortedFiles[] = RELATIVE_APP_ROOT . $file;
  55. }
  56. foreach ($loadFiles as $loadFile) {
  57. $found = false;
  58. $normalizedLoadFile = normalize($loadFile);
  59. foreach ($loadOrder as $orderFile) {
  60. if (strcmp(normalize(RELATIVE_APP_ROOT . $orderFile), $normalizedLoadFile) == 0) {
  61. $found = true;
  62. break;
  63. }
  64. }
  65. if (!$found) {
  66. array_push($sortedFiles, $loadFile);
  67. }
  68. }
  69. }
  70. $jsTestDriverConf = __DIR__ . '/jsTestDriver.conf';
  71. $fh = fopen($jsTestDriverConf, 'w');
  72. fwrite($fh, "server: $server" . PHP_EOL);
  73. if (count($proxies) > 0) {
  74. fwrite($fh, "proxy:" . PHP_EOL);
  75. foreach ($proxies as $proxy) {
  76. $proxyServer = sprintf($proxy['server'], $server, normalize(RELATIVE_APP_ROOT));
  77. fwrite($fh, ' - {matcher: "' . $proxy['matcher'] . '", server: "' . $proxyServer . '"}' . PHP_EOL);
  78. }
  79. }
  80. fwrite($fh, "load:" . PHP_EOL);
  81. foreach ($sortedFiles as $file) {
  82. if (!in_array($file, $serveFiles)) {
  83. fwrite($fh, " - " . $file . PHP_EOL);
  84. }
  85. }
  86. fwrite($fh, "test:" . PHP_EOL);
  87. foreach ($testFiles as $file) {
  88. fwrite($fh, " - " . $file . PHP_EOL);
  89. }
  90. if (count($serveFiles) > 0) {
  91. fwrite($fh, "serve:" . PHP_EOL);
  92. foreach ($serveFiles as $file) {
  93. fwrite($fh, " - " . $file . PHP_EOL);
  94. }
  95. }
  96. fclose($fh);
  97. $testOutput = __DIR__ . '/test-output';
  98. $filesystemAdapter = new \Magento\Framework\Filesystem\Driver\File();
  99. if ($filesystemAdapter->isExists($testOutput)) {
  100. $filesystemAdapter->deleteDirectory($testOutput);
  101. }
  102. mkdir($testOutput);
  103. $command
  104. = 'java -jar "' . $jsTestDriver . '" --config "' . $jsTestDriverConf . '" --reset --port ' . $port .
  105. ' --browser "' . $browser . '" --raiseOnFailure true --tests all --testOutput "' . $testOutput . '"';
  106. echo $command . PHP_EOL;
  107. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  108. system($command);
  109. } else {
  110. $commandFile = __DIR__ . '/run_js_tests.sh';
  111. $fh = fopen($commandFile, 'w');
  112. $shellCommand
  113. = 'LSOF=`/usr/sbin/lsof -i :' . $port . ' -t`
  114. if [ "$LSOF" != "" ];
  115. then
  116. kill -9 $LSOF
  117. fi
  118. # Skip Xvfb setup for OS X since there browsers do not support headless display that way
  119. if [ "$(uname)" != "Darwin" ]; then
  120. DISPLAY_NUM=99
  121. ps -ef | egrep "[X]vfb.*:$DISPLAY_NUM"
  122. if [ $? -eq 0 ] ; then
  123. pkill Xvfb
  124. fi
  125. XVFB=`which Xvfb`
  126. if [ "$?" -eq 1 ];
  127. then
  128. echo "Xvfb not found."
  129. exit 1
  130. fi
  131. $XVFB :$DISPLAY_NUM -nolisten inet6 -ac &
  132. PID_XVFB="$!" # take the process ID
  133. export DISPLAY=:$DISPLAY_NUM # set display to use that of the Xvfb
  134. fi
  135. USER=`whoami`
  136. SUDO=`which sudo`
  137. # run the tests
  138. $SUDO -u $USER ' . $command . '
  139. if [ "$(uname)" != "Darwin" ]; then
  140. kill -9 $PID_XVFB # shut down Xvfb (firefox will shut down cleanly by JsTestDriver)
  141. fi
  142. echo "Done."';
  143. fwrite($fh, $shellCommand . PHP_EOL);
  144. fclose($fh);
  145. chmod($commandFile, 0750);
  146. exec($commandFile);
  147. }
  148. /**
  149. * Show a message that displays how to use (invoke) this PHP script and exit.
  150. */
  151. function showUsage()
  152. {
  153. reportError('Usage: php run_js_tests.php');
  154. }
  155. /**
  156. * Reports an error given an error message and exits, effectively halting the PHP script's execution.
  157. *
  158. * @param string $message - Error message to be displayed to the user.
  159. *
  160. * @SuppressWarnings(PHPMD.ExitExpression)
  161. */
  162. function reportError($message)
  163. {
  164. echo $message . PHP_EOL;
  165. exit(1);
  166. }
  167. /**
  168. * Takes a file or directory path in any form and normalizes it to fully absolute canonical form
  169. * relative to this PHP script's location.
  170. *
  171. * @param string $filePath - File or directory path to be fully normalized to canonical form.
  172. *
  173. * @return string - The fully resolved path converted to absolute form.
  174. */
  175. function normalize($filePath)
  176. {
  177. return str_replace('\\', '/', realpath(__DIR__ . '/' . $filePath));
  178. }
  179. /**
  180. * Accepts an array of directories and generates a list of Javascript files (.js) in those directories and
  181. * all subdirectories recursively.
  182. *
  183. * @param array $dirs - An array of directories as specified in the configuration file (i.e. $configFile).
  184. *
  185. * @return array - An array of directory paths to all Javascript files found by recursively searching the
  186. * specified array of directories.
  187. */
  188. function listFiles($dirs)
  189. {
  190. $baseDir = normalize(RELATIVE_APP_ROOT);
  191. $result = [];
  192. foreach ($dirs as $dir) {
  193. $path = $baseDir . $dir;
  194. if (is_file($path)) {
  195. $path = substr_replace($path, RELATIVE_APP_ROOT, 0, strlen($baseDir));
  196. array_push($result, $path);
  197. } else {
  198. $paths = glob($path . '/*', GLOB_ONLYDIR | GLOB_NOSORT);
  199. $paths = substr_replace($paths, '', 0, strlen($baseDir));
  200. $result = array_merge($result, listFiles($paths));
  201. $files = glob($path . '/*.js', GLOB_NOSORT);
  202. $files = substr_replace($files, RELATIVE_APP_ROOT, 0, strlen($baseDir));
  203. $result = array_merge($result, $files);
  204. }
  205. }
  206. return $result;
  207. }