Skip to content

Commit

Permalink
chore(tests): delete traces when not needed (podman-desktop#7874)
Browse files Browse the repository at this point in the history
* chore(tests): delete traces when not needed
  • Loading branch information
cbr7 authored Jul 1, 2024
1 parent d0f9dae commit 772acf2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
41 changes: 40 additions & 1 deletion tests/playwright/src/runner/podman-desktop-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';

import type { ElectronApplication, JSHandle, Page } from '@playwright/test';
Expand All @@ -40,6 +40,7 @@ export class PodmanDesktopRunner {
private _videoAndTraceName: string | undefined;
private _autoUpdate: boolean;
private _autoCheckUpdate: boolean;
private _testPassed: boolean;

constructor({
profile = '',
Expand All @@ -54,6 +55,7 @@ export class PodmanDesktopRunner {
this._videoAndTraceName = undefined;
this._autoUpdate = autoUpdate;
this._autoCheckUpdate = autoCheckUpdate;
this._testPassed = true;

// Options setting always needs to be last action in constructor in order to apply settings correctly
this._options = this.defaultOptions();
Expand Down Expand Up @@ -175,6 +177,7 @@ export class PodmanDesktopRunner {
const video = this.getPage().video();
if (video) {
await video.saveAs(path);
await video.delete();
} else {
console.log(`Video file associated was not found`);
}
Expand All @@ -200,6 +203,34 @@ export class PodmanDesktopRunner {
console.log(`Video file saved as: ${videoPath}`);
}
this._running = false;
await this.removeTracesOnFinished();
}

async removeTracesOnFinished(): Promise<void> {
const rawTracesPath = join(this._testOutput, 'traces', 'raw');

if (existsSync(rawTracesPath)) {
console.log(`Removing raw traces folder: ${rawTracesPath}`);
rmSync(rawTracesPath, { recursive: true, force: true, maxRetries: 5 });
}

if (!this._testPassed || !this._videoAndTraceName) return;

if (!process.env.KEEP_TRACES_ON_PASS) {
const tracesPath = join(this._testOutput, 'traces', `${this._videoAndTraceName}_trace.zip`);
if (existsSync(tracesPath)) {
console.log(`Removing traces folder: ${tracesPath}`);
rmSync(tracesPath, { recursive: true, force: true, maxRetries: 5 });
}
}

if (!process.env.KEEP_VIDEOS_ON_PASS) {
const videoPath = join(this._testOutput, 'videos', `${this._videoAndTraceName}.webm`);
if (existsSync(videoPath)) {
console.log(`Removing video folder: ${videoPath}`);
rmSync(videoPath, { recursive: true, force: true, maxRetries: 5 });
}
}
}

protected async trackTime(fn: () => Promise<void>): Promise<number> {
Expand Down Expand Up @@ -288,6 +319,14 @@ export class PodmanDesktopRunner {
return this._testOutput;
}

public getTestPassed(): boolean {
return this._testPassed;
}

public setTestPassed(value: boolean): void {
this._testPassed = value;
}

public get options(): object {
return this._options;
}
Expand Down
1 change: 1 addition & 0 deletions tests/playwright/src/setupFiles/extended-hooks-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ export async function takeScreenshotHook(runner: PodmanDesktopRunner, taskName:
}
console.log(`Screenshot of the failed test will be saved to: ${fileName}`);
await runner.screenshot(`${fileName}.png`);
runner.setTestPassed(false);
}
4 changes: 2 additions & 2 deletions tests/playwright/src/setupFiles/extended-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { afterEach } from 'vitest';
import { afterEach, onTestFailed } from 'vitest';

import type { RunnerTestContext } from '../testContext/runner-test-context';
import { takeScreenshotHook } from './extended-hooks-utils';

afterEach(async (context: RunnerTestContext) => {
context.onTestFailed(async () => await takeScreenshotHook(context.pdRunner, context.task.name));
onTestFailed(async () => await takeScreenshotHook(context.pdRunner, context.task.name));
});

0 comments on commit 772acf2

Please sign in to comment.