Skip to content

Commit 9ce648e

Browse files
fix(rstack): isolate related-test JSON output
1 parent c020a7d commit 9ce648e

2 files changed

Lines changed: 47 additions & 20 deletions

File tree

‎packages/rstack/src/relatedTests.ts‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { execFile } from 'node:child_process';
2+
import { mkdtemp, readFile, rm } from 'node:fs/promises';
3+
import os from 'node:os';
24
import path from 'node:path';
35
import type { RelatedTestRequest } from '@rstackjs/context';
46

@@ -36,24 +38,34 @@ const resolveRelatedTests = async (
3638
request: RelatedTestRequest,
3739
dependencies: RelatedTestResolverDependencies = {},
3840
): Promise<string[]> => {
39-
const args = [
40-
path.join(import.meta.dirname, '..', 'bin', 'rs.js'),
41-
'test',
42-
'list',
43-
'--related',
44-
...request.sources,
45-
'--filesOnly',
46-
'--json',
47-
...(request.configPath === undefined ? [] : ['--config', request.configPath]),
48-
];
49-
const { stdout, stderr } = await (dependencies.runCli ?? runCli)({
50-
cwd: request.packageRoot,
51-
args,
52-
});
41+
const outputDirectory = await mkdtemp(path.join(os.tmpdir(), 'rstack-related-tests-'));
42+
const outputFile = path.join(outputDirectory, 'tests.json');
43+
let source: string;
44+
let stderr: string;
45+
try {
46+
const args = [
47+
path.join(import.meta.dirname, '..', 'bin', 'rs.js'),
48+
'test',
49+
'list',
50+
'--related',
51+
...request.sources,
52+
'--filesOnly',
53+
'--json',
54+
outputFile,
55+
...(request.configPath === undefined ? [] : ['--config', request.configPath]),
56+
];
57+
({ stderr } = await (dependencies.runCli ?? runCli)({
58+
cwd: request.packageRoot,
59+
args,
60+
}));
61+
source = await readFile(outputFile, 'utf8');
62+
} finally {
63+
await rm(outputDirectory, { force: true, recursive: true });
64+
}
5365

5466
let parsed: unknown;
5567
try {
56-
parsed = JSON.parse(stdout);
68+
parsed = JSON.parse(source);
5769
} catch {
5870
throw new Error(
5971
`Rstest related-test listing returned invalid JSON${stderr.length === 0 ? '.' : `: ${stderr.trim()}`}`,

‎packages/rstack/tests/context/relatedTests.test.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
import { writeFile } from 'node:fs/promises';
12
import path from 'node:path';
23
import { expect, test } from '@rstest/core';
34
import { resolveRelatedTests } from '../../src/relatedTests.ts';
45

6+
const getOutputFile = (args: string[]): string => {
7+
const outputFile = args[args.indexOf('--json') + 1];
8+
if (outputFile === undefined) throw new Error('Missing related-test output file.');
9+
return outputFile;
10+
};
11+
512
test('lists related tests through the current Rstack CLI and normalizes its JSON result', async () => {
613
const calls: unknown[] = [];
714
const packageRoot = path.resolve('/workspace/packages/app');
@@ -16,13 +23,14 @@ test('lists related tests through the current Rstack CLI and normalizes its JSON
1623
{
1724
runCli: (request) => {
1825
calls.push(request);
19-
return Promise.resolve({
20-
stdout: JSON.stringify([
26+
const outputFile = getOutputFile(request.args);
27+
return writeFile(
28+
outputFile,
29+
JSON.stringify([
2130
{ file: 'tests/index.test.ts', type: 'file' },
2231
{ file: path.join(packageRoot, 'tests/index.test.ts'), type: 'file' },
2332
]),
24-
stderr: '',
25-
});
33+
).then(() => ({ stdout: 'config log that is not JSON', stderr: '' }));
2634
},
2735
},
2836
);
@@ -38,6 +46,7 @@ test('lists related tests through the current Rstack CLI and normalizes its JSON
3846
path.join(packageRoot, 'src/index.ts'),
3947
'--filesOnly',
4048
'--json',
49+
expect.stringMatching(/rstack-related-tests-/u),
4150
'--config',
4251
configPath,
4352
],
@@ -50,7 +59,13 @@ test('reports invalid related-test JSON as an Rstest list failure', async () =>
5059
await expect(
5160
resolveRelatedTests(
5261
{ packageRoot: '/workspace', sources: ['/workspace/src/index.ts'] },
53-
{ runCli: () => Promise.resolve({ stdout: 'not json', stderr: 'list failed' }) },
62+
{
63+
runCli: (request) =>
64+
writeFile(getOutputFile(request.args), 'not json').then(() => ({
65+
stdout: '',
66+
stderr: 'list failed',
67+
})),
68+
},
5469
),
5570
).rejects.toThrow('Rstest related-test listing returned invalid JSON');
5671
});

0 commit comments

Comments
 (0)