From bf44ae85edac35627be9acaeebb969c4b29a6505 Mon Sep 17 00:00:00 2001 From: Rahul Zhade Date: Fri, 26 Jan 2024 16:26:11 -0800 Subject: [PATCH] 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 * Update tests --------- Co-authored-by: Tony Brix Co-authored-by: Steven --- bin/main.js | 8 ++++---- test/unit/bin.test.js | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bin/main.js b/bin/main.js index 97ac522c41..758ef56adb 100644 --- a/bin/main.js +++ b/bin/main.js @@ -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); } @@ -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); } } diff --git a/test/unit/bin.test.js b/test/unit/bin.test.js index e9e0bcbbd3..00c3fa06f7 100644 --- a/test/unit/bin.test.js +++ b/test/unit/bin.test.js @@ -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; }) } @@ -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) { @@ -91,9 +91,23 @@ describe('bin/marked', () => { stdout: '

line1
line2

' })); - 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 })); });