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 | |
| 5 | var childProcess = require('child_process'); |
| 6 | var fs = require('fs'); |
| 7 | var path = require('path'); |
| 8 | var shell = require('child_process').execSync; |
| 9 | |
| 10 | var utils = require('./utils'); |
| 11 | |
| 12 | var Flags = { |
| 13 | DEBUG_DEVTOOLS: '--debug-devtools', |
| 14 | DEBUG_DEVTOOLS_SHORTHAND: '-d', |
| 15 | FETCH_CONTENT_SHELL: '--fetch-content-shell', |
| 16 | CHROMIUM_PATH: '--chromium-path', // useful for bisecting |
| 17 | TARGET: '--target', // build sub-directory (e.g. Release, Default) |
| 18 | }; |
| 19 | |
| 20 | var IS_DEBUG_ENABLED = |
| 21 | utils.includes(process.argv, Flags.DEBUG_DEVTOOLS) || utils.includes(process.argv, Flags.DEBUG_DEVTOOLS_SHORTHAND); |
| 22 | var CUSTOM_CHROMIUM_PATH = utils.parseArgs(process.argv)[Flags.CHROMIUM_PATH]; |
| 23 | var IS_FETCH_CONTENT_SHELL = utils.includes(process.argv, Flags.FETCH_CONTENT_SHELL); |
| 24 | var TARGET = utils.parseArgs(process.argv)[Flags.TARGET] || 'Release'; |
| 25 | |
| 26 | var CONTENT_SHELL_ZIP = 'content-shell.zip'; |
| 27 | var MAX_CONTENT_SHELLS = 10; |
| 28 | var PLATFORM = getPlatform(); |
| 29 | var PYTHON = process.platform === 'win32' ? 'python.bat' : 'python'; |
| 30 | |
| 31 | var CHROMIUM_SRC_PATH = CUSTOM_CHROMIUM_PATH || path.resolve(__dirname, '..', '..', '..', '..', '..'); |
| 32 | var RELEASE_PATH = path.resolve(CHROMIUM_SRC_PATH, 'out', TARGET); |
| 33 | var BLINK_TEST_PATH = path.resolve(CHROMIUM_SRC_PATH, 'blink', 'tools', 'run_layout_tests.py'); |
| 34 | var DEVTOOLS_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'WebKit', 'Source', 'devtools'); |
| 35 | var CACHE_PATH = path.resolve(DEVTOOLS_PATH, '.test_cache'); |
| 36 | var SOURCE_PATH = path.resolve(DEVTOOLS_PATH, 'front_end'); |
| 37 | |
| 38 | function main() { |
| 39 | if (!utils.isDir(CACHE_PATH)) |
| 40 | fs.mkdirSync(CACHE_PATH); |
| 41 | deleteOldContentShells(); |
| 42 | |
| 43 | var hasUserCompiledContentShell = utils.isFile(getContentShellBinaryPath(RELEASE_PATH)); |
| 44 | if (!IS_FETCH_CONTENT_SHELL && hasUserCompiledContentShell) { |
| 45 | var outDir = path.resolve(RELEASE_PATH, '..'); |
| 46 | if (!IS_DEBUG_ENABLED) |
| 47 | compileFrontend(); |
| 48 | |
| 49 | runTests(outDir, IS_DEBUG_ENABLED); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | findPreviousUploadedPosition(findMostRecentChromiumCommit()).then(onUploadedCommitPosition).catch(onError); |
| 54 | |
| 55 | function onError(error) { |
| 56 | console.log('Unable to run tests because of error:', error); |
| 57 | console.log(`Try removing the .test_cache folder [${CACHE_PATH}] and retrying`); |
| 58 | } |
| 59 | } |
| 60 | main(); |
| 61 | |
| 62 | function compileFrontend() { |
| 63 | console.log('Compiling devtools frontend'); |
| 64 | try { |
| 65 | shell(`ninja -C ${RELEASE_PATH} devtools_frontend_resources`, {cwd: CHROMIUM_SRC_PATH}); |
| 66 | } catch (err) { |
| 67 | console.log(err.stdout.toString()); |
| 68 | console.log('ERROR: Cannot compile frontend\n' + err); |
| 69 | process.exit(1); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | function onUploadedCommitPosition(commitPosition) { |
| 74 | var contentShellDirPath = path.resolve(CACHE_PATH, commitPosition, 'out', TARGET); |
| 75 | var contentShellResourcesPath = path.resolve(contentShellDirPath, 'resources'); |
| 76 | var contentShellPath = path.resolve(CACHE_PATH, commitPosition, 'out'); |
| 77 | |
| 78 | var hasCachedContentShell = utils.isFile(getContentShellBinaryPath(contentShellDirPath)); |
| 79 | if (hasCachedContentShell) { |
| 80 | console.log(`Using cached content shell at: ${contentShellPath}`); |
| 81 | copyFrontend(contentShellResourcesPath); |
| 82 | return runTests(contentShellPath, true); |
| 83 | } |
| 84 | var url = `http://commondatastorage.googleapis.com/chromium-browser-snapshots/${PLATFORM}/${commitPosition |
| 85 | }/${CONTENT_SHELL_ZIP}`; |
| 86 | return prepareContentShellDirectory(commitPosition) |
| 87 | .then(() => downloadContentShell(url, commitPosition)) |
| 88 | .then(extractContentShell) |
| 89 | .then(() => copyFrontend(contentShellResourcesPath)) |
| 90 | .then(() => runTests(contentShellPath, true)); |
| 91 | } |
| 92 | |
| 93 | function copyFrontend(contentShellResourcesPath) { |
| 94 | var devtoolsResourcesPath = path.resolve(contentShellResourcesPath, 'inspector'); |
| 95 | var copiedFrontendPath = path.resolve(devtoolsResourcesPath, 'front_end'); |
| 96 | var debugFrontendPath = path.resolve(devtoolsResourcesPath, 'debug'); |
| 97 | var inspectorBackendCommandsPath = path.resolve(devtoolsResourcesPath, 'InspectorBackendCommands.js'); |
| 98 | var supportedCSSPropertiesPath = path.resolve(devtoolsResourcesPath, 'SupportedCSSProperties.js'); |
| 99 | utils.removeRecursive(copiedFrontendPath); |
| 100 | utils.removeRecursive(debugFrontendPath); |
| 101 | utils.copyRecursive(SOURCE_PATH, devtoolsResourcesPath); |
| 102 | fs.renameSync(copiedFrontendPath, debugFrontendPath); |
| 103 | utils.copy(inspectorBackendCommandsPath, debugFrontendPath); |
| 104 | utils.copy(supportedCSSPropertiesPath, debugFrontendPath); |
| 105 | } |
| 106 | |
| 107 | function getPlatform() { |
| 108 | if (process.platform === 'linux') { |
| 109 | return 'Linux_x64'; |
| 110 | } |
| 111 | if (process.platform === 'win32') { |
| 112 | return 'Win_x64'; |
| 113 | } |
| 114 | if (process.platform === 'darwin') |
| 115 | return 'Mac'; |
| 116 | |
| 117 | throw new Error(`Unrecognized platform detected: ${process.platform}`); |
| 118 | } |
| 119 | |
| 120 | function findMostRecentChromiumCommit() { |
| 121 | var commitMessage = shell(`git log --max-count=1 --grep="Cr-Commit-Position"`).toString().trim(); |
| 122 | var commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1]; |
| 123 | return commitPosition; |
| 124 | } |
| 125 | |
| 126 | function deleteOldContentShells() { |
| 127 | var files = fs.readdirSync(CACHE_PATH); |
| 128 | if (files.length < MAX_CONTENT_SHELLS) |
| 129 | return; |
| 130 | files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10)); |
| 131 | var remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2; |
| 132 | var oldContentShellDirs = files.slice(remainingNumberOfContentShells); |
| 133 | for (var i = 0; i < oldContentShellDirs.length; i++) |
| 134 | utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i])); |
| 135 | console.log(`Removed old content shells: ${oldContentShellDirs}`); |
| 136 | } |
| 137 | |
| 138 | function findPreviousUploadedPosition(commitPosition) { |
| 139 | var previousPosition = commitPosition - 100; |
| 140 | var positionsListURL = |
| 141 | `http://commondatastorage.googleapis.com/chromium-browser-snapshots/?delimiter=/&prefix=${PLATFORM |
| 142 | }/&marker=${PLATFORM}/${previousPosition}/`; |
| 143 | return utils.fetch(positionsListURL).then(onPositionsList).catch(onError); |
| 144 | |
| 145 | function onPositionsList(buffer) { |
| 146 | var positions = buffer.toString('binary') |
| 147 | .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g) |
| 148 | .map(prefixedPosition => prefixedPosition.split('/')[1]) |
| 149 | .map(positionString => parseInt(positionString, 10)); |
| 150 | var positionSet = new Set(positions); |
| 151 | var previousUploadedPosition = commitPosition; |
| 152 | while (commitPosition - previousUploadedPosition < 100) { |
| 153 | if (positionSet.has(previousUploadedPosition)) |
| 154 | return previousUploadedPosition.toString(); |
| 155 | previousUploadedPosition--; |
| 156 | } |
| 157 | onError(); |
| 158 | } |
| 159 | |
| 160 | function onError(error) { |
| 161 | if (error) |
| 162 | console.log(`Received error: ${error} trying to fetch positions list from url: ${positionsListURL}`); |
| 163 | throw new Error(`Unable to find a previous upload position for commit position: ${commitPosition}`); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | function prepareContentShellDirectory(folder) { |
| 168 | var contentShellPath = path.join(CACHE_PATH, folder); |
| 169 | if (utils.isDir(contentShellPath)) |
| 170 | utils.removeRecursive(contentShellPath); |
| 171 | fs.mkdirSync(contentShellPath); |
| 172 | return Promise.resolve(folder); |
| 173 | } |
| 174 | |
| 175 | function downloadContentShell(url, folder) { |
| 176 | console.log('Downloading content shell from:', url); |
| 177 | console.log('NOTE: Download is ~35-65 MB depending on OS'); |
| 178 | return utils.fetch(url).then(writeZip).catch(onError); |
| 179 | |
| 180 | function writeZip(buffer) { |
| 181 | console.log('Completed download of content shell'); |
| 182 | var contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP); |
| 183 | fs.writeFileSync(contentShellZipPath, buffer); |
| 184 | return contentShellZipPath; |
| 185 | } |
| 186 | |
| 187 | function onError(error) { |
| 188 | console.log(`Received error: ${error} trying to download content shell from url: ${url}`); |
| 189 | throw new Error('Unable to download content shell'); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | function extractContentShell(contentShellZipPath) { |
| 194 | console.log(`Extracting content shell zip: ${contentShellZipPath}`); |
| 195 | var unzipScriptPath = path.resolve(__dirname, 'unzip.py'); |
| 196 | var src = contentShellZipPath; |
| 197 | var dest = path.resolve(path.dirname(src), 'out'); |
| 198 | shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`); |
| 199 | fs.unlinkSync(src); |
| 200 | var originalDirPath = path.resolve(dest, 'content-shell'); |
| 201 | var newDirPath = path.resolve(dest, TARGET); |
| 202 | fs.renameSync(originalDirPath, newDirPath); |
| 203 | fs.chmodSync(getContentShellBinaryPath(newDirPath), '755'); |
| 204 | if (process.platform === 'darwin') { |
| 205 | var helperPath = path.resolve( |
| 206 | newDirPath, 'Content Shell.app', 'Contents', 'Frameworks', 'Content Shell Helper.app', 'Contents', 'MacOS', |
| 207 | 'Content Shell Helper'); |
| 208 | fs.chmodSync(helperPath, '755'); |
| 209 | } |
| 210 | return dest; |
| 211 | } |
| 212 | |
| 213 | function getContentShellBinaryPath(dirPath) { |
| 214 | if (process.platform === 'linux') |
| 215 | return path.resolve(dirPath, 'content_shell'); |
| 216 | |
| 217 | if (process.platform === 'win32') |
| 218 | return path.resolve(dirPath, 'content_shell.exe'); |
| 219 | |
| 220 | if (process.platform === 'darwin') |
| 221 | return path.resolve(dirPath, 'Content Shell.app', 'Contents', 'MacOS', 'Content Shell'); |
| 222 | } |
| 223 | |
| 224 | function runTests(buildDirectoryPath, useDebugDevtools) { |
| 225 | var testArgs = getInspectorTests().concat([ |
| 226 | '--no-pixel-tests', |
| 227 | '--build-directory', |
| 228 | buildDirectoryPath, |
| 229 | '--target', |
| 230 | TARGET, |
| 231 | ]); |
| 232 | if (useDebugDevtools) |
| 233 | testArgs.push('--additional-driver-flag=--debug-devtools'); |
| 234 | else |
| 235 | console.log('TIP: You can debug a test using: npm run debug-test inspector/test-name.html'); |
| 236 | |
| 237 | if (IS_DEBUG_ENABLED) { |
| 238 | testArgs.push('--additional-driver-flag=--remote-debugging-port=9222'); |
| 239 | testArgs.push('--time-out-ms=6000000'); |
| 240 | console.log('\n============================================='); |
| 241 | var unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/')); |
| 242 | if (unitTest) { |
| 243 | var unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`; |
| 244 | var link = |
| 245 | `http://localhost:8080/inspector-sources/debug/integration_test_runner.html?experiments=true&test=${ |
| 246 | unitTestPath |
| 247 | }`; |
| 248 | console.log('1) Go to: ', link); |
| 249 | console.log('2) Go to: http://localhost:9222/, click on "inspected-page.html", and copy the ws query parameter'); |
| 250 | console.log('3) Open DevTools on DevTools and you can refresh to re-run the test') |
| 251 | } else { |
| 252 | console.log('Go to: http://localhost:9222/'); |
| 253 | console.log('Click on link and in console execute: test()'); |
| 254 | } |
| 255 | console.log('=============================================\n'); |
| 256 | } |
| 257 | var args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags()); |
| 258 | console.log(`Running layout tests with args: ${args}`); |
| 259 | childProcess.spawn(PYTHON, args, {stdio: 'inherit'}); |
| 260 | } |
| 261 | |
| 262 | function getTestFlags() { |
| 263 | var flagValues = Object.keys(Flags).map(key => Flags[key]); |
| 264 | return process.argv.slice(2).filter(arg => { |
| 265 | var flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg; |
| 266 | return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') && |
| 267 | !utils.includes(arg, 'http/tests/devtools'); |
| 268 | }); |
| 269 | } |
| 270 | |
| 271 | function getInspectorTests() { |
| 272 | var specificTests = |
| 273 | process.argv.filter(arg => utils.includes(arg, 'inspector') || utils.includes(arg, 'http/tests/devtools')); |
| 274 | if (specificTests.length) |
| 275 | return specificTests; |
| 276 | return [ |
| 277 | 'inspector*', |
| 278 | 'http/tests/inspector*', |
| 279 | 'http/tests/devtools', |
| 280 | ]; |
| 281 | } |