yaml-language-server
Version:
644 lines • 33.9 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai = __importStar(require("chai"));
const request = __importStar(require("request-light"));
const sinon = __importStar(require("sinon"));
const sinon_chai_1 = __importDefault(require("sinon-chai"));
const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol");
const vscode_uri_1 = require("vscode-uri");
const src_1 = require("../src");
const settingsHandlers_1 = require("../src/languageserver/handlers/settingsHandlers");
const validationHandlers_1 = require("../src/languageserver/handlers/validationHandlers");
const schemaUrls_1 = require("../src/languageservice/utils/schemaUrls");
const yamlSettings_1 = require("../src/yamlSettings");
const testHelper_1 = require("./utils/testHelper");
const testsTypes_1 = require("./utils/testsTypes");
const expect = chai.expect;
chai.use(sinon_chai_1.default);
describe('Settings Handlers Tests', () => {
const sandbox = sinon.createSandbox();
const connection = {};
let workspaceStub;
let languageService;
let settingsState;
let validationHandler;
let xhrStub;
beforeEach(() => {
workspaceStub = sandbox.createStubInstance(testsTypes_1.TestWorkspace);
connection.workspace = workspaceStub;
connection.onDidChangeConfiguration = sandbox.mock();
connection.client = {};
connection.client.register = sandbox.mock();
const languageServerSetup = (0, testHelper_1.setupLanguageService)({});
languageService = languageServerSetup.languageService;
settingsState = new yamlSettings_1.SettingsState();
validationHandler = sandbox.mock(validationHandlers_1.ValidationHandler);
xhrStub = sandbox.stub(request, 'xhr');
const sendRequest = sandbox.fake();
connection.sendRequest = sendRequest;
});
afterEach(() => {
sandbox.restore();
});
it('should not register configuration notification handler if client not supports dynamic handlers', () => {
settingsState.clientDynamicRegisterSupport = false;
settingsState.hasConfigurationCapability = false;
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
settingsHandler.registerHandlers();
expect(connection.client.register).not.called;
});
it('should register configuration notification handler only if client supports dynamic handlers', () => {
settingsState.clientDynamicRegisterSupport = true;
settingsState.hasConfigurationCapability = true;
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
settingsHandler.registerHandlers();
expect(connection.client.register).calledOnce;
});
it('should request CodeLens refresh after schema settings update if client supports it', async () => {
settingsState.hasCodeLensRefreshSupport = true;
const sendRequest = sandbox.stub().resolves();
connection.sendRequest = sendRequest;
workspaceStub.getConfiguration.onFirstCall().resolves([{ schemaStore: { enable: false } }, {}, {}, {}, {}]);
workspaceStub.getConfiguration
.onSecondCall()
.resolves([
{ schemas: { 'https://example.com/schema.json': 'test.yaml' }, schemaStore: { enable: false } },
{},
{},
{},
{},
]);
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
await settingsHandler.pullConfiguration();
await settingsHandler.pullConfiguration();
expect(sendRequest).calledOnceWithExactly(vscode_languageserver_protocol_1.CodeLensRefreshRequest.type);
});
it('should not request CodeLens refresh when only non-schema settings update', async () => {
settingsState.hasCodeLensRefreshSupport = true;
const sendRequest = sandbox.stub().resolves();
connection.sendRequest = sendRequest;
const yamlSettings = {
schemas: { 'https://example.com/schema.json': 'test.yaml' },
schemaStore: { enable: false },
};
workspaceStub.getConfiguration.onFirstCall().resolves([yamlSettings, {}, {}, {}, {}]);
workspaceStub.getConfiguration.onSecondCall().resolves([{ ...yamlSettings, keyOrdering: true }, {}, {}, {}, {}]);
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
await settingsHandler.pullConfiguration();
await settingsHandler.pullConfiguration();
expect(sendRequest).not.called;
});
describe('Settings for YAML style should ', () => {
it(' reflect to the settings ', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{ style: { flowMapping: 'forbid', flowSequence: 'forbid' } }, {}, {}, {}, {}]);
await settingsHandler.pullConfiguration();
expect(settingsState.style).to.exist;
expect(settingsState.style.flowMapping).to.eqls('forbid');
expect(settingsState.style.flowSequence).to.eqls('forbid');
});
it(' reflect default values if no settings given', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}, {}]);
await settingsHandler.pullConfiguration();
expect(settingsState.style).to.exist;
expect(settingsState.style.flowMapping).to.eqls('allow');
expect(settingsState.style.flowSequence).to.eqls('allow');
});
});
describe('Settings for key ordering should ', () => {
it(' reflect to the settings ', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{ keyOrdering: true }, {}, {}, {}, {}]);
await settingsHandler.pullConfiguration();
expect(settingsState.keyOrdering).to.exist;
expect(settingsState.keyOrdering).to.be.true;
});
it(' reflect default values if no settings given', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}, {}]);
await settingsHandler.pullConfiguration();
expect(settingsState.style).to.exist;
expect(settingsState.keyOrdering).to.be.false;
});
});
describe('Settings for Kubernetes version should ', () => {
it('accepts versions with or without a v prefix', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration
.onFirstCall()
.resolves([{ kubernetesVersion: '1.36.1' }, {}, {}, {}, {}])
.onSecondCall()
.resolves([{ kubernetesVersion: 'v1.37.2' }, {}, {}, {}, {}]);
await settingsHandler.pullConfiguration();
expect(settingsState.kubernetesVersion).to.equal('v1.36.1');
await settingsHandler.pullConfiguration();
expect(settingsState.kubernetesVersion).to.equal('v1.37.2');
});
it('resolves to undefined for invalid or removed values so the default version is used', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration
.onFirstCall()
.resolves([{ kubernetesVersion: '1.36.1' }, {}, {}, {}, {}])
.onSecondCall()
.resolves([{ kubernetesVersion: 'invalid' }, {}, {}, {}, {}])
.onThirdCall()
.resolves([{}, {}, {}, {}, {}]);
await settingsHandler.pullConfiguration();
expect(settingsState.kubernetesVersion).to.equal('v1.36.1');
await settingsHandler.pullConfiguration();
expect(settingsState.kubernetesVersion).to.equal(undefined);
await settingsHandler.pullConfiguration();
expect(settingsState.kubernetesVersion).to.equal(undefined);
});
});
describe('Settings for file associations should ', () => {
it('reflect to settings state', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
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 = (0, testHelper_1.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 settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
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: src_1.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 = (0, testHelper_1.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 settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
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: src_1.SchemaPriority.SchemaStore,
name: 'Butane config schema',
description: 'Schema to validate butane files for Fedora CoreOS',
versions: undefined,
});
});
it('SettingsHandler should include schemas without file matches as selectable schemas', async () => {
const languageServerSetup = (0, testHelper_1.setupLanguageService)({});
const languageService = languageServerSetup.languageService;
xhrStub.resolves({
responseText: `{"schemas": [
{
"name": "Traefik v3",
"description": "Traefik v3 YAML configuration file",
"url": "https://www.schemastore.org/traefik-v3.json"
}]}`,
});
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
const configureSpy = sinon.stub(languageService, 'configure');
await settingsHandler.pullConfiguration();
configureSpy.restore();
expect(settingsState.schemaStoreSettings).deep.include({
uri: 'https://www.schemastore.org/traefik-v3.json',
fileMatch: [],
priority: src_1.SchemaPriority.SchemaStore,
name: 'Traefik v3',
description: 'Traefik v3 YAML configuration file',
versions: undefined,
});
});
});
it('SettingsHandler should not modify file match patterns', async () => {
const languageServerSetup = (0, testHelper_1.setupLanguageService)({});
const languageService = languageServerSetup.languageService;
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 settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
const configureSpy = sinon.stub(languageService, 'configure');
await settingsHandler.pullConfiguration();
configureSpy.restore();
expect(settingsState.schemaStoreSettings).deep.include({
uri: 'https://raw.githubusercontent.com/adonisjs/application/master/adonisrc.schema.json',
fileMatch: ['.adonisrc.yaml'],
priority: src_1.SchemaPriority.SchemaStore,
name: '.adonisrc.json',
description: 'AdonisJS configuration file',
versions: undefined,
});
});
describe('Schema URI normalization', () => {
const testSchemaFileMatch = ['foo/*.yml'];
async function configureSchemaSettingsTest() {
const telemetry = { send: sinon.stub(), sendError: sinon.stub() };
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, telemetry);
const configureSpy = sinon.spy(languageService, 'configure');
await settingsHandler.pullConfiguration();
configureSpy.restore();
return configureSpy.args[0][0];
}
it('Schema Settings should normalize absolute local paths', async () => {
xhrStub.resolves({
responseText: '{"schemas":[]}',
});
const absoluteSchemaPath = '/Users/test/schemas/schema.json';
const schemas = {};
schemas[absoluteSchemaPath] = testSchemaFileMatch;
workspaceStub.getConfiguration.resolves([{ schemas: schemas }, {}, {}, {}]);
const configureSpy = await configureSchemaSettingsTest();
expect(configureSpy.schemas).deep.include({
uri: vscode_uri_1.URI.file(absoluteSchemaPath).toString(),
fileMatch: testSchemaFileMatch,
schema: undefined,
priority: src_1.SchemaPriority.Settings,
});
});
it('Schema Settings should preserve fragments when normalizing absolute local paths', async () => {
xhrStub.resolves({
responseText: '{"schemas":[]}',
});
const absoluteSchemaPath = '/Users/test/schemas/schema.json';
const schemaUri = `${absoluteSchemaPath}#/definitions/foo`;
const schemas = {};
schemas[schemaUri] = testSchemaFileMatch;
workspaceStub.getConfiguration.resolves([{ schemas: schemas }, {}, {}, {}]);
const configureSpy = await configureSchemaSettingsTest();
expect(configureSpy.schemas).deep.include({
uri: `${vscode_uri_1.URI.file(absoluteSchemaPath).toString()}#/definitions/foo`,
fileMatch: testSchemaFileMatch,
schema: undefined,
priority: src_1.SchemaPriority.Settings,
});
});
it('Schema Settings should preserve remote schema URLs', async () => {
xhrStub.resolves({
responseText: '{"schemas":[]}',
});
const schemaUri = 'https://example.com/schemas/schema.json#/definitions/foo';
const schemas = {};
schemas[schemaUri] = testSchemaFileMatch;
workspaceStub.getConfiguration.resolves([{ schemas: schemas }, {}, {}, {}]);
const configureSpy = await configureSchemaSettingsTest();
expect(configureSpy.schemas).deep.include({
uri: schemaUri,
fileMatch: testSchemaFileMatch,
schema: undefined,
priority: src_1.SchemaPriority.Settings,
});
});
it('Schema Settings should treat direct Kubernetes standalone-strict/all.json URLs as Kubernetes associations', async () => {
xhrStub.resolves({
responseText: '{"schemas":[]}',
});
const schemaUri = 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.32.9-standalone-strict/all.json';
const schemas = {};
schemas[schemaUri] = ['*.yaml'];
workspaceStub.getConfiguration.resolves([{ schemas }, {}, {}, {}]);
const configureSpy = await configureSchemaSettingsTest();
expect(configureSpy.schemas).deep.include({
uri: schemaUri,
fileMatch: ['*.yaml'],
schema: undefined,
priority: src_1.SchemaPriority.Settings,
});
expect(settingsState.specificValidatorPaths).deep.include('*.yaml');
});
it('Schema Settings should normalize multiple absolute local paths for the same file', async () => {
xhrStub.resolves({
responseText: '{"schemas":[]}',
});
const schemaPath1 = '/Users/test/schemas/schema1.json';
const schemaPath2 = '/Users/test/schemas/schema2.json';
const fileMatch = ['asdf.yaml'];
const schemas = {};
schemas[schemaPath1] = fileMatch;
schemas[schemaPath2] = fileMatch;
workspaceStub.getConfiguration.resolves([{ schemas }, {}, {}, {}]);
const configureSpy = await configureSchemaSettingsTest();
expect(configureSpy.schemas).deep.include({
uri: vscode_uri_1.URI.file(schemaPath1).toString(),
fileMatch,
schema: undefined,
priority: src_1.SchemaPriority.Settings,
});
expect(configureSpy.schemas).deep.include({
uri: vscode_uri_1.URI.file(schemaPath2).toString(),
fileMatch,
schema: undefined,
priority: src_1.SchemaPriority.Settings,
});
});
});
describe('Test that schema priorities are available', async () => {
const testSchemaFileMatch = ['foo/*.yml'];
const testSchemaURI = 'file://foo.json';
async function configureSchemaPriorityTest() {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
const configureSpy = sinon.spy(languageService, 'configure');
await settingsHandler.pullConfiguration();
configureSpy.restore();
return configureSpy.args[0][0];
}
it('Schema Settings should have a priority', 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 schemas = {};
schemas[testSchemaURI] = testSchemaFileMatch;
workspaceStub.getConfiguration.resolves([{ schemas: schemas }, {}, {}, {}]);
const configureSpy = await configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: testSchemaURI,
fileMatch: testSchemaFileMatch,
schema: undefined,
priority: src_1.SchemaPriority.Settings,
});
});
it('SchemaDetectionDisabled should have a priority', 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 disabledSchemaFileMatch = ['foo/*.yml'];
workspaceStub.getConfiguration.resolves([{ disableSchemaDetection: disabledSchemaFileMatch }, {}, {}, {}, {}]);
const configureSpy = await configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: schemaUrls_1.EMPTY_SCHEMA_URL,
fileMatch: disabledSchemaFileMatch,
schema: true,
priority: src_1.SchemaPriority.SchemaDetectionDisabled,
});
});
it('SchemaDetectionDisabled should accept a single file match string', 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 disabledSchemaFileMatch = 'foo/*.yml';
workspaceStub.getConfiguration.resolves([{ disableSchemaDetection: disabledSchemaFileMatch }, {}, {}, {}, {}]);
const configureSpy = await configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: schemaUrls_1.EMPTY_SCHEMA_URL,
fileMatch: [disabledSchemaFileMatch],
schema: true,
priority: src_1.SchemaPriority.SchemaDetectionDisabled,
});
});
it('Schema Associations should have a priority when schema association is an array', 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"
}]}`,
});
settingsState.schemaAssociations = [
{
fileMatch: testSchemaFileMatch,
uri: testSchemaURI,
},
];
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
const configureSpy = await configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: testSchemaURI,
fileMatch: testSchemaFileMatch,
schema: undefined,
priority: src_1.SchemaPriority.SchemaAssociation,
});
});
it('Schema Associations should have a priority when schema association is a record', async () => {
settingsState.schemaAssociations = {
[testSchemaURI]: testSchemaFileMatch,
};
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
const configureSpy = await configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: testSchemaURI,
fileMatch: testSchemaFileMatch,
priority: src_1.SchemaPriority.SchemaAssociation,
});
});
});
describe('Test disableSchemaDetection validation behavior', () => {
const restrictiveSchema = {
type: 'object',
additionalProperties: false,
properties: {
allowed: {
type: 'string',
},
},
};
it('disableSchemaDetection should suppress yaml.schemas validation', async () => {
const schemaUri = 'file:///schemas/schema-detection-disable-settings.json';
const schemaProvider = testHelper_1.TestCustomSchemaProvider.instance();
schemaProvider.addSchemaWithUri('disableSchemaDetection-settings', schemaUri, restrictiveSchema);
xhrStub.resolves({ responseText: '{"schemas":[]}' });
const schemas = {};
schemas[schemaUri] = 'test.yaml';
workspaceStub.getConfiguration.resolves([{ disableSchemaDetection: ['test.yaml'], schemas }, {}, {}, {}, {}]);
try {
await new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {}).pullConfiguration();
const result = await languageService.doValidation((0, testHelper_1.setupTextDocument)('foo: bar'), false);
expect(result).length(0);
}
finally {
schemaProvider.deleteSchema('disableSchemaDetection-settings');
}
});
it('disableSchemaDetection should suppress SchemaStore validation', async () => {
const schemaUri = 'file:///schemas/github-workflow.json';
const githubWorkflowFileMatch = ['.github/workflows/*.yml'];
const schemaProvider = testHelper_1.TestCustomSchemaProvider.instance();
schemaProvider.addSchemaWithUri('disableSchemaDetection-github-actions', schemaUri, restrictiveSchema);
xhrStub.resolves({
responseText: JSON.stringify({
schemas: [
{
name: 'GitHub Workflow',
description: 'GitHub Actions workflow schema',
fileMatch: githubWorkflowFileMatch,
url: schemaUri,
},
],
}),
});
workspaceStub.getConfiguration.resolves([{ disableSchemaDetection: githubWorkflowFileMatch }, {}, {}, {}, {}]);
try {
await new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {}).pullConfiguration();
const result = await languageService.doValidation((0, testHelper_1.setupSchemaIDTextDocument)('foo: bar', 'file:///workspace/.github/workflows/build.yml'), false);
expect(result).length(0);
}
finally {
schemaProvider.deleteSchema('disableSchemaDetection-github-actions');
}
});
it('disableSchemaDetection should not suppress modeline validation', async () => {
const schemaUri = 'file:///schemas/schema-detection-disable-modeline.json';
const schemaProvider = testHelper_1.TestCustomSchemaProvider.instance();
schemaProvider.addSchemaWithUri('disableSchemaDetection-modeline', schemaUri, restrictiveSchema);
xhrStub.resolves({ responseText: '{"schemas":[]}' });
workspaceStub.getConfiguration.resolves([{ disableSchemaDetection: ['test.yaml'] }, {}, {}, {}, {}]);
try {
await new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {}).pullConfiguration();
const result = await languageService.doValidation((0, testHelper_1.setupTextDocument)(`# yaml-language-server: $schema=${schemaUri}\nfoo: bar`), false);
expect(result).length(1);
expect(result[0].message).to.equal('Property foo is not allowed.');
}
finally {
schemaProvider.deleteSchema('disableSchemaDetection-modeline');
}
});
});
describe('Settings fetch', () => {
it('should fetch preferences', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}, {}]);
await settingsHandler.pullConfiguration();
expect(workspaceStub.getConfiguration).calledOnceWith([
{ section: 'yaml' },
{ section: 'http' },
{ section: '[yaml]' },
{ section: 'editor' },
{ section: 'files' },
]);
});
it('should set schemaStoreSettings to empty when schemaStore is disabled', async () => {
const languageServerSetup = (0, testHelper_1.setupLanguageService)({});
const languageService = languageServerSetup.languageService;
settingsState.schemaStoreEnabled = true;
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{ schemaStore: { enable: false, url: 'http://shouldnot.activate' } }, {}, {}, {}]);
// const configureSpy = sinon.spy(languageService, 'configure');
await settingsHandler.pullConfiguration();
// configureSpy.restore();
expect(settingsState.schemaStoreEnabled).to.be.false;
expect(settingsState.schemaStoreSettings).to.be.empty;
});
it('detect indentation settings change', async () => {
const settingsHandler = new settingsHandlers_1.SettingsHandler(connection, languageService, settingsState, validationHandler, {});
workspaceStub.getConfiguration.resolves([{}, {}, {}, { tabSize: 4, detectIndentation: false }]);
await settingsHandler.pullConfiguration();
expect(workspaceStub.getConfiguration).calledOnceWith([
{ section: 'yaml' },
{ section: 'http' },
{ section: '[yaml]' },
{ section: 'editor' },
{ section: 'files' },
]);
});
});
});
//# sourceMappingURL=settingsHandlers.test.js.map