diff --git a/src/commands/export.ts b/src/commands/export.ts index 48ea348..11e5efe 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -26,7 +26,7 @@ export default class Export extends HackMDCommand { try { const APIClient = await this.getAPIClient() const note = await APIClient.getNote(noteId) - this.log(note.content) + process.stdout.write(note.content) } catch (error) { this.log('Export note content failed') this.error(error as Error) diff --git a/test/export.test.ts b/test/export.test.ts new file mode 100644 index 0000000..1e8b62a --- /dev/null +++ b/test/export.test.ts @@ -0,0 +1,56 @@ +import {expect} from 'chai' + +import Export from '../src/commands/export' + +async function captureExport(content: string): Promise { + const stdoutDescriptor = Object.getOwnPropertyDescriptor(process, 'stdout') + if (!stdoutDescriptor) throw new Error('process.stdout descriptor is unavailable') + + let output = '' + Object.defineProperty(process, 'stdout', { + configurable: true, + value: { + write(chunk: string) { + output += chunk + return true + }, + }, + }) + + try { + const command = { + error(error: Error | string): never { + throw typeof error === 'string' ? new Error(error) : error + }, + getAPIClient: async () => ({ + getNote: async () => ({content}), + }), + log: (message = '') => process.stdout.write(`${message}\n`), + parse: async () => ({flags: {noteId: 'note-id'}}), + } as unknown as Export + + await Export.prototype.run.call(command) + } finally { + Object.defineProperty(process, 'stdout', stdoutDescriptor) + } + + return output +} + +describe('Export note content', () => { + it('preserves content without a trailing newline', async () => { + const content = '# Exported note\n\nNo trailing newline.' + + expect(await captureExport(content)).to.equal(content) + }) + + it('preserves content with a trailing newline', async () => { + const content = '# Exported note\n\nTrailing newline.\n' + + expect(await captureExport(content)).to.equal(content) + }) + + it('preserves empty content', async () => { + expect(await captureExport('')).to.equal('') + }) +})