Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions test/export.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {expect} from 'chai'

import Export from '../src/commands/export'

async function captureExport(content: string): Promise<string> {
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('')
})
})
Loading