Skip to content

Commit

Permalink
Test for --parallel and -P option
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Jan 15, 2025
1 parent 313e7ef commit d2b6ca5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,19 @@ export class MarpCLIConfig {
})()

const parallel = (() => {
if (this.args.parallel === true) return DEFAULT_PARALLEL
if (this.args.parallel === false) return 1
if (typeof this.args.parallel === 'number')
return Math.max(1, this.args.parallel)
const parseParallel = (value: boolean | number | undefined) => {
if (value === true) return DEFAULT_PARALLEL
if (value === false) return 1
if (typeof value === 'number') return Math.max(1, value)

if (this.conf.parallel === true) return DEFAULT_PARALLEL
if (this.conf.parallel === false) return 1
if (typeof this.conf.parallel === 'number')
return Math.max(1, this.conf.parallel)
return undefined
}

return 1
return (
parseParallel(this.args.parallel) ??
parseParallel(this.conf.parallel) ??
DEFAULT_PARALLEL
)
})()

return {
Expand Down
2 changes: 1 addition & 1 deletion src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export const marpCli = async (
},
parallel: {
alias: ['P'],
default: DEFAULT_PARALLEL,
defaultDescription: DEFAULT_PARALLEL.toString(),
describe: 'Number of max parallel processes for multiple conversions',
group: OptionGroup.Basic,
type: 'number',
Expand Down
36 changes: 36 additions & 0 deletions test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,42 @@ describe('Marp CLI', () => {
})
})

for (const opt of ['--parallel', '-P']) {
describe(`with ${opt} option`, () => {
it('converts files in parallel with specified concurrency', async () => {
expect((await conversion(opt, '2', onePath)).options.parallel).toBe(2)
})

it('converts files in parallel with 5 concurrency if set as true', async () => {
expect((await conversion(onePath, opt)).options.parallel).toBe(5)
})

it('converts files in serial if set as 1', async () => {
expect((await conversion(opt, '1', onePath)).options.parallel).toBe(1)
})

it('converts files in serial if set invalid value', async () => {
expect((await conversion(opt, '-1', onePath)).options.parallel).toBe(
1
)
})
})
}

describe('without parallel option', () => {
it('converts files in parallel with 5 concurrency', async () => {
expect((await conversion(onePath)).options.parallel).toBe(5)
})
})

describe('with --no-parallel option', () => {
it('converts files in serial', async () => {
expect(
(await conversion('--no-parallel', onePath)).options.parallel
).toBe(1)
})
})

describe('with -o option', () => {
it('converts file and output to stdout when -o is "-"', async () => {
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation()
Expand Down

0 comments on commit d2b6ca5

Please sign in to comment.