mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-25 06:51:11 +00:00
204a63d945
* fix(config/parse): ignore trailing commas during jsonc validation * use codeBlock for wrapping code
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import { codeBlock } from 'common-tags';
|
|
import jsonValidator from 'json-dup-key-validator';
|
|
import { parseFileConfig } from './parse.ts';
|
|
|
|
vi.mock('json-dup-key-validator', { spy: true });
|
|
|
|
describe('config/parse', () => {
|
|
describe('json', () => {
|
|
it('parses', () => {
|
|
expect(parseFileConfig('config.json', '{}')).toEqual({
|
|
success: true,
|
|
parsedContents: {},
|
|
});
|
|
});
|
|
|
|
it('parses jsonc with trailing comma', () => {
|
|
const content = codeBlock`
|
|
// comment
|
|
{
|
|
"extends": ["config:best-practices"],
|
|
}
|
|
`;
|
|
expect(parseFileConfig('config.jsonc', content)).toEqual({
|
|
success: true,
|
|
parsedContents: { extends: ['config:best-practices'] },
|
|
});
|
|
});
|
|
|
|
it('keeps commas inside strings', () => {
|
|
const content = codeBlock`
|
|
{
|
|
"foo": "a,]",
|
|
"bar": "b,}",
|
|
}
|
|
`;
|
|
expect(parseFileConfig('config.jsonc', content)).toEqual({
|
|
success: true,
|
|
parsedContents: { foo: 'a,]', bar: 'b,}' },
|
|
});
|
|
});
|
|
|
|
it('returns error', () => {
|
|
// syntax validation
|
|
expect(parseFileConfig('config.json', '{')).toEqual({
|
|
success: false,
|
|
validationError: 'Invalid JSON (parsing failed)',
|
|
validationMessage: 'Syntax error: unclosed statement near {',
|
|
});
|
|
|
|
// duplicate keys
|
|
vi.mocked(jsonValidator).validate.mockReturnValueOnce(undefined);
|
|
expect(parseFileConfig('config.json', '{')).toEqual({
|
|
success: false,
|
|
validationError: 'Duplicate keys in JSON',
|
|
validationMessage: '"Syntax error: unclosed statement near {"',
|
|
});
|
|
|
|
// JSON.parse
|
|
vi.mocked(jsonValidator).validate.mockReturnValue(undefined);
|
|
expect(parseFileConfig('config.json', '{')).toEqual({
|
|
success: false,
|
|
validationError: 'Invalid JSON (parsing failed)',
|
|
validationMessage:
|
|
'JSON.parse error: `JSON5: invalid end of input at 1:2`',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('json5', () => {
|
|
it('parses', () => {
|
|
expect(parseFileConfig('config.json5', '{}')).toEqual({
|
|
success: true,
|
|
parsedContents: {},
|
|
});
|
|
});
|
|
|
|
it('returns error', () => {
|
|
expect(parseFileConfig('config.json5', '{')).toEqual({
|
|
success: false,
|
|
validationError: 'Invalid JSON5 (parsing failed)',
|
|
validationMessage:
|
|
'JSON5.parse error: `JSON5: invalid end of input at 1:2`',
|
|
});
|
|
});
|
|
});
|
|
});
|