Skip to content

Commit

Permalink
Fixed up schema sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed Nov 2, 2019
1 parent 9a1c4fe commit 49c61d8
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 102 deletions.
2 changes: 2 additions & 0 deletions src/languageservice/jsonSchema04.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export interface JSONSchema {
patternErrorMessage?: string; // VSCode extension
deprecationMessage?: string; // VSCode extension
enumDescriptions?: string[]; // VSCode extension

schemaSequence?: JSONSchema[]; // extension for multiple schemas related to multiple documents in single yaml file
}

export interface JSONSchemaMap {
Expand Down
2 changes: 2 additions & 0 deletions src/languageservice/jsonSchema07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export interface JSONSchema {
markdownDescription?: string; // VSCode extension
doNotSuggest?: boolean; // VSCode extension
allowComments?: boolean; // VSCode extension

schemaSequence?: JSONSchema[]; // extension for multiple schemas related to multiple documents in single yaml file
}

export interface JSONSchemaMap {
Expand Down
1 change: 1 addition & 0 deletions src/languageservice/parser/yamlParser07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class SingleYAMLDocument extends JSONDocument {
public errors;
public warnings;
public isKubernetes: boolean;
public currentDocIndex: number;

constructor(lines: number[]) {
super(null, []);
Expand Down
1 change: 1 addition & 0 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class YAMLCompletion {
this.getCustomTagValueCompletions(collector);
}

currentDoc.currentDocIndex = currentDocIndex;
return this.schemaService.getSchemaForResource(document.uri, currentDoc).then(schema => {

if (!schema) {
Expand Down
2 changes: 2 additions & 0 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class YAMLHover {
return this.promise.resolve(void 0);
}

const currentDocIndex = doc.documents.indexOf(currentDoc);
currentDoc.currentDocIndex = currentDocIndex;
return this.jsonHover.doHover(document, position, currentDoc);
}
}
7 changes: 6 additions & 1 deletion src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export class YAMLSchemaService extends JSONSchemaService {
}

if (schemas.length > 0) {
return super.createCombinedSchema(resource, schemas).getResolvedSchema();
return super.createCombinedSchema(resource, schemas).getResolvedSchema().then(schema => {
if (schema.schema && schema.schema.schemaSequence && schema.schema.schemaSequence[doc.currentDocIndex]) {
return new ResolvedSchema(schema.schema.schemaSequence[doc.currentDocIndex]);
}
return schema;
});
}

return Promise.resolve(null);
Expand Down
3 changes: 3 additions & 0 deletions src/languageservice/services/yamlValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export class YAMLValidation {
}
const yamlDocument: YAMLDocument = parseYAML(textDocument.getText(), this.customTags);
const validationResult: Diagnostic[] = [];
let index = 0;
for (const currentYAMLDoc of yamlDocument.documents) {
currentYAMLDoc.isKubernetes = isKubernetes;
currentYAMLDoc.currentDocIndex = index;

const validation = await this.jsonValidation.doValidation(textDocument, currentYAMLDoc);
const syd = currentYAMLDoc as unknown as SingleYAMLDocument;
Expand All @@ -51,6 +53,7 @@ export class YAMLValidation {
}

validationResult.push(...validation);
index++;
}

const foundSignatures = new Set();
Expand Down
17 changes: 15 additions & 2 deletions test/fixtures/customMultipleSchemaSequences.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@
"description": "The age of this person"
}
},
"required":["name","age"]
"required": ["name","age"]
},
{
"$ref": "http://json.schemastore.org/bowerrc"
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Location object",
"type": "object",
"properties": {
"longitude": {
"type": "number",
"description": "The longitude of the object"
},
"latitude": {
"type": "number",
"description": "The latitude of the object"
}
},
"required": ["longitude","latitude"]
}
]

Expand Down
176 changes: 77 additions & 99 deletions test/mulipleDocuments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,99 @@
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { getLanguageService, LanguageSettings } from '../src/languageservice/yamlLanguageService';
import path = require('path');
import { schemaRequestService, workspaceContext, createJSONLanguageService } from './utils/testHelper';
import { setupTextDocument, configureLanguageService, toFsPath } from './utils/testHelper';
import assert = require('assert');
import { ServiceSetup } from './utils/serviceSetup';

const languageService = getLanguageService(schemaRequestService, workspaceContext, [], null);

function toFsPath(str): string {
if (typeof str !== 'string') {
throw new TypeError(`Expected a string, got ${typeof str}`);
}

let pathName;
pathName = path.resolve(str);
pathName = pathName.replace(/\\/g, '/');
// Windows drive letter must be prefixed with a slash
if (pathName[0] !== '/') {
pathName = `/${pathName}`;
}
return encodeURI(`file://${pathName}`).replace(/[?#]/g, encodeURIComponent);
}

/**
* Setup the schema we are going to use with the language settings
*/
const uri = toFsPath(path.join(__dirname, './fixtures/customMultipleSchemaSequences.json'));
const languageSettings: LanguageSettings = {
schemas: [],
validate: true,
customTags: [],
hover: true
};
const fileMatch = ['*.yml', '*.yaml'];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
languageSettings.customTags.push('!Test');
languageSettings.customTags.push('!Ref sequence');
languageService.configure(languageSettings);
const languageSettingsSetup = new ServiceSetup()
.withHover()
.withValidate()
.withSchemaFileMatch({ uri, fileMatch: fileMatch })
.withCustomTags(['!Test', '!Ref sequence']);

// Defines a Mocha test suite to group tests of similar kind together
suite('Multiple Documents Validation Tests', () => {

// Tests for validator
describe('Multiple Documents Validation', function () {
// function setup(content: string){
// return TextDocument.create("file://~/Desktop/vscode-k8s/test.yaml", "yaml", 0, content);
// }

// function validatorSetup(content: string){
// const testTextDocument = setup(content);
// const yDoc = parseYAML(testTextDocument.getText(), languageSettings.customTags);
// return languageService.doValidation(testTextDocument, yDoc);
// }
function validatorSetup(content: string) {
const testTextDocument = setupTextDocument(content);
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);
return languageService.doValidation(testTextDocument, false);
}

// function hoverSetup(content: string, position){
// let testTextDocument = setup(content);
// let jsonDocument = parseYAML2(testTextDocument.getText());
// const jsonLanguageService = createJSONLanguageService();
// jsonLanguageService.configure({
// schemas: [{
// fileMatch,
// uri
// }]
// });
// return languageService.doHover(jsonLanguageService, testTextDocument, testTextDocument.positionAt(position), jsonDocument);
// }
function hoverSetup(content: string, position) {
const testTextDocument = setupTextDocument(content);
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);
return languageService.doHover(
testTextDocument,
testTextDocument.positionAt(position)
);
}

// it('Should validate multiple documents', (done) => {
// const content = `
// name: jack
// age: 22
// ---
// analytics: true
// `;
// const validator = validatorSetup(content);
// validator.then((result) => {
// assert.equal(result.length, 0);
// }).then(done, done);
// });
it('Should validate multiple documents', done => {
const content = `
name: jack
age: 22
---
longitude: 0
latitude: 0
`;
const validator = validatorSetup(content);
validator.then(result => {
assert.equal(result.length, 0);
}).then(done, done);
});

// it('Should find errors in both documents', (done) => {
// let content = `name1: jack
// age: asd
// ---
// cwd: False`;
// let validator = validatorSetup(content);
// validator.then(function(result){
// assert.equal(result.length, 3);
// }).then(done, done);
// });
it('Should find errors in both documents', done => {
const content = `name1: jack
age: asd
---
latitude: 2`;
const validator = validatorSetup(content);
validator.then(function (result) {
assert.equal(result.length, 3);
}).then(done, done);
});

// it('Should find errors in first document', (done) => {
// let content = `name: jack
// age: age
// ---
// analytics: true`;
// let validator = validatorSetup(content);
// validator.then(function(result){
// assert.equal(result.length, 1);
// }).then(done, done);
// });
it('Should find errors in first document', done => {
const content = `name: jack
age: age
---
longitude: 0
latitude: 0`;
const validator = validatorSetup(content);
validator.then(function (result) {
assert.equal(result.length, 1);
}).then(done, done);
});

// it('Should find errors in second document', (done) => {
// let content = `name: jack
// age: 22
// ---
// cwd: False`;
// let validator = validatorSetup(content);
// validator.then(function(result){
// assert.equal(result.length, 1);
// }).then(done, done);
// });
it('Should find errors in second document', done => {
const content = `name: jack
age: 22
---
longitude: 0
`;
const validator = validatorSetup(content);
validator.then(function (result) {
assert.equal(result.length, 1);
}).then(done, done);
});

// it('Should hover in first document', (done) => {
// let content = `name: jack\nage: 22\n---\ncwd: False`;
// let hover = hoverSetup(content, 1 + content.indexOf('age'));
// hover.then(function(result){
// assert.notEqual(result.contents.length, 0);
// assert.equal(result.contents[0], 'The age of this person');
// }).then(done, done);
// });
it('Should hover in first document', done => {
const content = 'name: jack\nage: 22\n---\ncwd: False';
const hover = hoverSetup(content, 1 + content.indexOf('age'));
hover.then(function (result) {
assert.notEqual((result.contents as []).length, 0);
assert.equal(result.contents[0], 'The age of this person');
}).then(done, done);
});
});
});

0 comments on commit 49c61d8

Please sign in to comment.