bin.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { main } from '../../bin/main.js';
  2. import { htmlIsEqual } from '@markedjs/testutils';
  3. import { dirname, resolve } from 'node:path';
  4. import { fileURLToPath } from 'node:url';
  5. import { describe, it, mock } from 'node:test';
  6. import assert from 'node:assert';
  7. const __dirname = dirname(fileURLToPath(import.meta.url));
  8. function createMocks() {
  9. const mocks = {
  10. stdout: '',
  11. stderr: '',
  12. code: null,
  13. stdin: {
  14. data: null,
  15. error: null,
  16. end: null,
  17. },
  18. process: {
  19. cwd: mock.fn(() => '/cwd'),
  20. env: [],
  21. argv: [],
  22. stdout: {
  23. write: mock.fn((str) => { mocks.stdout += str; }),
  24. },
  25. stderr: {
  26. write: mock.fn((str) => { mocks.stderr += str; }),
  27. },
  28. stdin: {
  29. setEncoding: mock.fn(),
  30. on: mock.fn((method, func) => {
  31. mocks.stdin[method] = func;
  32. }),
  33. resume: mock.fn(),
  34. },
  35. exit: mock.fn((code) => { mocks.code = code; }),
  36. },
  37. };
  38. return mocks;
  39. }
  40. function testInput({ args = [], stdin = '', stdinError = '', stdout = '', stderr = '', code = 0 } = {}) {
  41. return async() => {
  42. const mocks = createMocks();
  43. mocks.process.argv = ['node', 'marked', ...args];
  44. const mainPromise = main(mocks.process);
  45. if (typeof mocks.stdin.end === 'function') {
  46. if (stdin) {
  47. mocks.stdin.data(stdin);
  48. }
  49. if (stdinError) {
  50. mocks.stdin.error(stdinError);
  51. }
  52. mocks.stdin.end();
  53. }
  54. await mainPromise;
  55. assert.ok(await htmlIsEqual(mocks.stdout, stdout));
  56. assert.strictEqual(mocks.stderr, stderr);
  57. assert.strictEqual(mocks.code, code);
  58. };
  59. }
  60. function fixturePath(filePath) {
  61. return resolve(__dirname, './fixtures', filePath);
  62. }
  63. describe('bin/marked', () => {
  64. describe('string', () => {
  65. it('-s', testInput({
  66. args: ['-s', '# test'],
  67. stdout: '<h1>test</h1>',
  68. }));
  69. it('--string', testInput({
  70. args: ['--string', '# test'],
  71. stdout: '<h1>test</h1>',
  72. }));
  73. });
  74. describe('config', () => {
  75. it('-c', testInput({
  76. args: ['-c', fixturePath('bin-config.js'), '-s', 'line1\nline2'],
  77. stdout: '<p>line1<br>line2</p>',
  78. }));
  79. it('--config', testInput({
  80. args: ['--config', fixturePath('bin-config.js'), '-s', 'line1\nline2'],
  81. stdout: '<p>line1<br>line2</p>',
  82. }));
  83. it('config not found', testInput({
  84. args: ['--config', fixturePath('does-not-exist.js'), '-s', 'line1\nline2'],
  85. stderr: `Cannot load config file '${fixturePath('does-not-exist.js')}'`,
  86. code: 1,
  87. }));
  88. });
  89. describe('input', () => {
  90. it('input file not found', testInput({
  91. args: [fixturePath('does-not-exist.md')],
  92. stderr: `marked: ${fixturePath('does-not-exist.md')}: No such file or directory`,
  93. code: 1,
  94. }));
  95. it('input file not found --input', testInput({
  96. args: ['--input', fixturePath('does-not-exist.md')],
  97. stderr: `marked: ${fixturePath('does-not-exist.md')}: No such file or directory`,
  98. code: 1,
  99. }));
  100. });
  101. });