Skip to content

Commit

Permalink
fix: Ensure the right config for all docs (#866)
Browse files Browse the repository at this point in the history
Ensure that the right cspell config is returned even if the document is not in one of the workspace folders.

## Other improvements
* fix: improve the integration tests
* dev: Remove code for Node 8 and 10
* ci: increase integration timeouts for testing
* fix: Make sure to include the schema for cspell.json in the extension
* fix: make sure the correct config file is returned
  If the document is not part of the workspace folders, make sure the correct config file is returned.
* dev: Make sure `package.json` is in the list of possible config files
* test: add user words to the sampleWorkspaces
* Update cspell-words.txt
* debug: debug the integration tests
  • Loading branch information
Jason3S authored May 17, 2021
1 parent f059110 commit 032afe3
Show file tree
Hide file tree
Showing 24 changed files with 226 additions and 107 deletions.
3 changes: 3 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
!CHANGELOG.md
!images/SpellCheck.png

# Include the schema
!node_modules/@cspell/cspell-types/cspell.schema.json

# Dictionaries cannot be WebPacked
!node_modules/@cspell/cspell-bundled-dicts/**
!node_modules/@cspell/dict*/**/{README.md,LICENSE}
Expand Down
1 change: 1 addition & 0 deletions cspell-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ visualstudio
vsce
vsmarketplacebadge
webhook
webpack'ed
webview
yandex
12 changes: 8 additions & 4 deletions packages/_integrationTests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
"node": ">=12"
},
"scripts": {
"build": "tsc -p .",
"build": "echo skipping build for integration-test",
"build2": "tsc -p .",
"clean": "rimraf out temp",
"build-production": "yarn run build",
"lint": "eslint \"src/**/*.ts\"",
"test-integration": "cross-env CODE_DISABLE_EXTENSIONS=true CODE_TESTS_PATH=./../_integrationTests/out CODE_EXTENSIONS_PATH=../.. CODE_TESTS_DATA_DIR=./temp node ../_integrationTests/integrationTestRunner",
"test": "yarn run build && yarn test-integration"
"test-integration": "cross-env CODE_DISABLE_EXTENSIONS=true CODE_TESTS_PATH=./../_integrationTests/out CODE_TESTS_WORKSPACE=./testFixtures CODE_EXTENSIONS_PATH=../.. CODE_TESTS_DATA_DIR=./temp node ../_integrationTests/integrationTestRunner",
"test": "yarn run build2 && yarn test-integration"
},
"workspaces": {
"nohoist": [
Expand All @@ -32,11 +33,14 @@
"@types/node": "^14.14.35",
"@types/vscode": "^1.54.0",
"chai": "^4.3.4",
"chalk": "^4.1.1",
"cross-env": "^7.0.3",
"glob": "^7.1.6",
"mocha": "^8.3.2",
"rimraf": "^3.0.2",
"typescript": "^4.2.2",
"vscode-test": "^1.5.1"
}
},
"peerDependenciesMeta": {},
"dependencies": {}
}
15 changes: 15 additions & 0 deletions packages/_integrationTests/sampleWorkspaces/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "0.2",
"dictionaryDefinitions": [
{
"path": "./user-words.txt",
"name": "user-words",
"addWords": true,
"scope": "user"
}
],
"dictionaries": [
"user-words"
],
"words": []
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Workspace 1

Sample README file.
21 changes: 21 additions & 0 deletions packages/_integrationTests/sampleWorkspaces/workspace2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Jason Dent

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Workspace 2

This workspace has a `package.json` with a `cspell` section.

Custom words:

- Workspacetwo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "workspace2",
"version": "1.0.0",
"description": "Sample Workspace 2",
"main": "index.js",
"scripts": {
"test": "echo test"
},
"cspell": {
"version": "0.2",
"words": [
"workspacetwo"
]
},
"author": "",
"license": "MIT"
}
37 changes: 2 additions & 35 deletions packages/_integrationTests/src/ExtensionApi.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,2 @@
import type { CSpellUserSettings } from '@cspell/cspell-types';
import { TextDocument } from 'vscode';

