From 577a6b850bcc18f2911784cf81189d15f770851b Mon Sep 17 00:00:00 2001 From: YeonJoong Kim <4367890+kim-yeonjoong@users.noreply.github.com> Date: Tue, 3 Dec 2024 09:14:21 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20command=20=EB=A5=BC=20=EB=A7=A4=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C=20=EC=8B=9C=20=EC=9E=AC=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95,=20number=20?= =?UTF-8?q?=EA=B0=80=20=EC=95=84=EB=8B=8C=20=EA=B2=BD=EC=9A=B0=20=EA=B8=B0?= =?UTF-8?q?=EB=B3=B8=20=EA=B0=92=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: command 를 매 호출 시 재생성 하도록 수정 * chore: boolean 타입으로 사용할 수 있도록 변경 * fix: number 가 아닌 경우 기본 값 사용하도록 변경 * chore: 불필요한 설정파일 삭제 * chore: cli helper 테스트 코드 추가 * chore: remove unused import --- codecov.yml | 2 - lib/helper/cli.helper.spec.ts | 89 +++++++++++++++++++++++++++++++++++ lib/helper/cli.helper.ts | 20 +++++--- vitest.config.mjs | 3 +- 4 files changed, 105 insertions(+), 9 deletions(-) delete mode 100644 codecov.yml create mode 100644 lib/helper/cli.helper.spec.ts diff --git a/codecov.yml b/codecov.yml deleted file mode 100644 index 9354b53..0000000 --- a/codecov.yml +++ /dev/null @@ -1,2 +0,0 @@ -ignore: - - "src/**/index.ts" \ No newline at end of file diff --git a/lib/helper/cli.helper.spec.ts b/lib/helper/cli.helper.spec.ts new file mode 100644 index 0000000..6b2e3b5 --- /dev/null +++ b/lib/helper/cli.helper.spec.ts @@ -0,0 +1,89 @@ +import { describe, expect, it, vi } from 'vitest'; +import { initCli } from './cli.helper'; +import path from 'node:path'; + +const mockArgv = (arguments_: string[]) => { + process.argv = ['node', 'script', ...arguments_]; +}; + +describe.concurrent('initCli 함수 테스트', () => { + it('기본 옵션 값들을 설정해야 한다', () => { + mockArgv([]); + vi.unstubAllEnvs(); + const options = initCli(); + + expect(options).toEqual({ + MAX_STATUS_LOG: 100, + SOURCE_PATH: path.resolve(process.cwd(), './'), + OUTPUT_FILE_PATH: path.resolve(process.cwd(), './pulse.sqlite'), + FILE_CONCURRENCY: 5, + EXECUTE_CONCURRENCY: 50, + EXPORT_JSON: false, + }); + }); + + it('CLI 옵션으로 설정한 값을 반환해야 한다', () => { + mockArgv([ + './src', + '-m', + '200', + '-o', + './output.db', + '-c', + '10', + '-e', + '100', + '--json', + ]); + vi.unstubAllEnvs(); + + const options = initCli(); + + expect(options).toEqual({ + MAX_STATUS_LOG: 200, + SOURCE_PATH: path.resolve(process.cwd(), './src'), + OUTPUT_FILE_PATH: path.resolve(process.cwd(), './output.db'), + FILE_CONCURRENCY: 10, + EXECUTE_CONCURRENCY: 100, + EXPORT_JSON: true, + }); + }); + + it('환경 변수 값을 사용해야 한다', () => { + mockArgv([]); + vi.unstubAllEnvs(); + vi.stubEnv('PULSE_STATUS_LOGS_MAX', '300'); + vi.stubEnv('PULSE_OUTPUT_PATH', './env_output.db'); + vi.stubEnv('PULSE_FILE_CONCURRENCY', '15'); + vi.stubEnv('PULSE_EXECUTE_CONCURRENCY', '200'); + + const options = initCli(); + + expect(options).toEqual({ + MAX_STATUS_LOG: 300, + SOURCE_PATH: path.resolve(process.cwd(), './'), + OUTPUT_FILE_PATH: path.resolve(process.cwd(), './env_output.db'), + FILE_CONCURRENCY: 15, + EXECUTE_CONCURRENCY: 200, + EXPORT_JSON: false, + }); + }); + + it('숫자가 아닌 값은 기본값으로 대체해야 한다', () => { + mockArgv([]); + vi.unstubAllEnvs(); + vi.stubEnv('PULSE_STATUS_LOGS_MAX', 'not-a-number'); + vi.stubEnv('PULSE_FILE_CONCURRENCY', 'not-a-number'); + vi.stubEnv('PULSE_EXECUTE_CONCURRENCY', 'not-a-number'); + + const options = initCli(); + expect(options).toEqual({ + MAX_STATUS_LOG: 100, + SOURCE_PATH: path.resolve(process.cwd(), './'), + OUTPUT_FILE_PATH: path.resolve(process.cwd(), './pulse.sqlite'), + FILE_CONCURRENCY: 5, + EXECUTE_CONCURRENCY: 50, + EXPORT_JSON: false, + }); + }); +}); diff --git a/lib/helper/cli.helper.ts b/lib/helper/cli.helper.ts index a70196f..4bd0c2a 100644 --- a/lib/helper/cli.helper.ts +++ b/lib/helper/cli.helper.ts @@ -1,11 +1,10 @@ import { Argument, Command, Option } from 'commander'; import path from 'node:path'; -const program = new Command(); - const DEFAULT_PATH = './'; export const initCli = (): CliOptions => { + const program = new Command(); program .name('pulse') .description('Keeping the pulse of your APIs under watch.') @@ -18,7 +17,10 @@ export const initCli = (): CliOptions => { new Option('-m, --max ', 'max count of status logs') .default(100) .env('PULSE_STATUS_LOGS_MAX') - .argParser((value) => Number.parseInt(value, 10)), + .argParser((value) => { + const temporary = Number.parseInt(value, 10); + return Number.isNaN(temporary) ? 100 : temporary; + }), ) .addOption( new Option('-o, --out ', 'output file path') @@ -32,7 +34,10 @@ export const initCli = (): CliOptions => { ) .default(5) .env('PULSE_FILE_CONCURRENCY') - .argParser((value) => Number.parseInt(value, 10)), + .argParser((value) => { + const temporary = Number.parseInt(value, 10); + return Number.isNaN(temporary) ? 5 : temporary; + }), ) .addOption( new Option( @@ -41,7 +46,10 @@ export const initCli = (): CliOptions => { ) .default(50) .env('PULSE_EXECUTE_CONCURRENCY') - .argParser((value) => Number.parseInt(value, 10)), + .argParser((value) => { + const temporary = Number.parseInt(value, 10); + return Number.isNaN(temporary) ? 50 : temporary; + }), ) .addOption(new Option('--json', 'Export current result to json file')) .parse(process.argv); @@ -56,6 +64,6 @@ export const initCli = (): CliOptions => { OUTPUT_FILE_PATH: path.resolve(currentPath, String(options.out)), FILE_CONCURRENCY: Number(options.concurrency), EXECUTE_CONCURRENCY: Number(options.executeConcurrency), - EXPORT_JSON: options.json, + EXPORT_JSON: !!options.json, }; }; diff --git a/vitest.config.mjs b/vitest.config.mjs index 57e5982..3866f60 100644 --- a/vitest.config.mjs +++ b/vitest.config.mjs @@ -3,11 +3,12 @@ import { defineConfig, coverageConfigDefaults } from 'vitest/config'; export default defineConfig({ test: { clearMocks: true, + restoreMocks: true, coverage: { provider: 'v8', exclude: [ 'commitlint.config.js', - 'src/**/index.ts', + '**/index.ts', ...coverageConfigDefaults.exclude, ], },