blob: e3322873f6046e9d4fe7a11437f7dbb960eef12d [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');
100 const inspectorBackendCommandsPath = path.resolve(devtoolsResourcesPath, 'InspectorBackendCommands.js');
101 const supportedCSSPropertiesPath = path.resolve(devtoolsResourcesPath, 'SupportedCSSProperties.js');
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);
106 utils.copy(inspectorBackendCommandsPath, debugFrontendPath);
107 utils.copy(supportedCSSPropertiesPath, debugFrontendPath);
108}
109
Mathias Bynens98669992019-11-28 08:50:08 +0100110function getChromiumSrcPath(isThirdParty) {
111 if (isThirdParty)
112 // Assume we're in `chromium/src/third_party/devtools-frontend/src`.
113 return path.resolve(CURRENT_PATH, '..', '..', '..');
114 // 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');
117 if (!utils.isDir(srcPath))
118 throw new Error(`Chromium source directory not found at \`${srcPath}\`. ` +
119 `Either move your standalone \`devtools/devtools-frontend\` checkout ` +
120 `so that \`devtools\` is at the same level as \`chromium\` in ` +
121 `\`chromium/src\`, or use \`--chromium-path\`.`);
122 return srcPath;
123}
124
Blink Reformat4c46d092018-04-07 15:32:37 +0000125function getPlatform() {
126 if (process.platform === 'linux') {
127 return 'Linux_x64';
128 }
129 if (process.platform === 'win32') {
130 return 'Win_x64';
131 }
132 if (process.platform === 'darwin')
133 return 'Mac';
134
135 throw new Error(`Unrecognized platform detected: ${process.platform}`);
136}
137
138function findMostRecentChromiumCommit() {
Mathias Bynens98669992019-11-28 08:50:08 +0100139 const commitMessage = shell(`git log --max-count=1 --grep="Cr-Commit-Position"`).toString().trim();
140 const commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1];
Blink Reformat4c46d092018-04-07 15:32:37 +0000141 return commitPosition;
142}
143
144function deleteOldContentShells() {
Mathias Bynens98669992019-11-28 08:50:08 +0100145 const files = fs.readdirSync(CACHE_PATH);
Blink Reformat4c46d092018-04-07 15:32:37 +0000146 if (files.length < MAX_CONTENT_SHELLS)
147 return;
148 files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10));
Mathias Bynens98669992019-11-28 08:50:08 +0100149 const remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2;
150 const oldContentShellDirs = files.slice(remainingNumberOfContentShells);
151 for (let i = 0; i < oldContentShellDirs.length; i++)
Blink Reformat4c46d092018-04-07 15:32:37 +0000152 utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i]));
153 console.log(`Removed old content shells: ${oldContentShellDirs}`);
154}
155
156function findPreviousUploadedPosition(commitPosition) {
Mathias Bynens98669992019-11-28 08:50:08 +0100157 const previousPosition = commitPosition - 100;
158 const positionsListURL =
Blink Reformat4c46d092018-04-07 15:32:37 +0000159 `http://commondatastorage.googleapis.com/chromium-browser-snapshots/?delimiter=/&prefix=${PLATFORM
160 }/&marker=${PLATFORM}/${previousPosition}/`;
161 return utils.fetch(positionsListURL).then(onPositionsList).catch(onError);
162
163 function onPositionsList(buffer) {
Mathias Bynens98669992019-11-28 08:50:08 +0100164 const positions = buffer.toString('binary')
Blink Reformat4c46d092018-04-07 15:32:37 +0000165 .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g)
166 .map(prefixedPosition => prefixedPosition.split('/')[1])
167 .map(positionString => parseInt(positionString, 10));
Mathias Bynens98669992019-11-28 08:50:08 +0100168 const positionSet = new Set(positions);
169 let previousUploadedPosition = commitPosition;
Blink Reformat4c46d092018-04-07 15:32:37 +0000170 while (commitPosition - previousUploadedPosition < 100) {
171 if (positionSet.has(previousUploadedPosition))
172 return previousUploadedPosition.toString();
173 previousUploadedPosition--;
174 }
175 onError();
176 }
177
178 function onError(error) {
179 if (error)
180 console.log(`Received error: ${error} trying to fetch positions list from url: ${positionsListURL}`);
181 throw new Error(`Unable to find a previous upload position for commit position: ${commitPosition}`);
182 }
183}
184
Mathias Bynens98669992019-11-28 08:50:08 +0100185async function prepareContentShellDirectory(folder) {
186 const contentShellPath = path.join(CACHE_PATH, folder);
Blink Reformat4c46d092018-04-07 15:32:37 +0000187 if (utils.isDir(contentShellPath))
188 utils.removeRecursive(contentShellPath);
189 fs.mkdirSync(contentShellPath);
Mathias Bynens98669992019-11-28 08:50:08 +0100190 return folder;
Blink Reformat4c46d092018-04-07 15:32:37 +0000191}
192
193function downloadContentShell(url, folder) {
194 console.log('Downloading content shell from:', url);
195 console.log('NOTE: Download is ~35-65 MB depending on OS');
196 return utils.fetch(url).then(writeZip).catch(onError);
197
198 function writeZip(buffer) {
199 console.log('Completed download of content shell');
Mathias Bynens98669992019-11-28 08:50:08 +0100200 const contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP);
Blink Reformat4c46d092018-04-07 15:32:37 +0000201 fs.writeFileSync(contentShellZipPath, buffer);
202 return contentShellZipPath;
203 }
204
205 function onError(error) {
206 console.log(`Received error: ${error} trying to download content shell from url: ${url}`);
207 throw new Error('Unable to download content shell');
208 }
209}
210
211function extractContentShell(contentShellZipPath) {
212 console.log(`Extracting content shell zip: ${contentShellZipPath}`);
Mathias Bynens98669992019-11-28 08:50:08 +0100213 const unzipScriptPath = path.resolve(__dirname, 'unzip.py');
214 const src = contentShellZipPath;
215 const dest = path.resolve(path.dirname(src), 'out');
Blink Reformat4c46d092018-04-07 15:32:37 +0000216 shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`);
217 fs.unlinkSync(src);
Mathias Bynens98669992019-11-28 08:50:08 +0100218 const originalDirPath = path.resolve(dest, 'content-shell');
219 const newDirPath = path.resolve(dest, TARGET);
Blink Reformat4c46d092018-04-07 15:32:37 +0000220 fs.renameSync(originalDirPath, newDirPath);
221 fs.chmodSync(getContentShellBinaryPath(newDirPath), '755');
222 if (process.platform === 'darwin') {
Mathias Bynens98669992019-11-28 08:50:08 +0100223 const helperPath = path.resolve(
Blink Reformat4c46d092018-04-07 15:32:37 +0000224 newDirPath, 'Content Shell.app', 'Contents', 'Frameworks', 'Content Shell Helper.app', 'Contents', 'MacOS',
225 'Content Shell Helper');
226 fs.chmodSync(helperPath, '755');
227 }
228 return dest;
229}
230
231function getContentShellBinaryPath(dirPath) {
232 if (process.platform === 'linux')
233 return path.resolve(dirPath, 'content_shell');
234
235 if (process.platform === 'win32')
236 return path.resolve(dirPath, 'content_shell.exe');
237
238 if (process.platform === 'darwin')
239 return path.resolve(dirPath, 'Content Shell.app', 'Contents', 'MacOS', 'Content Shell');
240}
241
242function runTests(buildDirectoryPath, useDebugDevtools) {
Mathias Bynens98669992019-11-28 08:50:08 +0100243 const testArgs = getInspectorTests().concat([
Blink Reformat4c46d092018-04-07 15:32:37 +0000244 '--build-directory',
245 buildDirectoryPath,
246 '--target',
247 TARGET,
248 ]);
249 if (useDebugDevtools)
250 testArgs.push('--additional-driver-flag=--debug-devtools');
251 else
252 console.log('TIP: You can debug a test using: npm run debug-test inspector/test-name.html');
253
254 if (IS_DEBUG_ENABLED) {
255 testArgs.push('--additional-driver-flag=--remote-debugging-port=9222');
256 testArgs.push('--time-out-ms=6000000');
257 console.log('\n=============================================');
Mathias Bynens98669992019-11-28 08:50:08 +0100258 const unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/'));
Blink Reformat4c46d092018-04-07 15:32:37 +0000259 if (unitTest) {
Mathias Bynens98669992019-11-28 08:50:08 +0100260 const unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`;
261 const link =
Blink Reformat4c46d092018-04-07 15:32:37 +0000262 `http://localhost:8080/inspector-sources/debug/integration_test_runner.html?experiments=true&test=${
263 unitTestPath
264 }`;
265 console.log('1) Go to: ', link);
266 console.log('2) Go to: http://localhost:9222/, click on "inspected-page.html", and copy the ws query parameter');
267 console.log('3) Open DevTools on DevTools and you can refresh to re-run the test')
268 } else {
269 console.log('Go to: http://localhost:9222/');
270 console.log('Click on link and in console execute: test()');
271 }
272 console.log('=============================================\n');
273 }
Mathias Bynens98669992019-11-28 08:50:08 +0100274 const args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags());
Kent Tamurad3d3f042018-12-12 02:45:28 +0000275 console.log(`Running web tests with args: ${args}`);
Blink Reformat4c46d092018-04-07 15:32:37 +0000276 childProcess.spawn(PYTHON, args, {stdio: 'inherit'});
277}
278
279function getTestFlags() {
Mathias Bynens98669992019-11-28 08:50:08 +0100280 const flagValues = Object.keys(Flags).map(key => Flags[key]);
Blink Reformat4c46d092018-04-07 15:32:37 +0000281 return process.argv.slice(2).filter(arg => {
Mathias Bynens98669992019-11-28 08:50:08 +0100282 const flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg;
Blink Reformat4c46d092018-04-07 15:32:37 +0000283 return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') &&
284 !utils.includes(arg, 'http/tests/devtools');
285 });
286}
287
288function getInspectorTests() {
Mathias Bynens98669992019-11-28 08:50:08 +0100289 const specificTests =
Blink Reformat4c46d092018-04-07 15:32:37 +0000290 process.argv.filter(arg => utils.includes(arg, 'inspector') || utils.includes(arg, 'http/tests/devtools'));
291 if (specificTests.length)
292 return specificTests;
293 return [
294 'inspector*',
295 'http/tests/inspector*',
296 'http/tests/devtools',
297 ];
Kent Tamurad3d3f042018-12-12 02:45:28 +0000298}