forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettingsHandlers.test.ts
More file actions
239 lines (206 loc) · 8.05 KB
/
settingsHandlers.test.ts
File metadata and controls
239 lines (206 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*---------------------------------------------------------------------------------------------
* 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, RemoteClient, RemoteWorkspace } from 'vscode-languageserver';
import { SettingsState } from '../src/yamlSettings';
import { ValidationHandler } from '../src/languageserver/handlers/validationHandlers';
import { LanguageService, LanguageSettings, SchemaConfiguration, SchemaPriority } from '../src';
import * as request from 'request-light';
import { setupLanguageService } from './utils/testHelper';
import { Telemetry } from '../src/languageserver/telemetry';
import { TestWorkspace } from './utils/testsTypes';
const expect = chai.expect;
chai.use(sinonChai);
describe('Settings Handlers Tests', () => {
const sandbox = sinon.createSandbox();
const connection: Connection = {} as Connection;
let workspaceStub: sinon.SinonStubbedInstance<RemoteWorkspace>;
let languageService: sinon.SinonMockStatic;
let settingsState: SettingsState;
let validationHandler: sinon.SinonMock;
let xhrStub: sinon.SinonStub;
beforeEach(() => {
workspaceStub = sandbox.createStubInstance(TestWorkspace);
connection.workspace = (workspaceStub as unknown) as RemoteWorkspace;
connection.onDidChangeConfiguration = sandbox.mock();
connection.client = {} as RemoteClient;
connection.client.register = sandbox.mock();
languageService = sandbox.mock();
settingsState = new SettingsState();
validationHandler = sandbox.mock(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 SettingsHandler(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
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 SettingsHandler(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
settingsHandler.registerHandlers();
expect(connection.client.register).calledOnce;
});
it('SettingsHandler should not modify file match patterns', 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(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
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'],
priority: SchemaPriority.SchemaStore,
name: '.adonisrc.json',
description: 'AdonisJS configuration file',
versions: undefined,
});
});
describe('Test that schema priorities are available', () => {
const testSchemaFileMatch = ['foo/*.yml'];
const testSchemaURI = 'file://foo.json';
function configureSchemaPriorityTest(): LanguageSettings {
const languageServerSetup = setupLanguageService({});
const languageService = languageServerSetup.languageService;
const settingsHandler = new SettingsHandler(
connection,
languageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
const configureSpy = sinon.spy(languageService, 'configure');
settingsHandler.updateConfiguration();
// Check things here
configureSpy.restore();
return configureSpy.args[0][0];
}
it('Schema Settings should have a priority', async () => {
settingsState.schemaConfigurationSettings = [
{
fileMatch: testSchemaFileMatch,
uri: testSchemaURI,
},
];
const configureSpy = configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: testSchemaURI,
fileMatch: testSchemaFileMatch,
schema: undefined,
priority: SchemaPriority.Settings,
});
});
it('Schema Associations should have a priority when schema association is an array', async () => {
settingsState.schemaAssociations = [
{
fileMatch: testSchemaFileMatch,
uri: testSchemaURI,
},
] as SchemaConfiguration[];
const configureSpy = configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: testSchemaURI,
fileMatch: testSchemaFileMatch,
schema: undefined,
priority: SchemaPriority.SchemaAssociation,
});
});
it('Schema Associations should have a priority when schema association is a record', async () => {
settingsState.schemaAssociations = {
[testSchemaURI]: testSchemaFileMatch,
} as Record<string, string[]>;
const configureSpy = configureSchemaPriorityTest();
expect(configureSpy.schemas).deep.include({
uri: testSchemaURI,
fileMatch: testSchemaFileMatch,
priority: SchemaPriority.SchemaAssociation,
});
});
});
describe('Settings fetch', () => {
it('should fetch preferences', async () => {
const settingsHandler = new SettingsHandler(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
workspaceStub.getConfiguration.resolves([{}, {}, {}, {}]);
const setConfigurationStub = sandbox.stub(settingsHandler, 'setConfiguration');
await settingsHandler.pullConfiguration();
expect(workspaceStub.getConfiguration).calledOnceWith([
{ section: 'yaml' },
{ section: 'http' },
{ section: '[yaml]' },
{ section: 'editor' },
]);
expect(setConfigurationStub).calledOnceWith({
yaml: {},
http: {
proxy: '',
proxyStrictSSL: false,
},
yamlEditor: {},
vscodeEditor: {},
});
});
it('detect indentation settings change', async () => {
const settingsHandler = new SettingsHandler(
connection,
(languageService as unknown) as LanguageService,
settingsState,
(validationHandler as unknown) as ValidationHandler,
{} as Telemetry
);
workspaceStub.getConfiguration.resolves([{}, {}, {}, { tabSize: 4, detectIndentation: false }]);
await settingsHandler.pullConfiguration();
expect(workspaceStub.getConfiguration).calledOnceWith([
{ section: 'yaml' },
{ section: 'http' },
{ section: '[yaml]' },
{ section: 'editor' },
]);
});
});
});