Skip to content

Commit 065f241

Browse files
OrKoNDevtools-frontend LUCI CQ
authored and
Devtools-frontend LUCI CQ
committed
Optimize golden check script
Takes ~1 sec now instead of ~170 sec. Fixed: 408406314 Change-Id: I9f2f2a693a28fb93e34e6d98aaffa724cc1ef1d0 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6450888 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Alex Rudenko <alexrudenko@chromium.org>
1 parent 272fb83 commit 065f241

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

scripts/test/check_obsolete_goldens.js

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const childProcess = require('child_process');
66
const fs = require('fs');
7+
const glob = require('glob');
78
const path = require('path');
89
const util = require('util');
910
const yargs = require('yargs');
@@ -20,10 +21,16 @@ const yargsObject = yargs
2021
const shouldRemoveFiles = yargsObject.removeFiles === true;
2122
const SOURCE_ROOT = path.resolve(__dirname, path.join('..', '..'));
2223
const interactionTestRoot = path.join(SOURCE_ROOT, 'test', 'interactions');
23-
// TODO: grep seems slow on the entire front_end folder.
24-
const unitTestRoot = path.join(SOURCE_ROOT, 'front_end', 'panels');
24+
25+
const unitTestRoot = path.join(SOURCE_ROOT, 'front_end');
26+
// TODO: update the goldens location once interaction tests are
27+
// migrated.
2528
const GOLDENS_LOCATION = path.join(interactionTestRoot, 'goldens');
2629

30+
const interactionTestFiles =
31+
glob.sync('**/*_test.ts', {cwd: interactionTestRoot}).map(file => path.join(interactionTestRoot, file));
32+
const unitTestFiles = glob.sync('**/*.test.ts', {cwd: unitTestRoot}).map(file => path.join(unitTestRoot, file));
33+
2734
function findScreenshotsToCheck(folder) {
2835
const filesToCheck = [];
2936
const filesInFolder = fs.readdirSync(folder);
@@ -39,63 +46,42 @@ function findScreenshotsToCheck(folder) {
3946
return filesToCheck;
4047
}
4148

42-
async function checkFolder(relativeGoldenPath, searchRoot) {
43-
// Filepaths in screenshot tests assertions are used using forward slashes.
44-
// If this is executed in windows `relativeGoldenPath` will come with
45-
// backward slashes, so the path needs to be fixed.
46-
const unixRelativeGoldenPath = relativeGoldenPath.replace(/\\/g, '/');
47-
const isWin = process.platform === 'win32';
48-
if (isWin) {
49-
// Currently, we do not assert screenshots on Windows.
50-
// Eventually, if we support all platforms we can remove this early
51-
// exit.
52-
return true;
53-
}
54-
const textSearchCommand = isWin ?
55-
`GET-CHILDITEM ${searchRoot}* -recurs | Select-String -Pattern "${unixRelativeGoldenPath}" -CaseSensitive` :
56-
`grep -r ${unixRelativeGoldenPath} ${searchRoot}`;
57-
try {
58-
// If this doesn't throw, that means we found a match and we're fine.
59-
await exec(
60-
textSearchCommand,
61-
isWin ? {shell: 'powershell.exe'} : undefined,
62-
);
63-
return true;
64-
} catch (error) {
65-
if (error.code === 1) {
66-
return false;
49+
function checkFolder(relativeGoldenPath, filesToSearch) {
50+
for (const file of filesToSearch) {
51+
const content = fs.readFileSync(file, 'utf-8');
52+
if (content.includes(relativeGoldenPath)) {
53+
return true;
6754
}
68-
console.warn(error);
69-
return false;
7055
}
56+
return false;
7157
}
7258

73-
async function checkGoldensForPlatform(platform) {
59+
function checkGoldensForPlatform(platform) {
7460
const obsoleteImages = [];
7561

7662
const platformRoot = path.join(GOLDENS_LOCATION, platform);
7763
const goldens = findScreenshotsToCheck(platformRoot);
78-
for await (const golden of goldens) {
79-
const relativeGoldenPath = path.relative(platformRoot, golden);
80-
const interactions = await checkFolder(
64+
65+
for (const golden of goldens) {
66+
const relativeGoldenPath = path.relative(platformRoot, golden).replace(/\\/g, '/');
67+
const interactions = checkFolder(
8168
relativeGoldenPath,
82-
interactionTestRoot,
69+
interactionTestFiles,
8370
);
84-
const units = await checkFolder(relativeGoldenPath, unitTestRoot);
71+
const units = checkFolder(relativeGoldenPath, unitTestFiles);
8572

8673
if (!interactions && !units) {
8774
obsoleteImages.push(path.join(platform, relativeGoldenPath));
8875
}
8976
}
90-
9177
return obsoleteImages;
9278
}
9379

9480
async function run() {
9581
const obsoleteImages = [
96-
...(await checkGoldensForPlatform('linux')),
97-
...(await checkGoldensForPlatform('mac')),
98-
...(await checkGoldensForPlatform('win32')),
82+
...checkGoldensForPlatform('linux'),
83+
...checkGoldensForPlatform('mac'),
84+
...checkGoldensForPlatform('win32'),
9985
];
10086
if (obsoleteImages.length > 0) {
10187
console.log(

0 commit comments

Comments
 (0)