Skip to content

Commit

Permalink
refactor(betterer 🔧): fix test mock
Browse files Browse the repository at this point in the history
  • Loading branch information
phenomnomnominal committed Sep 8, 2024
1 parent 50a045f commit 3a0f3c4
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 17 deletions.
157 changes: 150 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@phenomnomnominal/commitlint-plugin": "^1.1.1",
"@types/eslint": "^9.6.1",
"@types/node": "^18.19.50",
"@typescript-eslint/parser": "^8.4.0",
"@vitest/coverage-v8": "^1.5.0",
"@vitest/ui": "^1.5.0",
"cz-conventional-changelog": "^3.3.0",
Expand Down Expand Up @@ -86,4 +87,4 @@
"path": "cz-conventional-changelog"
}
}
}
}
6 changes: 3 additions & 3 deletions packages/betterer/src/config/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'node:assert';
import os from 'node:os';

import { BettererError } from '@betterer/errors';

Expand Down Expand Up @@ -68,8 +67,9 @@ export function validateStringRegExpArray<Config extends object>(config: Config)
return config;
}

export function validateWorkers(workers: number | boolean = true): number {
const totalCPUs = os.cpus().length;
export async function validateWorkers(workers: number | boolean = true): Promise<number> {
const { cpus } = await import('node:os');
const totalCPUs = cpus().length;

if (workers === true || isUndefined(workers)) {
workers = totalCPUs >= 4 ? totalCPUs - 2 : false;
Expand Down
4 changes: 2 additions & 2 deletions packages/betterer/src/context/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
validateWorkers
} from '../config/index.js';

export function createContextConfig(options: BettererOptionsContext): BettererConfigContext {
export async function createContextConfig(options: BettererOptionsContext): Promise<BettererConfigContext> {
const ci = options.ci ?? false;
const filters = toRegExps(toArray<string | RegExp>(options.filters));
const excludes = toRegExps(toArray<string | RegExp>(options.excludes));
Expand All @@ -27,7 +27,7 @@ export function createContextConfig(options: BettererOptionsContext): BettererCo
validateStringRegExpArray({ filters });
validateStringArray({ includes });

const workers = validateWorkers(options.workers);
const workers = await validateWorkers(options.workers);

return {
ci,
Expand Down
2 changes: 1 addition & 1 deletion packages/betterer/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function createGlobals(
let reporter = await loadDefaultReporter();

try {
const configContext = createContextConfig(options);
const configContext = await createContextConfig(options);
const configFS = await createFSConfig(options);
const configReporter = await createReporterConfig(configFS, options);
const configWatcher = createWatcherConfig(configFS, optionsWatch);
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/config-validation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,23 @@ Error: "workers" must be a number. Received \`{}\`.
exports[`betterer > should throw when there is invalid config: { workers: -1 } 1`] = `
[
"
Error: "workers" must be more than zero and not more than the number of available CPUs (10). To disable workers, set to \`false\`. Received \`-1\`.
Error: "workers" must be more than zero and not more than the number of available CPUs (1). To disable workers, set to \`false\`. Received \`-1\`.
",
]
`;

exports[`betterer > should throw when there is invalid config: { workers: 1000 } 1`] = `
[
"
Error: "workers" must be more than zero and not more than the number of available CPUs (10). To disable workers, set to \`false\`. Received \`1000\`.
Error: "workers" must be more than zero and not more than the number of available CPUs (1). To disable workers, set to \`false\`. Received \`1000\`.
",
]
`;

exports[`betterer > should throw when there is invalid config: { workers: NaN } 1`] = `
[
"
Error: "workers" must be more than zero and not more than the number of available CPUs (10). To disable workers, set to \`false\`. Received \`null\`.
Error: "workers" must be more than zero and not more than the number of available CPUs (1). To disable workers, set to \`false\`. Received \`null\`.
",
]
`;

0 comments on commit 3a0f3c4

Please sign in to comment.