|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Management.Automation; |
| 6 | +using System.Management.Automation.Runspaces; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace UnitTests |
| 12 | +{ |
| 13 | + public class PowerShellTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void TestBasic() |
| 17 | + { |
| 18 | + using var ps = PowerShell.Create(); |
| 19 | + ps.AddCommand("Get-Date"); |
| 20 | + var rs = ps.Invoke(); |
| 21 | + Assert.NotEmpty(rs); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void TestLaunchNotepad() |
| 26 | + { |
| 27 | + using var ps = PowerShell.Create(); |
| 28 | + ps.AddCommand("Start-Process").AddArgument("notepad").AddParameter("-passthru");//.AddParameter("-Wait"); |
| 29 | + var rs = ps.Invoke(); |
| 30 | + Assert.NotEmpty(rs); |
| 31 | + Process process = rs[0].BaseObject as Process; |
| 32 | + |
| 33 | + using var ps2 = PowerShell.Create(); |
| 34 | + ps2.AddCommand("Stop-Process").AddParameter("-Id", process.Id); |
| 35 | + ps2.Invoke(); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void TestCopyItemWithArguments() |
| 40 | + { |
| 41 | + using var ps = PowerShell.Create(); |
| 42 | + ps.AddCommand("Copy-Item").AddArgument("../../../PowerShellTests.cs").AddArgument("./_PowerShellTests.cs"); // argument cannot have space |
| 43 | + var rs = ps.Invoke(); |
| 44 | + Assert.Empty(rs); |
| 45 | + Assert.False(ps.HadErrors); |
| 46 | + Assert.Empty(ps.Streams.Error); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void TestCopyItemWithParameters() |
| 51 | + { |
| 52 | + using var ps = PowerShell.Create(); |
| 53 | + ps.AddCommand("Copy-Item").AddParameter("-Path", "../../../PowerShellTests.cs").AddParameter("-Destination", "./CopiedPowerShellTests.cs"); |
| 54 | + var rs = ps.Invoke(); |
| 55 | + Assert.Empty(rs); |
| 56 | + Assert.False(ps.HadErrors); |
| 57 | + Assert.Empty(ps.Streams.Error); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void TestCopyItemNotExists() |
| 62 | + { |
| 63 | + using var ps = PowerShell.Create(); |
| 64 | + ps.AddCommand("Copy-Item").AddParameter("-Path", "../../../PowerShellKKK.cs").AddParameter("-Destination", "./CopiedPowerShellTests.cs"); |
| 65 | + var rs = ps.Invoke(); |
| 66 | + Assert.Empty(rs); |
| 67 | + Assert.True(ps.HadErrors); |
| 68 | + Assert.NotEmpty(ps.Streams.Error); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void TestCopyItemWithScript() |
| 73 | + { |
| 74 | + using var ps = PowerShell.Create(); |
| 75 | + ps.AddScript("Copy-Item ../../../PowerShellTests.cs ./A_PowerShellTests.cs"); |
| 76 | + var rs = ps.Invoke(); |
| 77 | + Assert.Empty(rs); |
| 78 | + Assert.False(ps.HadErrors); |
| 79 | + Assert.Empty(ps.Streams.Error); |
| 80 | + } |
| 81 | + |
| 82 | + //[Fact] |
| 83 | + //public void TestCopyItemWithStartJobThrowsError() |
| 84 | + //{ |
| 85 | + // using var ps = PowerShell.Create(); |
| 86 | + // ps.AddScript("Start-ThreadJob -ScriptBlock {Copy-Item ../../../PowerShellTests.cs ./A_PowerShellTests.cs}"); |
| 87 | + // var rs = ps.Invoke(); |
| 88 | + // Assert.Empty(rs); |
| 89 | + // Assert.True(ps.HadErrors); //not supported as of .NET 8, Microsoft.PowerShell.SDK v7.4.3 |
| 90 | + // //Assert.Empty(ps.Streams.Error); |
| 91 | + //} |
| 92 | + |
| 93 | + //[Fact] |
| 94 | + //public void TestThreadJobThrowsError()//not supported as of .NET 8, Microsoft.PowerShell.SDK v7.4.3 |
| 95 | + //{ |
| 96 | + // using var ps = PowerShell.Create(); |
| 97 | + // ps.AddCommand("Import-Module").AddArgument("ThreadJob").AddCommand("Start-ThreadJob") |
| 98 | + // .AddParameter("-ScriptBlock", "{Copy-Item ../../../PowerShellTests.cs ./A_PowerShellTests.cs}"); |
| 99 | + // var rs = ps.Invoke(); |
| 100 | + // //Assert.Empty(rs); |
| 101 | + // //Assert.True(ps.HadErrors); |
| 102 | + // //Assert.NotEmpty(ps.Streams.Error); |
| 103 | + //} |
| 104 | + |
| 105 | + //[Fact] |
| 106 | + //public void TestThreadJobThrows()//not supported as of .NET 8, Microsoft.PowerShell.SDK v7.4.3 |
| 107 | + //{ |
| 108 | + // using Runspace runSpace = RunspaceFactory.CreateRunspace(); |
| 109 | + // runSpace.Open(); |
| 110 | + // var pipeline = runSpace.CreatePipeline(); |
| 111 | + // pipeline.Commands.Add("Import-Module"); |
| 112 | + // pipeline.Commands[0].Parameters.Add("-Name", "ThreadJob"); |
| 113 | + // pipeline.Invoke(); |
| 114 | + // using var ps = PowerShell.Create(runSpace); |
| 115 | + // ps.AddCommand("Start-ThreadJob") |
| 116 | + // .AddParameter("-ScriptBlock", "{Copy-Item ../../../PowerShellTests.cs ./A_PowerShellTests.cs}"); |
| 117 | + // var rs = ps.Invoke(); |
| 118 | + // //Assert.Empty(rs); |
| 119 | + // //Assert.True(ps.HadErrors); |
| 120 | + // //Assert.NotEmpty(ps.Streams.Error); |
| 121 | + //} |
| 122 | + |
| 123 | + |
| 124 | + } |
| 125 | +} |
0 commit comments