Skip to content

Commit be08c18

Browse files
committed
feat(utils): parse boolean environment variables
1 parent 25ad63e commit be08c18

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

packages/utils/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {
99
export { filesCoverageToTree, type FileCoverage } from './lib/coverage-tree.js';
1010
export { createRunnerFiles } from './lib/create-runner-files.js';
1111
export { comparePairs, matchArrayItemsByKey, type Diff } from './lib/diff.js';
12+
export { isEnvVarEnabled, isVerbose } from './lib/env.js';
1213
export { stringifyError } from './lib/errors.js';
1314
export {
1415
executeProcess,
@@ -69,7 +70,7 @@ export {
6970
isPromiseRejectedResult,
7071
} from './lib/guards.js';
7172
export { logMultipleResults } from './lib/log-results.js';
72-
export { isVerbose, link, ui, type CliUi, type Column } from './lib/logging.js';
73+
export { link, ui, type CliUi, type Column } from './lib/logging.js';
7374
export { mergeConfigs } from './lib/merge-configs.js';
7475
export { getProgressBar, type ProgressBar } from './lib/progress.js';
7576
export { asyncSequential, groupByStatus } from './lib/promises.js';

packages/utils/src/lib/env.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ui } from './logging.js';
2+
3+
export function isVerbose() {
4+
return isEnvVarEnabled('CP_VERBOSE');
5+
}
6+
7+
export function isEnvVarEnabled(name: string): boolean {
8+
const value = process.env[name];
9+
10+
if (!value) {
11+
return false;
12+
}
13+
14+
if (value.toLowerCase() === 'true') {
15+
return true;
16+
}
17+
18+
const int = Number.parseInt(value, 10);
19+
if (!Number.isNaN(int) && int !== 0) {
20+
return true;
21+
}
22+
23+
ui().logger.warning(
24+
`Environment variable ${name} expected to be a boolean (true/false/1/0), but received value ${value}. Treating it as disabled.`,
25+
);
26+
27+
return false;
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { isEnvVarEnabled } from './env.js';
2+
import { ui } from './logging.js';
3+
4+
describe('isEnvVarEnabled', () => {
5+
beforeEach(() => {
6+
vi.unstubAllEnvs();
7+
});
8+
9+
it('should consider missing variable disabled', () => {
10+
expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false);
11+
});
12+
13+
it('should consider "true" enabled', () => {
14+
vi.stubEnv('CP_VERBOSE', 'true');
15+
expect(isEnvVarEnabled('CP_VERBOSE')).toBe(true);
16+
});
17+
18+
it('should consider "false" disabled', () => {
19+
vi.stubEnv('CP_VERBOSE', 'false');
20+
expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false);
21+
});
22+
23+
it('should consider "1" enabled', () => {
24+
vi.stubEnv('CP_VERBOSE', '1');
25+
expect(isEnvVarEnabled('CP_VERBOSE')).toBe(true);
26+
});
27+
28+
it('should consider "0" disabled', () => {
29+
vi.stubEnv('CP_VERBOSE', '0');
30+
expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false);
31+
});
32+
33+
it('should log a warning for unexpected values', () => {
34+
vi.stubEnv('CP_VERBOSE', 'unexpected');
35+
expect(isEnvVarEnabled('CP_VERBOSE')).toBe(false);
36+
expect(ui()).toHaveLogged(
37+
'warn',
38+
'Environment variable CP_VERBOSE expected to be a boolean (true/false/1/0), but received value unexpected. Treating it as disabled.',
39+
);
40+
});
41+
});

packages/utils/src/lib/execute-process.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
spawn,
77
} from 'node:child_process';
88
import type { Readable, Writable } from 'node:stream';
9+
import { isVerbose } from './env.js';
910
import { formatCommandLog } from './format-command-log.js';
10-
import { isVerbose, ui } from './logging.js';
11+
import { ui } from './logging.js';
1112
import { calcDuration } from './reports/utils.js';
1213

1314
/**

packages/utils/src/lib/logging.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export type Column = {
1919
};
2020
export type CliUi = CliUiBase & CliExtension;
2121

22-
export const isVerbose = () => process.env['CP_VERBOSE'] === 'true';
23-
2422
// eslint-disable-next-line functional/no-let
2523
let cliUISingleton: CliUiBase | undefined;
2624
// eslint-disable-next-line functional/no-let

packages/utils/src/lib/reports/log-stdout-summary.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { bold, cyan, cyanBright, green, red } from 'ansis';
22
import type { AuditReport } from '@code-pushup/models';
3-
import { isVerbose, ui } from '../logging.js';
3+
import { isVerbose } from '../env.js';
4+
import { ui } from '../logging.js';
45
import {
56
CODE_PUSHUP_DOMAIN,
67
FOOTER_PREFIX,

0 commit comments

Comments
 (0)