blob: fc498859cf80f40c8474e1d7e29e10fd99c37b00 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var childProcess = require('child_process');
6var fs = require('fs');
7var path = require('path');
8var shell = require('child_process').execSync;
9
10var utils = require('./utils');
11
12var Flags = {
13 DEBUG_DEVTOOLS: '--debug-devtools',
14 DEBUG_DEVTOOLS_SHORTHAND: '-d',
15 FETCH_CONTENT_SHELL: '--fetch-content-shell',
16 CHROMIUM_PATH: '--chromium-path', // useful for bisecting
17 TARGET: '--target', // build sub-directory (e.g. Release, Default)
18};
19
20var IS_DEBUG_ENABLED =
21 utils.includes(process.argv, Flags.DEBUG_DEVTOOLS) || utils.includes(process.argv, Flags.DEBUG_DEVTOOLS_SHORTHAND);
22var CUSTOM_CHROMIUM_PATH = utils.parseArgs(process.argv)[Flags.CHROMIUM_PATH];
23var IS_FETCH_CONTENT_SHELL = utils.includes(process.argv, Flags.FETCH_CONTENT_SHELL);
24var TARGET = utils.parseArgs(process.argv)[Flags.TARGET] || 'Release';
25
26var CONTENT_SHELL_ZIP = 'content-shell.zip';
27var MAX_CONTENT_SHELLS = 10;
28var PLATFORM = getPlatform();
29var PYTHON = process.platform === 'win32' ? 'python.bat' : 'python';
30
31var CHROMIUM_SRC_PATH = CUSTOM_CHROMIUM_PATH || path.resolve(__dirname, '..', '..', '..', '..', '..');
32var RELEASE_PATH = path.resolve(CHROMIUM_SRC_PATH, 'out', TARGET);
Joel Einbinder89226032018-05-04 21:36:12 +000033var BLINK_TEST_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'blink', 'tools', 'run_web_tests.py');
Dmitry Gozman61ca9312018-04-09 17:42:03 +000034var DEVTOOLS_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'blink', 'renderer', 'devtools');
Blink Reformat4c46d092018-04-07 15:32:37 +000035var CACHE_PATH = path.resolve(DEVTOOLS_PATH, '.test_cache');
36var SOURCE_PATH = path.resolve(DEVTOOLS_PATH, 'front_end');
37
38function main() {
39 if (!utils.isDir(CACHE_PATH))
40 fs.mkdirSync(CACHE_PATH);
41 deleteOldContentShells();
42
43 var hasUserCompiledContentShell = utils.isFile(getContentShellBinaryPath(RELEASE_PATH));
44 if (!IS_FETCH_CONTENT_SHELL && hasUserCompiledContentShell) {
45 var outDir = path.resolve(RELEASE_PATH, '..');
46 if (!IS_DEBUG_ENABLED)
47 compileFrontend();
48
49 runTests(outDir, IS_DEBUG_ENABLED);
50 return;
51 }
52
53 findPreviousUploadedPosition(findMostRecentChromiumCommit()).then(onUploadedCommitPosition).catch(onError);
54
55 function onError(error) {
56 console.log('Unable to run tests because of error:', error);
57 console.log(`Try removing the .test_cache folder [${CACHE_PATH}] and retrying`);
58 }
59}
60main();
61
62function compileFrontend() {
63 console.log('Compiling devtools frontend');
64 try {
65 shell(`ninja -C ${RELEASE_PATH} devtools_frontend_resources`, {cwd: CHROMIUM_SRC_PATH});
66 } catch (err) {
67 console.log(err.stdout.toString());
68 console.log('ERROR: Cannot compile frontend\n' + err);
69 process.exit(1);
70 }
71}
72
73function onUploadedCommitPosition(commitPosition) {
74 var contentShellDirPath = path.resolve(CACHE_PATH, commitPosition, 'out', TARGET);
75 var contentShellResourcesPath = path.resolve(contentShellDirPath, 'resources');
76 var contentShellPath = path.resolve(CACHE_PATH, commitPosition, 'out');
77
78 var hasCachedContentShell = utils.isFile(getContentShellBinaryPath(contentShellDirPath));
79 if (hasCachedContentShell) {
80 console.log(`Using cached content shell at: ${contentShellPath}`);
81 copyFrontend(contentShellResourcesPath);
82 return runTests(contentShellPath, true);
83 }
84 var url = `http://commondatastorage.googleapis.com/chromium-browser-snapshots/${PLATFORM}/${commitPosition
85 }/${CONTENT_SHELL_ZIP}`;
86 return prepareContentShellDirectory(commitPosition)
87 .then(() => downloadContentShell(url, commitPosition))
88 .then(extractContentShell)
89 .then(() => copyFrontend(contentShellResourcesPath))
90 .then(() => runTests(contentShellPath, true));
91}
92
93function copyFrontend(contentShellResourcesPath) {
94 var devtoolsResourcesPath = path.resolve(contentShellResourcesPath, 'inspector');
95 var copiedFrontendPath = path.resolve(devtoolsResourcesPath, 'front_end');
96 var debugFrontendPath = path.resolve(devtoolsResourcesPath, 'debug');
97 var inspectorBackendCommandsPath = path.resolve(devtoolsResourcesPath, 'InspectorBackendCommands.js');
98 var supportedCSSPropertiesPath = path.resolve(devtoolsResourcesPath, 'SupportedCSSProperties.js');
99 utils.removeRecursive(copiedFrontendPath);
100 utils.removeRecursive(debugFrontendPath);
101 utils.copyRecursive(SOURCE_PATH, devtoolsResourcesPath);
102 fs.renameSync(copiedFrontendPath, debugFrontendPath);
103 utils.copy(inspectorBackendCommandsPath, debugFrontendPath);
104 utils.copy(supportedCSSPropertiesPath, debugFrontendPath);
105}
106
107function getPlatform() {
108 if (process.platform === 'linux') {
109 return 'Linux_x64';
110 }
111 if (process.platform === 'win32') {
112 return 'Win_x64';
113 }
114 if (process.platform === 'darwin')
115 return 'Mac';
116
117 throw new Error(`Unrecognized platform detected: ${process.platform}`);
118}
119
120function findMostRecentChromiumCommit() {
121 var commitMessage = shell(`git log --max-count=1 --grep="Cr-Commit-Position"`).toString().trim();
122 var commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1];
123 return commitPosition;
124}
125
126function deleteOldContentShells() {
127 var files = fs.readdirSync(CACHE_PATH);
128 if (files.length < MAX_CONTENT_SHELLS)
129 return;
130 files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10));
131 var remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2;
132 var oldContentShellDirs = files.slice(remainingNumberOfContentShells);
133 for (var i = 0; i < oldContentShellDirs.length; i++)
134 utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i]));
135 console.log(`Removed old content shells: ${oldContentShellDirs}`);
136}
137
138function findPreviousUploadedPosition(commitPosition) {
139 var previousPosition = commitPosition - 100;
140 var positionsListURL =
141 `http://commondatastorage.googleapis.com/chromium-browser-snapshots/?delimiter=/&prefix=${PLATFORM
142 }/&marker=${PLATFORM}/${previousPosition}/`;
143 return utils.fetch(positionsListURL).then(onPositionsList).catch(onError);
144
145 function onPositionsList(buffer) {
146 var positions = buffer.toString('binary')
147 .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g)
148 .map(prefixedPosition => prefixedPosition.split('/')[1])
149 .map(positionString => parseInt(positionString, 10));
150 var positionSet = new Set(positions);
151 var previousUploadedPosition = commitPosition;
152 while (commitPosition - previousUploadedPosition < 100) {
153 if (positionSet.has(previousUploadedPosition))
154 return previousUploadedPosition.toString();
155 previousUploadedPosition--;
156 }
157 onError();
158 }
159
160 function onError(error) {
161 if (error)
162 console.log(`Received error: ${error} trying to fetch positions list from url: ${positionsListURL}`);
163 throw new Error(`Unable to find a previous upload position for commit position: ${commitPosition}`);
164 }
165}
166
167function prepareContentShellDirectory(folder) {
168 var contentShellPath = path.join(CACHE_PATH, folder);
169 if (utils.isDir(contentShellPath))
170 utils.removeRecursive(contentShellPath);
171 fs.mkdirSync(contentShellPath);
172 return Promise.resolve(folder);
173}
174
175function downloadContentShell(url, folder) {
176 console.log('Downloading content shell from:', url);
177 console.log('NOTE: Download is ~35-65 MB depending on OS');
178 return utils.fetch(url).then(writeZip).catch(onError);
179
180 function writeZip(buffer) {
181 console.log('Completed download of content shell');
182 var contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP);
183 fs.writeFileSync(contentShellZipPath, buffer);
184 return contentShellZipPath;
185 }
186
187 function onError(error) {
188 console.log(`Received error: ${error} trying to download content shell from url: ${url}`);
189 throw new Error('Unable to download content shell');
190 }
191}
192
193function extractContentShell(contentShellZipPath) {
194 console.log(`Extracting content shell zip: ${contentShellZipPath}`);
195 var unzipScriptPath = path.resolve(__dirname, 'unzip.py');
196 var src = contentShellZipPath;
197 var dest = path.resolve(path.dirname(src), 'out');
198 shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`);
199 fs.unlinkSync(src);
200 var originalDirPath = path.resolve(dest, 'content-shell');
201 var newDirPath = path.resolve(dest, TARGET);
202 fs.renameSync(originalDirPath, newDirPath);
203 fs.chmodSync(getContentShellBinaryPath(newDirPath), '755');
204 if (process.platform === 'darwin') {
205 var helperPath = path.resolve(
206 newDirPath, 'Content Shell.app', 'Contents', 'Frameworks', 'Content Shell Helper.app', 'Contents', 'MacOS',
207 'Content Shell Helper');
208 fs.chmodSync(helperPath, '755');
209 }
210 return dest;
211}
212
213function getContentShellBinaryPath(dirPath) {
214 if (process.platform === 'linux')
215 return path.resolve(dirPath, 'content_shell');
216
217 if (process.platform === 'win32')
218 return path.resolve(dirPath, 'content_shell.exe');
219
220 if (process.platform === 'darwin')
221 return path.resolve(dirPath, 'Content Shell.app', 'Contents', 'MacOS', 'Content Shell');
222}
223
224function runTests(buildDirectoryPath, useDebugDevtools) {
225 var testArgs = getInspectorTests().concat([
Blink Reformat4c46d092018-04-07 15:32:37 +0000226 '--build-directory',
227 buildDirectoryPath,
228 '--target',
229 TARGET,
230 ]);
231 if (useDebugDevtools)
232 testArgs.push('--additional-driver-flag=--debug-devtools');
233 else
234 console.log('TIP: You can debug a test using: npm run debug-test inspector/test-name.html');
235
236 if (IS_DEBUG_ENABLED) {
237 testArgs.push('--additional-driver-flag=--remote-debugging-port=9222');
238 testArgs.push('--time-out-ms=6000000');
239 console.log('\n=============================================');
240 var unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/'));
241 if (unitTest) {
242 var unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`;
243 var link =
244 `http://localhost:8080/inspector-sources/debug/integration_test_runner.html?experiments=true&test=${
245 unitTestPath
246 }`;
247 console.log('1) Go to: ', link);
248 console.log('2) Go to: http://localhost:9222/, click on "inspected-page.html", and copy the ws query parameter');
249 console.log('3) Open DevTools on DevTools and you can refresh to re-run the test')
250 } else {
251 console.log('Go to: http://localhost:9222/');
252 console.log('Click on link and in console execute: test()');
253 }
254 console.log('=============================================\n');
255 }
256 var args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags());
Kent Tamurad3d3f042018-12-12 02:45:28 +0000257 console.log(`Running web tests with args: ${args}`);
Blink Reformat4c46d092018-04-07 15:32:37 +0000258 childProcess.spawn(PYTHON, args, {stdio: 'inherit'});
259}
260
261function getTestFlags() {
262 var flagValues = Object.keys(Flags).map(key => Flags[key]);
263 return process.argv.slice(2).filter(arg => {
264 var flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg;
265 return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') &&
266 !utils.includes(arg, 'http/tests/devtools');
267 });
268}
269
270function getInspectorTests() {
271 var specificTests =
272 process.argv.filter(arg => utils.includes(arg, 'inspector') || utils.includes(arg, 'http/tests/devtools'));
273 if (specificTests.length)
274 return specificTests;
275 return [
276 'inspector*',
277 'http/tests/inspector*',
278 'http/tests/devtools',
279 ];
Kent Tamurad3d3f042018-12-12 02:45:28 +0000280}