downloadTestBrowsers.mjs (2302B)
1 /** 2 * @license 3 * Copyright 2023 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * Downloads test browser binaries to test/.cache/server folder that 9 * mirrors the structure of the download server. 10 */ 11 12 import {existsSync, mkdirSync, copyFileSync, rmSync} from 'fs'; 13 import {normalize, join, dirname} from 'path'; 14 15 import {BrowserPlatform, install} from '@puppeteer/browsers'; 16 17 import {downloadPaths} from '../lib/esm/browser-data/browser-data.js'; 18 import * as versions from '../test/build/versions.js'; 19 20 function getBrowser(str) { 21 const regex = /test(.+)BuildId/; 22 const match = str.match(regex); 23 24 if (match && match[1]) { 25 const lowercased = match[1].toLowerCase(); 26 if (lowercased === 'chromeheadlessshell') { 27 return 'chrome-headless-shell'; 28 } 29 return lowercased; 30 } else { 31 return null; 32 } 33 } 34 35 const cacheDir = normalize(join('.', 'test', '.cache')); 36 37 // Needed to test Firefox nightly build download 38 function mockFirefoxNightly(browser, platform, targetPath) { 39 if (browser === 'firefox' && platform === 'linux') { 40 const nightlyTarget = join( 41 cacheDir, 42 'server', 43 ...downloadPaths.firefox( 44 'linux', 45 versions.testFirefoxBuildId.split('_').at(-1), 46 ), 47 ); 48 49 if (existsSync(nightlyTarget)) { 50 return; 51 } 52 53 copyFileSync(targetPath, nightlyTarget); 54 } 55 } 56 57 for (const version of Object.keys(versions)) { 58 const browser = getBrowser(version); 59 if (!browser) { 60 continue; 61 } 62 63 const buildId = versions[version]; 64 65 for (const platform of Object.values(BrowserPlatform)) { 66 if (platform === BrowserPlatform.LINUX_ARM) { 67 continue; 68 } 69 const targetPath = join( 70 cacheDir, 71 'server', 72 ...downloadPaths[browser](platform, buildId), 73 ); 74 75 if (existsSync(targetPath)) { 76 mockFirefoxNightly(browser, platform, targetPath); 77 continue; 78 } 79 80 const archivePath = await install({ 81 browser, 82 buildId, 83 platform, 84 cacheDir: join(cacheDir, 'tmp'), 85 unpack: false, 86 }); 87 88 mkdirSync(dirname(targetPath), { 89 recursive: true, 90 }); 91 copyFileSync(archivePath, targetPath); 92 93 mockFirefoxNightly(browser, platform, targetPath); 94 } 95 } 96 97 rmSync(join(cacheDir, 'tmp'), { 98 recursive: true, 99 force: true, 100 maxRetries: 10, 101 });