Skip to content

Commit c88beaf

Browse files
committed
refactor(plugin-coverage): use runner function instead of runner config
1 parent 7b82402 commit c88beaf

File tree

6 files changed

+38
-138
lines changed

6 files changed

+38
-138
lines changed

packages/plugin-coverage/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"@code-pushup/models": "0.96.1",
3838
"@code-pushup/utils": "0.96.1",
3939
"parse-lcov": "^1.0.4",
40-
"yargs": "^17.7.2",
4140
"zod": "^4.0.5"
4241
},
4342
"peerDependencies": {

packages/plugin-coverage/src/bin.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/plugin-coverage/src/lib/coverage-plugin.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { createRequire } from 'node:module';
2-
import path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
42
import {
53
type Audit,
64
type Group,
@@ -13,7 +11,7 @@ import {
1311
type CoverageType,
1412
coveragePluginConfigSchema,
1513
} from './config.js';
16-
import { createRunnerConfig } from './runner/index.js';
14+
import { createRunnerFunction } from './runner/index.js';
1715
import { coverageDescription, coverageTypeWeightMapper } from './utils.js';
1816

1917
/**
@@ -60,12 +58,6 @@ export async function coveragePlugin(
6058
})),
6159
};
6260

63-
const runnerScriptPath = path.join(
64-
fileURLToPath(path.dirname(import.meta.url)),
65-
'..',
66-
'bin.js',
67-
);
68-
6961
const packageJson = createRequire(import.meta.url)(
7062
'../../package.json',
7163
) as typeof import('../../package.json');
@@ -82,7 +74,7 @@ export async function coveragePlugin(
8274
version: packageJson.version,
8375
audits,
8476
groups: [group],
85-
runner: await createRunnerConfig(runnerScriptPath, coverageConfig),
77+
runner: createRunnerFunction(coverageConfig),
8678
...(scoreTargets && { scoreTargets }),
8779
};
8880
}

packages/plugin-coverage/src/lib/coverage-plugin.unit.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import path from 'node:path';
22
import { describe, expect, it } from 'vitest';
3-
import { type RunnerConfig, pluginConfigSchema } from '@code-pushup/models';
3+
import { pluginConfigSchema } from '@code-pushup/models';
44
import { coveragePlugin } from './coverage-plugin.js';
55

6-
vi.mock('./runner/index.ts', () => ({
7-
createRunnerConfig: vi.fn().mockReturnValue({
8-
command: 'node',
9-
outputFile: 'runner-output.json',
10-
} satisfies RunnerConfig),
11-
}));
12-
136
describe('coveragePlugin', () => {
147
const LCOV_PATH = path.join(
158
'packages',
@@ -30,7 +23,7 @@ describe('coveragePlugin', () => {
3023
title: 'Code coverage',
3124
audits: expect.any(Array),
3225
groups: expect.any(Array),
33-
runner: expect.any(Object),
26+
runner: expect.any(Function),
3427
}),
3528
);
3629
});
Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,34 @@
1-
import { writeFile } from 'node:fs/promises';
2-
import path from 'node:path';
3-
import type { RunnerConfig, RunnerFilesPaths } from '@code-pushup/models';
4-
import {
5-
createRunnerFiles,
6-
ensureDirectoryExists,
7-
executeProcess,
8-
filePathToCliArg,
9-
objectToCliArgs,
10-
readJsonFile,
11-
} from '@code-pushup/utils';
1+
import type { RunnerFunction } from '@code-pushup/models';
2+
import { executeProcess } from '@code-pushup/utils';
123
import type { FinalCoveragePluginConfig } from '../config.js';
134
import { lcovResultsToAuditOutputs } from './lcov/lcov-runner.js';
145

15-
export async function executeRunner({
16-
runnerConfigPath,
17-
runnerOutputPath,
18-
}: RunnerFilesPaths): Promise<void> {
19-
const { reports, coverageToolCommand, continueOnCommandFail, coverageTypes } =
20-
await readJsonFile<FinalCoveragePluginConfig>(runnerConfigPath);
6+
export function createRunnerFunction(
7+
config: FinalCoveragePluginConfig,
8+
): RunnerFunction {
9+
return async () => {
10+
const {
11+
reports,
12+
coverageToolCommand,
13+
continueOnCommandFail,
14+
coverageTypes,
15+
} = config;
2116

22-
// Run coverage tool if provided
23-
if (coverageToolCommand != null) {
24-
const { command, args } = coverageToolCommand;
25-
try {
26-
await executeProcess({ command, args });
27-
} catch {
28-
if (!continueOnCommandFail) {
29-
throw new Error(
30-
'Coverage plugin: Running coverage tool failed. Make sure all your provided tests are passing.',
31-
);
17+
// Run coverage tool if provided
18+
if (coverageToolCommand != null) {
19+
const { command, args } = coverageToolCommand;
20+
try {
21+
await executeProcess({ command, args });
22+
} catch {
23+
if (!continueOnCommandFail) {
24+
throw new Error(
25+
'Coverage plugin: Running coverage tool failed. Make sure all your provided tests are passing.',
26+
);
27+
}
3228
}
3329
}
34-
}
35-
36-
// Calculate coverage from LCOV results
37-
const auditOutputs = await lcovResultsToAuditOutputs(reports, coverageTypes);
38-
39-
await ensureDirectoryExists(path.dirname(runnerOutputPath));
40-
await writeFile(runnerOutputPath, JSON.stringify(auditOutputs));
41-
}
42-
43-
export async function createRunnerConfig(
44-
scriptPath: string,
45-
config: FinalCoveragePluginConfig,
46-
): Promise<RunnerConfig> {
47-
// Create JSON config for executeRunner
48-
const { runnerConfigPath, runnerOutputPath } = await createRunnerFiles(
49-
'coverage',
50-
JSON.stringify(config),
51-
);
5230

53-
return {
54-
command: 'node',
55-
args: [
56-
filePathToCliArg(scriptPath),
57-
...objectToCliArgs({ runnerConfigPath, runnerOutputPath }),
58-
],
59-
configFile: runnerConfigPath,
60-
outputFile: runnerOutputPath,
31+
// Calculate coverage from LCOV results
32+
return lcovResultsToAuditOutputs(reports, coverageTypes);
6133
};
6234
}
Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,12 @@
11
import path from 'node:path';
22
import { fileURLToPath } from 'node:url';
33
import { expect } from 'vitest';
4-
import type { AuditOutputs, RunnerConfig } from '@code-pushup/models';
5-
import { createRunnerFiles, readJsonFile } from '@code-pushup/utils';
6-
import type { FinalCoveragePluginConfig } from '../config.js';
7-
import { createRunnerConfig, executeRunner } from './index.js';
4+
import { type AuditOutputs, DEFAULT_PERSIST_CONFIG } from '@code-pushup/models';
5+
import { createRunnerFunction } from './index.js';
86

9-
describe('createRunnerConfig', () => {
10-
it('should create a valid runner config', async () => {
11-
const runnerConfig = await createRunnerConfig('executeRunner.ts', {
12-
reports: ['coverage/lcov.info'],
13-
coverageTypes: ['branch'],
14-
scoreTargets: 0.85,
15-
continueOnCommandFail: true,
16-
});
17-
expect(runnerConfig).toStrictEqual<RunnerConfig>({
18-
command: 'node',
19-
args: [
20-
'"executeRunner.ts"',
21-
expect.stringContaining('plugin-config.json'),
22-
expect.stringContaining('runner-output.json'),
23-
],
24-
outputFile: expect.stringContaining('runner-output.json'),
25-
configFile: expect.stringContaining('plugin-config.json'),
26-
});
27-
});
28-
29-
it('should provide plugin config to runner in JSON file', async () => {
30-
const pluginConfig: FinalCoveragePluginConfig = {
31-
coverageTypes: ['line'],
32-
reports: ['coverage/lcov.info'],
33-
coverageToolCommand: { command: 'npm', args: ['run', 'test'] },
34-
scoreTargets: 0.85,
35-
continueOnCommandFail: true,
36-
};
37-
38-
const { configFile } = await createRunnerConfig(
39-
'executeRunner.ts',
40-
pluginConfig,
41-
);
42-
43-
expect(configFile).toMatch(/.*plugin-config\.json$/);
44-
const config = await readJsonFile<FinalCoveragePluginConfig>(configFile!);
45-
expect(config).toStrictEqual(pluginConfig);
46-
});
47-
});
48-
49-
describe('executeRunner', () => {
7+
describe('createRunnerFunction', () => {
508
it('should successfully execute runner', async () => {
51-
const config: FinalCoveragePluginConfig = {
9+
const runner = createRunnerFunction({
5210
reports: [
5311
path.join(
5412
fileURLToPath(path.dirname(import.meta.url)),
@@ -61,18 +19,11 @@ describe('executeRunner', () => {
6119
],
6220
coverageTypes: ['line'],
6321
continueOnCommandFail: true,
64-
};
65-
66-
const runnerFiles = await createRunnerFiles(
67-
'coverage',
68-
JSON.stringify(config),
69-
);
70-
await executeRunner(runnerFiles);
22+
});
7123

72-
const results = await readJsonFile<AuditOutputs>(
73-
runnerFiles.runnerOutputPath,
74-
);
75-
expect(results).toStrictEqual<AuditOutputs>([
24+
await expect(
25+
runner({ persist: DEFAULT_PERSIST_CONFIG }),
26+
).resolves.toStrictEqual([
7627
{
7728
slug: 'line-coverage',
7829
score: 0.7,
@@ -112,6 +63,6 @@ describe('executeRunner', () => {
11263
],
11364
},
11465
},
115-
]);
66+
] satisfies AuditOutputs);
11667
});
11768
});

0 commit comments

Comments
 (0)