@@ -17,39 +17,22 @@ func TestMain(m *testing.M) {
17
17
Main (m , hooks .Options )
18
18
}
19
19
20
- // TestWorkingSimpleModule ensures that we have a successful package load for a
21
- // simple module rooted in the workspace folder with a single CUE file at the
22
- // root .
23
- func TestWorkingSimpleModule (t * testing.T ) {
24
- const files = `
20
+ // TestWorkspaceFoldersRootURI tests that the server initialization
21
+ // works, or fails, as expected, due to various combinations of
22
+ // WorkspaceFolders and the RootURI being set or unset .
23
+ func TestWorkspaceFoldersRootURI (t * testing.T ) {
24
+ const filesOneModule = `
25
25
-- cue.mod/module.cue --
26
- module: "mod.example"
27
- language: {
28
- version: "v0.11.0"
29
- }
30
- -- a.cue --
26
+ module: "mod.example/b"
27
+ language: version: "v0.11.0"
28
+
29
+ -- a/a.cue --
31
30
package a
32
31
-- b/b.cue --
33
32
package c
34
33
`
35
- WithOptions ().Run (t , files , func (t * testing.T , env * Env ) {
36
- // Simulate a change and ensure we get diagnostics back
37
- env .OpenFile ("a.cue" )
38
- env .EditBuffer ("a.cue" , fake .NewEdit (1 , 0 , 1 , 0 , "\n x: 5\n " ))
39
- got := env .BufferText ("a.cue" )
40
- want := "package a\n \n x: 5\n "
41
- qt .Assert (t , qt .Equals (got , want ))
42
- env .Await (env .DoneWithChange ())
43
- })
44
- }
45
-
46
- // TestMultipleWorkspaceFolders verifies the behaviour of starting 'cue lsp'
47
- // with multiple WorkspaceFolders. This is currently not supported, and hence
48
- // the test is a negative test that asserts 'cue lsp' will fail (during the
49
- // Initialize phase).
50
- func TestMultipleWorkspaceFolders (t * testing.T ) {
51
- const files = `
52
34
35
+ const filesTwoModules = `
53
36
-- a/cue.mod/module.cue --
54
37
module: "mod.example/b"
55
38
language: version: "v0.11.0"
@@ -65,10 +48,103 @@ language: version: "v0.11.0"
65
48
package a
66
49
67
50
`
68
- WithOptions (
69
- WorkspaceFolders ("a" , "b" ),
70
- InitializeError ("initialize: got 2 WorkspaceFolders; expected 1" ),
71
- ).Run (t , files , nil )
51
+
52
+ type tc struct {
53
+ name string
54
+ opts []RunOption
55
+ files string
56
+ expectSuccess bool
57
+ }
58
+ tests := []tc {
59
+ {
60
+ // With no workspace folders and no rooturi, the server will
61
+ // return an error during initialization.
62
+ name : "no workspace folders, no rooturi" ,
63
+ opts : []RunOption {
64
+ WorkspaceFolders (),
65
+ InitializeError ("initialize: got 0 WorkspaceFolders; expected 1" ),
66
+ },
67
+ files : filesOneModule ,
68
+ expectSuccess : false ,
69
+ },
70
+ {
71
+ // If no workspace folders are set, but a rooturi is set, the
72
+ // server will treat the rooturi as if it is a workspace
73
+ // folder.
74
+ name : "no workspace folders, rooturi set" ,
75
+ opts : []RunOption {
76
+ WorkspaceFolders (),
77
+ RootURIAsDefaultFolder (),
78
+ },
79
+ files : filesOneModule ,
80
+ expectSuccess : true ,
81
+ },
82
+ {
83
+ // If both workspace folders and rooturi are provided, the
84
+ // rooturi is ignored, and only workspace folders are used.
85
+ name : "workspace folders, rooturi set" ,
86
+ opts : []RunOption {
87
+ WorkspaceFolders ("a" ),
88
+ RootURIAsDefaultFolder (),
89
+ },
90
+ files : filesOneModule ,
91
+ expectSuccess : true ,
92
+ },
93
+ {
94
+ // By default, the test framework will set one workspace
95
+ // folder, and will not set the rooturi.
96
+ name : "default workspace folders, no rooturi" ,
97
+ files : filesOneModule ,
98
+ expectSuccess : true ,
99
+ },
100
+ {
101
+ // cue lsp does not currently support multiple workspace folders.
102
+ name : "multiple folders, one module" ,
103
+ opts : []RunOption {
104
+ WorkspaceFolders ("a" , "b" ),
105
+ InitializeError ("initialize: got 2 WorkspaceFolders; expected 1" ),
106
+ },
107
+ files : filesOneModule ,
108
+ expectSuccess : false ,
109
+ },
110
+ {
111
+ // cue lsp does not currently support multiple workspace
112
+ // folders, even if they correctly refer to different
113
+ // modules.
114
+ name : "multiple folders, two modules" ,
115
+ opts : []RunOption {
116
+ WorkspaceFolders ("a" , "b" ),
117
+ InitializeError ("initialize: got 2 WorkspaceFolders; expected 1" ),
118
+ },
119
+ files : filesTwoModules ,
120
+ expectSuccess : false ,
121
+ },
122
+ }
123
+
124
+ for _ , tc := range tests {
125
+ t .Run (tc .name , func (t * testing.T ) {
126
+ hadSuccess := false
127
+ WithOptions (tc .opts ... ).Run (t , tc .files , func (t * testing.T , env * Env ) {
128
+ hadSuccess = true
129
+ if tc .expectSuccess {
130
+ // We do a trivial edit here, which must succeed, as a
131
+ // means of verifying basic plumbing is working
132
+ // correctly.
133
+ env .OpenFile ("a/a.cue" )
134
+ env .EditBuffer ("a/a.cue" , fake .NewEdit (1 , 0 , 1 , 0 , "\n x: 5\n " ))
135
+ got := env .BufferText ("a/a.cue" )
136
+ want := "package a\n \n x: 5\n "
137
+ qt .Assert (t , qt .Equals (got , want ))
138
+ env .Await (env .DoneWithChange ())
139
+ }
140
+ })
141
+ if tc .expectSuccess && ! hadSuccess {
142
+ t .Fatal ("Initialisation should have succeeded, but it failed" )
143
+ } else if ! tc .expectSuccess && hadSuccess {
144
+ t .Fatal ("Initialisation should have failed, but it succeeded" )
145
+ }
146
+ })
147
+ }
72
148
}
73
149
74
150
// TODO(myitcv): add a test that verifies we get an error in the case that a
0 commit comments