-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathLanguageServerTests.cs
893 lines (767 loc) · 33.5 KB
/
LanguageServerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.PowerShell.EditorServices.Protocol.Client;
using Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter;
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
using Microsoft.PowerShell.EditorServices.Protocol.Messages;
using Microsoft.PowerShell.EditorServices.Protocol.Server;
using Microsoft.PowerShell.EditorServices.Session;
using Microsoft.PowerShell.EditorServices.Utility;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.PowerShell.EditorServices.Test.Host
{
public class LanguageServerTests : ServerTestsBase, IAsyncLifetime
{
private ILogger logger;
private LanguageServiceClient languageServiceClient;
public async Task InitializeAsync()
{
string testLogPath =
Path.Combine(
#if CoreCLR
AppContext.BaseDirectory,
#else
AppDomain.CurrentDomain.BaseDirectory,
#endif
"logs",
this.GetType().Name,
Guid.NewGuid().ToString().Substring(0, 8));
this.logger = Logging.CreateLogger()
.LogLevel(LogLevel.Verbose)
.AddLogFile(testLogPath + "-client.log")
.Build();
testLogPath += "-server.log";
System.Console.WriteLine(" Output log at path: {0}", testLogPath);
Tuple<string, string> pipeNames =
await this.LaunchService(
testLogPath,
waitForDebugger: false);
//waitForDebugger: true);
this.languageServiceClient =
new LanguageServiceClient(
await NamedPipeClientChannel.Connect(
pipeNames.Item1,
MessageProtocolType.LanguageServer,
this.logger),
this.logger);
this.messageSender = this.languageServiceClient;
this.messageHandlers = this.languageServiceClient;
await this.languageServiceClient.Start();
}
public async Task DisposeAsync()
{
await this.languageServiceClient.Stop();
this.KillService();
}
[Fact]
public async Task ServiceReturnsSyntaxErrors()
{
// Send the 'didOpen' event
await this.SendOpenFileEvent("TestFiles\\SimpleSyntaxError.ps1", false);
// Wait for the diagnostic event
PublishDiagnosticsNotification diagnostics =
await this.WaitForEvent(
PublishDiagnosticsNotification.Type);
// Was there a syntax error?
Assert.NotEqual(0, diagnostics.Diagnostics.Length);
Assert.False(
string.IsNullOrEmpty(diagnostics.Diagnostics[0].Message));
}
[Fact(Skip = "Skipping until Script Analyzer integration is added back")]
public async Task ServiceReturnsSemanticMarkers()
{
// Send the 'didOpen' event
await this.SendOpenFileEvent("TestFiles\\SimpleSemanticError.ps1", false);
// Wait for the diagnostic event
PublishDiagnosticsNotification diagnostics =
await this.WaitForEvent(
PublishDiagnosticsNotification.Type);
// Was there a semantic error?
Assert.NotEqual(0, diagnostics.Diagnostics.Length);
Assert.Contains("unapproved", diagnostics.Diagnostics[0].Message);
}
[Fact]
public async Task ServiceReturnsNoErrorsForUsingRelativeModulePaths()
{
// Send the 'didOpen' event
await this.SendOpenFileEvent("TestFiles\\Module.psm1", false);
// Wait for the diagnostic event
PublishDiagnosticsNotification diagnostics =
await this.WaitForEvent(
PublishDiagnosticsNotification.Type);
// Was there a syntax error?
Assert.Equal(0, diagnostics.Diagnostics.Length);
}
[Fact]
public async Task ServiceCompletesFunctionName()
{
await this.SendOpenFileEvent("TestFiles\\CompleteFunctionName.ps1");
CompletionItem[] completions =
await this.SendRequest(
CompletionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\CompleteFunctionName.ps1",
},
Position = new Position
{
Line = 4,
Character = 3,
}
});
Assert.NotNull(completions);
Assert.NotEqual(completions.Length, 0);
// TODO: Add more asserts
}
[Fact]
public async Task CompletesDetailOnVariableSuggestion()
{
await this.SendOpenFileEvent("TestFiles\\CompleteFunctionName.ps1");
CompletionItem[] completions =
await this.SendRequest(
CompletionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\CompleteFunctionName.ps1"
},
Position = new Position
{
Line = 3,
Character = 5
}
});
CompletionItem consoleFileNameItem =
completions
.FirstOrDefault(
c => c.Label == "ConsoleFileName");
Assert.NotNull(consoleFileNameItem);
Assert.Equal("[string]", consoleFileNameItem.Detail);
}
[Fact(Skip = "Skipped until variable documentation gathering is added back.")]
public async Task CompletesDetailOnVariableDocSuggestion()
{
await this.SendOpenFileEvent("TestFiles\\CompleteFunctionName.ps1");
await this.SendRequest(
CompletionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\CompleteFunctionName.ps1"
},
Position = new Position
{
Line = 7,
Character = 5
}
});
// TODO: This section needs to be updated, seems that
// CompletionsResponse is missing.
//CompletionsResponse completion = this.WaitForMessage<CompletionsResponse>();
//List<string> entryName = new List<string>();
//entryName.Add("$HKCU:");
//await this.MessageWriter.WriteMessage(
// new CompletionDetailsRequest
// {
// Arguments = new CompletionDetailsRequestArgs
// {
// File = "TestFiles\\CompleteFunctionName.ps1",
// Line = 7,
// Offset = 5,
// EntryNames = entryName.ToArray()
// }
// });
//CompletionDetailsResponse completionDetail = this.WaitForMessage<CompletionDetailsResponse>();
//Assert.NotNull(completionDetail.Body[0]);
//Assert.Equal("The software settings for the current user", completionDetail.Body[0].DocString);
}
[Fact]
public async Task CompletesDetailOnCommandSuggestion()
{
await this.SendOpenFileEvent("TestFiles\\CompleteFunctionName.ps1");
CompletionItem[] completions =
await this.SendRequest(
CompletionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\CompleteFunctionName.ps1"
},
Position = new Position
{
Line = 5,
Character = 8
}
});
CompletionItem completionItem =
completions
.FirstOrDefault(
c => c.Label == "Get-Process");
Assert.NotNull(completionItem);
CompletionItem updatedCompletionItem =
await this.SendRequest(
CompletionResolveRequest.Type,
completionItem);
// Can't depend on a particular documentation string if the test machine
// hasn't run Update-Help, so just verify that a non-empty string was
// returned.
Assert.NotNull(updatedCompletionItem);
Assert.True(updatedCompletionItem.Documentation.Length > 0);
}
[Fact]
public async Task FindsReferencesOfVariable()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
ReferencesRequest.Type,
new ReferencesParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 7,
Character = 4,
}
});
Assert.NotNull(locations);
Assert.Equal(locations.Length, 3);
Assert.Equal(5, locations[0].Range.Start.Line);
Assert.Equal(0, locations[0].Range.Start.Character);
Assert.Equal(7, locations[1].Range.Start.Line);
Assert.Equal(0, locations[1].Range.Start.Character);
Assert.Equal(8, locations[2].Range.Start.Line);
Assert.Equal(12, locations[2].Range.Start.Character);
}
[Fact]
public async Task FindsNoReferencesOfEmptyLine()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
ReferencesRequest.Type,
new ReferencesParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 9,
Character = 0,
}
});
Assert.NotNull(locations);
Assert.Equal(0, locations.Length);
}
[Fact]
public async Task FindsReferencesOnFunctionDefinition()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
ReferencesRequest.Type,
new ReferencesParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 0,
Character = 17,
}
});
Assert.NotNull(locations);
Assert.Equal(3, locations.Length);
Assert.Equal(0, locations[0].Range.Start.Line);
Assert.Equal(9, locations[0].Range.Start.Character);
Assert.Equal(2, locations[1].Range.Start.Line);
Assert.Equal(4, locations[1].Range.Start.Character);
Assert.Equal(8, locations[2].Range.Start.Line);
Assert.Equal(0, locations[2].Range.Start.Character);
}
[Fact]
public async Task FindsReferencesOnCommand()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
ReferencesRequest.Type,
new ReferencesParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 0,
Character = 17,
}
});
Assert.NotNull(locations);
Assert.Equal(3, locations.Length);
Assert.Equal(0, locations[0].Range.Start.Line);
Assert.Equal(9, locations[0].Range.Start.Character);
Assert.Equal(2, locations[1].Range.Start.Line);
Assert.Equal(4, locations[1].Range.Start.Character);
Assert.Equal(8, locations[2].Range.Start.Line);
Assert.Equal(0, locations[2].Range.Start.Character);
}
[Fact]
public async Task FindsDefinitionOfCommand()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
DefinitionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1",
},
Position = new Position
{
Line = 2,
Character = 11,
}
});
Assert.NotNull(locations);
Assert.Equal(1, locations.Length);
Assert.Equal(0, locations[0].Range.Start.Line);
Assert.Equal(9, locations[0].Range.Start.Character);
}
[Fact]
public async Task FindsNoDefinitionOfBuiltinCommand()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
DefinitionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 10,
Character = 9,
}
});
Assert.NotNull(locations);
Assert.Equal(0, locations.Length);
}
[Fact]
public async Task FindsDefinitionOfVariable()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
DefinitionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 8,
Character = 13,
}
});
Assert.NotNull(locations);
Assert.Equal(1, locations.Length);
Assert.Equal(5, locations[0].Range.Start.Line);
Assert.Equal(0, locations[0].Range.Start.Character);
Assert.Equal(5, locations[0].Range.End.Line);
Assert.Equal(7, locations[0].Range.End.Character);
}
[Fact]
public async Task FindsDefinitionOfVariableInOtherFile()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
DefinitionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 15,
Character = 20,
}
});
Assert.NotNull(locations);
Assert.Equal(1, locations.Length);
Assert.EndsWith("VariableDefinition.ps1", locations[0].Uri);
Assert.Equal(0, locations[0].Range.Start.Line);
Assert.Equal(0, locations[0].Range.Start.Character);
Assert.Equal(0, locations[0].Range.End.Line);
Assert.Equal(20, locations[0].Range.End.Character);
}
[Fact]
public async Task FindDefinitionOfVariableWithSpecialChars()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
Location[] locations =
await this.SendRequest(
DefinitionRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 18,
Character = 24,
}
});
Assert.NotNull(locations);
Assert.Equal(1, locations.Length);
Assert.EndsWith("FindReferences.ps1", locations[0].Uri);
Assert.Equal(17, locations[0].Range.Start.Line);
Assert.Equal(0, locations[0].Range.Start.Character);
Assert.Equal(17, locations[0].Range.End.Line);
Assert.Equal(27, locations[0].Range.End.Character);
}
[Fact]
public async Task FindsOccurencesOnFunctionDefinition()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
DocumentHighlight[] highlights =
await this.SendRequest(
DocumentHighlightRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 0,
Character = 17,
}
});
Assert.NotNull(highlights);
Assert.Equal(3, highlights.Length);
Assert.Equal(2, highlights[1].Range.Start.Line);
}
[Fact(Skip = "This test hangs in VSTS for some reason...")]
public async Task GetsParameterHintsOnCommand()
{
await this.SendOpenFileEvent("TestFiles\\FindReferences.ps1");
SignatureHelp signatureHelp =
await this.SendRequest(
SignatureHelpRequest.Type,
new TextDocumentPositionParams
{
TextDocument = new TextDocumentIdentifier
{
Uri = "TestFiles\\FindReferences.ps1"
},
Position = new Position
{
Line = 12,
Character = 14
}
});
Assert.NotNull(signatureHelp);
Assert.Equal(1, signatureHelp.Signatures.Length);
Assert.Equal(2, signatureHelp.Signatures[0].Parameters.Length);
Assert.Equal(
"Write-Output [-InputObject] <psobject[]> [-NoEnumerate] [<CommonParameters>]",
signatureHelp.Signatures[0].Label);
}
[Fact]
public async Task ServiceExecutesReplCommandAndReceivesOutput()
{
OutputReader outputReader = new OutputReader(this.messageHandlers);
// Send the configuration request to initiate the command loop
await this.SendConfigurationRequest(outputReader);
await
this.SendRequest(
EvaluateRequest.Type,
new EvaluateRequestArguments
{
Expression = "1 + 2"
});
Assert.Equal("1 + 2", await outputReader.ReadLine());
Assert.Equal("3", await outputReader.ReadLine());
}
[Fact]
public async Task ServiceExpandsAliases()
{
string expandedText =
await this.SendRequest(
ExpandAliasRequest.Type,
"gci\r\npwd");
Assert.Equal("Get-ChildItem\r\nGet-Location", expandedText);
}
[Fact]
public async Task ServiceExecutesReplCommandAndReceivesChoicePrompt()
{
OutputReader outputReader = new OutputReader(this.messageHandlers);
// Send the configuration request to initiate the command loop
await this.SendConfigurationRequest(outputReader);
string choiceScript =
@"
$caption = ""Test Choice"";
$message = ""Make a selection"";
$choiceA = New-Object System.Management.Automation.Host.ChoiceDescription ""&Apple"",""Help for Apple"";
$choiceB = New-Object System.Management.Automation.Host.ChoiceDescription ""Banana"",""Help for Banana"";
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($choiceA,$choiceB);
$host.ui.PromptForChoice($caption, $message, $choices, 1)";
Task<Tuple<ShowChoicePromptRequest, RequestContext<ShowChoicePromptResponse>>> choicePromptTask =
this.WaitForRequest(ShowChoicePromptRequest.Type);
// Execute the script but don't await the task yet because
// the choice prompt will block execution from completing
Task<EvaluateResponseBody> evaluateTask =
this.SendRequest(
EvaluateRequest.Type,
new EvaluateRequestArguments
{
Expression = choiceScript,
Context = "repl"
});
// Wait for the choice prompt request and check expected values
Tuple<ShowChoicePromptRequest, RequestContext<ShowChoicePromptResponse>> requestResponseContext = await choicePromptTask;
ShowChoicePromptRequest showChoicePromptRequest = requestResponseContext.Item1;
RequestContext<ShowChoicePromptResponse> requestContext = requestResponseContext.Item2;
Assert.Equal(1, showChoicePromptRequest.DefaultChoices[0]);
// Respond to the prompt request
await requestContext.SendResult(
new ShowChoicePromptResponse
{
ResponseText = "a"
});
// Skip the initial script and prompt lines (6 script lines plus 3 prompt lines)
string[] outputLines = await outputReader.ReadLines(9);
// Wait for the selection to appear as output
await evaluateTask;
Assert.Equal("0", await outputReader.ReadLine());
}
[Fact]
public async Task ServiceExecutesReplCommandAndReceivesInputPrompt()
{
OutputReader outputReader = new OutputReader(this.messageHandlers);
// Send the configuration request to initiate the command loop
await this.SendConfigurationRequest(outputReader);
string promptScript =
@"
$NameField = New-Object System.Management.Automation.Host.FieldDescription ""Name""
$NameField.SetParameterType([System.String])
$fields = [System.Management.Automation.Host.FieldDescription[]]($NameField)
$host.ui.Prompt($null, $null, $fields)";
Task<Tuple<ShowInputPromptRequest, RequestContext<ShowInputPromptResponse>>> inputPromptTask =
this.WaitForRequest(ShowInputPromptRequest.Type);
// Execute the script but don't await the task yet because
// the choice prompt will block execution from completing
Task<EvaluateResponseBody> evaluateTask =
this.SendRequest(
EvaluateRequest.Type,
new EvaluateRequestArguments
{
Expression = promptScript,
Context = "repl"
});
// Wait for the input prompt request and check expected values
Tuple<ShowInputPromptRequest, RequestContext<ShowInputPromptResponse>> requestResponseContext = await inputPromptTask;
ShowInputPromptRequest showInputPromptRequest = requestResponseContext.Item1;
RequestContext<ShowInputPromptResponse> requestContext = requestResponseContext.Item2;
Assert.Equal("Name", showInputPromptRequest.Name);
// Respond to the prompt request
await requestContext.SendResult(
new ShowInputPromptResponse
{
ResponseText = "John"
});
// Skip the initial 4 script lines
string[] scriptLines = await outputReader.ReadLines(4);
// Verify the first line
Assert.Equal("Name: John", await outputReader.ReadLine());
// Verify the rest of the output
string[] outputLines = await outputReader.ReadLines(4);
Assert.Equal("", outputLines[0]);
Assert.Equal("Key Value", outputLines[1]);
Assert.Equal("--- -----", outputLines[2]);
Assert.Equal("Name John ", outputLines[3]);
// Wait for execution to complete
await evaluateTask;
}
[Fact(Skip = "Native command output in the legacy host has been disabled for now, may re-enable later")]
public async Task ServiceExecutesNativeCommandAndReceivesCommand()
{
OutputReader outputReader = new OutputReader(this.messageHandlers);
// Execute the script but don't await the task yet because
// the choice prompt will block execution from completing
Task<EvaluateResponseBody> evaluateTask =
this.SendRequest(
EvaluateRequest.Type,
new EvaluateRequestArguments
{
Expression = "cmd.exe /c 'echo Test Output'",
Context = "repl"
});
// Skip the command line and the following newline
await outputReader.ReadLines(2);
// Wait for the selection to appear as output
await evaluateTask;
Assert.Equal("Test Output", await outputReader.ReadLine());
}
[Fact]
public async Task ServiceLoadsProfilesOnDemand()
{
string testHostName = "Test.PowerShellEditorServices";
string profileName =
string.Format(
"{0}_{1}",
testHostName,
ProfilePaths.AllHostsProfileName);
string testProfilePath =
Path.Combine(
Path.GetFullPath(
@"..\..\..\..\PowerShellEditorServices.Test.Shared\Profile\"),
profileName);
string currentUserCurrentHostPath =
Path.Combine(
#if !CoreCLR
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"WindowsPowerShell",
#else
// TODO: This will need to be improved once we are running tests on CoreCLR
"~/.powershell",
#endif
profileName);
// Copy the test profile to the current user's host profile path
File.Copy(testProfilePath, currentUserCurrentHostPath, true);
Assert.True(
File.Exists(currentUserCurrentHostPath),
"Copied profile path does not exist!");
OutputReader outputReader = new OutputReader(this.messageHandlers);
// Send the configuration change to cause profiles to be loaded
await this.SendConfigurationRequest(outputReader, true);
Task<EvaluateResponseBody> evaluateTask =
this.SendRequest(
EvaluateRequest.Type,
new EvaluateRequestArguments
{
Expression = "\"PROFILE: $(Assert-ProfileLoaded)\"",
Context = "repl"
});
// Try reading up to 10 lines to find the expected output line
string outputString = null;
for (int i = 0; i < 10; i++)
{
outputString = await outputReader.ReadLine();
if (outputString.StartsWith("PROFILE"))
{
break;
}
}
// Delete the test profile before any assert failures
// cause the function to exit
File.Delete(currentUserCurrentHostPath);
// Wait for the selection to appear as output
await evaluateTask;
Assert.Equal("PROFILE: True", outputString);
}
public async Task ServiceReturnsPowerShellVersionDetails()
{
PowerShellVersion versionDetails =
await this.SendRequest(
PowerShellVersionRequest.Type,
new PowerShellVersionRequest());
// TODO: This should be more robust and predictable.
Assert.StartsWith("5.", versionDetails.Version);
Assert.StartsWith("5.", versionDetails.DisplayVersion);
Assert.Equal("Desktop", versionDetails.Edition);
string expectedArchitecture = (IntPtr.Size == 8) ? "x64" : "x86";
Assert.Equal(expectedArchitecture, versionDetails.Architecture);
}
private async Task SendOpenFileEvent(string filePath, bool waitForDiagnostics = true)
{
string fileContents = string.Join(Environment.NewLine, File.ReadAllLines(filePath));
// Start the event waiter for diagnostics before sending the
// open event to make sure that we catch it
Task<PublishDiagnosticsNotification> diagnosticWaitTask = null;
if (waitForDiagnostics)
{
// Wait for the diagnostic event
diagnosticWaitTask =
this.WaitForEvent(
PublishDiagnosticsNotification.Type);
}
await this.SendEvent(
DidOpenTextDocumentNotification.Type,
new DidOpenTextDocumentParams
{
TextDocument = new TextDocumentItem
{
Uri = filePath,
Text = fileContents,
LanguageId = "PowerShell",
Version = 0
}
});
if (diagnosticWaitTask != null)
{
await diagnosticWaitTask;
}
}
private async Task SendConfigurationRequest(
OutputReader outputReader,
bool enableProfileLoading = false)
{
// Send the configuration change to cause profiles to be loaded
await this.languageServiceClient.SendEvent(
DidChangeConfigurationNotification<LanguageServerSettingsWrapper>.Type,
new DidChangeConfigurationParams<LanguageServerSettingsWrapper>
{
Settings = new LanguageServerSettingsWrapper
{
Powershell = new LanguageServerSettings
{
EnableProfileLoading = enableProfileLoading,
ScriptAnalysis = new ScriptAnalysisSettings
{
Enable = false
},
CodeFormatting = new CodeFormattingSettings()
}
}
});
// Wait for the prompt to be written once the profile loads
Assert.StartsWith("PS ", await outputReader.ReadLine(waitForNewLine: false));
}
}
}