From 3c28eca715a7e4db3c73a70e7daa133eb34e5603 Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Sat, 25 Jun 2022 19:04:35 -0300 Subject: [PATCH 1/3] fix: Test that the tmp profiles that are created do not get deleted when browser is closed --- test/src/launcher.spec.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/src/launcher.spec.ts b/test/src/launcher.spec.ts index 4c934063c617d..697fcbbedb100 100644 --- a/test/src/launcher.spec.ts +++ b/test/src/launcher.spec.ts @@ -251,6 +251,35 @@ describe('Launcher specs', function () { // This might throw. See https://github.com/puppeteer/puppeteer/issues/2778 await rmAsync(userDataDir).catch(() => {}); }); + it('tmp profile should be deleted without userDataDir option', 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(); From cbfcf97f889a958a244551a695e30206987877c5 Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Sat, 25 Jun 2022 19:15:37 -0300 Subject: [PATCH 2/3] fix: Delete tmp profile dir when browser is closed --- src/node/ChromeLauncher.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/node/ChromeLauncher.ts b/src/node/ChromeLauncher.ts index 26b8ef28632ba..2db109a75280e 100644 --- a/src/node/ChromeLauncher.ts +++ b/src/node/ChromeLauncher.ts @@ -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. From e03b1a8136e551cd13f48a62444b292510646688 Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Sun, 26 Jun 2022 18:49:34 -0300 Subject: [PATCH 3/3] fix: run tmp profile cleanup test on chrome only --- test/src/launcher.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/launcher.spec.ts b/test/src/launcher.spec.ts index 697fcbbedb100..20e8331bffe39 100644 --- a/test/src/launcher.spec.ts +++ b/test/src/launcher.spec.ts @@ -251,7 +251,7 @@ describe('Launcher specs', function () { // This might throw. See https://github.com/puppeteer/puppeteer/issues/2778 await rmAsync(userDataDir).catch(() => {}); }); - it('tmp profile should be deleted without userDataDir option', async () => { + itChromeOnly('tmp profile should be cleaned up', async () => { const {defaultBrowserOptions, puppeteer} = getTestState(); // Set a custom test tmp dir so that we can validate that