Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ENOENT error message in CLI #3165

Merged
merged 10 commits into from
Jan 27, 2024
8 changes: 4 additions & 4 deletions bin/main.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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