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));
        }
        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;
        }
Beispiel #3
0
        public async Task ServiceExecutesReplCommandAndReceivesOutput()
        {
            OutputReader outputReader = new OutputReader(this.protocolClient);

            await
            this.SendRequest(
                EvaluateRequest.Type,
                new EvaluateRequestArguments
            {
                Expression = "1 + 2"
            });

            Assert.Equal("1 + 2", await outputReader.ReadLine());
            await outputReader.ReadLine(); // Skip the empty line

            Assert.Equal("3", await outputReader.ReadLine());
        }
        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());
        }
        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());
        }
        public async Task DebugAdapterReceivesOutputEvents()
        {
            OutputReader outputReader = new OutputReader(this.debugAdapterClient);

            await this.LaunchScript(DebugScriptPath);

            // Skip the first 2 lines which just report the script
            // that is being executed
            await outputReader.ReadLines(2);

            // Make sure we're getting output from the script
            Assert.Equal("Output 1", await outputReader.ReadLine());

            // Abort script execution
            await this.SendRequest(DisconnectRequest.Type, new object());
        }
Beispiel #7
0
        public async Task DebugAdapterReceivesOutputEvents()
        {
            OutputReader outputReader = new OutputReader(this.debugAdapterClient);

            await this.LaunchScript(DebugScriptPath);

            // Make sure we're getting output from the script
            Assert.Equal("Output 1", await outputReader.ReadLine());

            // Abort script execution
            Task terminatedEvent = this.WaitForEvent(TerminatedEvent.Type);
            await
            Task.WhenAll(
                this.SendRequest(DisconnectRequest.Type, new object()),
                terminatedEvent);
        }
        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());
        }
        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);
        }
Beispiel #10
0
        public async Task ServiceLoadsProfilesOnDemand()
        {
            // 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 = true,
                        ScriptAnalysis       = null
                    }
                }
            });

            string testProfilePath =
                Path.GetFullPath(
                    @"..\..\..\PowerShellEditorServices.Test.Shared\Profile\Profile.ps1");

            string testHostName = "Test.PowerShellEditorServices";
            string profileName  =
                string.Format(
                    "{0}_{1}",
                    testHostName,
                    ProfilePaths.AllHostsProfileName);

            string currentUserCurrentHostPath =
                Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                    "WindowsPowerShell",
                    profileName);

            // Copy the test profile to the current user's host profile path
            File.Copy(testProfilePath, currentUserCurrentHostPath, true);

            OutputReader outputReader = new OutputReader(this.protocolClient);

            Task <EvaluateResponseBody> evaluateTask =
                this.SendRequest(
                    EvaluateRequest.Type,
                    new EvaluateRequestArguments
            {
                Expression = "\"PROFILE: $(Assert-ProfileLoaded)\"",
                Context    = "repl"
            });

            // Try reading up to 10 lines to find the
            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 ServiceExecutesNativeCommandAndReceivesCommand()
        {
            OutputReader outputReader = new OutputReader(this.protocolClient);

            // 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());
        }
        public async Task ServiceExecutesReplCommandAndReceivesInputPrompt()
        {
            OutputReader outputReader = new OutputReader(this.protocolClient);

            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 script lines (4 script lines plus 2 blank lines)
            string[] scriptLines = await outputReader.ReadLines(6);

            // In some cases an extra newline appears after the script lines.
            // I have no idea why this happens, but it normally seems to occur
            // on my local machine and not the CI server.  For now, adjust for
            // it here.
            string outputLine = await outputReader.ReadLine();
            if (string.IsNullOrEmpty(outputLine))
            {
                outputLine = await outputReader.ReadLine();
            }

            // Verify the first line
            Assert.Equal("Name: John", outputLine);

            // 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;
        }
        public async Task ServiceExecutesReplCommandAndReceivesChoicePrompt()
        {
            OutputReader outputReader = new OutputReader(this.protocolClient);

            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.DefaultChoice);

            // Respond to the prompt request
            await requestContext.SendResult(
                new ShowChoicePromptResponse
                {
                    ChosenItem = "a"
                });

            // Skip the initial script lines (6 script lines plus 3 blank lines)
            await outputReader.ReadLines(9);

            // Wait for the selection to appear as output
            await evaluateTask;
            Assert.Equal("0", await outputReader.ReadLine());
        }
        public async Task ServiceExecutesReplCommandAndReceivesOutput()
        {
            OutputReader outputReader = new OutputReader(this.protocolClient);

            await 
                this.SendRequest(
                    EvaluateRequest.Type,
                    new EvaluateRequestArguments
                    {
                        Expression = "1 + 2"
                    });

            Assert.Equal("1 + 2", await outputReader.ReadLine());
            await outputReader.ReadLine(); // Skip the empty line
            Assert.Equal("3", await outputReader.ReadLine());
        }
        public async Task ServiceExecutesReplCommandAndReceivesInputPrompt()
        {
            OutputReader outputReader = new OutputReader(this.protocolClient);

            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 script lines (4 script lines plus 2 blank lines)
            string[] scriptLines = await outputReader.ReadLines(6);

            // In some cases an extra newline appears after the script lines.
            // I have no idea why this happens, but it normally seems to occur
            // on my local machine and not the CI server.  For now, adjust for
            // it here.
            string outputLine = await outputReader.ReadLine();

            if (string.IsNullOrEmpty(outputLine))
            {
                outputLine = await outputReader.ReadLine();
            }

            // Verify the first line
            Assert.Equal("Name: John", outputLine);

            // 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;
        }
        public async Task DebugAdapterReceivesOutputEvents()
        {
            OutputReader outputReader = new OutputReader(this.debugAdapterClient);

            await this.LaunchScript(DebugScriptPath);

            // Make sure we're getting output from the script
            Assert.Equal("Output 1", await outputReader.ReadLine());

            // Abort script execution
            Task terminatedEvent = this.WaitForEvent(TerminatedEvent.Type);
            await
                Task.WhenAll(
                    this.SendRequest(DisconnectRequest.Type, new object()),
                    terminatedEvent);
        }