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