From d231fa67372b3138d690f64611f10709b5ac808f Mon Sep 17 00:00:00 2001 From: waiting <1661926154@qq.com> Date: Tue, 25 Jul 2023 16:09:55 +0800 Subject: [PATCH] refactor(myca-cli): bin/cli.ts add test/bin/31.cli-args.test.ts --- packages/myca-cli/src/bin/cli.ts | 21 +-- .../myca-cli/test/bin/31.cli-args.test.ts | 131 ++++++++++++++++++ 2 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 packages/myca-cli/test/bin/31.cli-args.test.ts diff --git a/packages/myca-cli/src/bin/cli.ts b/packages/myca-cli/src/bin/cli.ts index e5e6380..052e890 100644 --- a/packages/myca-cli/src/bin/cli.ts +++ b/packages/myca-cli/src/bin/cli.ts @@ -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) @@ -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) } diff --git a/packages/myca-cli/test/bin/31.cli-args.test.ts b/packages/myca-cli/test/bin/31.cli-args.test.ts new file mode 100644 index 0000000..edf4621 --- /dev/null +++ b/packages/myca-cli/test/bin/31.cli-args.test.ts @@ -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 -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 -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}`) + }) + + }) + +})