Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

US152271 - Cleanup appropriate folders in the plugin during runs #36

Merged
merged 8 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 66 additions & 7 deletions src/server/visual-diff-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { access, constants, mkdir, rename, stat } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { access, constants, mkdir, readdir, rename, rm, stat } from 'node:fs/promises';
import { basename, dirname, join } from 'node:path';
import { env } from 'node:process';

const isCI = !!env['CI'];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to make CI as quick as possible, but we could leave it out if we'd rather keep things simple. It'll just search for fail, pass, and reports that won't exist.

const DEFAULT_MARGIN = 10;

async function checkFileExists(fileName) {
Expand All @@ -12,6 +14,39 @@ async function checkFileExists(fileName) {
}
}

async function clearDir(updateGoldens, path) {
if (updateGoldens) {
await rm(path, { force: true, recursive: true });
} else {
await rm(join(path, 'fail'), { force: true, recursive: true });
await rm(join(path, 'pass'), { force: true, recursive: true });
await rm(join(path, 'report.html'), { force: true });
dlockhart marked this conversation as resolved.
Show resolved Hide resolved
}
}

async function clearAllDirs(updateGoldens, vdiffPath) {
if (updateGoldens) {
await rm(vdiffPath, { force: true, recursive: true });
} else {
await clearDiffPaths(vdiffPath);
}
}

async function clearDiffPaths(dir) {
const paths = await readdir(dir, { withFileTypes: true });
for (const path of paths) {
const full = join(dir, path.name);
const base = basename(path.name);

if (path.isDirectory()) {
if (base === 'pass' || base === 'fail') await rm(full, { force: true, recursive: true });
else await clearDiffPaths(full);
} else {
if (base === 'report.html') await rm(full, { force: true });
}
}
}

async function tryMoveFile(srcFileName, destFileName) {
await mkdir(dirname(destFileName), { recursive: true });
try {
Expand Down Expand Up @@ -42,12 +77,18 @@ function extractTestPartsFromName(name) {
};
}

export function visualDiff({ updateGoldens = false } = {}) {
let rootDir;
export function visualDiff({ updateGoldens = false, runSubset = false } = {}) {
let currentRun = 0,
rootDir;
const clearedDirs = new Map();
return {
name: 'brightspace-visual-diff',
async serverStart({ config }) {
rootDir = config.rootDir;

if (runSubset || isCI) return;
// Do a more complete cleanup to remove orphaned directories
await clearAllDirs(updateGoldens, join(rootDir, '.vdiff'));
},
async executeCommand({ command, payload, session }) {

Expand All @@ -61,9 +102,27 @@ export function visualDiff({ updateGoldens = false } = {}) {
const browser = session.browser.name.toLowerCase();
const { dir, newName } = extractTestPartsFromName(payload.name);
const testPath = dirname(session.testFile).replace(rootDir, '');
const goldenFileName = `${join(rootDir, '.vdiff', testPath, 'golden', browser, dir, newName)}.png`;
const passFileName = `${join(rootDir, '.vdiff', testPath, 'pass', browser, dir, newName)}.png`;
const screenshotFileName = `${join(rootDir, '.vdiff', testPath, 'fail', browser, dir, newName)}.png`;
const newPath = join(rootDir, '.vdiff', testPath, dir);
const goldenFileName = `${join(newPath, 'golden', browser, newName)}.png`;
const passFileName = `${join(newPath, 'pass', browser, newName)}.png`;
const screenshotFileName = `${join(newPath, 'fail', browser, newName)}.png`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switches to the new directory structure we settled on in slack


if (!isCI) { // CI will be a fresh .vdiff folder each time and only one run
if (session.testRun !== currentRun) {
currentRun = session.testRun;
clearedDirs.clear();
}

if (runSubset || currentRun > 0) {
if (!clearedDirs.has(newPath)) {
const promise = clearDir(updateGoldens, newPath);
clearedDirs.set(newPath, promise);
await promise;
} else {
await clearedDirs.get(newPath);
}
svanherk marked this conversation as resolved.
Show resolved Hide resolved
}
}

const opts = payload.opts || {};
opts.margin = opts.margin || DEFAULT_MARGIN;
Expand Down
4 changes: 2 additions & 2 deletions src/server/wtr-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class WTRConfig {
...passthroughConfig
} = {}) {

const { watch, manual, group, files, playwright } = this.#cliArgs;
const { files, filter, golden, grep, group, manual, playwright, watch } = this.#cliArgs;

if (!group || group === 'default') {
if (playwright) {
Expand Down Expand Up @@ -188,7 +188,7 @@ export class WTRConfig {
//config.reporters.push(visualDiffReporter());

config.plugins ??= [];
config.plugins.push(visualDiff({ updateGoldens: this.#cliArgs.golden }));
config.plugins.push(visualDiff({ updateGoldens: golden, runSubset: filter?.length > 0 || grep?.length > 0 }));
svanherk marked this conversation as resolved.
Show resolved Hide resolved

config.groups.push(this.visualDiffGroup);
}
Expand Down