Skip to content

Commit

Permalink
feat: Support ${workspaceFolder} substituion
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Feb 21, 2020
1 parent 927e8f0 commit 6d1dfbc
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/_server/sampleSourceFiles/cSpell.json
Expand Up @@ -19,7 +19,7 @@
"hte"
],
"dictionaryDefinitions": [
{ "name": "cpp2", "path": "../dictionaries", "file": "cpp.txt" }
{ "name": "cpp2", "path": "../dictionaries/cpp.txt" }
],
"languageSettings": [
{ "languageId": "c", "dictionaries": ["cpp2"] },
Expand Down
6 changes: 6 additions & 0 deletions packages/_server/sampleSourceFiles/cspell-ext.json
@@ -0,0 +1,6 @@
{
"patterns": [
{ "name": "test", "pattern": "TEST", "description": "Test Pattern from extension" }
],
"words": ["TestWord"]
}
14 changes: 14 additions & 0 deletions packages/_server/sampleSourceFiles/overrides/cspell.json
@@ -0,0 +1,14 @@
{
"overrides": [
{
"name": "Override Typescript",
"filename": "**/*.ts",
"dictionaryDefinitions": [
{
"name": "Test Dictionary",
"path": "./words.txt"
}
]
}
]
}
5 changes: 5 additions & 0 deletions packages/_server/sampleSourceFiles/overrides/words.txt
@@ -0,0 +1,5 @@
encrypted
toggle
declare
concepts
NeXt
228 changes: 212 additions & 16 deletions packages/_server/src/documentSettings.test.ts
Expand Up @@ -13,17 +13,47 @@ jest.mock('./util');

const mockGetWorkspaceFolders = getWorkspaceFolders as jest.Mock;
const mockGetConfiguration = getConfiguration as jest.Mock;
const workspaceRoot = Path.resolve(Path.join(__dirname, '..'));
const workspaceFolder: WorkspaceFolder = {
uri: Uri.file(workspaceRoot).toString(),
const workspaceRoot = Path.resolve(Path.join(__dirname, '..', '..', '..'));
const workspaceServer = Path.resolve(Path.join(__dirname, '..'));
const workspaceClient = Path.resolve(Path.join(__dirname, '..', '..', 'client'));
const workspaceFolderServer: WorkspaceFolder = {
uri: Uri.file(workspaceServer).toString(),
name: '_server',
};
const workspaceFolderRoot: WorkspaceFolder = {
uri: Uri.file(workspaceRoot).toString(),
name: 'vscode-spell-checker',
};
const workspaceFolderClient: WorkspaceFolder = {
uri: Uri.file(workspaceClient).toString(),
name: 'client',
};

const cspellConfigInVsCode: CSpellUserSettings = {
ignorePaths: [
'${workspaceFolder:_server}/**/*.json'
],
import: [
'${workspaceFolder:_server}/sampleSourceFiles/overrides/cspell.json',
'${workspaceFolder:_server}/sampleSourceFiles/cSpell.json',
]
};

describe('Validate DocumentSettings', () => {
beforeEach(() => {
// Clear all mock instances and calls to constructor and all methods:
mockGetWorkspaceFolders.mockClear();
});
});

test('shallowCleanObject', () => {
const clean = debugExports.shallowCleanObject;
expect(clean('hello')).toBe('hello');
expect(clean(42)).toBe(42);
expect([1,2,3,4]).toEqual([1,2,3,4]);
expect({}).toEqual({});
expect({ name: 'name' }).toEqual({ name: 'name' });
expect({ name: 'name', age: undefined }).toEqual({ name: 'name' });
});

test('version', () => {
const docSettings = newDocumentSettings();
Expand All @@ -32,11 +62,11 @@ describe('Validate DocumentSettings', () => {
expect(docSettings.version).toEqual(1);
});

it('checks isUriAllowed', () => {
test('checks isUriAllowed', () => {
expect(isUriAllowed(Uri.file(__filename).toString())).toBe(true);
});

it('checks isUriBlackListed', () => {
test('checks isUriBlackListed', () => {
const uriFile = Uri.file(__filename);
expect(isUriBlackListed(uriFile.toString())).toBe(false);

Expand All @@ -45,17 +75,21 @@ describe('Validate DocumentSettings', () => {
expect(isUriBlackListed(uriGit.toString())).toBe(true);
});

it('folders', async () => {
const mockFolders: WorkspaceFolder[] = [workspaceFolder];
test('folders', async () => {
const mockFolders: WorkspaceFolder[] = [
workspaceFolderRoot,
workspaceFolderClient,
workspaceFolderServer,
];
mockGetWorkspaceFolders.mockReturnValue(mockFolders);
const docSettings = newDocumentSettings();

const folders = await docSettings.folders;
expect(folders).toBe(mockFolders);
});

it('tests register config path', () => {
const mockFolders: WorkspaceFolder[] = [workspaceFolder];
test('tests register config path', () => {
const mockFolders: WorkspaceFolder[] = [workspaceFolderServer];
mockGetWorkspaceFolders.mockReturnValue(mockFolders);

const docSettings = newDocumentSettings();
Expand All @@ -66,12 +100,16 @@ describe('Validate DocumentSettings', () => {
expect(docSettings.configsToImport).toContain(configFile);
});

it('test getSettings', async () => {
const mockFolders: WorkspaceFolder[] = [workspaceFolder];
test('test getSettings', async () => {
const mockFolders: WorkspaceFolder[] = [
workspaceFolderRoot,
workspaceFolderClient,
workspaceFolderServer,
];
mockGetWorkspaceFolders.mockReturnValue(mockFolders);
mockGetConfiguration.mockReturnValue([{}, {}]);
mockGetConfiguration.mockReturnValue([cspellConfigInVsCode, {}]);
const docSettings = newDocumentSettings();
const configFile = Path.resolve(Path.join(__dirname, '..', 'sampleSourceFiles', 'cSpell.json'));
const configFile = Path.resolve(Path.join(__dirname, '..', 'sampleSourceFiles', 'cspell-ext.json'));
docSettings.registerConfigurationFile(configFile);

const settings = await docSettings.getSettings({ uri: Uri.file(__filename).toString() });
Expand All @@ -80,8 +118,8 @@ describe('Validate DocumentSettings', () => {
expect(settings.language).toBe('en-gb');
});

it('test isExcluded', async () => {
const mockFolders: WorkspaceFolder[] = [workspaceFolder];
test('test isExcluded', async () => {
const mockFolders: WorkspaceFolder[] = [workspaceFolderServer];
mockGetWorkspaceFolders.mockReturnValue(mockFolders);
mockGetConfiguration.mockReturnValue([{}, {}]);
const docSettings = newDocumentSettings();
Expand Down Expand Up @@ -157,3 +195,161 @@ describe('Validate RegExp corrections', () => {
expect(correctBadSettings(settings)).not.toEqual(settings);
});
});

describe('Validate workspace substitution resolver', () => {
const rootPath = '/path to root/root';
const clientPath = Path.normalize(Path.join(rootPath, 'client'));
const serverPath = Path.normalize(Path.join(rootPath, '_server'));
const clientTestPath = Path.normalize(Path.join(clientPath, 'test'));
const rootUri = Uri.file(rootPath);
const clientUri = Uri.file(clientPath);
const serverUri = Uri.file(serverPath);
const testUri = Uri.file(clientTestPath);
const workspaceFolders = {
root:
{
name: 'Root',
uri: rootUri.toString()
},
client:
{
name: 'Client',
uri: clientUri.toString()
},
server:
{
name: 'Server',
uri: serverUri.toString()
},
test: {
name: 'client-test',
uri: testUri.toString()
}
};
const workspaces: WorkspaceFolder[] = [
workspaceFolders.root,
workspaceFolders.client,
workspaceFolders.server,
workspaceFolders.test,
];

const settingsImports: CSpellUserSettings = Object.freeze({
'import': [
'cspell.json',
'${workspaceFolder}/cspell.json',
'${workspaceFolder:Client}/cspell.json',
'${workspaceFolder:Server}/cspell.json',
'${workspaceFolder:Root}/cspell.json',
'${workspaceFolder:Failed}/cspell.json',
]
});

const settingsIgnorePaths: CSpellUserSettings = Object.freeze({
ignorePaths: [
'**/node_modules/**',
'${workspaceFolder}/node_modules/**',
'${workspaceFolder:Server}/samples/**',
'${workspaceFolder:client-test}/**/*.json',
]
});

const settingsDictionaryDefinitions: CSpellUserSettings = Object.freeze({
dictionaryDefinitions: [
{
name: 'My Dictionary',
path: '${workspaceFolder:Root}/words.txt'
},
{
name: 'Company Dictionary',
path: '${workspaceFolder}/node_modules/@company/terms/terms.txt'
},
].map(f => Object.freeze(f))
});

const settingsLanguageSettings: CSpellUserSettings = Object.freeze({
languageSettings: [
{
languageId: 'typescript',
dictionaryDefinitions: settingsDictionaryDefinitions.dictionaryDefinitions
}
].map(f => Object.freeze(f))
});

const settingsOverride: CSpellUserSettings = {
overrides: [
{
filename: '*.ts',
ignorePaths: settingsIgnorePaths.ignorePaths,
languageSettings: settingsLanguageSettings.languageSettings,
dictionaryDefinitions: settingsDictionaryDefinitions.dictionaryDefinitions
}
].map(f => Object.freeze(f))
};

test('resolveSettings Imports', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const result = debugExports.resolveSettings(settingsImports, resolver);
expect(result.import).toEqual([
'cspell.json',
`${clientUri.fsPath}/cspell.json`,
`${clientUri.fsPath}/cspell.json`,
`${serverUri.fsPath}/cspell.json`,
`${rootUri.fsPath}/cspell.json`,
'${workspaceFolder:Failed}/cspell.json',
]);
});

test('resolveSettings ignorePaths', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaceFolders.client, workspaces);
const result = debugExports.resolveSettings(settingsIgnorePaths, resolver);
expect(result.ignorePaths).toEqual([
'**/node_modules/**',
'/node_modules/**',
`${serverUri.path}/samples/**`,
'/test/**/*.json',
]);
});

test('resolveSettings dictionaryDefinitions', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const result = debugExports.resolveSettings(settingsDictionaryDefinitions, resolver);
expect(result.dictionaryDefinitions).toEqual([
{ name: 'My Dictionary', path: `${rootUri.fsPath}/words.txt`},
{ name: 'Company Dictionary', path: `${clientUri.fsPath}/node_modules/@company/terms/terms.txt`},
]);
});

test('resolveSettings languageSettings', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const result = debugExports.resolveSettings(settingsLanguageSettings, resolver);
expect(result?.languageSettings?.[0]).toEqual({
languageId: 'typescript',
dictionaryDefinitions: [
{ name: 'My Dictionary', path: `${rootUri.fsPath}/words.txt`},
{ name: 'Company Dictionary', path: `${clientUri.fsPath}/node_modules/@company/terms/terms.txt`},
]
});
});

test('resolveSettings overrides', () => {
const resolver = debugExports.createWorkspaceNamesResolver(workspaces[1], workspaces);
const result = debugExports.resolveSettings(settingsOverride, resolver);
expect(result?.overrides?.[0]?.languageSettings?.[0]).toEqual({
languageId: 'typescript',
dictionaryDefinitions: [
{ name: 'My Dictionary', path: `${rootUri.fsPath}/words.txt`},
{ name: 'Company Dictionary', path: `${clientUri.fsPath}/node_modules/@company/terms/terms.txt`},
]
});
expect(result?.overrides?.[0]?.dictionaryDefinitions).toEqual([
{ name: 'My Dictionary', path: `${rootUri.fsPath}/words.txt`},
{ name: 'Company Dictionary', path: `${clientUri.fsPath}/node_modules/@company/terms/terms.txt`},
]);
expect(result?.overrides?.[0]?.ignorePaths).toEqual([
'**/node_modules/**',
'/node_modules/**',
`${serverUri.path}/samples/**`,
'/test/**/*.json',
]);
});
});

0 comments on commit 6d1dfbc

Please sign in to comment.