public void WhenInvokeWriteHost()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { File = "Write-Host 'hello'" };
            var exception = Assert.Throws<ArgumentException>(() => InvokePowerShell.Execute(commandFactory, buildTaskLog));

            Assert.AreEqual(
                "Processing File 'Write-Host 'hello'' failed because the file does not have a '.ps1' " +
                "extension. Specify a valid Windows PowerShell script file name, and then try again.",
                exception.Message);
        }
        public void WhenInvokingNonExistentScriptFile()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { File = "7d680d7c-3214-43c6-8eec-3b00a40ab91e.ps1" };
            var exception = Assert.Throws<ArgumentException>(() => InvokePowerShell.Execute(commandFactory, buildTaskLog));

            Assert.AreEqual(
                "The argument '7d680d7c-3214-43c6-8eec-3b00a40ab91e.ps1' to the File parameter " +
                "does not exist. Provide the path to an existing '.ps1' file as an argument to the File parameter.",
                exception.Message);
        }
Example #3
0
        internal static void Execute(CommandFactory commandFactory, IBuildTaskLog log)
        {
            PowerShellHost.WithPowerShell(log, (shell, output) =>
            {
                var command = commandFactory.CreateCommand(new PowerShellCommandParameterProvider());

                shell.Commands.AddCommand(command);

                var outputList = new PowerShellOutputList(output, new PowerShellStringProvider(shell));
                shell.Invoke(null, outputList);
            });
        }
        public void WhenExpressionIsSpecified()
        {
            var commandProvider = new Mock<IPowerShellCommandParameterProvider>();

            var command = new CommandFactory
            {
                Expression = "test"
            }
            .CreateCommand(commandProvider.Object);

            Assert.IsTrue(command.IsScript);
            Assert.AreEqual("test", command.CommandText);
        }
        public void WhenInvokeReadHostAsSecureString()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = "Read-Host -AsSecureString" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogError(
                    file: "<No file>",
                    lineNumber: 1,
                    message: "Windows PowerShell is in non-interactive mode. Read and Prompt functionality is not available." + Environment.NewLine +
                             "at <ScriptBlock>, <No file>: line 1"));
        }
        public void WhenInvokeWriteDebug()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = "Write-Debug 'hello' -Debug" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("hello", MessageImportance.Low),
                new LogError(
                    file: "<No file>",
                    lineNumber: 1,
                    message: "Windows PowerShell is in non-interactive mode. Read and Prompt functionality is not available." + Environment.NewLine +
                             "at <ScriptBlock>, <No file>: line 1"));
        }
        public void WhenInvokingScriptFileThatHasDefaultParameterSetWithAutoArguments()
        {
            var buildTaskLog = new MockBuildTaskLog();
            var scriptFilePath = GetTestResourceFilePath("WhenInvokingScriptFileThatHasDefaultParameterSetWithAutoArguments.ps1");

            var commandFactory = new CommandFactory
            {
                File = scriptFilePath,
                AutoParameters = new ITaskItem[]
                {
                    new MockPropertyTaskItem(name: "Arg1", value: "foo"),
                    new MockPropertyTaskItem(name: "Arg2", value: "bar"),
                }
            };

            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("Arg1 = foo", MessageImportance.High),
                new LogMessage("Arg2 = ", MessageImportance.High));
        }
        public void WhenInvokingScriptFile()
        {
            var buildTaskLog = new MockBuildTaskLog();
            var scriptFilePath = GetTestResourceFilePath("WhenInvokingScriptFile.ps1");

            var commandFactory = new CommandFactory { File = scriptFilePath };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("Alive", MessageImportance.High),
                new LogWarning(
                    file: scriptFilePath,
                    lineNumber: 2,
                    message: "Danger"),
                new LogError(
                    file: scriptFilePath,
                    lineNumber: 3,
                    message: "Dead" + Environment.NewLine +
                             "at <ScriptBlock>, " + scriptFilePath + ": line 3" + Environment.NewLine +
                             "at <ScriptBlock>, <No file>: line 1"));
        }
        public void WhenInvokingComplexInlineScript()
        {
            var buildTaskLog = new MockBuildTaskLog();

            const string script = @"
            function foo
            {
            Write-Host 'in foo'
            }

            Write-Host 'hello'; foo

            Write-Host 'goodbye'
            ";
            var commandFactory = new CommandFactory { Expression = script };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("hello", MessageImportance.High),
                new LogMessage("in foo", MessageImportance.High),
                new LogMessage("goodbye", MessageImportance.High));
        }
        public void WhenInvokeWriteHostNoNewLine()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = "Write-Host 'hello' -NoNewLine" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("hello", MessageImportance.High));
        }
        public void WhenInvokeWriteErrorWithExplicitFilenameLineAndColumn()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = @"Write-Error 'c:\foo\bar.txt(123,456) : This is a test'" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogError(
                    file: @"c:\foo\bar.txt",
                    lineNumber: 123,
                    columnNumber: 456,
                    message: @"This is a test" + Environment.NewLine +
                             @"at c:\foo\bar.txt: line 123" + Environment.NewLine +
                             @"at <ScriptBlock>, <No file>: line 1"));
        }
        public void WhenInvokeWriteVerbose()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = "Write-Verbose 'hello' -Verbose" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("hello", MessageImportance.Low));
        }
        public void WhenInvokeWriteError()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = "Write-Error 'hello'" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogError(
                    file: "<No file>",
                    lineNumber: 1,
                    message: "hello" + Environment.NewLine +
                             "at <ScriptBlock>, <No file>: line 1"));
        }
        public void WhenInvokeWriteWarning()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = "Write-Warning 'hello'" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogWarning("hello"));
        }
