@@ -4,7 +4,7 @@ const { getTestNames } = require('find-test-names')
4
4
const fs = require ( 'fs' )
5
5
const path = require ( 'path' )
6
6
const { version } = require ( '../package.json' )
7
- const { parseGrep, shouldTestRun } = require ( './utils' )
7
+ const { resolveConfig , parseGrep, shouldTestRun } = require ( './utils' )
8
8
9
9
/**
10
10
* Prints the cypress-grep environment values if any.
@@ -15,64 +15,63 @@ function cypressGrepPlugin(config) {
15
15
return config
16
16
}
17
17
18
- debug ( 'Cypress config env object: %o' , config . env )
19
- debug ( 'plugin version %s' , version )
20
- const grep = config . env . grep ? String ( config . env . grep ) : undefined
18
+ const { env } = config
19
+
20
+ debug (
21
+ 'Config type: %s' ,
22
+ env . grepNewConfig
23
+ ? 'new (Cypress version >= v10.0.0)'
24
+ : 'legacy (Cypress version < v10.0.0)' ,
25
+ )
26
+ debug ( 'cypress-grep plugin version %s' , version )
27
+ debug ( 'Cypress config env object: %o' , env )
28
+
29
+ const grep = env . grep ? String ( env . grep ) : undefined
21
30
if ( grep ) {
22
31
console . log ( 'cypress-grep: tests with "%s" in their names' , grep . trim ( ) )
23
32
}
24
33
25
- const grepTags = config . env . grepTags || config . env [ 'grep-tags' ]
34
+ const grepTags = env . grepTags || env [ 'grep-tags' ]
26
35
if ( grepTags ) {
27
36
console . log ( 'cypress-grep: filtering using tag(s) "%s"' , grepTags )
28
37
const parsedGrep = parseGrep ( null , grepTags )
29
38
debug ( 'parsed grep tags %o' , parsedGrep . tags )
30
39
}
31
40
32
- const grepBurn =
33
- config . env . grepBurn || config . env [ 'grep-burn' ] || config . env . burn
41
+ const grepBurn = env . grepBurn || env [ 'grep-burn' ] || env . burn
34
42
if ( grepBurn ) {
35
43
console . log ( 'cypress-grep: running filtered tests %d times' , grepBurn )
36
44
}
37
45
38
- const grepUntagged = config . env . grepUntagged || config . env [ 'grep-untagged' ]
46
+ const grepUntagged = env . grepUntagged || env [ 'grep-untagged' ]
39
47
if ( grepUntagged ) {
40
48
console . log ( 'cypress-grep: running untagged tests' )
41
49
}
42
50
43
- const omitFiltered =
44
- config . env . grepOmitFiltered || config . env [ 'grep-omit-filtered' ]
51
+ const omitFiltered = env . grepOmitFiltered || env [ 'grep-omit-filtered' ]
45
52
if ( omitFiltered ) {
46
53
console . log ( 'cypress-grep: will omit filtered tests' )
47
54
}
48
55
49
- const grepFilterSpecs = config . env . grepFilterSpecs === true
56
+ const { resolvedConfig } = resolveConfig ( config )
57
+ const { specPattern, excludeSpecPattern, integrationFolder } = resolvedConfig
58
+
59
+ const grepFilterSpecs = env . grepFilterSpecs === true
50
60
if ( grepFilterSpecs ) {
61
+ debug ( resolvedConfig )
62
+ const specFiles = globby . sync ( specPattern , {
63
+ ignore : excludeSpecPattern ,
64
+ absolute : false ,
65
+ } )
66
+ debug ( 'found %d spec files' , specFiles . length )
67
+ debug ( '%o' , specFiles )
68
+ let greppedSpecs = [ ]
51
69
if ( grep ) {
52
70
console . log ( 'cypress-grep: filtering specs using "%s" in the title' , grep )
53
-
54
- debug ( {
55
- integrationFolder : config . integrationFolder ,
56
- testFiles : config . testFiles ,
57
- ignoreTestFiles : config . ignoreTestFiles ,
58
- } )
59
-
60
- const specFiles = globby . sync ( config . testFiles , {
61
- cwd : config . integrationFolder ,
62
- ignore : config . ignoreTestFiles ,
63
- absolute : false ,
64
- } )
65
- debug ( 'found %d spec files' , specFiles . length )
66
- debug ( '%o' , specFiles )
67
-
68
71
const parsedGrep = parseGrep ( grep )
69
72
debug ( 'parsed grep %o' , parsedGrep )
70
-
71
- const specsWithText = specFiles . filter ( ( specFile ) => {
72
- const text = fs . readFileSync (
73
- path . join ( config . integrationFolder , specFile ) ,
74
- 'utf8' ,
75
- )
73
+ greppedSpecs = specFiles . filter ( ( specFile ) => {
74
+ const text = fs . readFileSync ( specFile , { encoding : 'utf8' } )
76
75
try {
77
76
const names = getTestNames ( text )
78
77
const testAndSuiteNames = names . suiteNames . concat ( names . testNames )
@@ -91,36 +90,13 @@ function cypressGrepPlugin(config) {
91
90
return true
92
91
}
93
92
} )
94
-
95
- debug ( 'found grep "%s" in %d specs' , grep , specsWithText . length )
96
- debug ( '%o' , specsWithText )
97
-
98
- config . testFiles = specsWithText
93
+ debug ( 'found grep "%s" in %d specs' , grep , greppedSpecs . length )
94
+ debug ( '%o' , greppedSpecs )
99
95
} else if ( grepTags ) {
100
- console . log ( 'cypress-grep: filtering specs using tag "%s"' , grepTags )
101
-
102
- debug ( {
103
- integrationFolder : config . integrationFolder ,
104
- testFiles : config . testFiles ,
105
- ignoreTestFiles : config . ignoreTestFiles ,
106
- } )
107
-
108
- const specFiles = globby . sync ( config . testFiles , {
109
- cwd : config . integrationFolder ,
110
- ignore : config . ignoreTestFiles ,
111
- absolute : false ,
112
- } )
113
- debug ( 'found %d spec files' , specFiles . length )
114
- debug ( '%o' , specFiles )
115
-
116
96
const parsedGrep = parseGrep ( null , grepTags )
117
97
debug ( 'parsed grep tags %o' , parsedGrep )
118
-
119
- const specsWithText = specFiles . filter ( ( specFile ) => {
120
- const text = fs . readFileSync (
121
- path . join ( config . integrationFolder , specFile ) ,
122
- 'utf8' ,
123
- )
98
+ greppedSpecs = specFiles . filter ( ( specFile ) => {
99
+ const text = fs . readFileSync ( specFile , { encoding : 'utf8' } )
124
100
try {
125
101
const testInfo = getTestNames ( text )
126
102
debug ( 'spec file %s' , specFile )
@@ -136,17 +112,19 @@ function cypressGrepPlugin(config) {
136
112
return true
137
113
}
138
114
} )
139
-
140
- debug ( 'found grep tags "%s" in %d specs' , grepTags , specsWithText . length )
141
- debug ( '%o' , specsWithText )
142
-
143
- if ( specsWithText . length ) {
144
- config . testFiles = specsWithText
145
- } else {
146
- // hmm, we filtered out all specs, probably something is wrong
147
- console . warn ( 'Grep "%s" has eliminated all specs' , grep )
148
- console . warn ( 'Will leave all specs to run to filter at run-time' )
149
- }
115
+ debug ( 'found grep tags "%s" in %d specs' , grepTags , greppedSpecs . length )
116
+ debug ( '%o' , greppedSpecs )
117
+ }
118
+ if ( greppedSpecs . length ) {
119
+ env . grepNewConfig
120
+ ? ( config . specPattern = greppedSpecs )
121
+ : ( config . testFiles = greppedSpecs )
122
+ } else {
123
+ // hmm, we filtered out all specs, probably something is wrong
124
+ console . warn ( 'grep and/or grepTags has eliminated all specs' )
125
+ grep ? console . warn ( 'grep: %s' , grep ) : null
126
+ grepTags ? console . warn ( 'grepTags: %s' , grepTags ) : null
127
+ console . warn ( 'Will leave all specs to run to filter at run-time' )
150
128
}
151
129
}
152
130
0 commit comments