Skip to content

Commit bf44ae8

Browse files
rzhade3UziTechstyfle
authoredJan 27, 2024
fix: Fix ENOENT error message in CLI (#3165)
* 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>
1 parent 47a140a commit bf44ae8

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed
 

‎bin/main.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ export async function main(nodeProcess) {
222222

223223
if (output) {
224224
if (noclobber && await fileExists(output)) {
225-
nodeProcess.stderr.write('marked: output file \'' + output + '\' already exists, disable the \'-n\' / \'--no-clobber\' flag to overwrite\n');
226-
nodeProcess.exit(1);
225+
throw Error('marked: output file \'' + output + '\' already exists, disable the \'-n\' / \'--no-clobber\' flag to overwrite\n');
227226
}
228227
return await writeFile(output, html);
229228
}
@@ -271,9 +270,10 @@ export async function main(nodeProcess) {
271270
nodeProcess.exit(0);
272271
} catch (err) {
273272
if (err.code === 'ENOENT') {
274-
nodeProcess.stderr.write('marked: output to ' + err.path + ': No such directory');
273+
nodeProcess.stderr.write('marked: ' + err.path + ': No such file or directory');
274+
} else {
275+
nodeProcess.stderr.write(err.message);
275276
}
276-
nodeProcess.stderr.write(err);
277277
return nodeProcess.exit(1);
278278
}
279279
}

‎test/unit/bin.test.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function createMocks() {
3232
on: mock.fn((method, func) => {
3333
mocks.stdin[method] = func;
3434
}),
35-
resume: mock.fn
35+
resume: mock.fn()
3636
},
3737
exit: mock.fn((code) => { mocks.code = code; })
3838
}
@@ -44,7 +44,7 @@ function createMocks() {
4444
function testInput({ args = [], stdin = '', stdinError = '', stdout = '', stderr = '', code = 0 } = {}) {
4545
return async() => {
4646
const mocks = createMocks();
47-
mocks.process.argv = args;
47+
mocks.process.argv = ['node', 'marked', ...args];
4848
const mainPromise = main(mocks.process);
4949
if (typeof mocks.stdin.end === 'function') {
5050
if (stdin) {
@@ -91,9 +91,23 @@ describe('bin/marked', () => {
9191
stdout: '<p>line1<br>line2</p>'
9292
}));
9393

94-
it('not found', testInput({
94+
it('config not found', testInput({
9595
args: ['--config', fixturePath('does-not-exist.js'), '-s', 'line1\nline2'],
96-
stderr: `Error: Cannot load config file '${fixturePath('does-not-exist.js')}'`,
96+
stderr: `Cannot load config file '${fixturePath('does-not-exist.js')}'`,
97+
code: 1
98+
}));
99+
});
100+
101+
describe('input', () => {
102+
it('input file not found', testInput({
103+
args: [fixturePath('does-not-exist.md')],
104+
stderr: `marked: ${fixturePath('does-not-exist.md')}: No such file or directory`,
105+
code: 1
106+
}));
107+
108+
it('input file not found --input', testInput({
109+
args: ['--input', fixturePath('does-not-exist.md')],
110+
stderr: `marked: ${fixturePath('does-not-exist.md')}: No such file or directory`,
97111
code: 1
98112
}));
99113
});

1 commit comments

Comments
 (1)

vercel[bot] commented on Jan 27, 2024

@vercel[bot]
Please sign in to comment.