Skip to content

Commit 1bc9f46

Browse files
committed
test_runner: create reporter destination directories
1 parent fb6693e commit 1bc9f46

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

lib/internal/test_runner/utils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const {
3535

3636
const { AsyncResource } = require('async_hooks');
3737
const { tracingChannel } = require('diagnostics_channel');
38-
const { relative, sep, resolve } = require('path');
39-
const { createWriteStream, readFileSync } = require('fs');
38+
const { dirname, relative, sep, resolve } = require('path');
39+
const { createWriteStream, mkdirSync, readFileSync } = require('fs');
4040
const { pathToFileURL } = require('internal/url');
4141
const { getOptionValue } = require('internal/options');
4242
const { green, yellow, red, white, shouldColorize } = require('internal/util/colors');
@@ -199,8 +199,11 @@ function parsePreviousRuns(rerunFailuresFilePath) {
199199

200200
async function getReportersMap(reporters, destinations) {
201201
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
202-
const destination = kBuiltinDestinations.get(destinations[i]) ??
203-
createWriteStream(destinations[i], { __proto__: null, flush: true });
202+
const destinationPath = destinations[i];
203+
const destination = kBuiltinDestinations.get(destinationPath) ?? (
204+
mkdirSync(dirname(destinationPath), { recursive: true }),
205+
createWriteStream(destinationPath, { __proto__: null, flush: true })
206+
);
204207

205208
// Load the test reporter passed to --test-reporter
206209
let reporter = tryBuiltinReporter(name);

test/parallel/test-runner-reporters.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ describe('node:test reporters', { concurrency: true }, () => {
7676
assert.match(fileContents, / nested/);
7777
});
7878

79+
it('should create parent directories for a file destination', async () => {
80+
const file = tmpdir.resolve(`${tmpFiles++}/nested/report.out`);
81+
const child = spawnSync(process.execPath,
82+
['--test', '--test-reporter', 'dot', '--test-reporter-destination', file, testFile]);
83+
assert.strictEqual(child.stderr.toString(), '');
84+
assert.strictEqual(child.stdout.toString(), '');
85+
const fileContents = fs.readFileSync(file, 'utf8');
86+
assert.match(fileContents, /\.XX\.\n/);
87+
assert.match(fileContents, /Failed tests:/);
88+
assert.match(fileContents, / failing/);
89+
assert.match(fileContents, / nested/);
90+
});
91+
7992
it('should disallow using v8-serializer as reporter', async () => {
8093
const child = spawnSync(process.execPath, ['--test', '--test-reporter', 'v8-serializer', testFile]);
8194
assert.strictEqual(child.stdout.toString(), '');

0 commit comments

Comments
 (0)