export interface ExtensionApi {
registerConfig(path: string): void;
triggerGetSettings(): void;
enableLanguageId(languageId: string, uri?: string): Thenable<void>;
disableLanguageId(languageId: string, uri?: string): Thenable<void>;
enableCurrentLanguage(): Thenable<void>;
disableCurrentLanguage(): Thenable<void>;
addWordToUserDictionary(word: string): Thenable<void>;
addWordToWorkspaceDictionary(word: string, uri?: string | null): Thenable<void>;
// enableLocale(target: ConfigTarget, locale: string): Thenable<void>;
// disableLocale(target: ConfigTarget, locale: string): Thenable<void>;
updateSettings(): boolean;
cSpellClient(): CSpellClient;
}

export interface CSpellClient {
getConfigurationForDocument(document: TextDocument | undefined): Promise<GetConfigurationForDocumentResult>;
}
export interface GetConfigurationForDocumentResult {
languageEnabled: boolean | undefined;
fileEnabled: boolean | undefined;
settings: CSpellUserSettings | undefined;
docSettings: CSpellUserSettings | undefined;
excludedBy: ExcludeRef[] | undefined;
}

export interface ExcludeRef {
glob: string;
id: string | undefined;
name: string | undefined;
filename: string | undefined;
}
export type { ExtensionApi } from '../../client/dist/extensionApi';
export type { CSpellClient } from '../../client/dist/client';
121 changes: 87 additions & 34 deletions packages/_integrationTests/src/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import { getDocUri, activateExtension, loadDocument, sleep } from './helper';
import { getDocUri, activateExtension, loadDocument, sleep, log, chalk, sampleWorkspaceUri } from './helper';
import { expect } from 'chai';
import { ExtensionApi } from './ExtensionApi';
import * as vscode from 'vscode';
Expand All @@ -15,24 +15,24 @@ type Api = {
const apiSignature: Api = {
addWordToUserDictionary: 'addWordToUserDictionary',
addWordToWorkspaceDictionary: 'addWordToWorkspaceDictionary',
// cSpellClient: 'cSpellClient',
disableCurrentLanguage: 'disableCurrentLanguage',
disableLanguageId: 'disableLanguageId',
// disableLocale: 'disableLocale',
disableLocale: 'disableLocale',
enableCurrentLanguage: 'enableCurrentLanguage',
enableLanguageId: 'enableLanguageId',
// enableLocale: 'enableLocale',
enableLocale: 'enableLocale',
registerConfig: 'registerConfig',
triggerGetSettings: 'triggerGetSettings',
updateSettings: 'updateSettings',
cSpellClient: 'cSpellClient',
};

describe('Launch code spell extension', function () {
this.timeout(60000);
this.timeout(120000);
const docUri = getDocUri('diagnostics.txt');

it('Verify the extension starts', async () => {
log(chalk.yellow('Verify the extension starts'));
const extContext = await activateExtension();
const docContext = await loadDocument(docUri);
expect(extContext).to.not.be.undefined;
Expand All @@ -42,12 +42,44 @@ describe('Launch code spell extension', function () {
expect(extApi).to.equal(extContext?.extActivate);
expect(extApi).haveOwnProperty(apiSignature.addWordToUserDictionary);
expect(extApi).to.include.all.keys(...Object.keys(apiSignature));
log(chalk.yellow('Done: Verify the extension starts'));
});

[
[getDocUri('example.md'), getDocUri('cspell.json')],
[sampleWorkspaceUri('workspace1/README.md'), sampleWorkspaceUri('cspell.json')],
].forEach(([docUri, expectedConfigUri]) => {
it(`Verifies that the right config was found for ${docUri.toString()}`, async () => {
log(chalk.yellow('Verifies that the right config was found'));
const ext = isDefined(await activateExtension());
const uri = docUri;
const folders = vscode.workspace.workspaceFolders;
log(
`Workspace Folders:
%O
`,
folders
);
const docContextMaybe = await loadDocument(uri);
expect(docContextMaybe).to.not.be.undefined;
const docContext = isDefined(docContextMaybe);

const config = await ext.extApi.cSpellClient().getConfigurationForDocument(docContext.doc);

const { excludedBy, fileEnabled, configFiles } = config;
log('config: %o', { excludedBy, fileEnabled, configFiles });

const configUri = vscode.Uri.parse(config.configFiles[0] || '');
expect(configUri.toString()).to.equal(expectedConfigUri.toString());
log(chalk.yellow('Done: Verifies that the right config was found'));
});
});

it('Verifies that some spelling errors were found', async () => {
log(chalk.yellow('Verifies that some spelling errors were found'));
const ext = isDefined(await activateExtension());
const uri = getDocUri('example.md');
const diagsListener = waitForDiag(uri);
const diagsListener = waitForDiag(uri, 60000);
try {
const docContextMaybe = await loadDocument(uri);
expect(docContextMaybe).to.not.be.undefined;
Expand All @@ -56,21 +88,17 @@ describe('Launch code spell extension', function () {
const config = await ext.extApi.cSpellClient().getConfigurationForDocument(docContext.doc);

const { excludedBy, fileEnabled } = config;
console.log(`config: ${JSON.stringify({ excludedBy, fileEnabled })}`);
log('config: %O', { excludedBy, fileEnabled });

const cfg = config.docSettings || config.settings;
const { enabled, dictionaries, languageId } = cfg || {};

console.log(JSON.stringify({ enabled, dictionaries, languageId }));

const diags = await Promise.race([diagsListener.diags, sleep(10000)]);
log('cfg: %O', { enabled, dictionaries, languageId });

await sleep(3000);
const msgs = diags ? diags.map((a) => `C: ${a.source} M: ${a.message}`).join(', ') : 'Timeout';
console.log(`Diag Messages: size(${diags?.length}) msg: ${msgs}`);

console.log('getDiagnostics:');
console.log(JSON.stringify(vscode.languages.getDiagnostics()));
const diags = await diagsListener.diags;
const msgs = diags.map((a) => `C: ${a.source} M: ${a.message}`).join('\n');
log(`Diag Messages: size(${diags.length}) msg: \n${msgs}`);
log('diags: \n%o', diags);

expect(fileEnabled).to.be.true;

Expand All @@ -79,37 +107,62 @@ describe('Launch code spell extension', function () {
} finally {
diagsListener.dispose();
}
log(chalk.yellow('Done: Verifies that some spelling errors were found'));
});

function waitForDiag(uri: vscode.Uri) {
function waitForDiag(uri: vscode.Uri, timeout: number) {
type R = vscode.Diagnostic[];
const diags: R = [];
const source = 'cSpell';
const diags: R = [];
const uriStr = uri.toString();
let resolver: (value: R | PromiseLike<R>) => void;
let dispose: vscode.Disposable | undefined;
dispose = vscode.languages.onDidChangeDiagnostics((event) => {
console.log(JSON.stringify(event));
const matches = event.uris.map((u) => u.toString()).filter((u) => u === uriStr);
if (matches.length) {
console.log(JSON.stringify(vscode.languages.getDiagnostics()));
vscode.languages
.getDiagnostics(uri)
.map((d) => (console.log(JSON.stringify(d)), d))
.filter((diag) => diag.source === source)
.forEach((diag) => diags.push(diag));
resolver?.(diags);
}
});

function fetchDiags() {
return vscode.languages.getDiagnostics(uri).filter((diag) => diag.source === source);
}

function updateDiags() {
diags.splice(0, diags.length, ...fetchDiags());
}

function cleanUp() {
dispose?.dispose();
dispose = undefined;
}
return {
diags: new Promise<R>((resolve) => (resolver = resolve)),

updateDiags();

const diagsP = new Promise<R>((resolve) => {
let resolved = false;
function resolveP() {
if (!resolved) {
resolved = true;
resolve(diags);
}
}

dispose = vscode.languages.onDidChangeDiagnostics((event) => {
log('onDidChangeDiagnostics %o', event);
log('All diags: %o', vscode.languages.getDiagnostics(uri));
const matches = event.uris.map((u) => u.toString()).filter((u) => u === uriStr);
if (matches.length) {
updateDiags();
log('Matching Diags: %o', diags);
resolveP();
}
});

if (diags.length) {
resolveP();
}
});

const waitResult = {
diags: Promise.race([diagsP, sleep(timeout)]).then((r) => r || diags),
dispose: cleanUp,
};

return waitResult;
}
});

Expand Down
Loading

0 comments on commit 032afe3

Please sign in to comment.