fix: clean up tmp profile dirs when browser is closed (#8580)

* fix: clean up tmp profile dirs when browser is closed
This commit is contained in:
Pablo Matías Gomez 2022-06-27 02:40:30 -03:00 committed by GitHub
parent f2e9cbb849
commit 9787a1d8df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View File

@ -92,7 +92,7 @@ export class ChromeLauncher implements ProductLauncher {
}
}
let isTempUserDataDir = true;
let isTempUserDataDir = false;
// Check for the user data dir argument, which will always be set even
// with a custom directory specified via the userDataDir option.
@ -100,6 +100,7 @@ export class ChromeLauncher implements ProductLauncher {
return arg.startsWith('--user-data-dir');
});
if (userDataDirIndex < 0) {
isTempUserDataDir = true;
chromeArguments.push(
`--user-data-dir=${await fs.promises.mkdtemp(
path.join(tmpdir(), 'puppeteer_dev_chrome_profile-')
@ -111,8 +112,6 @@ export class ChromeLauncher implements ProductLauncher {
const userDataDir = chromeArguments[userDataDirIndex]!.split('=', 2)[1];
assert(typeof userDataDir === 'string', '`--user-data-dir` is malformed');
isTempUserDataDir = false;
let chromeExecutable = executablePath;
if (channel) {
// executablePath is detected by channel, so it should not be specified by user.

View File

@ -251,6 +251,35 @@ describe('Launcher specs', function () {
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
await rmAsync(userDataDir).catch(() => {});
});
itChromeOnly('tmp profile should be cleaned up', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
// Set a custom test tmp dir so that we can validate that
// the profile dir is created and then cleaned up.
const testTmpDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'puppeteer_test_chrome_profile-')
);
process.env['PUPPETEER_TMP_DIR'] = testTmpDir;
// Path should be empty before starting the browser.
expect(fs.readdirSync(testTmpDir).length).toEqual(0);
const browser = await puppeteer.launch(defaultBrowserOptions);
// One profile folder should have been created at this moment.
const profiles = fs.readdirSync(testTmpDir);
expect(profiles.length).toEqual(1);
expect(profiles[0]?.startsWith('puppeteer_dev_chrome_profile-')).toBe(
true
);
// Open a page to make sure its functional.
await browser.newPage();
await browser.close();
// Profile should be deleted after closing the browser
expect(fs.readdirSync(testTmpDir).length).toEqual(0);
// Restore env var
process.env['PUPPETEER_TMP_DIR'] = '';
});
itFirefoxOnly('userDataDir option restores preferences', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();