44
return filename;
45
}
46
47
+/**
48
+ * Strip all extensions from a filename
49
+ * e.g., "foo.expect.md" -> "foo"
50
+ */
51
+function stripAllExtensions(filename: string): string {
52
+ let result = filename;
53
+ while (true) {
54
+ const extension = path.extname(result);
55
+ if (extension === '') {
56
+ return result;
57
+ }
58
+ result = path.basename(result, extension);
59
+ }
60
+}
61
+
62
export async function readTestFilter(): Promise<TestFilter | null> {
63
if (!(await exists(FILTER_PATH))) {
64
throw new Error(`testfilter file not found at \`${FILTER_PATH}\``);
126
} else {
127
inputFiles = (
128
await Promise.all(
114
- filter.paths.map(pattern =>
115
- glob.glob(`${pattern}{${INPUT_EXTENSIONS.join(',')}}`, {
129
+ filter.paths.map(pattern => {
130
+ // If the pattern already has an extension other than .expect.md,
131
+ // search for the pattern directly. Otherwise, search for the
132
+ // pattern with the expected input extensions added.
133
+ // Eg
134
+ // `alias-while` => search for `alias-while{.js,.jsx,.ts,.tsx}`
135
+ // `alias-while.js` => search as-is
136
+ // `alias-while.expect.md` => search for `alias-while{.js,.jsx,.ts,.tsx}`
137
+ const basename = path.basename(pattern);
138
+ const basenameWithoutExt = stripAllExtensions(basename);
139
+ const hasExtension = basename !== basenameWithoutExt;
140
+ const globPattern =
141
+ hasExtension && !pattern.endsWith(SNAPSHOT_EXTENSION)
142
+ ? pattern
143
+ : `${basenameWithoutExt}{${INPUT_EXTENSIONS.join(',')}}`;
144
+ return glob.glob(globPattern, {
145
cwd: rootDir,
117
- }),
118
- ),
146
+ });
147
+ }),
148
)
149
).flat();
150
}
179
} else {
180
outputFiles = (
181
await Promise.all(
153
- filter.paths.map(pattern =>
154
- glob.glob(`${pattern}${SNAPSHOT_EXTENSION}`, {
182
+ filter.paths.map(pattern => {
183
+ // Strip all extensions and find matching .expect.md files
184
+ const basenameWithoutExt = stripAllExtensions(pattern);
185
+ return glob.glob(`${basenameWithoutExt}${SNAPSHOT_EXTENSION}`, {
186
cwd: rootDir,
156
- }),
157
- ),
187
+ });
188
+ }),
189
)
190
).flat();
191
}