55
55
throw $msg
56
56
}
57
57
58
+ function New-TestFile
59
+ {
60
+ param (
61
+ [Parameter (Mandatory )]
62
+ [string ]
63
+ $Script
64
+ )
65
+
66
+ $file = Set-Content - Path (Join-Path $TestDrive " $ ( [System.IO.Path ]::GetRandomFileName()) .ps1" ) - Value $Script - PassThru - Force
67
+
68
+ $request = Send-LspDidOpenTextDocumentRequest - Client $client `
69
+ - Uri ([Uri ]::new($file.PSPath ).AbsoluteUri) `
70
+ - Text ($file [0 ].ToString())
71
+
72
+ # To give PSScriptAnalyzer a chance to run.
73
+ Start-Sleep 1
74
+
75
+ # There's no response for this message, but we need to call Get-LspResponse
76
+ # to increment the counter.
77
+ Get-LspResponse - Client $client - Id $request.Id | Out-Null
78
+
79
+ # Throw out any notifications from the first PSScriptAnalyzer run.
80
+ Get-LspNotification - Client $client | Out-Null
81
+
82
+ $file.PSPath
83
+ }
84
+
58
85
Describe " Loading and running PowerShellEditorServices" {
59
86
BeforeAll {
60
87
Import-Module - Force " $PSScriptRoot /../../module/PowerShellEditorServices"
@@ -89,21 +116,12 @@ Describe "Loading and running PowerShellEditorServices" {
89
116
}
90
117
91
118
It " Can handle WorkspaceSymbol request" {
92
- $script = "
119
+ New-TestFile - Script "
93
120
function Get-Foo {
94
121
Write-Host 'hello'
95
122
}
96
123
"
97
124
98
- $file = Set-Content - Path (Join-Path $TestDrive " $ ( [System.IO.Path ]::GetRandomFileName()) .ps1" ) - Value $script - PassThru - Force
99
- $request = Send-LspDidOpenTextDocumentRequest - Client $client `
100
- - Uri ([Uri ]::new($file.PSPath ).AbsoluteUri) `
101
- - Text ($file [0 ].ToString())
102
-
103
- # There's no response for this message, but we need to call Get-LspResponse
104
- # to increment the counter.
105
- Get-LspResponse - Client $client - Id $request.Id | Out-Null
106
-
107
125
$request = Send-LspRequest - Client $client - Method " workspace/symbol" - Parameters @ {
108
126
query = " "
109
127
}
@@ -141,19 +159,7 @@ function Get-Foo {
141
159
}
142
160
143
161
It " Can get Diagnostics after changing settings" {
144
- $script = ' gci | % { $_ }'
145
- $file = Set-Content - Path (Join-Path $TestDrive " $ ( [System.IO.Path ]::GetRandomFileName()) .ps1" ) - Value $script - PassThru - Force
146
-
147
- $request = Send-LspDidOpenTextDocumentRequest - Client $client `
148
- - Uri ([Uri ]::new($file.PSPath ).AbsoluteUri) `
149
- - Text ($file [0 ].ToString())
150
-
151
- # There's no response for this message, but we need to call Get-LspResponse
152
- # to increment the counter.
153
- Get-LspResponse - Client $client - Id $request.Id | Out-Null
154
-
155
- # Throw out any notifications from the first PSScriptAnalyzer run.
156
- Get-LspNotification - Client $client | Out-Null
162
+ $file = New-TestFile - Script ' gci | % { $_ }'
157
163
158
164
$request = Send-LspDidChangeConfigurationRequest - Client $client - Settings @ {
159
165
PowerShell = @ {
@@ -172,32 +178,17 @@ function Get-Foo {
172
178
}
173
179
174
180
It " Can handle folding request" {
175
- $script = ' gci | % {
181
+ $filePath = New-TestFile - Script ' gci | % {
176
182
$_
177
183
178
184
@"
179
185
$_
180
186
"@
181
187
}'
182
188
183
- $file = Set-Content - Path (Join-Path $TestDrive " $ ( [System.IO.Path ]::GetRandomFileName()) .ps1" ) - Value $script - PassThru - Force
184
-
185
- $request = Send-LspDidOpenTextDocumentRequest - Client $client `
186
- - Uri ([Uri ]::new($file.PSPath ).AbsoluteUri) `
187
- - Text ($file [0 ].ToString())
188
-
189
- # There's no response for this message, but we need to call Get-LspResponse
190
- # to increment the counter.
191
- Get-LspResponse - Client $client - Id $request.Id | Out-Null
192
-
193
- # Throw out any notifications from the first PSScriptAnalyzer run.
194
- Get-LspNotification - Client $client | Out-Null
195
-
196
-
197
-
198
189
$request = Send-LspRequest - Client $client - Method " textDocument/foldingRange" - Parameters ([Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.FoldingRangeParams ] @ {
199
190
TextDocument = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.TextDocumentIdentifier ] @ {
200
- Uri = ([Uri ]::new($file .PSPath ).AbsoluteUri)
191
+ Uri = ([Uri ]::new($filePath ).AbsoluteUri)
201
192
}
202
193
})
203
194
215
206
$sortedResults [1 ].endCharacter | Should - Be 2
216
207
}
217
208
209
+ It " can handle a normal formatting request" {
210
+ $filePath = New-TestFile - Script '
211
+ gci | % {
212
+ Get-Process
213
+ }
214
+
215
+ '
216
+
217
+ $request = Send-LspFormattingRequest - Client $client `
218
+ - Uri ([Uri ]::new($filePath ).AbsoluteUri)
219
+
220
+ $response = Get-LspResponse - Client $client - Id $request.Id
221
+
222
+ # If we have a tab, formatting ran.
223
+ $response.Result.newText.Contains (" `t " ) | Should - BeTrue - Because " We expect a tab."
224
+ }
225
+
226
+ It " can handle a range formatting request" {
227
+ $filePath = New-TestFile - Script '
228
+ gci | % {
229
+ Get-Process
230
+ }
231
+
232
+ '
233
+
234
+ $range = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.Range ]@ {
235
+ Start = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.Position ]@ {
236
+ Line = 2
237
+ Character = 0
238
+ }
239
+ End = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.Position ]@ {
240
+ Line = 3
241
+ Character = 0
242
+ }
243
+ }
244
+
245
+ $request = Send-LspRangeFormattingRequest - Client $client `
246
+ - Uri ([Uri ]::new($filePath ).AbsoluteUri) `
247
+ - Range $range
248
+
249
+ $response = Get-LspResponse - Client $client - Id $request.Id
250
+
251
+ # If we have a tab, formatting ran.
252
+ $response.Result.newText.Contains (" `t " ) | Should - BeTrue - Because " We expect a tab."
253
+ }
254
+
218
255
# This test MUST be last
219
256
It " Shuts down the process properly" {
220
257
$request = Send-LspShutdownRequest - Client $client
0 commit comments