Skip to content

Commit 627b583

Browse files
authored
[compiler][snap] Fix for filter mode with nested files, 'error.' prefix (facebook#35215)
Fixes some issues i ran into w my recent snap changes: * Correctly match against patterns that contain subdirectories, eg `fbt/fbt-call` * When checking if the input pattern has an extension, only prune known supported extensions. Our convention of `error.<name>` for fixtures that error makes the rest of the test name look like an extension to `path.extname()`. Tested with lots of different patterns including `error.` examples at the top level and in nested directories, etc.
1 parent fb18ad3 commit 627b583

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

compiler/packages/snap/src/fixture-utils.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,6 @@ function stripExtension(filename: string, extensions: Array<string>): string {
4444
return filename;
4545
}
4646

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-
6247
export async function readTestFilter(): Promise<TestFilter | null> {
6348
if (!(await exists(FILTER_PATH))) {
6449
throw new Error(`testfilter file not found at \`${FILTER_PATH}\``);
@@ -134,13 +119,15 @@ async function readInputFixtures(
134119
// `alias-while` => search for `alias-while{.js,.jsx,.ts,.tsx}`
135120
// `alias-while.js` => search as-is
136121
// `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;
122+
const patternWithoutExt = stripExtension(pattern, [
123+
...INPUT_EXTENSIONS,
124+
SNAPSHOT_EXTENSION,
125+
]);
126+
const hasExtension = pattern !== patternWithoutExt;
140127
const globPattern =
141128
hasExtension && !pattern.endsWith(SNAPSHOT_EXTENSION)
142129
? pattern
143-
: `${basenameWithoutExt}{${INPUT_EXTENSIONS.join(',')}}`;
130+
: `${patternWithoutExt}{${INPUT_EXTENSIONS.join(',')}}`;
144131
return glob.glob(globPattern, {
145132
cwd: rootDir,
146133
});
@@ -181,7 +168,10 @@ async function readOutputFixtures(
181168
await Promise.all(
182169
filter.paths.map(pattern => {
183170
// Strip all extensions and find matching .expect.md files
184-
const basenameWithoutExt = stripAllExtensions(pattern);
171+
const basenameWithoutExt = stripExtension(pattern, [
172+
...INPUT_EXTENSIONS,
173+
SNAPSHOT_EXTENSION,
174+
]);
185175
return glob.glob(`${basenameWithoutExt}${SNAPSHOT_EXTENSION}`, {
186176
cwd: rootDir,
187177
});

0 commit comments

Comments
 (0)