Skip to content

Commit

Permalink
update test for $schema
Browse files Browse the repository at this point in the history
  • Loading branch information
bollwyvl committed May 17, 2024
1 parent 6767a56 commit 940bc69
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/languageservice/services/dollarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { JSONDocument } from '../parser/jsonParser07';
* @param doc
*/
export function getDollarSchema(doc: SingleYAMLDocument | JSONDocument): string | undefined {
if (doc instanceof SingleYAMLDocument && doc.root && doc.root.type === 'object') {
if (doc.root && doc.root.type === 'object') {
let dollarSchema = doc.root.properties['$schema'];
dollarSchema = typeof dollarSchema === 'string' ? dollarSchema.trim() : undefined;
if (typeof dollarSchema === 'string') {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/sample-dollar-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"properties": {
"dollar-schema": {
"type":"string"
}
}
}
34 changes: 33 additions & 1 deletion test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ describe('JSON Schema', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const schemaModelineSample = path.join(__dirname, './fixtures/sample-modeline.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const schemaDollarSample = path.join(__dirname, './fixtures/sample-dollar-schema.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const schemaDefaultSnippetSample = require(path.join(__dirname, './fixtures/defaultSnippets-const-if-else.json'));
const languageSettingsSetup = new ServiceSetup().withCompletion();

Expand All @@ -615,12 +617,42 @@ describe('JSON Schema', () => {
});
languageService.configure(languageSettingsSetup.languageSettings);
languageService.registerCustomSchemaProvider((uri: string) => Promise.resolve(uri));
const testTextDocument = setupTextDocument(`# yaml-language-server: $schema=${schemaModelineSample}\n\n`);
const testTextDocument = setupTextDocument(
`# yaml-language-server: $schema=${schemaModelineSample}\n$schema: ${schemaDollarSample}\n\n`
);
const result = await languageService.doComplete(testTextDocument, Position.create(1, 0), false);
assert.strictEqual(result.items.length, 1);
assert.strictEqual(result.items[0].label, 'modeline');
});

it('Explicit $schema takes precedence over all other lower priority schemas', async () => {
languageSettingsSetup
.withSchemaFileMatch({
fileMatch: ['test.yaml'],
uri: TEST_URI,
priority: SchemaPriority.SchemaStore,
schema: schemaStoreSample,
})
.withSchemaFileMatch({
fileMatch: ['test.yaml'],
uri: TEST_URI,
priority: SchemaPriority.SchemaAssociation,
schema: schemaAssociationSample,
})
.withSchemaFileMatch({
fileMatch: ['test.yaml'],
uri: TEST_URI,
priority: SchemaPriority.Settings,
schema: schemaSettingsSample,
});
languageService.configure(languageSettingsSetup.languageSettings);
languageService.registerCustomSchemaProvider((uri: string) => Promise.resolve(uri));
const testTextDocument = setupTextDocument(`$schema: ${schemaDollarSample}\n\n`);
const result = await languageService.doComplete(testTextDocument, Position.create(1, 0), false);
assert.strictEqual(result.items.length, 1);
assert.strictEqual(result.items[0].label, 'dollar-schema');
});

it('Manually setting schema takes precendence over all other lower priority schemas', async () => {
languageSettingsSetup
.withSchemaFileMatch({
Expand Down

0 comments on commit 940bc69

Please sign in to comment.