Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | // 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 Guo | b7a4426 | 2019-11-05 15:25:23 +0100 | [diff] [blame] | 5 | const childProcess = require('child_process'); |
| 6 | const fs = require('fs'); |
| 7 | const path = require('path'); |
| 8 | const shell = require('child_process').execSync; |
| 9 | const utils = require('./utils'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 10 | |
Yang Guo | b7a4426 | 2019-11-05 15:25:23 +0100 | [diff] [blame] | 11 | const Flags = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 12 | 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 Guo | b7a4426 | 2019-11-05 15:25:23 +0100 | [diff] [blame] | 19 | const IS_DEBUG_ENABLED = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 20 | utils.includes(process.argv, Flags.DEBUG_DEVTOOLS) || utils.includes(process.argv, Flags.DEBUG_DEVTOOLS_SHORTHAND); |
Yang Guo | b7a4426 | 2019-11-05 15:25:23 +0100 | [diff] [blame] | 21 | const CUSTOM_CHROMIUM_PATH = utils.parseArgs(process.argv)[Flags.CHROMIUM_PATH]; |
| 22 | const IS_FETCH_CONTENT_SHELL = utils.includes(process.argv, Flags.FETCH_CONTENT_SHELL); |
| 23 | const TARGET = utils.parseArgs(process.argv)[Flags.TARGET] || 'Release'; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 24 | |
Yang Guo | b7a4426 | 2019-11-05 15:25:23 +0100 | [diff] [blame] | 25 | const CONTENT_SHELL_ZIP = 'content-shell.zip'; |
| 26 | const MAX_CONTENT_SHELLS = 10; |
| 27 | const PLATFORM = getPlatform(); |
| 28 | const PYTHON = process.platform === 'win32' ? 'python.bat' : 'python'; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 29 | |
Yang Guo | b7a4426 | 2019-11-05 15:25:23 +0100 | [diff] [blame] | 30 | const CURRENT_PATH = process.env.PWD; // Using env.PWD to account for symlinks. |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 31 | const isThirdParty = CURRENT_PATH.includes('third_party'); |
| 32 | const CHROMIUM_SRC_PATH = CUSTOM_CHROMIUM_PATH || getChromiumSrcPath(isThirdParty); |
Yang Guo | b7a4426 | 2019-11-05 15:25:23 +0100 | [diff] [blame] | 33 | const RELEASE_PATH = path.resolve(CHROMIUM_SRC_PATH, 'out', TARGET); |
| 34 | const BLINK_TEST_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'blink', 'tools', 'run_web_tests.py'); |
| 35 | const DEVTOOLS_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'devtools-frontend', 'src'); |
| 36 | const CACHE_PATH = path.resolve(DEVTOOLS_PATH, '.test_cache'); |
| 37 | const SOURCE_PATH = path.resolve(DEVTOOLS_PATH, 'front_end'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 38 | |
| 39 | function main() { |
| 40 | if (!utils.isDir(CACHE_PATH)) |
| 41 | fs.mkdirSync(CACHE_PATH); |
| 42 | deleteOldContentShells(); |
| 43 | |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 44 | const hasUserCompiledContentShell = utils.isFile(getContentShellBinaryPath(RELEASE_PATH)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 45 | if (!IS_FETCH_CONTENT_SHELL && hasUserCompiledContentShell) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 46 | const outDir = path.resolve(RELEASE_PATH, '..'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 47 | if (!IS_DEBUG_ENABLED) |
| 48 | compileFrontend(); |
| 49 | |
| 50 | runTests(outDir, IS_DEBUG_ENABLED); |
| 51 | return; |
| 52 | } |
| 53 | |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 54 | findPreviousUploadedPosition(findMostRecentChromiumCommit()) |
| 55 | .then(onUploadedCommitPosition) |
| 56 | .catch(onError); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 57 | |
| 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 | } |
| 63 | main(); |
| 64 | |
| 65 | function 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 | |
| 76 | function onUploadedCommitPosition(commitPosition) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 77 | 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 80 | |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 81 | const hasCachedContentShell = utils.isFile(getContentShellBinaryPath(contentShellDirPath)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 82 | if (hasCachedContentShell) { |
| 83 | console.log(`Using cached content shell at: ${contentShellPath}`); |
| 84 | copyFrontend(contentShellResourcesPath); |
| 85 | return runTests(contentShellPath, true); |
| 86 | } |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 87 | const url = `http://commondatastorage.googleapis.com/chromium-browser-snapshots/${PLATFORM}/${commitPosition |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 88 | }/${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 | |
| 96 | function copyFrontend(contentShellResourcesPath) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 97 | const devtoolsResourcesPath = path.resolve(contentShellResourcesPath, 'inspector'); |
| 98 | const copiedFrontendPath = path.resolve(devtoolsResourcesPath, 'front_end'); |
| 99 | const debugFrontendPath = path.resolve(devtoolsResourcesPath, 'debug'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 100 | utils.removeRecursive(copiedFrontendPath); |
| 101 | utils.removeRecursive(debugFrontendPath); |
| 102 | utils.copyRecursive(SOURCE_PATH, devtoolsResourcesPath); |
| 103 | fs.renameSync(copiedFrontendPath, debugFrontendPath); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 106 | function 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 121 | function 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 | |
| 134 | function findMostRecentChromiumCommit() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 135 | 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 137 | return commitPosition; |
| 138 | } |
| 139 | |
| 140 | function deleteOldContentShells() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 141 | const files = fs.readdirSync(CACHE_PATH); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 142 | if (files.length < MAX_CONTENT_SHELLS) |
| 143 | return; |
| 144 | files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10)); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 145 | const remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2; |
| 146 | const oldContentShellDirs = files.slice(remainingNumberOfContentShells); |
| 147 | for (let i = 0; i < oldContentShellDirs.length; i++) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 148 | utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i])); |
| 149 | console.log(`Removed old content shells: ${oldContentShellDirs}`); |
| 150 | } |
| 151 | |
| 152 | function findPreviousUploadedPosition(commitPosition) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 153 | const previousPosition = commitPosition - 100; |
| 154 | const positionsListURL = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 155 | `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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 160 | const positions = buffer.toString('binary') |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 161 | .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g) |
| 162 | .map(prefixedPosition => prefixedPosition.split('/')[1]) |
| 163 | .map(positionString => parseInt(positionString, 10)); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 164 | const positionSet = new Set(positions); |
| 165 | let previousUploadedPosition = commitPosition; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 166 | 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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 181 | async function prepareContentShellDirectory(folder) { |
| 182 | const contentShellPath = path.join(CACHE_PATH, folder); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 183 | if (utils.isDir(contentShellPath)) |
| 184 | utils.removeRecursive(contentShellPath); |
| 185 | fs.mkdirSync(contentShellPath); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 186 | return folder; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | function 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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 196 | const contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 197 | 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 | |
| 207 | function extractContentShell(contentShellZipPath) { |
| 208 | console.log(`Extracting content shell zip: ${contentShellZipPath}`); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 209 | const unzipScriptPath = path.resolve(__dirname, 'unzip.py'); |
| 210 | const src = contentShellZipPath; |
| 211 | const dest = path.resolve(path.dirname(src), 'out'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 212 | shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`); |
| 213 | fs.unlinkSync(src); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 214 | const originalDirPath = path.resolve(dest, 'content-shell'); |
| 215 | const newDirPath = path.resolve(dest, TARGET); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 216 | fs.renameSync(originalDirPath, newDirPath); |
| 217 | fs.chmodSync(getContentShellBinaryPath(newDirPath), '755'); |
| 218 | if (process.platform === 'darwin') { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 219 | const helperPath = path.resolve( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 220 | 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 | |
| 227 | function 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 | |
| 238 | function runTests(buildDirectoryPath, useDebugDevtools) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 239 | const testArgs = getInspectorTests().concat([ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 240 | '--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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 254 | const unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 255 | if (unitTest) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 256 | const unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`; |
| 257 | const link = |
Yang Guo | 49346f1 | 2020-02-06 10:52:02 +0100 | [diff] [blame] | 258 | `http://localhost:8080/inspector-sources/debug/integration_test_runner.html?test=${unitTestPath}`; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 259 | 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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 268 | const args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags()); |
Mathias Bynens | b0693f2 | 2020-01-23 13:17:15 +0100 | [diff] [blame] | 269 | console.log(`Running web tests with args: ${args.join(' ')}`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 270 | childProcess.spawn(PYTHON, args, {stdio: 'inherit'}); |
| 271 | } |
| 272 | |
| 273 | function getTestFlags() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 274 | const flagValues = Object.keys(Flags).map(key => Flags[key]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 275 | return process.argv.slice(2).filter(arg => { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 276 | const flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 277 | return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') && |
| 278 | !utils.includes(arg, 'http/tests/devtools'); |
| 279 | }); |
| 280 | } |
| 281 | |
| 282 | function getInspectorTests() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 283 | const specificTests = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 284 | 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 Tamura | d3d3f04 | 2018-12-12 02:45:28 +0000 | [diff] [blame] | 292 | } |