Skip to content

Commit

Permalink
fix: Allow config file version to be a number. (#1730)
Browse files Browse the repository at this point in the history
fix: #1729
  • Loading branch information
Jason3S authored Sep 21, 2021
1 parent 9922998 commit 9fa2eee
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
31 changes: 29 additions & 2 deletions packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const { normalizeSettings, validateRawConfigVersion, validateRawConfigExports }
const rootCspellLib = path.resolve(path.join(__dirname, '../..'));
const samplesDir = path.resolve(rootCspellLib, 'samples');
const samplesSrc = path.join(samplesDir, 'src');
const testFixtures = path.join(rootCspellLib, '../../test-fixtures');

jest.mock('../util/logger');

Expand Down Expand Up @@ -514,6 +515,8 @@ describe('Validate search/load config files', () => {
`('Load from $file', async ({ file, expectedConfig }: TestLoadConfig) => {
const searchResult = await loadConfig(file);
expect(searchResult).toEqual(expectedConfig ? expect.objectContaining(expectedConfig) : undefined);
expect(mockedLogWarning).toHaveBeenCalledTimes(0);
expect(mockedLogError).toHaveBeenCalledTimes(0);
});

test.each`
Expand All @@ -525,10 +528,21 @@ describe('Validate search/load config files', () => {
${s('js-config/cspell-no-export.js')} | ${cfg(s('js-config/cspell-no-export.js'))}
${s('js-config/cspell-bad.js')} | ${cfg(readError(s('js-config/cspell-bad.js')))}
`('ReadRawSettings from $file', async ({ file, expectedConfig }: TestLoadConfig) => {
// js-config/cspell-no-export.js logs a message.
jest.spyOn(console, 'log').mockImplementation(() => undefined);
const searchResult = await readRawSettings(file);
expect(searchResult).toEqual(expectedConfig ? expect.objectContaining(expectedConfig) : undefined);
expect(mockedLogWarning).toHaveBeenCalledTimes(0);
expect(mockedLogError).toHaveBeenCalledTimes(0);
});

test.each`
file | expectedConfig
${path.join(testFixtures, 'issues/issue-1729/a.yaml')} | ${oc({ version: '0.2' })}
${path.join(testFixtures, 'issues/issue-1729/b.yaml')} | ${oc({ version: '0.2' })}
`('ReadRawSettings from $file', async ({ file, expectedConfig }: TestLoadConfig) => {
const searchResult = await readRawSettings(file);
expect(searchResult).toEqual(expectedConfig);
expect(mockedLogWarning).toHaveBeenCalledTimes(0);
expect(mockedLogError).toHaveBeenCalledTimes(0);
});

test('loadPnP', async () => {
Expand All @@ -550,11 +564,24 @@ describe('Validate search/load config files', () => {
expect(result.dictionaries).toEqual(['medical terms']);
});

test.each`
config
${{ version: '0.2' }}
${{}}
${{ version: undefined }}
`('validateRawConfigVersion valid $config', ({ config }) => {
validateRawConfigVersion(config, { filename: 'filename' });
expect(mockedLogWarning).toHaveBeenCalledTimes(0);
expect(mockedLogError).toHaveBeenCalledTimes(0);
});

test.each`
config | mocked | expected
${{ version: 'hello' }} | ${mockedLogError} | ${'Unsupported config file version: "hello"\n File: "filename"'}
${{ version: '0.1' }} | ${mockedLogWarning} | ${'Legacy config file version found: "0.1", upgrade to "0.2"\n File: "filename"'}
${{ version: '0.3' }} | ${mockedLogWarning} | ${'Newer config file version found: "0.3". Supported version is "0.2"\n File: "filename"'}
${{ version: 0.2 }} | ${mockedLogError} | ${'Unsupported config file version: "0.2", string expected\n File: "filename"'}
${{ version: 0.3 }} | ${mockedLogError} | ${'Unsupported config file version: "0.3", string expected\n File: "filename"'}
`('validateRawConfigVersion $config', ({ config, mocked, expected }) => {
validateRawConfigVersion(config, { filename: 'filename' });
expect(mocked).toHaveBeenCalledWith(expected);
Expand Down
25 changes: 23 additions & 2 deletions packages/cspell-lib/src/Settings/CSpellSettingsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const configSettingsFileVersion0_1 = '0.1';
const configSettingsFileVersion0_2 = '0.2';
const currentSettingsFileVersion = configSettingsFileVersion0_2;

const setOfSupportedConfigVersions = new Set<string>(supportedCSpellConfigVersions);

export const sectionCSpell = 'cSpell';

export const defaultFileName = 'cspell.json';
Expand Down Expand Up @@ -113,6 +115,7 @@ function readConfig(fileRef: ImportFileRef): CSpellSettings {
const r = cspellConfigExplorerSync.load(filename);
if (!r?.config) throw 'not found';
Object.assign(s, r.config);
normalizeRawConfig(s);
validateRawConfig(s, fileRef);
} catch (err) {
fileRef.error =
Expand Down Expand Up @@ -822,9 +825,17 @@ function validationMessage(msg: string, fileRef: ImportFileRef) {
return msg + `\n File: "${fileRef.filename}"`;
}

function validateRawConfigVersion(config: CSpellUserSettings, fileRef: ImportFileRef): void {
function validateRawConfigVersion(config: CSpellUserSettings | { version: unknown }, fileRef: ImportFileRef): void {
const { version } = config;
if (version === undefined || supportedCSpellConfigVersions.includes(version)) return;

if (version === undefined) return;

if (typeof version !== 'string') {
logError(validationMessage(`Unsupported config file version: "${version}", string expected`, fileRef));
return;
}

if (setOfSupportedConfigVersions.has(version)) return;

if (!/^\d+(\.\d+)*$/.test(version)) {
logError(validationMessage(`Unsupported config file version: "${version}"`, fileRef));
Expand All @@ -847,6 +858,16 @@ function validateRawConfigExports(config: CSpellUserSettings, fileRef: ImportFil
}
}

interface NormalizableFields {
version?: string | number;
}

function normalizeRawConfig(config: CSpellUserSettings | NormalizableFields) {
if (typeof config.version === 'number') {
config.version = config.version.toString();
}
}

function validateRawConfig(config: CSpellUserSettings, fileRef: ImportFileRef): void {
const validations = [validateRawConfigExports, validateRawConfigVersion];
validations.forEach((fn) => fn(config, fileRef));
Expand Down
4 changes: 4 additions & 0 deletions test-fixtures/issues/issue-1729/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: 0.2
language: en
import:
- b.yaml
2 changes: 2 additions & 0 deletions test-fixtures/issues/issue-1729/b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 0.2
language: en

0 comments on commit 9fa2eee

Please sign in to comment.