-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Expand file tree
/
Copy pathget-test-filter.js
More file actions
151 lines (132 loc) · 4.61 KB
/
Copy pathget-test-filter.js
File metadata and controls
151 lines (132 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const path = require('path')
const minimatch = require('minimatch')
function mergeManifests(manifests) {
return manifests.reduce((mergedManifest, manifest) => {
if (!mergedManifest) {
return structuredClone(manifest)
}
if (manifest.version === 2) {
for (const suite in manifest.suites) {
if (mergedManifest.suites[suite]) {
const mergedSuite = mergedManifest.suites[suite]
const currentSuite = manifest.suites[suite]
mergedSuite.failed = [
...(mergedSuite.failed || []),
...(currentSuite.failed || []),
]
mergedSuite.flakey = [
...(mergedSuite.flakey || []),
...(currentSuite.flakey || []),
]
} else {
mergedManifest.suites[suite] = structuredClone(manifest.suites[suite])
}
}
mergedManifest.rules.include.push(...(manifest.rules.include || []))
mergedManifest.rules.exclude.push(...(manifest.rules.exclude || []))
return mergedManifest
}
throw new Error(`Merging manifests is only supported for version 2`)
}, null)
}
function getManifest() {
const nextExternalTestFilters = process.env.NEXT_EXTERNAL_TESTS_FILTERS
if (!nextExternalTestFilters) {
return null
}
return mergeManifests(
nextExternalTestFilters.split(',').map((manifestPath) => {
return require(path.resolve(manifestPath))
})
)
}
function getTestFilterFromManifest(manifest) {
if (!manifest) return null
// For the legacy manifest without a version, we assume it's a complete list
// of all the tests.
if (!manifest.version || typeof manifest.version !== 'number') {
return (tests) =>
tests
.filter((test) => {
const info = manifest[test.file]
// Include tests that are not in the manifest
return !info || !info.runtimeError
})
.map((test) => {
const info = manifest[test.file]
// Exclude failing and flakey tests, newly added tests are automatically included
if (info && (info.failed.length > 0 || info.flakey.length > 0)) {
test.excludedCases = info.failed.concat(info.flakey)
}
return test
})
}
// The new manifest version 2 only contains the list of tests that should
// be run, with exclusions added based on rules. Any new tests that are added
// will be automatically included if they match the include rules.
if (manifest.version === 2) {
return (tests) =>
tests
.filter((test) => {
// Check to see if this was included as-is in the manifest.
if (test.file in manifest.suites) {
// When merging multiple manifests, a test file may be included in
// the suites by one manifest, but excluded in the rules by another.
// If it's excluded by filename (and not by pattern), the exclusion
// takes precedence over the inclusion.
return !manifest.rules.exclude?.includes(test.file)
}
// If this file doesn't match any of the include patterns, then it
// should be excluded.
if (
manifest.rules.include.every(
(pattern) => !minimatch(test.file, pattern)
)
) {
return false
}
// If the file matches any of the exclude patterns, then it should be
// excluded.
if (
manifest.rules.exclude?.some((pattern) =>
minimatch(test.file, pattern)
)
) {
return false
}
// Otherwise, it should be included.
return true
})
.map((test) => {
const info = manifest.suites[test.file]
// If there's no info for this test, then it's a test that has no
// failures or flakey tests, so we can just include it as-is.
if (!info) {
return test
}
// Exclude failing and flakey tests, newly added tests are
// automatically included.
const { failed = [], flakey = [] } = info
if (failed.length > 0 || flakey.length > 0) {
test.excludedCases = failed.concat(flakey)
}
return test
})
}
throw new Error(`Unknown manifest version: ${manifest.version}`)
}
function getTestFilter() {
const manifest = getManifest()
if (!manifest) return null
console.log(
'Filtering tests using manifest:',
process.env.NEXT_EXTERNAL_TESTS_FILTERS
)
return getTestFilterFromManifest(manifest)
}
module.exports = {
getManifest,
getTestFilter,
getTestFilterFromManifest,
mergeManifests,
}