4
4
5
5
const childProcess = require ( 'child_process' ) ;
6
6
const fs = require ( 'fs' ) ;
7
+ const glob = require ( 'glob' ) ;
7
8
const path = require ( 'path' ) ;
8
9
const util = require ( 'util' ) ;
9
10
const yargs = require ( 'yargs' ) ;
@@ -20,10 +21,16 @@ const yargsObject = yargs
20
21
const shouldRemoveFiles = yargsObject . removeFiles === true ;
21
22
const SOURCE_ROOT = path . resolve ( __dirname , path . join ( '..' , '..' ) ) ;
22
23
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.
25
28
const GOLDENS_LOCATION = path . join ( interactionTestRoot , 'goldens' ) ;
26
29
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
+
27
34
function findScreenshotsToCheck ( folder ) {
28
35
const filesToCheck = [ ] ;
29
36
const filesInFolder = fs . readdirSync ( folder ) ;
@@ -39,63 +46,42 @@ function findScreenshotsToCheck(folder) {
39
46
return filesToCheck ;
40
47
}
41
48
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 ;
67
54
}
68
- console . warn ( error ) ;
69
- return false ;
70
55
}
56
+ return false ;
71
57
}
72
58
73
- async function checkGoldensForPlatform ( platform ) {
59
+ function checkGoldensForPlatform ( platform ) {
74
60
const obsoleteImages = [ ] ;
75
61
76
62
const platformRoot = path . join ( GOLDENS_LOCATION , platform ) ;
77
63
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 (
81
68
relativeGoldenPath ,
82
- interactionTestRoot ,
69
+ interactionTestFiles ,
83
70
) ;
84
- const units = await checkFolder ( relativeGoldenPath , unitTestRoot ) ;
71
+ const units = checkFolder ( relativeGoldenPath , unitTestFiles ) ;
85
72
86
73
if ( ! interactions && ! units ) {
87
74
obsoleteImages . push ( path . join ( platform , relativeGoldenPath ) ) ;
88
75
}
89
76
}
90
-
91
77
return obsoleteImages ;
92
78
}
93
79
94
80
async function run ( ) {
95
81
const obsoleteImages = [
96
- ...( await checkGoldensForPlatform ( 'linux' ) ) ,
97
- ...( await checkGoldensForPlatform ( 'mac' ) ) ,
98
- ...( await checkGoldensForPlatform ( 'win32' ) ) ,
82
+ ...checkGoldensForPlatform ( 'linux' ) ,
83
+ ...checkGoldensForPlatform ( 'mac' ) ,
84
+ ...checkGoldensForPlatform ( 'win32' ) ,
99
85
] ;
100
86
if ( obsoleteImages . length > 0 ) {
101
87
console . log (
0 commit comments