44
55const childProcess = require ( 'child_process' ) ;
66const fs = require ( 'fs' ) ;
7+ const glob = require ( 'glob' ) ;
78const path = require ( 'path' ) ;
89const util = require ( 'util' ) ;
910const yargs = require ( 'yargs' ) ;
@@ -20,10 +21,16 @@ const yargsObject = yargs
2021const shouldRemoveFiles = yargsObject . removeFiles === true ;
2122const SOURCE_ROOT = path . resolve ( __dirname , path . join ( '..' , '..' ) ) ;
2223const 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.
2528const 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+
2734function 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
9480async 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