Skip to content

Commit

Permalink
refactor(myca-cli): bin/cli.ts
Browse files Browse the repository at this point in the history
add test/bin/31.cli-args.test.ts
  • Loading branch information
waitingsong committed Jul 25, 2023
1 parent f3de0d7 commit d231fa6
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/myca-cli/src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
* command: init|initca|issue|initcenter case insensitive
*/

import assert from 'assert'

import { argv } from 'zx'

import { genCmdHelp, helpDefault } from '../lib/helper.js'
import { genCmdHelp } from '../lib/helper.js'
import { runCmd } from '../lib/index.js'
import { parseCliArgs, parseOpts } from '../lib/parse-opts.js'
import { InitCenterArgs } from '../lib/types.js'
import type { CliArgs, InitCenterArgs } from '../lib/types.js'


const args = parseCliArgs(argv)
let args: CliArgs | undefined
try {
args = parseCliArgs(argv)
assert(args.cmd, 'args.cmd empty')

if (args.cmd) {
if (args.needHelp) {
const msg = genCmdHelp(args.cmd)
console.info(msg)
Expand All @@ -26,10 +30,11 @@ if (args.cmd) {
const ret = await runCmd(args)
console.info(ret)
}

}
else {
const msg = helpDefault()
console.info(msg)
process.exit(0)
catch (ex) {
assert(ex instanceof Error)
console.info(ex.message)
process.exit(1)
}

131 changes: 131 additions & 0 deletions packages/myca-cli/test/bin/31.cli-args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import assert from 'node:assert/strict'

import { fileShortPath } from '@waiting/shared-core'
import { $, ProcessOutput } from 'zx'



describe(fileShortPath(import.meta.url), () => {
const cli = './src/bin/cli.ts'

describe('Should cli work', () => {
it('no args', async () => {
try {
await $`node --enable-source-maps --loader ts-node/esm ${cli} `
assert(false, 'should throw')
}
catch (ex) {
assert(ex instanceof Error)
const output = ex as ProcessOutput
assert(typeof output.exitCode === 'number')
assert(output.exitCode === 1, `exitCode: ${output.exitCode}`)
assert(output.stdout.length > 0, `stdout: ${output.stdout}`)
assert(output.stdout.includes('Standard commands'), `stdout: ${output.stdout}`)
assert(output.stdout.includes('init\tinitca\tissue\tinitcenter'), `stdout: ${output.stdout}`)
assert(output.stdout.includes('More help: myca <command> -h'), `stdout: ${output.stdout}`)
}
})

it('-h without cmd', async () => {
try {
const args = ['-h']
await $`node --enable-source-maps --loader ts-node/esm ${cli} ${args} `
assert(false, 'should throw')
}
catch (ex) {
assert(ex instanceof Error)
const output = ex as ProcessOutput
assert(typeof output.exitCode === 'number')
assert(output.exitCode === 1, `exitCode: ${output.exitCode}`)
assert(output.stdout.length > 0, `stdout: ${output.stdout}`)
assert(output.stdout.includes('Standard commands'), `stdout: ${output.stdout}`)
assert(output.stdout.includes('init\tinitca\tissue\tinitcenter'), `stdout: ${output.stdout}`)
assert(output.stdout.includes('More help: myca <command> -h'), `stdout: ${output.stdout}`)
}
})

it('init -h', async () => {
const cmd = 'init'
const args = ['-h']
const { stdout } = await $`node --enable-source-maps --loader ts-node/esm ${cli} ${cmd} ${args} `
assert(stdout.length > 0, `stdout: ${stdout}`)
assert(stdout.includes(`Usage: ${cmd} [options]`), `stdout: ${stdout}`)
assert(stdout.includes('Valid options are:'), `stdout: ${stdout}`)
assert(stdout.includes('Display this summary'), `stdout: ${stdout}`)
assert(stdout.includes('Display debug info'), `stdout: ${stdout}`)
})

it('initca -h', async () => {
const cmd = 'initca'
const args = ['-h']
const { stdout } = await $`node --enable-source-maps --loader ts-node/esm ${cli} ${cmd} ${args} `
assert(stdout.length > 0, `stdout: ${stdout}`)
assert(stdout.includes(`Usage: ${cmd} [options]`), `stdout: ${stdout}`)
assert(stdout.includes('Valid options are:'), `stdout: ${stdout}`)
assert(stdout.includes('Display this summary'), `stdout: ${stdout}`)
assert(stdout.includes('Display debug info'), `stdout: ${stdout}`)

assert(stdout.includes('--centerName'), `stdout: ${stdout}`)
assert(stdout.includes('--alg'), `stdout: ${stdout}`)
assert(stdout.includes('--days'), `stdout: ${stdout}`)
assert(stdout.includes('--pass'), `stdout: ${stdout}`)
assert(stdout.includes('--keyBits'), `stdout: ${stdout}`)
assert(stdout.includes('--ecParamgenCurve'), `stdout: ${stdout}`)
assert(stdout.includes('--hash'), `stdout: ${stdout}`)
assert(stdout.includes('--CN'), `stdout: ${stdout}`)
assert(stdout.includes('--OU'), `stdout: ${stdout}`)
assert(stdout.includes('--O'), `stdout: ${stdout}`)
assert(stdout.includes('--C'), `stdout: ${stdout}`)
assert(stdout.includes('--ST'), `stdout: ${stdout}`)
assert(stdout.includes('--L'), `stdout: ${stdout}`)
assert(stdout.includes('--emailAddress'), `stdout: ${stdout}`)
})

it('issue -h', async () => {
const cmd = 'issue'
const args = ['-h']
const { stdout } = await $`node --enable-source-maps --loader ts-node/esm ${cli} ${cmd} ${args} `
assert(stdout.length > 0, `stdout: ${stdout}`)
assert(stdout.includes(`Usage: ${cmd} [options]`), `stdout: ${stdout}`)
assert(stdout.includes('Valid options are:'), `stdout: ${stdout}`)
assert(stdout.includes('Display this summary'), `stdout: ${stdout}`)
assert(stdout.includes('Display debug info'), `stdout: ${stdout}`)

assert(stdout.includes('--kind'), `stdout: ${stdout}`)
assert(stdout.includes('--centerName'), `stdout: ${stdout}`)
assert(stdout.includes('--caKeyPass'), `stdout: ${stdout}`)
assert(stdout.includes('--alg'), `stdout: ${stdout}`)
assert(stdout.includes('--days'), `stdout: ${stdout}`)
assert(stdout.includes('--pass'), `stdout: ${stdout}`)
assert(stdout.includes('--keyBits'), `stdout: ${stdout}`)
assert(stdout.includes('--ecParamgenCurve'), `stdout: ${stdout}`)
assert(stdout.includes('--hash'), `stdout: ${stdout}`)
assert(stdout.includes('--CN'), `stdout: ${stdout}`)
assert(stdout.includes('--OU'), `stdout: ${stdout}`)
assert(stdout.includes('--O'), `stdout: ${stdout}`)
assert(stdout.includes('--C'), `stdout: ${stdout}`)
assert(stdout.includes('--ST'), `stdout: ${stdout}`)
assert(stdout.includes('--L'), `stdout: ${stdout}`)
assert(stdout.includes('--emailAddress'), `stdout: ${stdout}`)

assert(stdout.includes('--SAN'), `stdout: ${stdout}`)
assert(stdout.includes('--ips'), `stdout: ${stdout}`)
})

it('initcenter -h', async () => {
const cmd = 'initcenter'
const args = ['-h']
const { stdout } = await $`node --enable-source-maps --loader ts-node/esm ${cli} ${cmd} ${args} `
assert(stdout.length > 0, `stdout: ${stdout}`)
assert(stdout.includes(`Usage: ${cmd} [options]`), `stdout: ${stdout}`)
assert(stdout.includes('Valid options are:'), `stdout: ${stdout}`)
assert(stdout.includes('Display this summary'), `stdout: ${stdout}`)
assert(stdout.includes('Display debug info'), `stdout: ${stdout}`)

assert(stdout.includes('--name'), `stdout: ${stdout}`)
assert(stdout.includes('--path'), `stdout: ${stdout}`)
})

})

})

0 comments on commit d231fa6

Please sign in to comment.