Skip to content

Commit

Permalink
Fix for #73
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed Jun 7, 2018
1 parent ccad78a commit 30baee0
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Expand Up @@ -8,8 +8,8 @@
"request": "attach",
"port": 6009,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/server/**/*.js" ],
"protocol": "auto",
"outFiles": [ "${workspaceRoot}/out/server/**/*.ts" ],
"protocol": "inspector",
"trace": true
},
{
Expand Down
26 changes: 23 additions & 3 deletions src/languageservice/services/yamlValidation.ts
Expand Up @@ -37,6 +37,9 @@ export class YAMLValidation {
}

return this.jsonSchemaService.getSchemaForResource(textDocument.uri).then(function (schema) {
var diagnostics = [];
var added = {};

if (schema) {

for(let currentYAMLDoc in yamlDocument.documents){
Expand All @@ -47,11 +50,28 @@ export class YAMLValidation {
currentDoc.errors.push({ location: { start: curDiagnostic.location.start, end: curDiagnostic.location.end }, message: curDiagnostic.message })
}
}

}
if(schema && schema.errors.length > 0){


for(let curDiagnostic of schema.errors){
diagnostics.push({
severity: DiagnosticSeverity.Error,
range: {
start: {
line: 0,
character: 0
},
end: {
line: 0,
character: 1
}
},
message: curDiagnostic
});
}

}
var diagnostics = [];
var added = {};
for(let currentYAMLDoc in yamlDocument.documents){
let currentDoc = yamlDocument.documents[currentYAMLDoc];
currentDoc.errors.concat(currentDoc.warnings).forEach(function (error, idx) {
Expand Down
10 changes: 10 additions & 0 deletions test/schema.test.ts
Expand Up @@ -289,4 +289,14 @@ suite('JSON Schema', () => {
testDone(error);
});
});

test('Schema not found', function (testDone) {
let service = new SchemaService.JSONSchemaService(requestServiceMock, workspaceContext);

service.loadSchema('test.json').then((schema) => {
assert.notEqual(schema.errors.length, 0);
}).then(() => testDone(), (error) => {
testDone(error);
});
});
});
68 changes: 68 additions & 0 deletions test/schemaNotFound.ts
@@ -0,0 +1,68 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {
IPCMessageReader, IPCMessageWriter,
createConnection, IConnection, TextDocumentSyncKind,
TextDocuments, TextDocument, Diagnostic, DiagnosticSeverity,
InitializeParams, InitializeResult, TextDocumentPositionParams,
CompletionItem, CompletionItemKind, RequestType
} from 'vscode-languageserver';
import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
import {getLanguageService} from '../src/languageservice/yamlLanguageService'
import Strings = require( '../src/languageservice/utils/strings');
import URI from '../src/languageservice/utils/uri';
import * as URL from 'url';
import fs = require('fs');
import {JSONSchemaService} from '../src/languageservice/services/jsonSchemaService'
import {schemaRequestService, workspaceContext} from './testHelper';
import { parse as parseYAML } from '../src/languageservice/parser/yamlParser';
import { YAMLDocument } from 'vscode-yaml-languageservice';
var assert = require('assert');

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

let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext);

let uri = 'SchemaDoesNotExist';
let languageSettings = {
schemas: [],
validate: true,
customTags: []
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
languageService.configure(languageSettings);

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

// Tests for validator
describe('Validation', function() {

function setup(content: string){
return TextDocument.create("file://~/Desktop/vscode-k8s/test.yaml", "yaml", 0, content);
}

function parseSetup(content: string){
let testTextDocument = setup(content);
let yDoc = parseYAML(testTextDocument.getText(), languageSettings.customTags);
return languageService.doValidation(testTextDocument, yDoc);
}

//Validating basic nodes
describe('Test that validation throws error when schema is not found', function(){

it('Basic test', (done) => {
let content = `testing: true`;
let validator = parseSetup(content);
validator.then(function(result){
assert.NotEqual(result.length, 0);
}).then(done, done);
});

});

});
});

0 comments on commit 30baee0

Please sign in to comment.