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

/vendor/magento/magento2-base/dev/tests/js/JsTestDriver/run_js_tests.php

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