Skip to content

Commit

Permalink
fix: command 를 매 호출 시 재생성 하도록 수정, number 가 아닌 경우 기본 값 사용하도록 변경
Browse files Browse the repository at this point in the history
* fix: command 를 매 호출 시 재생성 하도록 수정

* chore: boolean 타입으로 사용할 수 있도록 변경

* fix: number 가 아닌 경우 기본 값 사용하도록 변경

* chore: 불필요한 설정파일 삭제

* chore: cli helper 테스트 코드 추가

* chore: remove unused import
  • Loading branch information
kim-yeonjoong authored Dec 3, 2024
1 parent 374952b commit 577a6b8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
2 changes: 0 additions & 2 deletions codecov.yml

This file was deleted.

89 changes: 89 additions & 0 deletions lib/helper/cli.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
20 changes: 14 additions & 6 deletions lib/helper/cli.helper.ts
Original file line number Diff line number Diff line change
@@ -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.')
Expand All @@ -18,7 +17,10 @@ export const initCli = (): CliOptions => {
new Option('-m, --max <number>', '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 <file-path>', 'output file path')
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -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,
};
};
3 changes: 2 additions & 1 deletion vitest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
},
Expand Down

0 comments on commit 577a6b8

Please sign in to comment.