Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ export async function main(nodeProcess) {
if (string) {
return string;
}
if (!input) {
if (files.length === 0) {
return await getStdin();
}
input = files.pop();
if (input) {
return await readFile(input, 'utf8');
}
if (files.length > 0) {
return await readFile(files.pop(), 'utf8');
}
return await readFile(input, 'utf8');
return await getStdin();
}

function resolveFile(file) {
Expand Down Expand Up @@ -271,7 +271,7 @@ export async function main(nodeProcess) {
}

try {
await start(nodeProcess.argv.slice());
await start(nodeProcess.argv.slice(2));
nodeProcess.exit(0);
} catch(err) {
if (err.code === 'ENOENT') {
Expand Down
60 changes: 56 additions & 4 deletions test/unit/bin.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { main } from '../../bin/main.js';
import { htmlIsEqual } from '@markedjs/testutils';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolve } from 'node:path';
import { describe, it, mock } from 'node:test';
import assert from 'node:assert';
import { exec } from 'node:child_process';
import { promisify } from 'node:util';

const __dirname = dirname(fileURLToPath(import.meta.url));
const execAsync = promisify(exec);
Comment thread
UziTech marked this conversation as resolved.

function createMocks() {
const mocks = {
Expand Down Expand Up @@ -64,7 +65,26 @@ function testInput({ args = [], stdin = '', stdinError = '', stdout = '', stderr
}

function fixturePath(filePath) {
return resolve(__dirname, './fixtures', filePath);
return resolve(import.meta.dirname, './fixtures', filePath);
}

function execMarked({ args = '', stdin = '', stdout = '', stderr = '' }) {
const execPath = resolve(import.meta.dirname, '../../bin/marked');
Comment thread
UziTech marked this conversation as resolved.
return async() => {
let command = '';

if (stdin) {
command += `node -e "process.stdout.write('${stdin}')" | `;
Comment thread
UziTech marked this conversation as resolved.
}
command += `node ${execPath}`;
if (args) {
command += ` ${args}`;
}
const res = await execAsync(command);

assert.ok(await htmlIsEqual(res.stdout, stdout));
assert.strictEqual(res.stderr, stderr);
};
}

describe('bin/marked', () => {
Expand Down Expand Up @@ -120,5 +140,37 @@ describe('bin/marked', () => {
stderr: `marked: ${fixturePath('does-not-exist.md')}: No such file or directory`,
code: 1,
}));

it('reads from stdin when no files are provided', testInput({
stdin: '# test\n',
stdout: '<h1>test</h1>',
}));
});
});

describe('exec', () => {
it('stdin', execMarked({
stdin: '# test',
stdout: '<h1>test</h1>',
}));

it('string', execMarked({
args: '-s "# test"',
stdout: '<h1>test</h1>',
}));

it('input', execMarked({
args: `-i ${fixturePath('bin-input.md')}`,
stdout: '<h1>file</h1>',
}));

it('input file positional', execMarked({
args: fixturePath('bin-input.md'),
stdout: '<h1>file</h1>',
}));

it('input last file positional', execMarked({
args: `${fixturePath('does-not-exist.md')} ${fixturePath('bin-input.md')}`,
stdout: '<h1>file</h1>',
}));
});