1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Red Hat. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { TextDocument } from 'vscode-languageserver' ;
6
+ import { getLanguageService } from '../src/languageservice/yamlLanguageService'
7
+ import path = require( 'path' ) ;
8
+ import { schemaRequestService , workspaceContext } from './testHelper' ;
9
+ import { parse as parseYAML } from '../src/languageservice/parser/yamlParser' ;
10
+ var assert = require ( 'assert' ) ;
11
+
12
+ let languageService = getLanguageService ( schemaRequestService , workspaceContext , [ ] , null ) ;
13
+
14
+ function toFsPath ( str ) : string {
15
+ if ( typeof str !== 'string' ) {
16
+ throw new TypeError ( `Expected a string, got ${ typeof str } ` ) ;
17
+ }
18
+
19
+ let pathName ;
20
+ pathName = path . resolve ( str ) ;
21
+ pathName = pathName . replace ( / \\ / g, '/' ) ;
22
+ // Windows drive letter must be prefixed with a slash
23
+ if ( pathName [ 0 ] !== '/' ) {
24
+ pathName = `/${ pathName } ` ;
25
+ }
26
+ return encodeURI ( `file://${ pathName } ` ) . replace ( / [ ? # ] / g, encodeURIComponent ) ;
27
+ }
28
+
29
+ let uri = toFsPath ( path . join ( __dirname , './fixtures/customMultipleSchemaSequences.json' ) ) ;
30
+ let languageSettings = {
31
+ schemas : [ ] ,
32
+ validate : true ,
33
+ customTags : [ ]
34
+ } ;
35
+ let fileMatch = [ "*.yml" , "*.yaml" ] ;
36
+ languageSettings . schemas . push ( { uri, fileMatch : fileMatch } ) ;
37
+ languageSettings . customTags . push ( "!Test" ) ;
38
+ languageSettings . customTags . push ( "!Ref sequence" ) ;
39
+ languageService . configure ( languageSettings ) ;
40
+ // Defines a Mocha test suite to group tests of similar kind together
41
+ suite ( "Multiple Documents Validation Tests" , ( ) => {
42
+
43
+ // Tests for validator
44
+ describe ( 'Multiple Documents Validation' , function ( ) {
45
+ function setup ( content : string ) {
46
+ return TextDocument . create ( "file://~/Desktop/vscode-k8s/test.yaml" , "yaml" , 0 , content ) ;
47
+ }
48
+
49
+ function validatorSetup ( content : string ) {
50
+ const testTextDocument = setup ( content ) ;
51
+ const yDoc = parseYAML ( testTextDocument . getText ( ) , languageSettings . customTags ) ;
52
+ return languageService . doValidation ( testTextDocument , yDoc ) ;
53
+ }
54
+
55
+ function hoverSetup ( content : string , position ) {
56
+ let testTextDocument = setup ( content ) ;
57
+ let jsonDocument = parseYAML ( testTextDocument . getText ( ) ) ;
58
+ return languageService . doHover ( testTextDocument , testTextDocument . positionAt ( position ) , jsonDocument ) ;
59
+ }
60
+
61
+ it ( 'Should validate multiple documents' , ( done ) => {
62
+ const content = `
63
+ name: jack
64
+ age: 22
65
+ ---
66
+ analytics: true
67
+ ` ;
68
+ const validator = validatorSetup ( content ) ;
69
+ validator . then ( ( result ) => {
70
+ assert . equal ( result . length , 0 ) ;
71
+ } ) . then ( done , done ) ;
72
+ } ) ;
73
+
74
+ it ( 'Should find errors in both documents' , ( done ) => {
75
+ let content = `name1: jack
76
+ age: asd
77
+ ---
78
+ cwd: False` ;
79
+ let validator = validatorSetup ( content ) ;
80
+ validator . then ( function ( result ) {
81
+ assert . equal ( result . length , 3 ) ;
82
+ } ) . then ( done , done ) ;
83
+ } ) ;
84
+
85
+ it ( 'Should find errors in first document' , ( done ) => {
86
+ let content = `name: jack
87
+ age: age
88
+ ---
89
+ analytics: true` ;
90
+ let validator = validatorSetup ( content ) ;
91
+ validator . then ( function ( result ) {
92
+ assert . equal ( result . length , 1 ) ;
93
+ } ) . then ( done , done ) ;
94
+ } ) ;
95
+
96
+ it ( 'Should find errors in second document' , ( done ) => {
97
+ let content = `name: jack
98
+ age: 22
99
+ ---
100
+ cwd: False` ;
101
+ let validator = validatorSetup ( content ) ;
102
+ validator . then ( function ( result ) {
103
+ assert . equal ( result . length , 1 ) ;
104
+ } ) . then ( done , done ) ;
105
+ } ) ;
106
+
107
+ it ( 'Should hover in first document' , ( done ) => {
108
+ let content = `name: jack\nage: 22\n---\ncwd: False` ;
109
+ let hover = hoverSetup ( content , 1 + content . indexOf ( 'age' ) ) ;
110
+ hover . then ( function ( result ) {
111
+ assert . notEqual ( result . contents . length , 0 ) ;
112
+ assert . equal ( result . contents [ 0 ] , 'The age of this person' ) ;
113
+ } ) . then ( done , done ) ;
114
+ } ) ;
115
+ } ) ;
116
+ } ) ;
0 commit comments