Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route help messages to stdout instead of stderr #532

Merged
merged 1 commit into from
Jul 6, 2023
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
13 changes: 8 additions & 5 deletions src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export const marpCli = async (
}

if (args.help) {
program.showHelp()
program.showHelp('log')
return 0
}

Expand Down Expand Up @@ -325,11 +325,14 @@ export const marpCli = async (
const { length } = foundFiles

if (length === 0) {
if (config.files.length > 0)
if (config.files.length > 0) {
cli.warn('Not found processable Markdown file(s).\n')

program.showHelp()
return config.files.length > 0 ? 1 : 0
program.showHelp('error')
return 1
} else {
program.showHelp('log')
return 0
}
}

// Convert markdown into HTML
Expand Down
20 changes: 9 additions & 11 deletions test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,27 +199,25 @@ describe('Marp CLI', () => {
describe(`with ${cmd || 'empty'} option`, () => {
const run = (...args) => marpCli([...(cmd ? [cmd] : []), ...args])

let error: jest.SpyInstance<ReturnType<Console['error']>, any>
let log: jest.SpyInstance<void, any>

beforeEach(() => {
error = jest.spyOn(console, 'error').mockImplementation()
log = jest.spyOn(console, 'log').mockImplementation()
})

afterEach(() => {
error?.mockRestore()
log?.mockRestore()
})

it('outputs help to stderr', async () => {
it('outputs help to stdout', async () => {
expect(await run()).toBe(0)
expect(error).toHaveBeenCalledWith(expect.stringContaining('Usage'))
expect(log).toHaveBeenCalledWith(expect.stringContaining('Usage'))
})

describe('Preview option', () => {
it('outputs help about --preview option', async () => {
expect(await run()).toBe(0)
expect(error).toHaveBeenCalledWith(
expect.stringContaining('--preview')
)
expect(log).toHaveBeenCalledWith(expect.stringContaining('--preview'))
})

describe('when CLI is running in an official Docker image', () => {
Expand All @@ -230,7 +228,7 @@ describe('Marp CLI', () => {

try {
expect(await run()).toBe(0)
expect(error).toHaveBeenCalledWith(
expect(log).toHaveBeenCalledWith(
expect.not.stringContaining('--preview')
)
} finally {
Expand Down Expand Up @@ -304,7 +302,7 @@ describe('Marp CLI', () => {
.mockReturnValue(false)

try {
const error = jest.spyOn(console, 'error').mockImplementation()
const log = jest.spyOn(console, 'log').mockImplementation()

try {
const silentImportSpy = jest.spyOn(
Expand All @@ -324,7 +322,7 @@ describe('Marp CLI', () => {
expect(silentImportSpy).not.toHaveBeenCalled()
expect(silentRequireSpy).toHaveBeenCalled()
} finally {
error.mockRestore()
log.mockRestore()
}
} finally {
isESMAvailable.mockRestore()
Expand Down