Skip to content

Commit

Permalink
tests(cli): ensure missing directories are created when writing to file
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Bracy <[email protected]>
  • Loading branch information
servusdei2018 committed Nov 13, 2024
1 parent 5ea6d4d commit 7c9b5b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function writeFile(filePath, output, outputMode) {
if (err) {
return reject(err);
}
log.log('Printer', `${LH.OutputMode[outputMode]} output written to ${filePath}`);
log.log('Printer', `${OutputMode[outputMode]} output written to ${filePath}`);
resolve();
});
});
Expand Down
17 changes: 17 additions & 0 deletions cli/test/cli/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import assert from 'assert/strict';
import fs from 'fs';
import path from 'path';

import {readJson} from '../../../core/test/test-utils.js';
import * as Printer from '../../printer.js';
Expand Down Expand Up @@ -49,4 +50,20 @@ describe('Printer', () => {
assert.strictEqual(typeof mode, 'string');
});
});

it('creates missing directories when writing to file', () => {
const dirPath = './non/existent/directory/.test-file.json';
const report = JSON.stringify(sampleResults);
const dir = path.dirname(dirPath);
if (fs.existsSync(dir)) {
fs.rmdirSync(dir, { recursive: true });
}
return Printer.write(report, 'json', dirPath).then(_ => {
assert.ok(fs.existsSync(dir), `Directory ${dir} should exist now`);
const fileContents = fs.readFileSync(dirPath, 'utf8');
assert.ok(/lighthouseVersion/gim.test(fileContents));
fs.unlinkSync(dirPath);
fs.rmdirSync(dir, { recursive: true });
});
});
});

0 comments on commit 7c9b5b8

Please sign in to comment.