Skip to content

Commit 44257f4

Browse files
committed
feat: add normalize helper for normalization
1 parent 023fbf6 commit 44257f4

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

packages/cli/src/lib/implementation/core-config.middleware.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,28 @@ export async function coreConfigMiddleware<
5353
format: normalizeFormats(
5454
cliPersist?.format ?? rcPersist?.format ?? DEFAULT_PERSIST_FORMAT,
5555
),
56-
report: !('no-report' in (cliPersist ?? {})),
56+
report: normalizeBooleanWithNegation(
57+
'report',
58+
cliPersist as Record<string, unknown>,
59+
rcPersist as Record<string, unknown>,
60+
),
5761
},
5862
...(upload != null && { upload }),
5963
...remainingRcConfig,
6064
...remainingCliOptions,
6165
};
6266
}
6367

68+
export const normalizeBooleanWithNegation = <T extends string>(
69+
propertyName: T,
70+
cliOptions?: Record<T, unknown>,
71+
rcOptions?: Record<T, unknown>,
72+
): boolean =>
73+
propertyName in (cliOptions ?? {})
74+
? (cliOptions?.[propertyName] as boolean)
75+
: `no-${propertyName}` in (cliOptions ?? {})
76+
? false
77+
: ((rcOptions?.[propertyName] as boolean) ?? true);
78+
6479
export const normalizeFormats = (formats?: string[]): Format[] =>
6580
(formats ?? []).flatMap(format => format.split(',') as Format[]);

packages/cli/src/lib/implementation/core-config.middleware.unit.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, vi } from 'vitest';
22
import { autoloadRc, readRcByPath } from '@code-pushup/core';
33
import {
44
coreConfigMiddleware,
5+
normalizeBooleanWithNegation,
56
normalizeFormats,
67
} from './core-config.middleware.js';
78
import type { CoreConfigCliOptions } from './core-config.model.js';
@@ -19,6 +20,36 @@ vi.mock('@code-pushup/core', async () => {
1920
};
2021
});
2122

23+
describe('normalizeBooleanWithNegation', () => {
24+
it('should return true when CLI property is true', () => {
25+
expect(normalizeBooleanWithNegation('report', { report: true }, {})).toBe(
26+
true,
27+
);
28+
});
29+
30+
it('should return false when CLI property is false', () => {
31+
expect(normalizeBooleanWithNegation('report', { report: false }, {})).toBe(
32+
false,
33+
);
34+
});
35+
36+
it('should return false when no-property exists in CLI persist', () => {
37+
expect(
38+
normalizeBooleanWithNegation('report', { 'no-report': true }, {}),
39+
).toBe(false);
40+
});
41+
42+
it('should fallback to RC persist when no CLI property', () => {
43+
expect(normalizeBooleanWithNegation('report', {}, { report: false })).toBe(
44+
false,
45+
);
46+
});
47+
48+
it('should return default true when no property anywhere', () => {
49+
expect(normalizeBooleanWithNegation('report', {}, {})).toBe(true);
50+
});
51+
});
52+
2253
describe('normalizeFormats', () => {
2354
it('should forward valid formats', () => {
2455
expect(normalizeFormats(['json', 'md'])).toEqual(['json', 'md']);

0 commit comments

Comments
 (0)