Fix `npm test` when run from the devtools/devtools-frontend repo

The script previously assumed it would always be run from within the
chromium/src/third_party/devtools-frontend/src folder. It would fail
when run from the standalone repository at devtools/devtools-frontend.

This patch makes it work even in that case, as long as the `devtools`
directory in `devtools/devtools-frontend` lives on the same level as
the `chromium` repository.

Change-Id: I3cf81c8144fd3f03389a4ec395b8ec3acdbb8e55
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1939798
Reviewed-by: Yang Guo <yangguo@chromium.org>
Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
diff --git a/scripts/npm_test.js b/scripts/npm_test.js
index 185225c..e332287 100644
--- a/scripts/npm_test.js
+++ b/scripts/npm_test.js
@@ -28,7 +28,8 @@
 const PYTHON = process.platform === 'win32' ? 'python.bat' : 'python';
 
 const CURRENT_PATH = process.env.PWD;  // Using env.PWD to account for symlinks.
-const CHROMIUM_SRC_PATH = CUSTOM_CHROMIUM_PATH || path.resolve(CURRENT_PATH, '..', '..', '..');
+const isThirdParty = CURRENT_PATH.includes('third_party');
+const CHROMIUM_SRC_PATH = CUSTOM_CHROMIUM_PATH || getChromiumSrcPath(isThirdParty);
 const RELEASE_PATH = path.resolve(CHROMIUM_SRC_PATH, 'out', TARGET);
 const BLINK_TEST_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'blink', 'tools', 'run_web_tests.py');
 const DEVTOOLS_PATH = path.resolve(CHROMIUM_SRC_PATH, 'third_party', 'devtools-frontend', 'src');
@@ -40,9 +41,9 @@
     fs.mkdirSync(CACHE_PATH);
   deleteOldContentShells();
 
-  var hasUserCompiledContentShell = utils.isFile(getContentShellBinaryPath(RELEASE_PATH));
+  const hasUserCompiledContentShell = utils.isFile(getContentShellBinaryPath(RELEASE_PATH));
   if (!IS_FETCH_CONTENT_SHELL && hasUserCompiledContentShell) {
-    var outDir = path.resolve(RELEASE_PATH, '..');
+    const outDir = path.resolve(RELEASE_PATH, '..');
     if (!IS_DEBUG_ENABLED)
       compileFrontend();
 
@@ -50,7 +51,9 @@
     return;
   }
 
