Skip to content

Commit 0de060d

Browse files
authored
Merge pull request #895 from finos/better-test-archives
Better test screenshot archiving.
2 parents 02bbaf7 + d358438 commit 0de060d

File tree

8 files changed

+330
-335
lines changed

8 files changed

+330
-335
lines changed

packages/perspective-test/src/js/index.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ exports.with_server = function with_server({paths}, body) {
4646
};
4747

4848
let results;
49+
const seen_results = new Set();
4950

5051
const new_results = {};
5152

@@ -179,10 +180,7 @@ function mkdirSyncRec(targetDir) {
179180
describe.page = (url, body, {reload_page = true, name, root} = {}) => {
180181
let _url = url ? url : page_url;
181182
test_root = root ? root : test_root;
182-
const dir_name = path.join(test_root, "screenshots", RESULTS_TAGNAME, _url.replace(".html", ""));
183-
if (!fs.existsSync(dir_name)) {
184-
mkdirSyncRec(dir_name);
185-
}
183+
186184
describe(name ? name : _url, () => {
187185
let old = page_url;
188186
let old_reload = page_reload;
@@ -243,7 +241,7 @@ expect.extend({
243241
test.capture = function capture(name, body, {timeout = 60000, viewport = null, wait_for_update = true, fail_on_errors = true} = {}) {
244242
const _url = page_url;
245243
const _reload_page = page_reload;
246-
test(
244+
const spec = test(
247245
name,
248246
async () => {
249247
errors = [];
@@ -256,6 +254,15 @@ test.capture = function capture(name, body, {timeout = 60000, viewport = null, w
256254

257255
const iterations = process.env.PSP_SATURATE ? 10 : 1;
258256

257+
const test_name = `${name.replace(/[ \.']/g, "_")}`;
258+
const path_name = `${spec.result.fullName.replace(".html", "").replace(/[ \.']/g, "_")}`;
259+
let dir_name = path.join(test_root, "screenshots", RESULTS_TAGNAME, path_name);
260+
dir_name = dir_name.slice(0, dir_name.length - test_name.length - 1);
261+
const filename = path.join(dir_name, test_name);
262+
if (!fs.existsSync(dir_name)) {
263+
mkdirSyncRec(dir_name);
264+
}
265+
259266
for (let x = 0; x < iterations; x++) {
260267
if (_reload_page) {
261268
await page.close();
@@ -313,9 +320,7 @@ test.capture = function capture(name, body, {timeout = 60000, viewport = null, w
313320
.update(screenshot)
314321
.digest("hex");
315322

316-
const filename = path.join(test_root, "screenshots", RESULTS_TAGNAME, `${_url.replace(".html", "")}`, `${name.replace(/ /g, "_").replace(/[\.']/g, "")}`);
317-
318-
if (hash === results[_url + "/" + name]) {
323+
if (hash === results[path_name]) {
319324
fs.writeFileSync(filename + ".png", screenshot);
320325
} else {
321326
fs.writeFileSync(filename + ".failed.png", screenshot);
@@ -329,20 +334,22 @@ test.capture = function capture(name, body, {timeout = 60000, viewport = null, w
329334
}
330335

331336
if (process.env.WRITE_TESTS) {
332-
new_results[_url + "/" + name] = hash;
337+
new_results[path_name] = hash;
333338
}
334339

335340
if (process.env.PSP_PAUSE_ON_FAILURE) {
336-
if (!process.env.WRITE_TESTS && (hash !== results[_url + "/" + name] || errors.length > 0)) {
341+
if (!process.env.WRITE_TESTS && (hash !== results[path_name] || errors.length > 0)) {
337342
private_console.error(`Failed ${name}, pausing`);
338343
await prompt(`Failed ${name}, pausing. Press enter to continue ..`);
339344
}
340345
}
341346
if (fail_on_errors) {
342347
expect(errors).toNotError();
343348
}
344-
expect(hash).toBe(results[_url + "/" + name]);
349+
expect(hash).toBe(results[path_name]);
350+
expect(seen_results.has(path_name)).toBeFalsy();
345351
}
352+
seen_results.add(path_name);
346353
},
347354
timeout
348355
);

packages/perspective-test/src/js/reporter.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,23 @@ module.exports = class ImageViewerReporter {
4646
onTestResult(testRunConfig, testResults) {
4747
for (const test of testResults.testResults) {
4848
if (test.status === "failed") {
49-
const ancestors = test.ancestorTitles.filter(x => x.indexOf(".html") > -1).map(x => x.replace(".html", "").replace(/ /g, "_"));
50-
const desc = ancestors.join("/");
51-
const name = test.title.replace(/ /g, "_").replace(/[\.']/g, "");
52-
const filename = `${testRunConfig.path.split("/test")[0]}/screenshots/${paths.RESULTS_TAGNAME}/${desc}/${name}.diff.png`;
53-
const alt_filename = `screenshots/${desc}/${name}.diff.png`;
54-
if (filename) {
55-
this.write_img(test.title, ancestors, filename);
56-
} else if (fs.existsSync(alt_filename)) {
57-
this.write_img(test.title, ancestors, alt_filename);
49+
const name = test.title.replace(/[ \.']/g, "_");
50+
let desc = test.fullName.replace(".html", "").replace(/ /g, "_");
51+
desc = desc.slice(0, desc.length - name.length - 1);
52+
const candidates = [
53+
`${testRunConfig.path.split("/test")[0]}/screenshots/${paths.RESULTS_TAGNAME}/${desc}/${name}.diff.png`,
54+
`screenshots/${desc}/${name}.diff.png`,
55+
`${testRunConfig.path.split("/test")[0]}/screenshots/${paths.RESULTS_TAGNAME}/${desc}/${name}.failed.png`,
56+
`screenshots/${desc}/${name}.failed.png`,
57+
`${testRunConfig.path.split("/test")[0]}/screenshots/${paths.RESULTS_TAGNAME}/${desc}/${name}.png`,
58+
`screenshots/${desc}/${name}.png`
59+
];
60+
61+
for (const filename of candidates) {
62+
if (fs.existsSync(filename)) {
63+
this.write_img(test.title, test.ancestorTitles, filename);
64+
break;
65+
}
5866
}
5967
}
6068
}

0 commit comments

Comments
 (0)