blob: a35bab85376ea612f6daa2d0607a059ed5bdc924 [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
Yang Guob7a44262019-11-05 15:25:23 +01005const childProcess = require('child_process');
6const fs = require('fs');
7const path = require('path');
8const shell = require('child_process').execSync;
9const utils = require('./utils');
Blink Reformat4c46d092018-04-07 15:32:37 +000010
Yang Guob7a44262019-11-05 15:25:23 +010011const Flags = {
Blink Reformat4c46d092018-04-07 15:32:37 +000012 DEBUG_DEVTOOLS: '--debug-devtools',
13 DEBUG_DEVTOOLS_SHORTHAND: '-d',
14 FETCH_CONTENT_SHELL: '--fetch-content-shell',
15 CHROMIUM_PATH: '--chromium-path', // useful for bisecting
16 TARGET: '--target', // build sub-directory (e.g. Release, Default)
17};
18
Yang Guob7a44262019-11-05 15:25:23 +010019const IS_DEBUG_ENABLED =
Blink Reformat4c46d092018-04-07 15:32:37 +000020 utils.includes(process.argv, Flags.DEBUG_DEVTOOLS) || utils.includes(process.argv, Flags.DEBUG_DEVTOOLS_SHORTHAND);
Yang Guob7a44262019-11-05 15:25:23 +010021const CUSTOM_CHROMIUM_PATH = utils.parseArgs(process.argv)[Flags.CHROMIUM_PATH];
22const IS_FETCH_CONTENT_SHELL = utils.includes(process.argv, Flags.FETCH_CONTENT_SHELL);
23const TARGET = utils.parseArgs(process.argv)[Flags.TARGET] || 'Release';
Blink Reformat4c46d092018-04-07 15:32:37 +000024
Yang Guob7a44262019-11-05 15:25:23 +010025const CONTENT_SHELL_ZIP = 'content-shell.zip';
26const MAX_CONTENT_SHELLS = 10;
27const PLATFORM = getPlatform();
28const PYTHON = process.platform === 'win32' ? 'python.bat' : 'python';
Blink Reformat4c46d092018-04-07 15:32:37 +000029
Paul Lewis4d806b92020-03-13 14:51:06 +000030const CURRENT_PATH = process.env.PWD || process.cwd(); // Using env.PWD to account for symlinks.
Mathias Bynens98669992019-11-28 08:50:08 +010031const isThirdParty = CURRENT_PATH.includes('third_party');
32const CHROMIUM_SRC_PATH = CUSTOM_CHROMIUM_PATH || getChromiumSrcPath(isThirdParty);
Yang Guob7a44262019-11-05 15:25:23 +010033const RELEASE_PATH = path.resolve(CHROMIUM_SRC_PATH, 'out', TARGET);
34const BLINK_TEST_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'blink', 'tools', 'run_web_tests.py');
35const DEVTOOLS_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'devtools-frontend', 'src');
36const CACHE_PATH = path.resolve(DEVTOOLS_PATH, '.test_cache');
37const SOURCE_PATH = path.resolve(DEVTOOLS_PATH, 'front_end');
Blink Reformat4c46d092018-04-07 15:32:37 +000038
39function main() {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000040 if (!utils.isDir(CACHE_PATH)) {
Blink Reformat4c46d092018-04-07 15:32:37 +000041 fs.mkdirSync(CACHE_PATH);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000042 }
Blink Reformat4c46d092018-04-07 15:32:37 +000043 deleteOldContentShells();
44
Mathias Bynens98669992019-11-28 08:50:08 +010045 const hasUserCompiledContentShell = utils.isFile(getContentShellBinaryPath(RELEASE_PATH));
Blink Reformat4c46d092018-04-07 15:32:37 +000046 if (!IS_FETCH_CONTENT_SHELL && hasUserCompiledContentShell) {
Mathias Bynens98669992019-11-28 08:50:08 +010047 const outDir = path.resolve(RELEASE_PATH, '..');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000048 if (!IS_DEBUG_ENABLED) {
Blink Reformat4c46d092018-04-07 15:32:37 +000049 compileFrontend();
Tim van der Lippeba26b2b2020-03-11 14:40:00 +000050 }
Blink Reformat4c46d092018-04-07 15:32:37 +000051
52 runTests(outDir, IS_DEBUG_ENABLED);
53 return;
54 }
55
Mathias Bynens98669992019-11-28 08:50:08 +010056 findPreviousUploadedPosition(findMostRecentChromiumCommit())
57 .then(onUploadedCommitPosition)
58 .catch(onError);
Blink Reformat4c46d092018-04-07 15:32:37 +000059
60 function onError(error) {
61 console.log('Unable to run tests because of error:', error);
62 console.log(`Try removing the .test_cache folder [${CACHE_PATH}] and retrying`);
63 }
64}
65main();
66
67function compileFrontend() {
68 console.log('Compiling devtools frontend');
69 try {
70 shell(`ninja -C ${RELEASE_PATH} devtools_frontend_resources`, {cwd: CHROMIUM_SRC_PATH});
71 } catch (err) {
72 console.log(err.stdout.toString());
73 console.log('ERROR: Cannot compile frontend\n' + err);
74 process.exit(1);
75 }
76}
77
78function onUploadedCommitPosition(commitPosition) {
Mathias Bynens98669992019-11-28 08:50:08 +010079 const contentShellDirPath = path.resolve(CACHE_PATH, commitPosition, 'out', TARGET);
80 const contentShellResourcesPath = path.resolve(contentShellDirPath, 'resources');
81 const contentShellPath = path.resolve(CACHE_PATH, commitPosition, 'out');
Blink Reformat4c46d092018-04-07 15:32:37 +000082
Mathias Bynens98669992019-11-28 08:50:08 +010083 const hasCachedContentShell = utils.isFile(getContentShellBinaryPath(contentShellDirPath));
Blink Reformat4c46d092018-04-07 15:32:37 +000084 if (hasCachedContentShell) {
85 console.log(`Using cached content shell at: ${contentShellPath}`);
86 copyFrontend(contentShellResourcesPath);
87 return runTests(contentShellPath, true);
88 }
Mathias Bynens98669992019-11-28 08:50:08 +010089 const url = `http://commondatastorage.googleapis.com/chromium-browser-snapshots/${PLATFORM}/${commitPosition
Blink Reformat4c46d092018-04-07 15:32:37 +000090 }/${CONTENT_SHELL_ZIP}`;
91 return prepareContentShellDirectory(commitPosition)
92 .then(() => downloadContentShell(url, commitPosition))
93 .then(extractContentShell)
94 .then(() => copyFrontend(contentShellResourcesPath))
95 .then(() => runTests(contentShellPath, true));
96}
97
98function copyFrontend(contentShellResourcesPath) {
Mathias Bynens98669992019-11-28 08:50:08 +010099 const devtoolsResourcesPath = path.resolve(contentShellResourcesPath, 'inspector');
100 const copiedFrontendPath = path.resolve(devtoolsResourcesPath, 'front_end');
101 const debugFrontendPath = path.resolve(devtoolsResourcesPath, 'debug');
Blink Reformat4c46d092018-04-07 15:32:37 +0000102 utils.removeRecursive(copiedFrontendPath);
103 utils.removeRecursive(debugFrontendPath);
104 utils.copyRecursive(SOURCE_PATH, devtoolsResourcesPath);
105 fs.renameSync(copiedFrontendPath, debugFrontendPath);
Blink Reformat4c46d092018-04-07 15:32:37 +0000106}
107
Mathias Bynens98669992019-11-28 08:50:08 +0100108function getChromiumSrcPath(isThirdParty) {
109 if (isThirdParty)
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000110 // Assume we're in `chromium/src/third_party/devtools-frontend/src`.
111 {
Mathias Bynens98669992019-11-28 08:50:08 +0100112 return path.resolve(CURRENT_PATH, '..', '..', '..');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000113 }
Mathias Bynens98669992019-11-28 08:50:08 +0100114 // Assume we're in `devtools/devtools-frontend`, where `devtools` is
115 // on the same level as `chromium`.
116 const srcPath = path.resolve(CURRENT_PATH, '..', '..', 'chromium', 'src');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000117 if (!utils.isDir(srcPath)) {
118 throw new Error(
119 `Chromium source directory not found at \`${srcPath}\`. ` +
120 'Either move your standalone `devtools/devtools-frontend` checkout ' +
121 'so that `devtools` is at the same level as `chromium` in ' +
122 '`chromium/src`, or use `--chromium-path`.');
123 }
Mathias Bynens98669992019-11-28 08:50:08 +0100124 return srcPath;
125}
126
Blink Reformat4c46d092018-04-07 15:32:37 +0000127function getPlatform() {
128 if (process.platform === 'linux') {
129 return 'Linux_x64';
130 }
131 if (process.platform === 'win32') {
132 return 'Win_x64';
133 }
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000134 if (process.platform === 'darwin') {
Blink Reformat4c46d092018-04-07 15:32:37 +0000135 return 'Mac';
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000136 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000137
138 throw new Error(`Unrecognized platform detected: ${process.platform}`);
139}
140
141function findMostRecentChromiumCommit() {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000142 const commitMessage = shell('git log --max-count=1 --grep="Cr-Commit-Position"').toString().trim();
Mathias Bynens98669992019-11-28 08:50:08 +0100143 const commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1];
Blink Reformat4c46d092018-04-07 15:32:37 +0000144 return commitPosition;
145}
146
147function deleteOldContentShells() {
Mathias Bynens98669992019-11-28 08:50:08 +0100148 const files = fs.readdirSync(CACHE_PATH);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000149 if (files.length < MAX_CONTENT_SHELLS) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000150 return;
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000151 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000152 files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10));
Mathias Bynens98669992019-11-28 08:50:08 +0100153 const remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2;
154 const oldContentShellDirs = files.slice(remainingNumberOfContentShells);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000155 for (let i = 0; i < oldContentShellDirs.length; i++) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000156 utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i]));
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000157 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000158 console.log(`Removed old content shells: ${oldContentShellDirs}`);
159}
160
161function findPreviousUploadedPosition(commitPosition) {
Mathias Bynens98669992019-11-28 08:50:08 +0100162 const previousPosition = commitPosition - 100;
163 const positionsListURL =
Blink Reformat4c46d092018-04-07 15:32:37 +0000164 `http://commondatastorage.googleapis.com/chromium-browser-snapshots/?delimiter=/&prefix=${PLATFORM
165 }/&marker=${PLATFORM}/${previousPosition}/`;
166 return utils.fetch(positionsListURL).then(onPositionsList).catch(onError);
167
168 function onPositionsList(buffer) {
Mathias Bynens98669992019-11-28 08:50:08 +0100169 const positions = buffer.toString('binary')
Blink Reformat4c46d092018-04-07 15:32:37 +0000170 .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g)
171 .map(prefixedPosition => prefixedPosition.split('/')[1])
172 .map(positionString => parseInt(positionString, 10));
Mathias Bynens98669992019-11-28 08:50:08 +0100173 const positionSet = new Set(positions);
174 let previousUploadedPosition = commitPosition;
Blink Reformat4c46d092018-04-07 15:32:37 +0000175 while (commitPosition - previousUploadedPosition < 100) {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000176 if (positionSet.has(previousUploadedPosition)) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000177 return previousUploadedPosition.toString();
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000178 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000179 previousUploadedPosition--;
180 }
181 onError();
182 }
183
184 function onError(error) {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000185 if (error) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000186 console.log(`Received error: ${error} trying to fetch positions list from url: ${positionsListURL}`);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000187 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000188 throw new Error(`Unable to find a previous upload position for commit position: ${commitPosition}`);
189 }
190}
191
Mathias Bynens98669992019-11-28 08:50:08 +0100192async function prepareContentShellDirectory(folder) {
193 const contentShellPath = path.join(CACHE_PATH, folder);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000194 if (utils.isDir(contentShellPath)) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000195 utils.removeRecursive(contentShellPath);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000196 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000197 fs.mkdirSync(contentShellPath);
Mathias Bynens98669992019-11-28 08:50:08 +0100198 return folder;
Blink Reformat4c46d092018-04-07 15:32:37 +0000199}
200
201function downloadContentShell(url, folder) {
202 console.log('Downloading content shell from:', url);
203 console.log('NOTE: Download is ~35-65 MB depending on OS');
204 return utils.fetch(url).then(writeZip).catch(onError);
205
206 function writeZip(buffer) {
207 console.log('Completed download of content shell');
Mathias Bynens98669992019-11-28 08:50:08 +0100208 const contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP);
Blink Reformat4c46d092018-04-07 15:32:37 +0000209 fs.writeFileSync(contentShellZipPath, buffer);
210 return contentShellZipPath;
211 }
212
213 function onError(error) {
214 console.log(`Received error: ${error} trying to download content shell from url: ${url}`);
215 throw new Error('Unable to download content shell');
216 }
217}
218
219function extractContentShell(contentShellZipPath) {
220 console.log(`Extracting content shell zip: ${contentShellZipPath}`);
Mathias Bynens98669992019-11-28 08:50:08 +0100221 const unzipScriptPath = path.resolve(__dirname, 'unzip.py');
222 const src = contentShellZipPath;
223 const dest = path.resolve(path.dirname(src), 'out');
Blink Reformat4c46d092018-04-07 15:32:37 +0000224 shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`);
225 fs.unlinkSync(src);
Mathias Bynens98669992019-11-28 08:50:08 +0100226 const originalDirPath = path.resolve(dest, 'content-shell');
227 const newDirPath = path.resolve(dest, TARGET);
Blink Reformat4c46d092018-04-07 15:32:37 +0000228 fs.renameSync(originalDirPath, newDirPath);
229 fs.chmodSync(getContentShellBinaryPath(newDirPath), '755');
230 if (process.platform === 'darwin') {
Mathias Bynens98669992019-11-28 08:50:08 +0100231 const helperPath = path.resolve(
Blink Reformat4c46d092018-04-07 15:32:37 +0000232 newDirPath, 'Content Shell.app', 'Contents', 'Frameworks', 'Content Shell Helper.app', 'Contents', 'MacOS',
233 'Content Shell Helper');
234 fs.chmodSync(helperPath, '755');
235 }
236 return dest;
237}
238
239function getContentShellBinaryPath(dirPath) {
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000240 if (process.platform === 'linux') {
Blink Reformat4c46d092018-04-07 15:32:37 +0000241 return path.resolve(dirPath, 'content_shell');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000242 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000243
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000244 if (process.platform === 'win32') {
Blink Reformat4c46d092018-04-07 15:32:37 +0000245 return path.resolve(dirPath, 'content_shell.exe');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000246 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000247
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000248 if (process.platform === 'darwin') {
Blink Reformat4c46d092018-04-07 15:32:37 +0000249 return path.resolve(dirPath, 'Content Shell.app', 'Contents', 'MacOS', 'Content Shell');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000250 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000251}
252
253function runTests(buildDirectoryPath, useDebugDevtools) {
Mathias Bynens98669992019-11-28 08:50:08 +0100254 const testArgs = getInspectorTests().concat([
Blink Reformat4c46d092018-04-07 15:32:37 +0000255 '--build-directory',
256 buildDirectoryPath,
257 '--target',
258 TARGET,
259 ]);
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000260 if (useDebugDevtools) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000261 testArgs.push('--additional-driver-flag=--debug-devtools');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000262 } else {
Blink Reformat4c46d092018-04-07 15:32:37 +0000263 console.log('TIP: You can debug a test using: npm run debug-test inspector/test-name.html');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000264 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000265
266 if (IS_DEBUG_ENABLED) {
267 testArgs.push('--additional-driver-flag=--remote-debugging-port=9222');
268 testArgs.push('--time-out-ms=6000000');
269 console.log('\n=============================================');
Mathias Bynens98669992019-11-28 08:50:08 +0100270 const unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/'));
Blink Reformat4c46d092018-04-07 15:32:37 +0000271 if (unitTest) {
Mathias Bynens98669992019-11-28 08:50:08 +0100272 const unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`;
273 const link =
Yang Guo49346f12020-02-06 10:52:02 +0100274 `http://localhost:8080/inspector-sources/debug/integration_test_runner.html?test=${unitTestPath}`;
Blink Reformat4c46d092018-04-07 15:32:37 +0000275 console.log('1) Go to: ', link);
276 console.log('2) Go to: http://localhost:9222/, click on "inspected-page.html", and copy the ws query parameter');
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000277 console.log('3) Open DevTools on DevTools and you can refresh to re-run the test');
Blink Reformat4c46d092018-04-07 15:32:37 +0000278 } else {
279 console.log('Go to: http://localhost:9222/');
280 console.log('Click on link and in console execute: test()');
281 }
282 console.log('=============================================\n');
283 }
Mathias Bynens98669992019-11-28 08:50:08 +0100284 const args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags());
Mathias Bynensb0693f22020-01-23 13:17:15 +0100285 console.log(`Running web tests with args: ${args.join(' ')}`);
Blink Reformat4c46d092018-04-07 15:32:37 +0000286 childProcess.spawn(PYTHON, args, {stdio: 'inherit'});
287}
288
289function getTestFlags() {
Mathias Bynens98669992019-11-28 08:50:08 +0100290 const flagValues = Object.keys(Flags).map(key => Flags[key]);
Blink Reformat4c46d092018-04-07 15:32:37 +0000291 return process.argv.slice(2).filter(arg => {
Mathias Bynens98669992019-11-28 08:50:08 +0100292 const flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg;
Blink Reformat4c46d092018-04-07 15:32:37 +0000293 return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') &&
294 !utils.includes(arg, 'http/tests/devtools');
295 });
296}
297
298function getInspectorTests() {
Mathias Bynens98669992019-11-28 08:50:08 +0100299 const specificTests =
Blink Reformat4c46d092018-04-07 15:32:37 +0000300 process.argv.filter(arg => utils.includes(arg, 'inspector') || utils.includes(arg, 'http/tests/devtools'));
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000301 if (specificTests.length) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000302 return specificTests;
Tim van der Lippeba26b2b2020-03-11 14:40:00 +0000303 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000304 return [
305 'inspector*',
306 'http/tests/inspector*',
307 'http/tests/devtools',
308 ];
Kent Tamurad3d3f042018-12-12 02:45:28 +0000309}