mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
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>
This commit is contained in:
co-authored by
Steven
Tony Brix
parent
47a140acd0
commit
bf44ae85ed
+4
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+18
-4
@@ -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: '<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
|
||||
}));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user