Skip to content

Commit

Permalink
Add test for preparation of CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Sep 27, 2024
1 parent 5cd6e85 commit 77b9565
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 7 deletions.
40 changes: 33 additions & 7 deletions test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,35 @@ describe('Marp CLI', () => {
})
}

describe('with DEBUG env', () => {
let originalEnv: Record<string, string | undefined>

beforeEach(() => (originalEnv = { ...process.env }))
afterEach(() => (process.env = { ...originalEnv }))

it('shows enabling debug logging and set pattern', async () => {
process.env.DEBUG = 'debug-pattern,debug-pattern:*'

const warn = jest.spyOn(console, 'warn').mockImplementation()
const log = jest.spyOn(console, 'log').mockImplementation()

try {
expect(await marpCli(['-v'])).toBe(0)
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('Debug logging is enabled')
)
expect(warn).toHaveBeenCalledWith(
expect.stringContaining(
'Filter pattern: debug-pattern,debug-pattern:*'
)
)
} finally {
warn.mockRestore()
log.mockRestore()
}
})
})

describe('when passed file is not found', () => {
it('outputs warning and help with exit code 1', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation()
Expand Down Expand Up @@ -1334,23 +1363,20 @@ describe('Marp CLI', () => {
})

describe('with PUPPETEER_TIMEOUT env', () => {
beforeEach(() => {
process.env.PUPPETEER_TIMEOUT = '12345'
})
let originalEnv: Record<string, string | undefined>

afterEach(() => {
delete process.env.PUPPETEER_TIMEOUT
})
beforeEach(() => (originalEnv = { ...process.env }))
afterEach(() => (process.env = { ...originalEnv }))

it('follows specified timeout in conversion that is using Puppeteer', async () => {
process.env.PUPPETEER_TIMEOUT = '12345'
expect(
(await conversion(onePath, '--pdf')).options.browserManager.timeout
).toBe(12345)
})

it('does not follows specified timeout if the env value is not valid number', async () => {
process.env.PUPPETEER_TIMEOUT = 'invalid'

expect(
(await conversion(onePath, '--pdf')).options.browserManager.timeout
).toBeUndefined()
Expand Down
157 changes: 157 additions & 0 deletions test/prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { cliPrepare, defaultDebug } from '../src/prepare'

afterEach(() => {
jest.resetAllMocks()
jest.restoreAllMocks()
})

describe('Prepare for CLI interface', () => {
it('parses argv and return normalized result', () => {
const argv = ['node', 'marp-cli.js', '--pdf', 'test.md']
jest.replaceProperty(process, 'argv', argv)

expect(cliPrepare()).toStrictEqual({
args: ['--pdf', 'test.md'],
debug: false,
})
})

it('allows passing arguments', () => {
const args = ['--pdf', 'test.md']
expect(cliPrepare(args)).toStrictEqual({ args, debug: false })
})

describe('Debug', () => {
it('parses --debug option', () => {
expect(cliPrepare(['--debug'])).toStrictEqual({
args: [],
debug: defaultDebug,
})
expect(cliPrepare(['--pdf', '--debug'])).toStrictEqual({
args: ['--pdf'],
debug: defaultDebug,
})
expect(cliPrepare(['--debug', '--pptx'])).toStrictEqual({
args: ['--pptx'],
debug: defaultDebug,
})
})

it('parses -d option', () => {
expect(cliPrepare(['-d'])).toStrictEqual({
args: [],
debug: defaultDebug,
})
expect(cliPrepare(['-d', '-w'])).toStrictEqual({
args: ['-w'],
debug: defaultDebug,
})
expect(cliPrepare(['-s', '-d'])).toStrictEqual({
args: ['-s'],
debug: defaultDebug,
})
})

it('parses debug option with filter', () => {
// --debug
expect(cliPrepare(['--debug', 'filter'])).toStrictEqual({
args: [],
debug: 'filter',
})
expect(cliPrepare(['--server', '--debug', 'a', 'b'])).toStrictEqual({
args: ['--server', 'b'],
debug: 'a',
})
expect(cliPrepare(['--debug=a=b'])).toStrictEqual({
args: [],
debug: 'a=b',
})
expect(cliPrepare(['--watch', '--debug=a', 'b'])).toStrictEqual({
args: ['--watch', 'b'],
debug: 'a',
})

// -d
expect(cliPrepare(['-d', 'filter'])).toStrictEqual({
args: [],
debug: 'filter',
})
expect(cliPrepare(['-s', '-d', 'a', 'b'])).toStrictEqual({
args: ['-s', 'b'],
debug: 'a',
})
expect(cliPrepare(['-d= e:* '])).toStrictEqual({
args: [],
debug: 'e:*',
})
expect(cliPrepare(['-w', '-d=a', 'b'])).toStrictEqual({
args: ['-w', 'b'],
debug: 'a',
})

// Fallback to default
expect(cliPrepare(['--debug='])).toStrictEqual({
args: [],
debug: defaultDebug,
})
expect(cliPrepare(['-d='])).toStrictEqual({
args: [],
debug: defaultDebug,
})

// Case sensitive
expect(cliPrepare(['-D'])).toStrictEqual({
args: ['-D'],
debug: false,
})
expect(cliPrepare(['--DEBUG'])).toStrictEqual({
args: ['--DEBUG'],
debug: false,
})
expect(cliPrepare(['-D=abc'])).toStrictEqual({
args: ['-D=abc'],
debug: false,
})
expect(cliPrepare(['--DEBUG=abc'])).toStrictEqual({
args: ['--DEBUG=abc'],
debug: false,
})

// Arguments should be splitted into each array element
expect(cliPrepare(['--debug foo'])).toStrictEqual({
args: ['--debug foo'],
debug: false,
})
expect(cliPrepare(['-d foo'])).toStrictEqual({
args: ['-d foo'],
debug: false,
})

// Keywords
expect(cliPrepare(['a', '--debug=true', 'b'])).toStrictEqual({
args: ['a', 'b'],
debug: defaultDebug,
})
expect(cliPrepare(['c', '--debug', '1'])).toStrictEqual({
args: ['c'],
debug: defaultDebug,
})
expect(cliPrepare(['a', '--debug=0', 'b'])).toStrictEqual({
args: ['a', 'b'],
debug: false,
})
expect(cliPrepare(['c', '--debug', '0'])).toStrictEqual({
args: ['c'],
debug: false,
})
expect(cliPrepare(['a', '--debug=all', 'b'])).toStrictEqual({
args: ['a', 'b'],
debug: '*',
})
expect(cliPrepare(['c', '--debug', 'full'])).toStrictEqual({
args: ['c'],
debug: '*',
})
})
})
})

0 comments on commit 77b9565

Please sign in to comment.