/
DictionarySettings.test.ts
128 lines (112 loc) · 5.31 KB
/
DictionarySettings.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { DictionaryDefinition, DictionaryDefinitionLegacy } from '@cspell/cspell-types';
import * as fsp from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { getDefaultSettings } from './DefaultSettings';
import * as DictSettings from './DictionarySettings';
const defaultSettings = getDefaultSettings();
const oc = expect.objectContaining;
describe('Validate DictionarySettings', () => {
test('expects default to not be empty', () => {
const mapDefs = DictSettings.filterDictDefsToLoad(
['php', 'wordsEn', 'unknown', 'en_us'],
defaultSettings.dictionaryDefinitions!
);
const files = mapDefs.map((def) => def.name!);
expect(mapDefs).toHaveLength(2);
expect(files.filter((a) => a.includes('php'))).toHaveLength(1);
expect(files.filter((a) => a.includes('wordsEn'))).toHaveLength(0);
expect(files.filter((a) => a.includes('en_us'))).toHaveLength(1);
expect(files.filter((a) => a.includes('unknown'))).toHaveLength(0);
});
test('tests exclusions and empty ids', () => {
const ids = [
'php',
'cpp', // add cpp
'wordsEn',
' ', // empty entry
'unknown',
'!cpp', // remove cpp
'en_us',
];
const expected = ['php', 'en_us'].sort();
const mapDefs = DictSettings.filterDictDefsToLoad(ids, defaultSettings.dictionaryDefinitions!);
const dicts = mapDefs.map((def) => def.name!).sort();
expect(dicts).toEqual(expected);
});
test.each`
ids | expected
${'!php, php, cpp, !!cpp'} | ${'cpp'}
${'!php, php, !!php, !!cpp'} | ${'cpp, php'}
${'!!!!!!!!!!cpp, !cpp'} | ${'cpp'}
`('validate dictionary exclusions $ids', ({ ids, expected }: { ids: string; expected: string }) => {
const dictIds = ids.split(',');
const expectedIds = expected.split(',').map((id) => id.trim());
const mapDefs = DictSettings.filterDictDefsToLoad(dictIds, defaultSettings.dictionaryDefinitions!);
const dicts = mapDefs.map((def) => def.name!).sort();
expect(dicts).toEqual(expectedIds);
});
test('tests that the files exist', () => {
const defaultDicts = defaultSettings.dictionaryDefinitions!;
const dictIds = defaultDicts.map((def) => def.name);
const mapDefs = DictSettings.filterDictDefsToLoad(dictIds, defaultSettings.dictionaryDefinitions!);
const access = mapDefs.map((def) => def.path!).map((path) => fsp.access(path));
expect(mapDefs.length).toBeGreaterThan(0);
return Promise.all(access);
});
test('tests default dictionary settings have been normalized', () => {
const { dictionaryDefinitions } = defaultSettings;
expect(dictionaryDefinitions?.length).toBeGreaterThan(1);
dictionaryDefinitions?.forEach((def) => {
expect(DictSettings.isDictionaryDefinitionWithSource(def)).toBe(true);
expect(path.isAbsolute(def.path || '')).toBe(true);
});
});
test('tests normalizing home dir', () => {
const pathToConfig = './cspell.json';
const basePath = path.join('some', 'dir', 'words.txt');
const absolutePath = path.join(os.homedir(), basePath);
const def: DictionaryDefinition = {
name: 'words',
path: path.join('~', basePath),
};
const nDef = DictSettings.mapDictDefToInternal(def, pathToConfig);
expect(nDef).toEqual(
oc({
name: 'words',
path: absolutePath,
__source: pathToConfig,
})
);
const legacyDef: DictionaryDefinitionLegacy = {
name: 'words',
path: path.dirname(path.join('~', basePath)),
file: path.basename(basePath),
};
const nLegacyDef = DictSettings.mapDictDefToInternal(legacyDef, pathToConfig);
expect(nLegacyDef).toEqual(nDef);
});
test('Double normalize', () => {
const configFile = './cspell.json';
const def: DictionaryDefinition = {
name: 'Text Dict',
path: './words.txt',
};
const normalizedDef = DictSettings.mapDictDefToInternal(def, configFile);
expect(DictSettings.isDictionaryDefinitionWithSource(normalizedDef)).toBe(true);
expect(normalizedDef).toEqual(expect.objectContaining({ __source: configFile }));
const normalizedDef2 = DictSettings.mapDictDefToInternal(normalizedDef, configFile);
expect(normalizedDef2).toBe(normalizedDef);
expect(() => DictSettings.mapDictDefToInternal(normalizedDef, './different.config.json')).toThrowError(
'Trying to normalize a dictionary definition with a different source.'
);
});
test.each`
def | expected
${{}} | ${false}
${DictSettings.mapDictDefToInternal({ name: 'def', path: './dict.txt' }, __filename)} | ${true}
`('isDictionaryDefinitionInternal', ({ def, expected }) => {
expect(DictSettings.isDictionaryDefinitionInternal(def)).toBe(expected);
});
});