Skip to content

Commit 097d6c5

Browse files
committed
[compiler][snap] Support pattern of files to test as CLI argument
Vibe-coded. When trying to use vibe coding on actual compiler work, the agent kept wanting to do `yarn snap <test>` to get the output of a file. It was tedious to tell it to use the testfilter file. This lets us quickly run a set of files or a specific path and get debug output. Open to feedback!
1 parent 189efa4 commit 097d6c5

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ 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+
4762
export async function readTestFilter(): Promise<TestFilter | null> {
4863
if (!(await exists(FILTER_PATH))) {
4964
throw new Error(`testfilter file not found at \`${FILTER_PATH}\``);
@@ -111,11 +126,18 @@ async function readInputFixtures(
111126
} else {
112127
inputFiles = (
113128
await Promise.all(
114-
filter.paths.map(pattern =>
115-
glob.glob(`${pattern}{${INPUT_EXTENSIONS.join(',')}}`, {
129+
filter.paths.map(pattern => {
130+
// Check if pattern already has an extension
131+
const basename = path.basename(pattern);
132+
const basenameWithoutExt = stripAllExtensions(basename);
133+
const hasExtension = basename !== basenameWithoutExt;
134+
const globPattern = hasExtension
135+
? pattern
136+
: `${pattern}{${INPUT_EXTENSIONS.join(',')}}`;
137+
return glob.glob(globPattern, {
116138
cwd: rootDir,
117-
}),
118-
),
139+
});
140+
}),
119141
)
120142
).flat();
121143
}
@@ -150,11 +172,24 @@ async function readOutputFixtures(
150172
} else {
151173
outputFiles = (
152174
await Promise.all(
153-
filter.paths.map(pattern =>
154-
glob.glob(`${pattern}${SNAPSHOT_EXTENSION}`, {
175+
filter.paths.map(pattern => {
176+
// If pattern already ends with .expect.md, use it as-is
177+
if (pattern.endsWith(SNAPSHOT_EXTENSION)) {
178+
return glob.glob(pattern, {
179+
cwd: rootDir,
180+
});
181+
}
182+
// Otherwise strip any extension before adding .expect.md
183+
const basename = path.basename(pattern);
184+
const basenameWithoutExt = stripAllExtensions(pattern);
185+
const patternWithoutExt =
186+
basename !== basenameWithoutExt
187+
? pattern.slice(0, -path.extname(pattern).length)
188+
: pattern;
189+
return glob.glob(`${patternWithoutExt}${SNAPSHOT_EXTENSION}`, {
155190
cwd: rootDir,
156-
}),
157-
),
191+
});
192+
}),
158193
)
159194
).flat();
160195
}

compiler/packages/snap/src/runner.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type RunnerOptions = {
3535
watch: boolean;
3636
filter: boolean;
3737
update: boolean;
38+
pattern?: string;
3839
};
3940

4041
const opts: RunnerOptions = yargs
@@ -62,9 +63,15 @@ const opts: RunnerOptions = yargs
6263
'Only run fixtures which match the contents of testfilter.txt',
6364
)
6465
.default('filter', false)
66+
.string('pattern')
67+
.alias('p', 'pattern')
68+
.describe(
69+
'pattern',
70+
'Optional glob pattern to filter fixtures (e.g., "error.*", "use-memo")',
71+
)
6572
.help('help')
6673
.strict()
67-
.parseSync(hideBin(process.argv));
74+
.parseSync(hideBin(process.argv)) as RunnerOptions;
6875

6976
/**
7077
* Do a test run and return the test results
@@ -171,7 +178,10 @@ export async function main(opts: RunnerOptions): Promise<void> {
171178
worker.getStderr().pipe(process.stderr);
172179
worker.getStdout().pipe(process.stdout);
173180

174-
if (opts.watch) {
181+
// If pattern is provided, force watch mode off and use pattern filter
182+
const shouldWatch = opts.watch && opts.pattern == null;
183+
184+
if (shouldWatch) {
175185
makeWatchRunner(state => onChange(worker, state), opts.filter);
176186
if (opts.filter) {
177187
/**
@@ -216,7 +226,18 @@ export async function main(opts: RunnerOptions): Promise<void> {
216226
try {
217227
execSync('yarn build', {cwd: PROJECT_ROOT});
218228
console.log('Built compiler successfully with tsup');
219-
const testFilter = opts.filter ? await readTestFilter() : null;
229+
230+
// Determine which filter to use
231+
let testFilter: TestFilter | null = null;
232+
if (opts.pattern) {
233+
testFilter = {
234+
debug: true,
235+
paths: [opts.pattern],
236+
};
237+
} else if (opts.filter) {
238+
testFilter = await readTestFilter();
239+
}
240+
220241
const results = await runFixtures(worker, testFilter, 0);
221242
if (opts.update) {
222243
update(results);

0 commit comments

Comments
 (0)