-  findPreviousUploadedPosition(findMostRecentChromiumCommit()).then(onUploadedCommitPosition).catch(onError);
+  findPreviousUploadedPosition(findMostRecentChromiumCommit())
+    .then(onUploadedCommitPosition)
+    .catch(onError);
 
   function onError(error) {
     console.log('Unable to run tests because of error:', error);
@@ -71,17 +74,17 @@
 }
 
 function onUploadedCommitPosition(commitPosition) {
-  var contentShellDirPath = path.resolve(CACHE_PATH, commitPosition, 'out', TARGET);
-  var contentShellResourcesPath = path.resolve(contentShellDirPath, 'resources');
-  var contentShellPath = path.resolve(CACHE_PATH, commitPosition, 'out');
+  const contentShellDirPath = path.resolve(CACHE_PATH, commitPosition, 'out', TARGET);
+  const contentShellResourcesPath = path.resolve(contentShellDirPath, 'resources');
+  const contentShellPath = path.resolve(CACHE_PATH, commitPosition, 'out');
 
-  var hasCachedContentShell = utils.isFile(getContentShellBinaryPath(contentShellDirPath));
+  const hasCachedContentShell = utils.isFile(getContentShellBinaryPath(contentShellDirPath));
   if (hasCachedContentShell) {
     console.log(`Using cached content shell at: ${contentShellPath}`);
     copyFrontend(contentShellResourcesPath);
     return runTests(contentShellPath, true);
   }
-  var url = `http://commondatastorage.googleapis.com/chromium-browser-snapshots/${PLATFORM}/${commitPosition
+  const url = `http://commondatastorage.googleapis.com/chromium-browser-snapshots/${PLATFORM}/${commitPosition
   }/${CONTENT_SHELL_ZIP}`;
   return prepareContentShellDirectory(commitPosition)
       .then(() => downloadContentShell(url, commitPosition))
@@ -91,11 +94,11 @@
 }
 
 function copyFrontend(contentShellResourcesPath) {
-  var devtoolsResourcesPath = path.resolve(contentShellResourcesPath, 'inspector');
-  var copiedFrontendPath = path.resolve(devtoolsResourcesPath, 'front_end');
-  var debugFrontendPath = path.resolve(devtoolsResourcesPath, 'debug');
-  var inspectorBackendCommandsPath = path.resolve(devtoolsResourcesPath, 'InspectorBackendCommands.js');
-  var supportedCSSPropertiesPath = path.resolve(devtoolsResourcesPath, 'SupportedCSSProperties.js');
+  const devtoolsResourcesPath = path.resolve(contentShellResourcesPath, 'inspector');
+  const copiedFrontendPath = path.resolve(devtoolsResourcesPath, 'front_end');
+  const debugFrontendPath = path.resolve(devtoolsResourcesPath, 'debug');
+  const inspectorBackendCommandsPath = path.resolve(devtoolsResourcesPath, 'InspectorBackendCommands.js');
+  const supportedCSSPropertiesPath = path.resolve(devtoolsResourcesPath, 'SupportedCSSProperties.js');
   utils.removeRecursive(copiedFrontendPath);
   utils.removeRecursive(debugFrontendPath);
   utils.copyRecursive(SOURCE_PATH, devtoolsResourcesPath);
@@ -104,6 +107,21 @@
   utils.copy(supportedCSSPropertiesPath, debugFrontendPath);
 }
 
+function getChromiumSrcPath(isThirdParty) {
+  if (isThirdParty)
+    // Assume we're in `chromium/src/third_party/devtools-frontend/src`.
+    return path.resolve(CURRENT_PATH, '..', '..', '..');
+  // Assume we're in `devtools/devtools-frontend`, where `devtools` is
+  // on the same level as `chromium`.
+  const srcPath = path.resolve(CURRENT_PATH, '..', '..', 'chromium', 'src');
+  if (!utils.isDir(srcPath))
+    throw new Error(`Chromium source directory not found at \`${srcPath}\`. ` +
+        `Either move your standalone \`devtools/devtools-frontend\` checkout ` +
+        `so that \`devtools\` is at the same level as \`chromium\` in ` +
+        `\`chromium/src\`, or use \`--chromium-path\`.`);
+  return srcPath;
+}
+
 function getPlatform() {
   if (process.platform === 'linux') {
     return 'Linux_x64';
@@ -118,37 +136,37 @@
 }
 
 function findMostRecentChromiumCommit() {
-  var commitMessage = shell(`git log --max-count=1 --grep="Cr-Commit-Position"`).toString().trim();
-  var commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1];
+  const commitMessage = shell(`git log --max-count=1 --grep="Cr-Commit-Position"`).toString().trim();
+  const commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1];
   return commitPosition;
 }
 
 function deleteOldContentShells() {
-  var files = fs.readdirSync(CACHE_PATH);
+  const files = fs.readdirSync(CACHE_PATH);
   if (files.length < MAX_CONTENT_SHELLS)
     return;
   files.sort((a, b) => parseInt(b, 10) - parseInt(a, 10));
-  var remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2;
-  var oldContentShellDirs = files.slice(remainingNumberOfContentShells);
-  for (var i = 0; i < oldContentShellDirs.length; i++)
+  const remainingNumberOfContentShells = MAX_CONTENT_SHELLS / 2;
+  const oldContentShellDirs = files.slice(remainingNumberOfContentShells);
+  for (let i = 0; i < oldContentShellDirs.length; i++)
     utils.removeRecursive(path.resolve(CACHE_PATH, oldContentShellDirs[i]));
   console.log(`Removed old content shells: ${oldContentShellDirs}`);
 }
 
 function findPreviousUploadedPosition(commitPosition) {
-  var previousPosition = commitPosition - 100;
-  var positionsListURL =
+  const previousPosition = commitPosition - 100;
+  const positionsListURL =
       `http://commondatastorage.googleapis.com/chromium-browser-snapshots/?delimiter=/&prefix=${PLATFORM
   }/&marker=${PLATFORM}/${previousPosition}/`;
   return utils.fetch(positionsListURL).then(onPositionsList).catch(onError);
 
   function onPositionsList(buffer) {
-    var positions = buffer.toString('binary')
+    const positions = buffer.toString('binary')
                         .match(/([^<>]+)(?=<\/Prefix><\/CommonPrefixes>)/g)
                         .map(prefixedPosition => prefixedPosition.split('/')[1])
                         .map(positionString => parseInt(positionString, 10));
-    var positionSet = new Set(positions);
-    var previousUploadedPosition = commitPosition;
+    const positionSet = new Set(positions);
+    let previousUploadedPosition = commitPosition;
     while (commitPosition - previousUploadedPosition < 100) {
       if (positionSet.has(previousUploadedPosition))
         return previousUploadedPosition.toString();
@@ -164,12 +182,12 @@
   }
 }
 
-function prepareContentShellDirectory(folder) {
-  var contentShellPath = path.join(CACHE_PATH, folder);
+async function prepareContentShellDirectory(folder) {
+  const contentShellPath = path.join(CACHE_PATH, folder);
   if (utils.isDir(contentShellPath))
     utils.removeRecursive(contentShellPath);
   fs.mkdirSync(contentShellPath);
-  return Promise.resolve(folder);
+  return folder;
 }
 
 function downloadContentShell(url, folder) {
@@ -179,7 +197,7 @@
 
   function writeZip(buffer) {
     console.log('Completed download of content shell');
-    var contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP);
+    const contentShellZipPath = path.join(CACHE_PATH, folder, CONTENT_SHELL_ZIP);
     fs.writeFileSync(contentShellZipPath, buffer);
     return contentShellZipPath;
   }
@@ -192,17 +210,17 @@
 
 function extractContentShell(contentShellZipPath) {
   console.log(`Extracting content shell zip: ${contentShellZipPath}`);
-  var unzipScriptPath = path.resolve(__dirname, 'unzip.py');
-  var src = contentShellZipPath;
-  var dest = path.resolve(path.dirname(src), 'out');
+  const unzipScriptPath = path.resolve(__dirname, 'unzip.py');
+  const src = contentShellZipPath;
+  const dest = path.resolve(path.dirname(src), 'out');
   shell(`${PYTHON} ${unzipScriptPath} ${src} ${dest}`);
   fs.unlinkSync(src);
-  var originalDirPath = path.resolve(dest, 'content-shell');
-  var newDirPath = path.resolve(dest, TARGET);
+  const originalDirPath = path.resolve(dest, 'content-shell');
+  const newDirPath = path.resolve(dest, TARGET);
   fs.renameSync(originalDirPath, newDirPath);
   fs.chmodSync(getContentShellBinaryPath(newDirPath), '755');
   if (process.platform === 'darwin') {
-    var helperPath = path.resolve(
+    const helperPath = path.resolve(
         newDirPath, 'Content Shell.app', 'Contents', 'Frameworks', 'Content Shell Helper.app', 'Contents', 'MacOS',
         'Content Shell Helper');
     fs.chmodSync(helperPath, '755');
@@ -222,7 +240,7 @@
 }
 
 function runTests(buildDirectoryPath, useDebugDevtools) {
-  var testArgs = getInspectorTests().concat([
+  const testArgs = getInspectorTests().concat([
     '--build-directory',
     buildDirectoryPath,
     '--target',
@@ -237,10 +255,10 @@
     testArgs.push('--additional-driver-flag=--remote-debugging-port=9222');
     testArgs.push('--time-out-ms=6000000');
     console.log('\n=============================================');
-    var unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/'));
+    const unitTest = testArgs.find(arg => arg.includes('http/tests/devtools/unit/'));
     if (unitTest) {
-      var unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`;
-      var link =
+      const unitTestPath = `http://localhost:8080/${unitTest.slice('http/tests/'.length)}`;
+      const link =
           `http://localhost:8080/inspector-sources/debug/integration_test_runner.html?experiments=true&test=${
                                                                                                               unitTestPath
                                                                                                             }`;
@@ -253,22 +271,22 @@
     }
     console.log('=============================================\n');
   }
-  var args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags());
+  const args = [BLINK_TEST_PATH].concat(testArgs).concat(getTestFlags());
   console.log(`Running web tests with args: ${args}`);
   childProcess.spawn(PYTHON, args, {stdio: 'inherit'});
 }
 
 function getTestFlags() {
-  var flagValues = Object.keys(Flags).map(key => Flags[key]);
+  const flagValues = Object.keys(Flags).map(key => Flags[key]);
   return process.argv.slice(2).filter(arg => {
-    var flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg;
+    const flagName = utils.includes(arg, '=') ? arg.slice(0, arg.indexOf('=')) : arg;
     return !utils.includes(flagValues, flagName) && !utils.includes(arg, 'inspector') &&
         !utils.includes(arg, 'http/tests/devtools');
   });
 }
 
 function getInspectorTests() {
-  var specificTests =
+  const specificTests =
       process.argv.filter(arg => utils.includes(arg, 'inspector') || utils.includes(arg, 'http/tests/devtools'));
   if (specificTests.length)
     return specificTests;