fix(rimraf): allow 10 retires when removing the profile folder (#5826)

This commit is contained in:
Pavel Feldman 2021-03-17 10:31:35 +08:00 committed by GitHub
parent d1a3a5d589
commit bf36b487fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 26 deletions

View File

@ -17,11 +17,10 @@
import fs from 'fs';
import path from 'path';
import util from 'util';
import removeFolder from 'rimraf';
import lockfile from 'proper-lockfile';
import {Registry, allBrowserNames, isBrowserDirectory, BrowserName, registryDirectory} from '../utils/registry';
import * as browserFetcher from './browserFetcher';
import { getAsBooleanFromENV, calculateSha1 } from '../utils/utils';
import { getAsBooleanFromENV, calculateSha1, removeFolders } from '../utils/utils';
const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs));
const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
@ -29,7 +28,6 @@ const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
const fsExistsAsync = (filePath: string) => fsReadFileAsync(filePath).then(() => true).catch(e => false);
const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
const removeFolderAsync = util.promisify(removeFolder);
const PACKAGE_PATH = path.join(__dirname, '..', '..');
@ -101,10 +99,9 @@ async function validateCache(linksDir: string, browserNames: BrowserName[]) {
const directories = new Set<string>(downloadedBrowsers);
for (const browserDirectory of usedBrowserPaths)
directories.delete(browserDirectory);
for (const directory of directories) {
for (const directory of directories)
browserFetcher.logPolitely('Removing unused browser at ' + directory);
await removeFolderAsync(directory).catch(e => {});
}
await removeFolders([...directories]);
}
// 3. Install missing browsers for this package.

View File

@ -16,14 +16,10 @@
*/
import { EventEmitter } from 'events';
import removeFolder from 'rimraf';
import util from 'util';
import * as types from './types';
import { Progress } from './progress';
import { debugLogger } from '../utils/debugLogger';
const removeFolderAsync = util.promisify(removeFolder);
export type RegisteredListener = {
emitter: EventEmitter;
eventName: (string | symbol);
@ -77,12 +73,6 @@ class Helper {
return null;
}
static async removeFolders(dirs: string[]) {
await Promise.all(dirs.map(dir => {
return removeFolderAsync(dir).catch((err: Error) => console.error(err));
}));
}
static waitForEvent(progress: Progress | null, emitter: EventEmitter, event: string | symbol, predicate?: Function): { promise: Promise<any>, dispose: () => void } {
const listeners: RegisteredListener[] = [];
const promise = new Promise((resolve, reject) => {

View File

@ -17,10 +17,9 @@
import * as childProcess from 'child_process';
import * as readline from 'readline';
import * as removeFolder from 'rimraf';
import { helper } from './helper';
import * as types from './types';
import { isUnderTest } from '../utils/utils';
import { isUnderTest, removeFolders } from '../utils/utils';
export type Env = {[key: string]: string | number | boolean | undefined};
@ -62,7 +61,7 @@ if (maxListeners !== 0)
process.setMaxListeners(Math.max(maxListeners || 0, 100));
export async function launchProcess(options: LaunchProcessOptions): Promise<LaunchResult> {
const cleanup = () => helper.removeFolders(options.tempDirectories);
const cleanup = () => removeFolders(options.tempDirectories);
const stdio: ('ignore' | 'pipe')[] = options.stdio === 'pipe' ? ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe'];
options.log(`<launching> ${options.executablePath} ${options.args.join(' ')}`);
@ -172,11 +171,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
// the process might have already stopped
}
}
try {
// Attempt to remove temporary directories to avoid littering.
for (const dir of options.tempDirectories)
removeFolder.sync(dir);
} catch (e) { }
cleanup();
}
function killAndWait() {

View File

@ -16,6 +16,7 @@
import path from 'path';
import fs from 'fs';
import removeFolder from 'rimraf';
import * as util from 'util';
import * as crypto from 'crypto';
@ -145,3 +146,15 @@ export function calculateSha1(buffer: Buffer | string): string {
export function createGuid(): string {
return crypto.randomBytes(16).toString('hex');
}
export async function removeFolders(dirs: string[]) {
await Promise.all(dirs.map((dir: string) => {
return new Promise<void>(fulfill => {
removeFolder(dir, { maxBusyTries: 10 }, error => {
if (error)
console.error(error); // eslint-disable no-console
fulfill();
});
});
}));
}

View File

@ -236,4 +236,5 @@ it('should connect to a browser with the default page', (test, { mode }) => {
const options = { ...browserOptions, __testHookOnConnectToBrowser: () => new Promise(f => setTimeout(f, 3000)) };
const context = await browserType.launchPersistentContext(await createUserDataDir(), options);
expect(context.pages().length).toBe(1);
await context.close();
});

View File

@ -26,9 +26,9 @@ import { folio as httpFolio } from './http.fixtures';
import { folio as playwrightFolio } from './playwright.fixtures';
import { PlaywrightClient } from '../lib/remote/playwrightClient';
import { start } from '../lib/outofprocess';
import { removeFolders } from '../lib/utils/utils';
export { expect, config } from 'folio';
const removeFolderAsync = util.promisify(require('rimraf'));
const mkdtempAsync = util.promisify(fs.mkdtemp);
const getExecutablePath = browserName => {
@ -69,7 +69,7 @@ fixtures.createUserDataDir.init(async ({ }, run) => {
return dir;
}
await run(createUserDataDir);
await Promise.all(dirs.map(dir => removeFolderAsync(dir).catch(e => { })));
await removeFolders(dirs);
});
fixtures.launchPersistent.init(async ({ createUserDataDir, browserOptions, browserType }, run) => {