Example #15
0
        public void WhenRelativeFilePathIsSpecified()
        {
            var fileSystem = new Mock<IFileSystem>();
            fileSystem.Setup(x => x.FileExists(It.IsAny<string>())).Returns((string path) => true);
            fileSystem.Setup(x => x.GetFullPath(It.IsAny<string>())).Returns((string path) => Path.Combine(@"C:\", path));

            var commandProvider = new Mock<IPowerShellCommandParameterProvider>();

            var command = new CommandFactory(fileSystem: fileSystem.Object)
            {
                File = @"test.ps1"
            }
            .CreateCommand(commandProvider.Object);
            Assert.IsTrue(command.IsScript);
            Assert.AreEqual(@"& 'C:\test.ps1'", command.CommandText);
        }
        public void WhenInvokingScriptFileThatHasNoDefaultParameterSetWithAutoArguments()
        {
            var buildTaskLog = new MockBuildTaskLog();
            var scriptFilePath = GetTestResourceFilePath("WhenInvokingScriptFileThatHasNoDefaultParameterSetWithAutoArguments.ps1");

            var commandFactory = new CommandFactory
            {
                File = scriptFilePath,
                AutoParameters = new ITaskItem[]
                {
                    new MockPropertyTaskItem(name: "Arg1", value: "foo"),
                    new MockPropertyTaskItem(name: "Arg2", value: "bar"),
                }
            };

            var exception = Assert.Throws<InvalidOperationException>(() => InvokePowerShell.Execute(commandFactory, buildTaskLog));
            Assert.AreEqual(scriptFilePath + " does not have a default parameter set.",  exception.Message);
        }
        public void WhenInvokingCommandLineApp()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = "& cmd /c echo hi" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("hi", MessageImportance.High));
        }
        public void WhenInvokeWriteWarningWithExplicitFilenameLineAndColumn()
        {
            var buildTaskLog = new MockBuildTaskLog();

            var commandFactory = new CommandFactory { Expression = @"Write-Warning 'c:\foo\bar.txt(123,456) : This is a test'" };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogWarning(
                    file: @"c:\foo\bar.txt",
                    lineNumber: 123,
                    columnNumber: 456,
                    message: @"This is a test"));
        }
        public void WhenObjectsFallOffPipeline()
        {
            var buildTaskLog = new MockBuildTaskLog();

            const string script = @"
            'hi'
            'there'
            1
            Write-Error boom
            @()
            @{ foo = 'bar' } | Format-Table -AutoSize
            ";

            var commandFactory = new CommandFactory { Expression = script };
            InvokePowerShell.Execute(commandFactory, buildTaskLog);

            buildTaskLog.AssertLogEntriesAre(
                new LogMessage("hi", MessageImportance.High),
                new LogMessage("there", MessageImportance.High),
                new LogMessage("1", MessageImportance.High),
                new LogError(
                    file: "<No file>",
                    lineNumber: 5,
                    message: "boom" + Environment.NewLine +
                             "at <ScriptBlock>, <No file>: line 5"),
                new LogMessage(messageImportance: MessageImportance.High, message: @"
            Name Value
            ---- -----
            foo  bar

            "));
        }
Example #20
0
        public void WhenFileIsSpecifiedWithArguments()
        {
            var fileSystem = new Mock<IFileSystem>();
            fileSystem.Setup(x => x.FileExists(It.IsAny<string>())).Returns((string path) => true);
            fileSystem.Setup(x => x.GetFullPath(It.IsAny<string>())).Returns((string path) => path);

            var commandProvider = new Mock<IPowerShellCommandParameterProvider>();

            var command = new CommandFactory(fileSystem: fileSystem.Object)
            {
                File = @"C:\test.ps1",
                Arguments = "-Arg1 foo -Arg2 bar"
            }
            .CreateCommand(commandProvider.Object);

            Assert.IsTrue(command.IsScript);
            Assert.AreEqual(@"& 'C:\test.ps1' -Arg1 foo -Arg2 bar", command.CommandText);
        }