Skip to content

Commit

Permalink
fix: Fix ENOENT error message in CLI (#3165)
Browse files Browse the repository at this point in the history
* Update utility to only output err path

* Fix failing tests

* Update code to explicitly check for presence of input

* Add input file not found test

* Make error messaging consistent

* fix bin test mocks

* add --input test

* Remove race condition causing exist check

* Update bin/main.js

Co-authored-by: Steven <steven@ceriously.com>

* Update tests

---------

Co-authored-by: Tony Brix <tony@brix.ninja>
Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
3 people committed Jan 27, 2024
1 parent 47a140a commit bf44ae8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
8 changes: 4 additions & 4 deletions bin/main.js
Expand Up @@ -222,8 +222,7 @@ export async function main(nodeProcess) {

if (output) {
if (noclobber && await fileExists(output)) {
nodeProcess.stderr.write('marked: output file \'' + output + '\' already exists, disable the \'-n\' / \'--no-clobber\' flag to overwrite\n');
nodeProcess.exit(1);
throw Error('marked: output file \'' + output + '\' already exists, disable the \'-n\' / \'--no-clobber\' flag to overwrite\n');
}
return await writeFile(output, html);
}
Expand Down Expand Up @@ -271,9 +270,10 @@ export async function main(nodeProcess) {
nodeProcess.exit(0);
} catch (err) {
if (err.code === 'ENOENT') {
nodeProcess.stderr.write('marked: output to ' + err.path + ': No such directory');
nodeProcess.stderr.write('marked: ' + err.path + ': No such file or directory');
} else {
nodeProcess.stderr.write(err.message);
}
nodeProcess.stderr.write(err);
return nodeProcess.exit(1);
}
}
22 changes: 18 additions & 4 deletions test/unit/bin.test.js
Expand Up @@ -32,7 +32,7 @@ function createMocks() {
on: mock.fn((method, func) => {
mocks.stdin[method] = func;
}),
resume: mock.fn
resume: mock.fn()
},
exit: mock.fn((code) => { mocks.code = code; })
}
Expand All @@ -44,7 +44,7 @@ function createMocks() {
function testInput({ args = [], stdin = '', stdinError = '', stdout = '', stderr = '', code = 0 } = {}) {
return async() => {
const mocks = createMocks();
mocks.process.argv = args;
mocks.process.argv = ['node', 'marked', ...args];
const mainPromise = main(mocks.process);
if (typeof mocks.stdin.end === 'function') {
if (stdin) {
Expand Down Expand Up @@ -91,9 +91,23 @@ describe('bin/marked', () => {
stdout: '<p>line1<br>line2</p>'
}));

it('not found', testInput({
it('config not found', testInput({
args: ['--config', fixturePath('does-not-exist.js'), '-s', 'line1\nline2'],
stderr: `Error: Cannot load config file '${fixturePath('does-not-exist.js')}'`,
stderr: `Cannot load config file '${fixturePath('does-not-exist.js')}'`,
code: 1
}));
});

describe('input', () => {
it('input file not found', testInput({
args: [fixturePath('does-not-exist.md')],
stderr: `marked: ${fixturePath('does-not-exist.md')}: No such file or directory`,
code: 1
}));

it('input file not found --input', testInput({
args: ['--input', fixturePath('does-not-exist.md')],
stderr: `marked: ${fixturePath('does-not-exist.md')}: No such file or directory`,
code: 1
}));
});
Expand Down

1 comment on commit bf44ae8

@vercel
Copy link

@vercel vercel bot commented on bf44ae8 Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.