Skip to content

Commit

Permalink
Fix 'fileMatch' pattern to match whole file name (redhat-developer#417)
Browse files Browse the repository at this point in the history
* redhat-developer#354 Fix 'fileMatch' pattern to match whole file name

Signed-off-by: Yevhen Vydolob <[email protected]>

* add test

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob authored Mar 2, 2021
1 parent ed266ae commit 501d75f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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();
Expand All @@ -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],
});
}
}
Expand Down
67 changes: 67 additions & 0 deletions test/settingsHandlers.test.ts
Original file line number Diff line number Diff line change
@@ -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'],
});
});
});
5 changes: 1 addition & 4 deletions test/yamlValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
});
Expand Down

0 comments on commit 501d75f

Please sign in to comment.