From 0968f47e5c2779e7352902b82fa2ee17af7a4a64 Mon Sep 17 00:00:00 2001 From: Dave Lockhart Date: Wed, 26 Jul 2023 17:41:53 -0400 Subject: [PATCH] fix: treat metadata file differently locally from CI (#103) --- .vdiff.json | 16 ---------------- src/server/visual-diff-plugin.js | 6 ++++-- src/server/visual-diff-reporter.js | 22 +++++++++++++--------- src/server/wtr-config.js | 2 +- 4 files changed, 18 insertions(+), 28 deletions(-) delete mode 100644 .vdiff.json diff --git a/.vdiff.json b/.vdiff.json deleted file mode 100644 index adbe97c5..00000000 --- a/.vdiff.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "browsers": [ - { - "name": "Chromium", - "version": 115 - }, - { - "name": "Firefox", - "version": 115 - }, - { - "name": "Webkit", - "version": 17 - } - ] -} \ No newline at end of file diff --git a/src/server/visual-diff-plugin.js b/src/server/visual-diff-plugin.js index 2806139e..3df50d59 100644 --- a/src/server/visual-diff-plugin.js +++ b/src/server/visual-diff-plugin.js @@ -7,13 +7,15 @@ import { PNG } from 'pngjs'; const isCI = !!env['CI']; const DEFAULT_MARGIN = 10; const DEFAULT_TOLERANCE = 0; // TODO: Support tolerance override? +const METADATA_NAME = '.vdiff.json'; +const ROOT_NAME = '.vdiff'; export const PATHS = { FAIL: 'fail', GOLDEN: 'golden', PASS: 'pass', - METADATA: '.vdiff.json', + METADATA: isCI ? METADATA_NAME : join(ROOT_NAME, METADATA_NAME), REPORT_ROOT: '.report', - VDIFF_ROOT: '.vdiff' + VDIFF_ROOT: ROOT_NAME }; async function checkFileExists(fileName) { diff --git a/src/server/visual-diff-reporter.js b/src/server/visual-diff-reporter.js index 27aa348b..e92c1d6f 100644 --- a/src/server/visual-diff-reporter.js +++ b/src/server/visual-diff-reporter.js @@ -1,12 +1,14 @@ import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { getTestInfo, PATHS } from './visual-diff-plugin.js'; +import { env } from 'node:process'; import { execSync } from 'node:child_process'; import { fileURLToPath } from 'node:url'; const __dirname = dirname(fileURLToPath(import.meta.url)); +const isCI = !!env['CI']; -function createData(rootDir, sessions) { +function createData(rootDir, updateGoldens, sessions) { let metadata = {}; const metadataPath = join(rootDir, PATHS.METADATA); @@ -51,10 +53,12 @@ function createData(rootDir, sessions) { }); }); - metadata.browsers = Array.from(browsers.values()).map(b => { - return { name: b.name, version: b.version }; - }); - writeFileSync(metadataPath, JSON.stringify(metadata, undefined, '\t')); + if (isCI || updateGoldens) { + metadata.browsers = Array.from(browsers.values()).map(b => { + return { name: b.name, version: b.version }; + }); + writeFileSync(metadataPath, JSON.stringify(metadata, undefined, '\t')); + } return { browsers, files, numFailed, numTests }; @@ -99,7 +103,7 @@ function flattenResults(session, browserData, fileData) { } -export function visualDiffReporter({ reportResults = true } = {}) { +export function visualDiffReporter({ updateGoldens } = {}) { let rootDir; return { start({ config }) { @@ -108,14 +112,14 @@ export function visualDiffReporter({ reportResults = true } = {}) { }, stop({ sessions }) { - if (!reportResults) return; - - const data = createData(rootDir, sessions); + const data = createData(rootDir, updateGoldens, sessions); const json = JSON.stringify(data, (_key, val) => { if (val instanceof Map) return [...val.values()].sort((a, b) => a.name.localeCompare(b.name)); return val; }, '\t'); + if (updateGoldens) return; + const inputDir = join(__dirname, 'report'); const reportDir = join(rootDir, PATHS.VDIFF_ROOT, PATHS.REPORT_ROOT); const tempDir = join(reportDir, 'temp'); diff --git a/src/server/wtr-config.js b/src/server/wtr-config.js index 1ae9bc17..0721653d 100644 --- a/src/server/wtr-config.js +++ b/src/server/wtr-config.js @@ -175,7 +175,7 @@ export class WTRConfig { }); } else if (group === 'vdiff') { config.reporters ??= [ defaultReporter() ]; - config.reporters.push(visualDiffReporter({ reportResults: !golden })); + config.reporters.push(visualDiffReporter({ updateGoldens: golden })); config.plugins ??= []; config.plugins.push(visualDiff({ updateGoldens: golden, runSubset: !!(filter || grep) }));