Skip to content

Commit d399583

Browse files
committed
refactor(core): improve read-code-pushup-config error handling and tests
1 parent 5f54283 commit d399583

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ describe('configMiddleware', () => {
3232
it('should throw with invalid config path', async () => {
3333
await expect(
3434
configMiddleware({ config: 'wrong/path/to/config' }),
35-
).rejects.toThrow(/no such file or directory/);
35+
).rejects.toThrow(/Provided path .* is not valid./);
3636
});
3737
});
Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
1-
import { join } from 'path';
2-
import * as process from 'process';
1+
import { dirname, join } from 'path';
2+
import { fileURLToPath } from 'url';
33
import { describe, expect } from 'vitest';
4-
import { persistConfig, uploadConfig } from '@code-pushup/models/testing';
54
import { readCodePushupConfig } from './read-code-pushup-config';
65

76
describe('readCodePushupConfig', () => {
8-
it('should load file', async () => {
9-
const module = await readCodePushupConfig(
10-
join(process.cwd(), 'packages', 'core', 'test', 'minimal.config.mjs'),
11-
);
12-
expect(module).toEqual(
7+
const configDirPath = join(
8+
fileURLToPath(dirname(import.meta.url)),
9+
'..',
10+
'..',
11+
'..',
12+
'..',
13+
'..',
14+
'testing-utils',
15+
'src',
16+
'lib',
17+
'fixtures',
18+
'configs',
19+
);
20+
21+
it('should load a valid configuration file', async () => {
22+
await expect(
23+
readCodePushupConfig(join(configDirPath, 'code-pushup.config.ts')),
24+
).resolves.toEqual(
1325
expect.objectContaining({
14-
persist: expect.objectContaining(persistConfig()),
1526
upload: expect.objectContaining({
16-
...uploadConfig(),
1727
organization: 'code-pushup',
1828
}),
19-
categories: expect.arrayContaining([
20-
expect.objectContaining({
21-
slug: 'category-slug-1',
22-
}),
23-
]),
29+
categories: expect.any(Array),
2430
plugins: expect.arrayContaining([
2531
expect.objectContaining({
26-
slug: 'mock-plugin-slug',
32+
slug: 'node',
2733
}),
2834
]),
2935
}),
3036
);
3137
});
3238

33-
it('should throw if filepath not given', async () => {
34-
const filepath = '';
35-
await expect(readCodePushupConfig(filepath)).rejects.toThrow(
36-
`No filepath provided`,
39+
it('should throw if the path is empty', async () => {
40+
await expect(readCodePushupConfig('')).rejects.toThrow(
41+
'The configuration path is empty.',
3742
);
3843
});
3944

40-
it('should throw if file does not exist', async () => {
41-
const filepath = join('invalid-path', 'valid-export.mjs');
42-
await expect(readCodePushupConfig(filepath)).rejects.toThrow(
43-
`invalid-path`,
44-
);
45+
it('should throw if the file does not exist', async () => {
46+
await expect(
47+
readCodePushupConfig(join('non-existent', 'config.file.js')),
48+
).rejects.toThrow(/Provided path .* is not valid./);
4549
});
4650

47-
it('should throw if config is invalid', async () => {
48-
const filepath = join(
49-
process.cwd(),
50-
'packages',
51-
'core',
52-
'test',
53-
'invaid.config.mjs',
54-
);
55-
await expect(readCodePushupConfig(filepath)).rejects.toThrow(
56-
`"code": "invalid_type",`,
57-
);
51+
it('should throw if the configuration is empty', async () => {
52+
await expect(
53+
readCodePushupConfig(join(configDirPath, 'code-pushup.empty.config.js')),
54+
).rejects.toThrow(`"code": "invalid_type",`);
55+
});
56+
57+
it('should throw if the configuration is invalid', async () => {
58+
await expect(
59+
readCodePushupConfig(
60+
join(configDirPath, 'code-pushup.invalid.config.ts'),
61+
),
62+
).rejects.toThrow(/refs are duplicates/);
5863
});
5964
});

packages/core/src/lib/implementation/read-code-pushup-config.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { stat } from 'fs/promises';
21
import { CoreConfig, coreConfigSchema } from '@code-pushup/models';
3-
import { importEsmModule } from '@code-pushup/utils';
2+
import { fileExists, importEsmModule } from '@code-pushup/utils';
43

54
export class ConfigPathError extends Error {
65
constructor(configPath: string) {
7-
super(`Config path ${configPath} is not a file.`);
6+
super(`Provided path '${configPath}' is not valid.`);
87
}
98
}
109

1110
export async function readCodePushupConfig(filepath: string) {
1211
if (!filepath.length) {
13-
throw new Error('No filepath provided');
12+
throw new Error('The configuration path is empty.');
1413
}
1514

16-
const isFile = (await stat(filepath)).isFile();
17-
if (!isFile) {
15+
if (!(await fileExists(filepath))) {
1816
throw new ConfigPathError(filepath);
1917
}
2018

testing-utils/src/lib/fixtures/configs/code-pushup.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export default {
2626
outputFile: 'output.json',
2727
},
2828
groups: [],
29-
slug: 'plugin-1',
30-
title: 'plugin 1',
29+
slug: 'node',
30+
title: 'Node.js',
3131
icon: 'javascript',
3232
},
3333
],

0 commit comments

Comments
 (0)