Index: test/runners/chromium_download.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/test/runners/chromium_download.js |
@@ -0,0 +1,146 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-present eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+/* eslint-env node */ |
+/* eslint no-console: "off" */ |
+ |
+"use strict"; |
+ |
+const fs = require("fs"); |
+const https = require("https"); |
+const path = require("path"); |
+ |
+const extractZip = require("extract-zip"); |
+ |
+function getChromiumExecutable(chromiumDir) |
+{ |
+ switch (process.platform) |
+ { |
+ case "win32": |
+ return path.join(chromiumDir, "chrome-win32", "chrome.exe"); |
+ case "linux": |
+ return path.join(chromiumDir, "chrome-linux", "chrome"); |
+ case "darwin": |
+ return path.join(chromiumDir, "chrome-mac", "Chromium.app", "Contents", |
+ "MacOS", "Chromium"); |
+ default: |
+ throw new Error("Unexpected platform"); |
+ } |
+} |
+ |
+function ensureChromium(chromiumRevision) |
+{ |
+ let {platform} = process; |
+ if (platform == "win32") |
+ platform += "-" + process.arch; |
+ let buildTypes = { |
+ "win32-ia32": ["Win", "chrome-win32.zip"], |
+ "win32-x64": ["Win_x64", "chrome-win32.zip"], |
+ "linux": ["Linux_x64", "chrome-linux.zip"], |
+ "darwin": ["Mac", "chrome-mac.zip"] |
+ }; |
+ |
+ if (!buildTypes.hasOwnProperty(platform)) |
+ { |
+ let err = new Error(`Cannot run browser tests, ${platform} is unsupported`); |
+ return Promise.reject(err); |
+ } |
+ |
+ |
+ return Promise.resolve().then(() => |
+ { |
+ let snapshotsDir = path.join(__dirname, "..", "..", "chromium-snapshots"); |
+ let chromiumDir = path.join(snapshotsDir, |
+ `chromium-${platform}-${chromiumRevision}`); |
+ if (fs.existsSync(chromiumDir)) |
+ return chromiumDir; |
+ |
+ if (!fs.existsSync(path.dirname(chromiumDir))) |
+ fs.mkdirSync(path.dirname(chromiumDir)); |
+ |
+ let [dir, fileName] = buildTypes[platform]; |
+ let archive = path.join(snapshotsDir, "download-cache", |
+ `${chromiumRevision}-${fileName}`); |
+ |
+ return Promise.resolve() |
+ .then(() => |
+ { |
+ if (!fs.existsSync(archive)) |
+ { |
+ let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/${dir}%2F${chromiumRevision}%2F${fileName}?alt=media`; |
+ console.info("Downloading Chromium..."); |
+ return download(url, archive); |
+ } |
+ console.info(`Reusing cached archive ${archive}`); |
+ }) |
+ .then(() => unzipArchive(archive, chromiumDir)) |
+ .then(() => chromiumDir); |
+ }).then(dir => getChromiumExecutable(dir)); |
+} |
+ |
+function download(url, destFile) |
+{ |
+ return new Promise((resolve, reject) => |
+ { |
+ let cacheDir = path.dirname(destFile); |
+ if (!fs.existsSync(cacheDir)) |
+ fs.mkdirSync(cacheDir); |
+ let tempDest = destFile + "-" + process.pid; |
+ let writable = fs.createWriteStream(tempDest); |
+ |
+ https.get(url, response => |
+ { |
+ if (response.statusCode != 200) |
+ { |
+ reject( |
+ new Error(`Unexpected server response: ${response.statusCode}`)); |
+ response.resume(); |
+ return; |
+ } |
+ |
+ response.pipe(writable) |
+ .on("error", error => |
+ { |
+ writable.close(); |
+ fs.unlinkSync(tempDest); |
+ reject(error); |
+ }) |
+ .on("close", () => |
+ { |
+ writable.close(); |
+ fs.renameSync(tempDest, destFile); |
+ resolve(); |
+ }); |
+ }).on("error", reject); |
+ }); |
+} |
+ |
+function unzipArchive(archive, destDir) |
+{ |
+ return new Promise((resolve, reject) => |
+ { |
+ extractZip(archive, {dir: destDir}, err => |
+ { |
+ if (err) |
+ reject(err); |
+ else |
+ resolve(); |
+ }); |
+ }); |
+} |
+ |
+module.exports.ensureChromium = ensureChromium; |