Skip to content

Commit

Permalink
Schemas through files.association settings are enabled (redhat-develo…
Browse files Browse the repository at this point in the history
  • Loading branch information
gorkem authored May 18, 2022
1 parent f4365bd commit 417a68e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class SettingsHandler {
{ section: 'http' },
{ section: '[yaml]' },
{ section: 'editor' },
{ section: 'files' },
]);
const settings: Settings = {
yaml: config[0],
Expand All @@ -52,6 +53,7 @@ export class SettingsHandler {
},
yamlEditor: config[2],
vscodeEditor: config[3],
files: config[4],
};
await this.setConfiguration(settings);
}
Expand Down Expand Up @@ -83,6 +85,13 @@ export class SettingsHandler {
this.yamlSettings.schemaStoreUrl = settings.yaml.schemaStore.url;
}
}
if (settings.files?.associations) {
for (const [ext, languageId] of Object.entries(settings.files.associations)) {
if (languageId === 'yaml') {
this.yamlSettings.fileExtensions.push(ext);
}
}
}
this.yamlSettings.yamlVersion = settings.yaml.yamlVersion ?? '1.2';

if (settings.yaml.format) {
Expand Down Expand Up @@ -205,7 +214,11 @@ export class SettingsHandler {
for (const fileMatch in schema.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) {
if (
this.yamlSettings.fileExtensions.findIndex((value) => {
return currFileMatch.indexOf(value) > -1;
}) > -1
) {
languageSettings.schemas.push({
uri: schema.url,
fileMatch: [currFileMatch],
Expand Down
4 changes: 4 additions & 0 deletions src/yamlSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export interface Settings {
vscodeEditor: {
detectIndentation: boolean;
};
files: {
associations: Map<string, string>;
};
}

export interface JSONSchemaSettings {
Expand Down Expand Up @@ -97,6 +100,7 @@ export class SettingsState {
yamlVersion: YamlVersion = '1.2';
useSchemaSelectionRequests = false;
hasWsChangeWatchedFileDynamicRegistration = false;
fileExtensions: string[] = ['.yml', '.yaml'];
}

export class TextDocumentTestManager extends TextDocuments<TextDocument> {
Expand Down
91 changes: 90 additions & 1 deletion test/settingsHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,93 @@ describe('Settings Handlers Tests', () => {
expect(connection.client.register).calledOnce;
});

describe('Settings for file associations should ', () => {
it('reflect to settings state', async () => {
const settingsHandler = new SettingsHandler(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}, { associations: { '*.bu': 'yaml' } }]);

await settingsHandler.pullConfiguration();
expect(settingsState.fileExtensions).to.include('*.bu');
expect(settingsState.fileExtensions).to.include('.yml');
expect(settingsState.fileExtensions).to.include('.yaml');
});
it('SettingsHandler should match patterns from file associations', async () => {
const languageServerSetup = setupLanguageService({});
const languageService = languageServerSetup.languageService;
xhrStub.resolves({
responseText: `{"schemas": [
{
"name": "Butane config schema",
"description": "Schema to validate butane files for Fedora CoreOS",
"fileMatch": [
"*.bu"
],
"url": "https://raw.githubusercontent.com/Relativ-IT/Butane-Schemas/Release/Butane-Schema.json"
}]}`,
});
settingsState.fileExtensions.push('*.bu');
const settingsHandler = new SettingsHandler(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
const configureSpy = sinon.stub(languageService, 'configure');
await settingsHandler.pullConfiguration();
configureSpy.restore();
expect(settingsState.schemaStoreSettings).deep.include({
uri: 'https://raw.githubusercontent.com/Relativ-IT/Butane-Schemas/Release/Butane-Schema.json',
fileMatch: ['*.bu'],
priority: SchemaPriority.SchemaStore,
name: 'Butane config schema',
description: 'Schema to validate butane files for Fedora CoreOS',
versions: undefined,
});
});
it('SettingsHandler should not match non-yaml files if there is no file assosication', async () => {
const languageServerSetup = setupLanguageService({});
const languageService = languageServerSetup.languageService;
xhrStub.resolves({
responseText: `{"schemas": [
{
"name": "Butane config schema",
"description": "Schema to validate butane files for Fedora CoreOS",
"fileMatch": [
"*.bu"
],
"url": "https://raw.githubusercontent.com/Relativ-IT/Butane-Schemas/Release/Butane-Schema.json"
}]}`,
});
const settingsHandler = new SettingsHandler(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
const configureSpy = sinon.stub(languageService, 'configure');
await settingsHandler.pullConfiguration();
configureSpy.restore();
expect(settingsState.schemaStoreSettings).not.deep.include({
uri: 'https://raw.githubusercontent.com/Relativ-IT/Butane-Schemas/Release/Butane-Schema.json',
fileMatch: ['*.bu'],
priority: SchemaPriority.SchemaStore,
name: 'Butane config schema',
description: 'Schema to validate butane files for Fedora CoreOS',
versions: undefined,
});
});
});

it('SettingsHandler should not modify file match patterns', async () => {
const languageServerSetup = setupLanguageService({});

Expand Down Expand Up @@ -211,7 +298,7 @@ describe('Settings Handlers Tests', () => {
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}, {}]);

await settingsHandler.pullConfiguration();

Expand All @@ -220,6 +307,7 @@ describe('Settings Handlers Tests', () => {
{ section: 'http' },
{ section: '[yaml]' },
{ section: 'editor' },
{ section: 'files' },
]);
});
it('should set schemaStoreSettings to empty when schemaStore is disabled', async () => {
Expand Down Expand Up @@ -259,6 +347,7 @@ describe('Settings Handlers Tests', () => {
{ section: 'http' },
{ section: '[yaml]' },
{ section: 'editor' },
{ section: 'files' },
]);
});
});
Expand Down

0 comments on commit 417a68e

Please sign in to comment.