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'); |
| 100 | const inspectorBackendCommandsPath = path.resolve(devtoolsResourcesPath, 'InspectorBackendCommands.js'); |
| 101 | const supportedCSSPropertiesPath = path.resolve(devtoolsResourcesPath, 'SupportedCSSProperties.js'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 102 | 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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 110 | function 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 125 | function 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 | |
| 138 | function findMostRecentChromiumCommit() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 139 | 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 141 | return commitPosition; |
| 142 | } |
| 143 | |
| 144 | function deleteOldContentShells() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 145 | const files = fs.readdirSync(CACHE_PATH); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 146 | if (files.length < MAX_CONTENT_SHELLS) |
| 147 | return; |
| 148 | files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10)); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 149 | const remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2; |
| 150 | const oldContentShellDirs = files.slice(remainingNumberOfContentShells); |
| 151 | for (let i = 0; i < oldContentShellDirs.length; i++) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 152 | utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i])); |
| 153 | console.log(`Removed old content shells: ${oldContentShellDirs}`); |
| 154 | } |
| 155 | |
| 156 | function findPreviousUploadedPosition(commitPosition) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 157 | const previousPosition = commitPosition - 100; |
| 158 | const positionsListURL = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 159 | `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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 164 | const positions = buffer.toString('binary') |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 165 | .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g) |
| 166 | .map(prefixedPosition => prefixedPosition.split('/')[1]) |
| 167 | .map(positionString => parseInt(positionString, 10)); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 168 | const positionSet = new Set(positions); |
| 169 | let previousUploadedPosition = commitPosition; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 170 | 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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 185 | async function prepareContentShellDirectory(folder) { |
| 186 | const contentShellPath = path.join(CACHE_PATH, folder); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 187 | if (utils.isDir(contentShellPath)) |
| 188 | utils.removeRecursive(contentShellPath); |
| 189 | fs.mkdirSync(contentShellPath); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 190 | return folder; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | function 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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 200 | const contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 201 | 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 | |
| 211 | function extractContentShell(contentShellZipPath) { |
| 212 | console.log(`Extracting content shell zip: ${contentShellZipPath}`); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 213 | const unzipScriptPath = path.resolve(__dirname, 'unzip.py'); |
| 214 | const src = contentShellZipPath; |
| 215 | const dest = path.resolve(path.dirname(src), 'out'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 216 | shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`); |
| 217 | fs.unlinkSync(src); |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 218 | const originalDirPath = path.resolve(dest, 'content-shell'); |
| 219 | const newDirPath = path.resolve(dest, TARGET); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 220 | fs.renameSync(originalDirPath, newDirPath); |
| 221 | fs.chmodSync(getContentShellBinaryPath(newDirPath), '755'); |
| 222 | if (process.platform === 'darwin') { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 223 | const helperPath = path.resolve( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 224 | 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 | |
| 231 | function 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 | |
| 242 | function runTests(buildDirectoryPath, useDebugDevtools) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 243 | const testArgs = getInspectorTests().concat([ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 244 | '--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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 258 | const unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 259 | if (unitTest) { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 260 | const unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`; |
| 261 | const link = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 262 | `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 Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 274 | const args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags()); |
Mathias Bynens | b0693f2 | 2020-01-23 13:17:15 +0100 | [diff] [blame] | 275 | console.log(`Running web tests with args: ${args.join(' ')}`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 276 | childProcess.spawn(PYTHON, args, {stdio: 'inherit'}); |
| 277 | } |
| 278 | |
| 279 | function getTestFlags() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 280 | const flagValues = Object.keys(Flags).map(key => Flags[key]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 281 | return process.argv.slice(2).filter(arg => { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 282 | const flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 283 | return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') && |
| 284 | !utils.includes(arg, 'http/tests/devtools'); |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | function getInspectorTests() { |
Mathias Bynens | 9866999 | 2019-11-28 08:50:08 +0100 | [diff] [blame] | 289 | const specificTests = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 290 | 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 Tamura | d3d3f04 | 2018-12-12 02:45:28 +0000 | [diff] [blame] | 298 | } |