From 501d75f15ebb036f79f9ecb018f6bb268c61f7ec Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Tue, 2 Mar 2021 17:44:31 +0200 Subject: [PATCH] Fix 'fileMatch' pattern to match whole file name (#417) * #354 Fix 'fileMatch' pattern to match whole file name Signed-off-by: Yevhen Vydolob * add test Signed-off-by: Yevhen Vydolob --- .../handlers/settingsHandlers.ts | 22 +++--- test/settingsHandlers.test.ts | 67 +++++++++++++++++++ test/yamlValidation.test.ts | 5 +- 3 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 test/settingsHandlers.test.ts diff --git a/src/languageserver/handlers/settingsHandlers.ts b/src/languageserver/handlers/settingsHandlers.ts index 0e1e72c8..bc386adf 100644 --- a/src/languageserver/handlers/settingsHandlers.ts +++ b/src/languageserver/handlers/settingsHandlers.ts @@ -116,18 +116,17 @@ export class SettingsHandler { * AND the schema store setting is enabled. If the schema store setting * is not enabled we need to clear the schemas. */ - public setSchemaStoreSettingsIfNotSet(): void { + public async setSchemaStoreSettingsIfNotSet(): Promise { const schemaStoreIsSet = this.yamlSettings.schemaStoreSettings.length !== 0; if (this.yamlSettings.schemaStoreEnabled && !schemaStoreIsSet) { - this.getSchemaStoreMatchingSchemas() - .then((schemaStore) => { - this.yamlSettings.schemaStoreSettings = schemaStore.schemas; - this.updateConfiguration(); - }) - .catch(() => { - // ignore - }); + try { + const schemaStore = await this.getSchemaStoreMatchingSchemas(); + this.yamlSettings.schemaStoreSettings = schemaStore.schemas; + this.updateConfiguration(); + } catch (err) { + // ignore + } } else if (!this.yamlSettings.schemaStoreEnabled) { this.yamlSettings.schemaStoreSettings = []; this.updateConfiguration(); @@ -152,12 +151,13 @@ export class SettingsHandler { if (schema && schema.fileMatch) { for (const fileMatch in schema.fileMatch) { - const currFileMatch = schema.fileMatch[fileMatch]; + const currFileMatch: string = schema.fileMatch[fileMatch]; // If the schema is for files with a YAML extension, save the schema association if (currFileMatch.indexOf('.yml') !== -1 || currFileMatch.indexOf('.yaml') !== -1) { languageSettings.schemas.push({ uri: schema.url, - fileMatch: [currFileMatch], + // this is workaround to fix file matcher, adding '/' force to match full file name instead of just file name ends + fileMatch: [currFileMatch.indexOf('/') === -1 ? '/' + currFileMatch : currFileMatch], }); } } diff --git a/test/settingsHandlers.test.ts b/test/settingsHandlers.test.ts new file mode 100644 index 00000000..aee9b095 --- /dev/null +++ b/test/settingsHandlers.test.ts @@ -0,0 +1,67 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Red Hat, Inc. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { SettingsHandler } from '../src/languageserver/handlers/settingsHandlers'; +import * as sinon from 'sinon'; +import * as chai from 'chai'; +import * as sinonChai from 'sinon-chai'; +import { Connection } from 'vscode-languageserver'; +import { SettingsState } from '../src/yamlSettings'; +import { ValidationHandler } from '../src/languageserver/handlers/validationHandlers'; +import { LanguageService } from '../src'; +import * as request from 'request-light'; + +const expect = chai.expect; +chai.use(sinonChai); + +describe('Settings Handlers Tests', () => { + const sandbox = sinon.createSandbox(); + let connectionStub: sinon.SinonMockStatic; + let languageService: sinon.SinonMockStatic; + let settingsState: SettingsState; + let validationHandler: sinon.SinonMock; + let xhrStub: sinon.SinonStub; + + beforeEach(() => { + connectionStub = sandbox.mock(); + languageService = sandbox.mock(); + settingsState = new SettingsState(); + validationHandler = sandbox.mock(ValidationHandler); + xhrStub = sandbox.stub(request, 'xhr'); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it('SettingsHandler should modify file match patters', async () => { + xhrStub.resolves({ + responseText: `{"schemas": [ + { + "name": ".adonisrc.json", + "description": "AdonisJS configuration file", + "fileMatch": [ + ".adonisrc.yaml" + ], + "url": "https://raw.githubusercontent.com/adonisjs/application/master/adonisrc.schema.json" + }]}`, + }); + const settingsHandler = new SettingsHandler( + (connectionStub as unknown) as Connection, + (languageService as unknown) as LanguageService, + settingsState, + (validationHandler as unknown) as ValidationHandler + ); + + sandbox.stub(settingsHandler, 'updateConfiguration').returns(); + + await settingsHandler.setSchemaStoreSettingsIfNotSet(); + + expect(settingsState.schemaStoreSettings).deep.include({ + uri: 'https://raw.githubusercontent.com/adonisjs/application/master/adonisrc.schema.json', + fileMatch: ['/.adonisrc.yaml'], + }); + }); +}); diff --git a/test/yamlValidation.test.ts b/test/yamlValidation.test.ts index 644abe9b..6b3805ed 100644 --- a/test/yamlValidation.test.ts +++ b/test/yamlValidation.test.ts @@ -4,7 +4,6 @@ *--------------------------------------------------------------------------------------------*/ import { Diagnostic } from 'vscode-languageserver-types'; import { ValidationHandler } from '../src/languageserver/handlers/validationHandlers'; -import { LanguageService } from '../src/languageservice/yamlLanguageService'; import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings'; import { ServiceSetup } from './utils/serviceSetup'; import { setupLanguageService, setupSchemaIDTextDocument } from './utils/testHelper'; @@ -13,15 +12,13 @@ import { createExpectedError } from './utils/verifyError'; describe('YAML Validation Tests', () => { let languageSettingsSetup: ServiceSetup; - let languageService: LanguageService; let validationHandler: ValidationHandler; let yamlSettings: SettingsState; before(() => { languageSettingsSetup = new ServiceSetup().withValidate(); - const { languageService: langService, validationHandler: valHandler, yamlSettings: settings } = setupLanguageService( + const { validationHandler: valHandler, yamlSettings: settings } = setupLanguageService( languageSettingsSetup.languageSettings ); - languageService = langService; validationHandler = valHandler; yamlSettings = settings; });