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 all 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
78 changes: 71 additions & 7 deletions src/server/visual-diff-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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;
const PATHS = {
FAIL: 'fail',
GOLDEN: 'golden',
PASS: 'pass',
VDIFF_ROOT: '.vdiff'
};

async function checkFileExists(fileName) {
try {
Expand All @@ -12,6 +20,41 @@ async function checkFileExists(fileName) {
}
}

async function clearDir(updateGoldens, path) {
if (updateGoldens) {
await rm(path, { force: true, recursive: true });
} else {
await Promise.all([
rm(join(path, PATHS.FAIL), { force: true, recursive: true }),
rm(join(path, PATHS.PASS), { force: true, recursive: true }),
rm(join(path, 'report.html'), { force: true })
]);
}
}

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 === PATHS.PASS || base === PATHS.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 +85,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, PATHS.VDIFF_ROOT));
},
async executeCommand({ command, payload, session }) {

Expand All @@ -61,9 +110,24 @@ 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, PATHS.VDIFF_ROOT, testPath, dir);
const goldenFileName = `${join(newPath, PATHS.GOLDEN, browser, newName)}.png`;
const passFileName = `${join(newPath, PATHS.PASS, browser, newName)}.png`;
const screenshotFileName = `${join(newPath, PATHS.FAIL, browser, newName)}.png`;

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)) {
clearedDirs.set(newPath, clearDir(updateGoldens, newPath));
}
await clearedDirs.get(newPath);
}
}

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 || grep) }));

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