fix(test-runner): video: 'retain-on-failure' leaves empty folders behind (#7125)

This commit is contained in:
Pavel Feldman 2021-06-15 10:06:49 -07:00 committed by GitHub
parent 66f9aeee17
commit 9550106e1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 10 deletions

View file

@ -16,12 +16,16 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os';
import type { LaunchOptions, BrowserContextOptions, Page } from '../../types/types'; import type { LaunchOptions, BrowserContextOptions, Page } from '../../types/types';
import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions } from '../../types/test'; import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions } from '../../types/test';
import { rootTestType } from './testType'; import { rootTestType } from './testType';
import { createGuid, removeFolders } from '../utils/utils';
export { expect } from './expect'; export { expect } from './expect';
export const _baseTest: TestType<{}, {}> = rootTestType.test; export const _baseTest: TestType<{}, {}> = rootTestType.test;
const artifactsFolder = path.join(os.tmpdir(), 'pwt-' + createGuid());
export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions, PlaywrightWorkerArgs & PlaywrightWorkerOptions>({ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions, PlaywrightWorkerArgs & PlaywrightWorkerOptions>({
defaultBrowserType: [ 'chromium', { scope: 'worker' } ], defaultBrowserType: [ 'chromium', { scope: 'worker' } ],
browserName: [ ({ defaultBrowserType }, use) => use(defaultBrowserType), { scope: 'worker' } ], browserName: [ ({ defaultBrowserType }, use) => use(defaultBrowserType), { scope: 'worker' } ],
@ -44,6 +48,7 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,
const browser = await playwright[browserName].launch(options); const browser = await playwright[browserName].launch(options);
await use(browser); await use(browser);
await browser.close(); await browser.close();
await removeFolders([artifactsFolder]);
}, { scope: 'worker' } ], }, { scope: 'worker' } ],
screenshot: 'off', screenshot: 'off',
@ -75,10 +80,16 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,
if (process.env.PWDEBUG) if (process.env.PWDEBUG)
testInfo.setTimeout(0); testInfo.setTimeout(0);
const recordVideo = video === 'on' || video === 'retain-on-failure' || let recordVideoDir: string | null = null;
(video === 'retry-with-video' && !!testInfo.retry); if (video === 'on' || (video === 'retry-with-video' && !!testInfo.retry))
recordVideoDir = testInfo.outputPath('');
if (video === 'retain-on-failure') {
await fs.promises.mkdir(artifactsFolder, { recursive: true });
recordVideoDir = artifactsFolder;
}
const options: BrowserContextOptions = { const options: BrowserContextOptions = {
recordVideo: recordVideo ? { dir: testInfo.outputPath('') } : undefined, recordVideo: recordVideoDir ? { dir: recordVideoDir } : undefined,
...contextOptions, ...contextOptions,
}; };
if (acceptDownloads !== undefined) if (acceptDownloads !== undefined)
@ -150,17 +161,17 @@ export const test = _baseTest.extend<PlaywrightTestArgs & PlaywrightTestOptions,
} }
await context.close(); await context.close();
const deleteVideos = video === 'retain-on-failure' && !testFailed; if (video === 'retain-on-failure' && testFailed) {
if (deleteVideos) {
await Promise.all(allPages.map(async page => { await Promise.all(allPages.map(async page => {
const video = page.video(); const video = page.video();
if (!video) if (!video)
return; return;
try { try {
const videoPath = await video.path(); const videoPath = await video.path();
await fs.promises.unlink(videoPath); const fileName = path.basename(videoPath);
await video.saveAs(testInfo.outputPath(fileName));
} catch (e) { } catch (e) {
// Silent catch. // Silent catch empty videos.
} }
})); }));
} }

View file

@ -163,8 +163,7 @@ DEPS['src/server/trace/common/'] = ['src/server/snapshot/', ...DEPS['src/server/
DEPS['src/server/trace/recorder/'] = ['src/server/trace/common/', ...DEPS['src/server/trace/common/']]; DEPS['src/server/trace/recorder/'] = ['src/server/trace/common/', ...DEPS['src/server/trace/common/']];
DEPS['src/server/trace/viewer/'] = ['src/server/trace/common/', ...DEPS['src/server/trace/common/']]; DEPS['src/server/trace/viewer/'] = ['src/server/trace/common/', ...DEPS['src/server/trace/common/']];
// No dependencies for test runner. DEPS['src/test/'] = ['src/test/**', 'src/utils/utils.ts'];
DEPS['src/test/'] = ['src/test/**'];
checkDeps().catch(e => { checkDeps().catch(e => {
console.error(e && e.stack ? e.stack : e); console.error(e && e.stack ? e.stack : e);