Skip to content

Commit 0a24525

Browse files
add formatting tests
1 parent b7aaae8 commit 0a24525

File tree

3 files changed

+152
-40
lines changed

3 files changed

+152
-40
lines changed

test/Pester/EditorServices.Integration.Tests.ps1

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ Data:
5555
throw $msg
5656
}
5757

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+
5885
Describe "Loading and running PowerShellEditorServices" {
5986
BeforeAll {
6087
Import-Module -Force "$PSScriptRoot/../../module/PowerShellEditorServices"
@@ -89,21 +116,12 @@ Describe "Loading and running PowerShellEditorServices" {
89116
}
90117

91118
It "Can handle WorkspaceSymbol request" {
92-
$script = "
119+
New-TestFile -Script "
93120
function Get-Foo {
94121
Write-Host 'hello'
95122
}
96123
"
97124

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-
107125
$request = Send-LspRequest -Client $client -Method "workspace/symbol" -Parameters @{
108126
query = ""
109127
}
@@ -141,19 +159,7 @@ function Get-Foo {
141159
}
142160

143161
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 | % { $_ }'
157163

158164
$request = Send-LspDidChangeConfigurationRequest -Client $client -Settings @{
159165
PowerShell = @{
@@ -172,32 +178,17 @@ function Get-Foo {
172178
}
173179

174180
It "Can handle folding request" {
175-
$script = 'gci | % {
181+
$filePath = New-TestFile -Script 'gci | % {
176182
$_
177183
178184
@"
179185
$_
180186
"@
181187
}'
182188

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-
198189
$request = Send-LspRequest -Client $client -Method "textDocument/foldingRange" -Parameters ([Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.FoldingRangeParams] @{
199190
TextDocument = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.TextDocumentIdentifier] @{
200-
Uri = ([Uri]::new($file.PSPath).AbsoluteUri)
191+
Uri = ([Uri]::new($filePath).AbsoluteUri)
201192
}
202193
})
203194

@@ -215,6 +206,52 @@ $_
215206
$sortedResults[1].endCharacter | Should -Be 2
216207
}
217208

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+
218255
# This test MUST be last
219256
It "Shuts down the process properly" {
220257
$request = Send-LspShutdownRequest -Client $client

tools/PsesPsClient/PsesPsClient.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ FunctionsToExport = @(
7676
'Send-LspInitializeRequest',
7777
'Send-LspDidOpenTextDocumentRequest',
7878
'Send-LspDidChangeConfigurationRequest',
79+
'Send-LspFormattingRequest',
80+
'Send-LspRangeFormattingRequest',
7981
'Send-LspShutdownRequest',
8082
'Get-LspNotification',
8183
'Get-LspResponse'

tools/PsesPsClient/PsesPsClient.psm1

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,79 @@ function Send-LspDidChangeConfigurationRequest
319319
$result
320320
}
321321

322+
function Send-LspFormattingRequest
323+
{
324+
[OutputType([PsesPsClient.LspRequest])]
325+
param(
326+
[Parameter(Position = 0, Mandatory)]
327+
[PsesPsClient.PsesLspClient]
328+
$Client,
329+
330+
[Parameter(Mandatory)]
331+
[string]
332+
$Uri,
333+
334+
[Parameter()]
335+
[int]
336+
$TabSize = 4,
337+
338+
[Parameter()]
339+
[switch]
340+
$InsertSpaces
341+
)
342+
343+
$params = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.DocumentFormattingParams]@{
344+
TextDocument = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.TextDocumentIdentifier]@{
345+
Uri = $Uri
346+
}
347+
options = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.FormattingOptions]@{
348+
TabSize = $TabSize
349+
InsertSpaces = $InsertSpaces.IsPresent
350+
}
351+
}
352+
353+
return Send-LspRequest -Client $Client -Method 'textDocument/formatting' -Parameters $params
354+
}
355+
356+
function Send-LspRangeFormattingRequest
357+
{
358+
[OutputType([PsesPsClient.LspRequest])]
359+
param(
360+
[Parameter(Position = 0, Mandatory)]
361+
[PsesPsClient.PsesLspClient]
362+
$Client,
363+
364+
[Parameter(Mandatory)]
365+
[string]
366+
$Uri,
367+
368+
[Parameter(Mandatory)]
369+
[Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.Range]
370+
$Range,
371+
372+
[Parameter()]
373+
[int]
374+
$TabSize = 4,
375+
376+
[Parameter()]
377+
[switch]
378+
$InsertSpaces
379+
)
380+
381+
$params = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.DocumentRangeFormattingParams]@{
382+
TextDocument = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.TextDocumentIdentifier]@{
383+
Uri = $Uri
384+
}
385+
Range = $Range
386+
options = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.FormattingOptions]@{
387+
TabSize = $TabSize
388+
InsertSpaces = $InsertSpaces.IsPresent
389+
}
390+
}
391+
392+
return Send-LspRequest -Client $Client -Method 'textDocument/rangeFormatting' -Parameters $params
393+
}
394+
322395
function Send-LspShutdownRequest
323396
{
324397
[OutputType([PsesPsClient.LspRequest])]

0 commit comments

Comments
 (0)