blob: 9dd980c764f3271a83f23e877230b378e72d5d23 [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
Yang Guob7a44262019-11-05 15:25:23 +010030const CURRENT_PATH = process.env.PWD; // 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() {
40 if (!utils.isDir(CACHE_PATH))
41 fs.mkdirSync(CACHE_PATH);
42 deleteOldContentShells();
43
Mathias Bynens98669992019-11-28 08:50:08 +010044 const hasUserCompiledContentShell = utils.isFile(getContentShellBinaryPath(RELEASE_PATH));
Blink Reformat4c46d092018-04-07 15:32:37 +000045 if (!IS_FETCH_CONTENT_SHELL && hasUserCompiledContentShell) {
Mathias Bynens98669992019-11-28 08:50:08 +010046 const outDir = path.resolve(RELEASE_PATH, '..');
Blink Reformat4c46d092018-04-07 15:32:37 +000047 if (!IS_DEBUG_ENABLED)
48 compileFrontend();
49
50 runTests(outDir, IS_DEBUG_ENABLED);
51 return;
52 }
53
Mathias Bynens98669992019-11-28 08:50:08 +010054 findPreviousUploadedPosition(findMostRecentChromiumCommit())
55 .then(onUploadedCommitPosition)
56 .catch(onError);
Blink Reformat4c46d092018-04-07 15:32:37 +000057
58 function onError(error) {
59 console.log('Unable to run tests because of error:', error);
60 console.log(`Try removing the .test_cache folder [${CACHE_PATH}] and retrying`);
61 }
62}
63main();
64
65function compileFrontend() {
66 console.log('Compiling devtools frontend');
67 try {
68 shell(`ninja -C ${RELEASE_PATH} devtools_frontend_resources`, {cwd: CHROMIUM_SRC_PATH});
69 } catch (err) {
70 console.log(err.stdout.toString());
71 console.log('ERROR: Cannot compile frontend\n' + err);
72 process.exit(1);
73 }
74}
75
76function onUploadedCommitPosition(commitPosition) {
Mathias Bynens98669992019-11-28 08:50:08 +010077 const contentShellDirPath = path.resolve(CACHE_PATH, commitPosition, 'out', TARGET);
78 const contentShellResourcesPath = path.resolve(contentShellDirPath, 'resources');
79 const contentShellPath = path.resolve(CACHE_PATH, commitPosition, 'out');
Blink Reformat4c46d092018-04-07 15:32:37 +000080
Mathias Bynens98669992019-11-28 08:50:08 +010081 const hasCachedContentShell = utils.isFile(getContentShellBinaryPath(contentShellDirPath));
Blink Reformat4c46d092018-04-07 15:32:37 +000082 if (hasCachedContentShell) {
83 console.log(`Using cached content shell at: ${contentShellPath}`);
84 copyFrontend(contentShellResourcesPath);
85 return runTests(contentShellPath, true);
86 }
Mathias Bynens98669992019-11-28 08:50:08 +010087 const url = `http://commondatastorage.googleapis.com/chromium-browser-snapshots/${PLATFORM}/${commitPosition
Blink Reformat4c46d092018-04-07 15:32:37 +000088 }/${CONTENT_SHELL_ZIP}`;
89 return prepareContentShellDirectory(commitPosition)
90 .then(() => downloadContentShell(url, commitPosition))
91 .then(extractContentShell)
92 .then(() => copyFrontend(contentShellResourcesPath))
93 .then(() => runTests(contentShellPath, true));
94}
95
96function copyFrontend(contentShellResourcesPath) {
Mathias Bynens98669992019-11-28 08:50:08 +010097 const devtoolsResourcesPath = path.resolve(contentShellResourcesPath, 'inspector');
98 const copiedFrontendPath = path.resolve(devtoolsResourcesPath, 'front_end');
99 const debugFrontendPath = path.resolve(devtoolsResourcesPath, 'debug');
Blink Reformat4c46d092018-04-07 15:32:37 +0000100 utils.removeRecursive(copiedFrontendPath);
101 utils.removeRecursive(debugFrontendPath);
102 utils.copyRecursive(SOURCE_PATH, devtoolsResourcesPath);
103 fs.renameSync(copiedFrontendPath, debugFrontendPath);
Blink Reformat4c46d092018-04-07 15:32:37 +0000104}
105
Mathias Bynens98669992019-11-28 08:50:08 +0100106function getChromiumSrcPath(isThirdParty) {
107 if (isThirdParty)
108 // Assume we're in `chromium/src/third_party/devtools-frontend/src`.
109 return path.resolve(CURRENT_PATH, '..', '..', '..');
110 // Assume we're in `devtools/devtools-frontend`, where `devtools` is
111 // on the same level as `chromium`.
112 const srcPath = path.resolve(CURRENT_PATH, '..', '..', 'chromium', 'src');
113 if (!utils.isDir(srcPath))
114 throw new Error(`Chromium source directory not found at \`${srcPath}\`. ` +
115 `Either move your standalone \`devtools/devtools-frontend\` checkout ` +
116 `so that \`devtools\` is at the same level as \`chromium\` in ` +
117 `\`chromium/src\`, or use \`--chromium-path\`.`);
118 return srcPath;
119}
120
Blink Reformat4c46d092018-04-07 15:32:37 +0000121function getPlatform() {
122 if (process.platform === 'linux') {
123 return 'Linux_x64';
124 }
125 if (process.platform === 'win32') {
126 return 'Win_x64';
127 }
128 if (process.platform === 'darwin')
129 return 'Mac';
130
131 throw new Error(`Unrecognized platform detected: ${process.platform}`);
132}
133
134function findMostRecentChromiumCommit() {
Mathias Bynens98669992019-11-28 08:50:08 +0100135 const commitMessage = shell(`git log --max-count=1 --grep="Cr-Commit-Position"`).toString().trim();
136 const commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1];
Blink Reformat4c46d092018-04-07 15:32:37 +0000137 return commitPosition;
138}
139
140function deleteOldContentShells() {
Mathias Bynens98669992019-11-28 08:50:08 +0100141 const files = fs.readdirSync(CACHE_PATH);
Blink Reformat4c46d092018-04-07 15:32:37 +0000142 if (files.length < MAX_CONTENT_SHELLS)
143 return;
144 files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10));
Mathias Bynens98669992019-11-28 08:50:08 +0100145 const remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2;
146 const oldContentShellDirs = files.slice(remainingNumberOfContentShells);
147 for (let i = 0; i < oldContentShellDirs.length; i++)
Blink Reformat4c46d092018-04-07 15:32:37 +0000148 utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i]));
149 console.log(`Removed old content shells: ${oldContentShellDirs}`);
150}
151
152function findPreviousUploadedPosition(commitPosition) {
Mathias Bynens98669992019-11-28 08:50:08 +0100153 const previousPosition = commitPosition - 100;
154 const positionsListURL =
Blink Reformat4c46d092018-04-07 15:32:37 +0000155 `http://commondatastorage.googleapis.com/chromium-browser-snapshots/?delimiter=/&prefix=${PLATFORM
156 }/&marker=${PLATFORM}/${previousPosition}/`;
157 return utils.fetch(positionsListURL).then(onPositionsList).catch(onError);
158
159 function onPositionsList(buffer) {
Mathias Bynens98669992019-11-28 08:50:08 +0100160 const positions = buffer.toString('binary')
Blink Reformat4c46d092018-04-07 15:32:37 +0000161 .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g)
162 .map(prefixedPosition => prefixedPosition.split('/')[1])
163 .map(positionString => parseInt(positionString, 10));
Mathias Bynens98669992019-11-28 08:50:08 +0100164 const positionSet = new Set(positions);
165 let previousUploadedPosition = commitPosition;
Blink Reformat4c46d092018-04-07 15:32:37 +0000166 while (commitPosition - previousUploadedPosition < 100) {
167 if (positionSet.has(previousUploadedPosition))
168 return previousUploadedPosition.toString();
169 previousUploadedPosition--;
170 }
171 onError();
172 }
173
174 function onError(error) {
175 if (error)
176 console.log(`Received error: ${error} trying to fetch positions list from url: ${positionsListURL}`);
177 throw new Error(`Unable to find a previous upload position for commit position: ${commitPosition}`);
178 }
179}
180
Mathias Bynens98669992019-11-28 08:50:08 +0100181async function prepareContentShellDirectory(folder) {
182 const contentShellPath = path.join(CACHE_PATH, folder);
Blink Reformat4c46d092018-04-07 15:32:37 +0000183 if (utils.isDir(contentShellPath))
184 utils.removeRecursive(contentShellPath);
185 fs.mkdirSync(contentShellPath);
Mathias Bynens98669992019-11-28 08:50:08 +0100186 return folder;
Blink Reformat4c46d092018-04-07 15:32:37 +0000187}
188
189function downloadContentShell(url, folder) {
190 console.log('Downloading content shell from:', url);
191 console.log('NOTE: Download is ~35-65 MB depending on OS');
192 return utils.fetch(url).then(writeZip).catch(onError);
193
194 function writeZip(buffer) {
195 console.log('Completed download of content shell');
Mathias Bynens98669992019-11-28 08:50:08 +0100196 const contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP);
Blink Reformat4c46d092018-04-07 15:32:37 +0000197 fs.writeFileSync(contentShellZipPath, buffer);
198 return contentShellZipPath;
199 }
200
201 function onError(error) {
202 console.log(`Received error: ${error} trying to download content shell from url: ${url}`);
203 throw new Error('Unable to download content shell');
204 }
205}
206
207function extractContentShell(contentShellZipPath) {
208 console.log(`Extracting content shell zip: ${contentShellZipPath}`);
Mathias Bynens98669992019-11-28 08:50:08 +0100209 const unzipScriptPath = path.resolve(__dirname, 'unzip.py');
210 const src = contentShellZipPath;
211 const dest = path.resolve(path.dirname(src), 'out');
Blink Reformat4c46d092018-04-07 15:32:37 +0000212 shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`);
213 fs.unlinkSync(src);
Mathias Bynens98669992019-11-28 08:50:08 +0100214 const originalDirPath = path.resolve(dest, 'content-shell');
215 const newDirPath = path.resolve(dest, TARGET);
Blink Reformat4c46d092018-04-07 15:32:37 +0000216 fs.renameSync(originalDirPath, newDirPath);
217 fs.chmodSync(getContentShellBinaryPath(newDirPath), '755');
218 if (process.platform === 'darwin') {
Mathias Bynens98669992019-11-28 08:50:08 +0100219 const helperPath = path.resolve(
Blink Reformat4c46d092018-04-07 15:32:37 +0000220 newDirPath, 'Content Shell.app', 'Contents', 'Frameworks', 'Content Shell Helper.app', 'Contents', 'MacOS',
221 'Content Shell Helper');
222 fs.chmodSync(helperPath, '755');
223 }
224 return dest;
225}
226
227function getContentShellBinaryPath(dirPath) {
228 if (process.platform === 'linux')
229 return path.resolve(dirPath, 'content_shell');
230
231 if (process.platform === 'win32')
232 return path.resolve(dirPath, 'content_shell.exe');
233
234 if (process.platform === 'darwin')
235 return path.resolve(dirPath, 'Content Shell.app', 'Contents', 'MacOS', 'Content Shell');
236}
237
238function runTests(buildDirectoryPath, useDebugDevtools) {
Mathias Bynens98669992019-11-28 08:50:08 +0100239 const testArgs = getInspectorTests().concat([
Blink Reformat4c46d092018-04-07 15:32:37 +0000240 '--build-directory',
241 buildDirectoryPath,
242 '--target',
243 TARGET,
244 ]);
245 if (useDebugDevtools)
246 testArgs.push('--additional-driver-flag=--debug-devtools');
247 else
248 console.log('TIP: You can debug a test using: npm run debug-test inspector/test-name.html');
249
250 if (IS_DEBUG_ENABLED) {
251 testArgs.push('--additional-driver-flag=--remote-debugging-port=9222');
252 testArgs.push('--time-out-ms=6000000');
253 console.log('\n=============================================');
Mathias Bynens98669992019-11-28 08:50:08 +0100254 const unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/'));
Blink Reformat4c46d092018-04-07 15:32:37 +0000255 if (unitTest) {
Mathias Bynens98669992019-11-28 08:50:08 +0100256 const unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`;
257 const link =
Yang Guo49346f12020-02-06 10:52:02 +0100258 `http://localhost:8080/inspector-sources/debug/integration_test_runner.html?test=${unitTestPath}`;
Blink Reformat4c46d092018-04-07 15:32:37 +0000259 console.log('1) Go to: ', link);
260 console.log('2) Go to: http://localhost:9222/, click on "inspected-page.html", and copy the ws query parameter');
261 console.log('3) Open DevTools on DevTools and you can refresh to re-run the test')
262 } else {
263 console.log('Go to: http://localhost:9222/');
264 console.log('Click on link and in console execute: test()');
265 }
266 console.log('=============================================\n');
267 }
Mathias Bynens98669992019-11-28 08:50:08 +0100268 const args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags());
Mathias Bynensb0693f22020-01-23 13:17:15 +0100269 console.log(`Running web tests with args: ${args.join(' ')}`);
Blink Reformat4c46d092018-04-07 15:32:37 +0000270 childProcess.spawn(PYTHON, args, {stdio: 'inherit'});
271}
272
273function getTestFlags() {
Mathias Bynens98669992019-11-28 08:50:08 +0100274 const flagValues = Object.keys(Flags).map(key => Flags[key]);
Blink Reformat4c46d092018-04-07 15:32:37 +0000275 return process.argv.slice(2).filter(arg => {
Mathias Bynens98669992019-11-28 08:50:08 +0100276 const flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg;
Blink Reformat4c46d092018-04-07 15:32:37 +0000277 return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') &&
278 !utils.includes(arg, 'http/tests/devtools');
279 });
280}
281
282function getInspectorTests() {
Mathias Bynens98669992019-11-28 08:50:08 +0100283 const specificTests =
Blink Reformat4c46d092018-04-07 15:32:37 +0000284 process.argv.filter(arg => utils.includes(arg, 'inspector') || utils.includes(arg, 'http/tests/devtools'));
285 if (specificTests.length)
286 return specificTests;
287 return [
288 'inspector*',
289 'http/tests/inspector*',
290 'http/tests/devtools',
291 ];
Kent Tamurad3d3f042018-12-12 02:45:28 +0000292}