Skip to content

Commit cb428a9

Browse files
muskankhediaMuskan Khedia
andauthored
Add support for Pwsh execution (#541)
* Add support for Pwsh execution * fix test cases * fix test case * fix * Fix * another try --------- Co-authored-by: Muskan Khedia <[email protected]>
1 parent 1d74242 commit cb428a9

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/VirtualClient/VirtualClient.Actions.UnitTests/ScriptExecutor/PowershellExecutorTests.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,25 @@ public void SetupTest(PlatformID platform = PlatformID.Win32NT)
132132
{ nameof(PowershellExecutor.CommandLine), "parameter1 parameter2" },
133133
{ nameof(PowershellExecutor.ScriptPath), "genericScript.ps1" },
134134
{ nameof(PowershellExecutor.LogPaths), "*.log;*.txt;*.json" },
135-
{ nameof(PowershellExecutor.ToolName), "GenericTool" }
135+
{ nameof(PowershellExecutor.ToolName), "GenericTool" },
136+
{ nameof(PowershellExecutor.UsePwsh), false }
136137
};
137138

138139
this.fixture.ProcessManager.OnCreateProcess = (command, arguments, directory) => this.fixture.Process;
139140
}
140141

141142
[Test]
142-
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", true)]
143-
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", false)]
143+
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", true, false, "powershell")]
144+
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", false, false, "powershell")]
145+
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", true, true, "pwsh")]
146+
[TestCase(PlatformID.Win32NT, @"\win-x64", @"genericScript.ps1", false, true, "pwsh")]
144147
[Platform(Exclude = "Unix,Linux,MacOsX")]
145-
public async Task PowershellExecutorExecutesTheCorrectWorkloadCommands(PlatformID platform, string platformSpecificPath, string command, bool runElevated)
148+
public async Task PowershellExecutorExecutesTheCorrectWorkloadCommands(PlatformID platform, string platformSpecificPath, string command, bool runElevated, bool usePwsh, string executorType)
146149
{
147150
this.SetupTest(platform);
148151
this.fixture.Parameters[nameof(PowershellExecutor.RunElevated)] = runElevated;
149152
this.fixture.Parameters[nameof(PowershellExecutor.ScriptPath)] = command;
153+
this.fixture.Parameters[nameof(PowershellExecutor.UsePwsh)] = usePwsh;
150154

151155
string fullCommand = $"{this.mockPackage.Path}{platformSpecificPath}\\{command} parameter1 parameter2";
152156

@@ -159,7 +163,7 @@ await executor.InitializeAsync(EventContext.None, CancellationToken.None)
159163

160164
string workingDirectory = executor.ExecutableDirectory;
161165

162-
string expectedCommand = $"powershell -ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \"cd '{workingDirectory}';{fullCommand}\"";
166+
string expectedCommand = $"{executorType} -ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \"cd '{workingDirectory}';{fullCommand}\"";
163167
this.fixture.ProcessManager.OnCreateProcess = (exe, arguments, workingDirectory) =>
164168
{
165169
if(expectedCommand == $"{exe} {arguments}")

src/VirtualClient/VirtualClient.Actions/ScriptExecutor/PowershellExecutor.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace VirtualClient.Actions
99
using System.Threading.Tasks;
1010
using Microsoft.Extensions.DependencyInjection;
1111
using VirtualClient.Common;
12+
using VirtualClient.Common.Extensions;
1213
using VirtualClient.Common.Telemetry;
1314

1415
/// <summary>
@@ -26,14 +27,25 @@ public PowershellExecutor(IServiceCollection dependencies, IDictionary<string, I
2627
{
2728
}
2829

30+
/// <summary>
31+
/// The parameter specifies whether to use pwsh, by default it is false
32+
/// </summary>
33+
public bool UsePwsh
34+
{
35+
get
36+
{
37+
return this.Parameters.GetValue<bool>(nameof(this.UsePwsh), false);
38+
}
39+
}
40+
2941
/// <summary>
3042
/// Executes the PowerShell script.
3143
/// </summary>
3244
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
3345
{
3446
using (BackgroundOperations profiling = BackgroundOperations.BeginProfiling(this, cancellationToken))
3547
{
36-
string command = "powershell";
48+
string command = this.UsePwsh ? "pwsh" : "powershell";
3749
string commandArguments = SensitiveData.ObscureSecrets(
3850
$"-ExecutionPolicy Bypass -NoProfile -NonInteractive -WindowStyle Hidden -Command \"cd '{this.ExecutableDirectory}';{this.ExecutablePath} {this.CommandLine}\"");
3951

src/VirtualClient/VirtualClient.Contracts.UnitTests/VirtualClientComponentExtensionsTests.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33

44
namespace VirtualClient.Contracts
55
{
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.DependencyInjection.Extensions;
8+
using Moq;
9+
using NUnit.Framework;
610
using System;
711
using System.Collections.Generic;
812
using System.Diagnostics;
913
using System.IO;
1014
using System.Linq;
1115
using System.Runtime.InteropServices;
16+
using System.Runtime.Versioning;
1217
using System.Text;
1318
using System.Threading;
1419
using System.Threading.Tasks;
15-
using Microsoft.Extensions.DependencyInjection;
16-
using Microsoft.Extensions.DependencyInjection.Extensions;
17-
using Moq;
18-
using NUnit.Framework;
1920
using VirtualClient.Common.Contracts;
2021
using VirtualClient.Common.Extensions;
2122
using VirtualClient.Common.Telemetry;
@@ -252,7 +253,7 @@ public void CombineExtensionProducesTheExpectedPathOnUnixSystems()
252253
}
253254

254255
[Test]
255-
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_1()
256+
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_1()
256257
{
257258
this.fixture.Setup(PlatformID.Unix);
258259

@@ -297,7 +298,7 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript
297298
}
298299

299300
[Test]
300-
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_2()
301+
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnUnixSystems_2()
301302
{
302303
this.fixture.Setup(PlatformID.Unix);
303304

@@ -351,7 +352,8 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript
351352
}
352353

353354
[Test]
354-
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_1()
355+
[Platform(Include = "Win")]
356+
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_1()
355357
{
356358
string directory = "C:\\Users\\User\\Logs";
357359
string[] expectedFiles = new string[]
@@ -394,7 +396,8 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript
394396
}
395397

396398
[Test]
397-
public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_2()
399+
[Platform(Include = "Win")]
400+
public void CreateFileUploadDescriptorsExtensionCreatesTheExpectedDescriptorsOnWindowsSystems_2()
398401
{
399402
string directory = "C:\\Users\\User\\Logs";
400403
string[] expectedFiles = new string[]
@@ -406,7 +409,7 @@ public void CreateCreateFileUploadDescriptorsExtensionCreatesTheExpectedDescript
406409

407410
this.fixture.FileSystem
408411
.Setup(fs => fs.Path.GetDirectoryName(It.IsAny<string>()))
409-
.Returns<string>(file => file.Replace(Path.GetFileName(file), string.Empty));
412+
.Returns<string>(file => Path.GetDirectoryName(file));
410413

411414
this.fixture.FileSystem
412415
.Setup(fs => fs.Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories))

website/docs/guides/0221-usage-extensions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ This component can be used to execute generic scripts using facilities common to
568568
| PackageName | Name of the workload package built for running the script. If the workload package is being downloaded from blob package store, this needs to match with the package name defined in DependencyPackageInstallation. | String |
569569
| FailFast | Flag indicates that the application should exit immediately on first/any errors regardless of their severity. | Boolean |
570570
| UsePython3 | (Only valid for PythonExecutor) A true value indicates use of "python3" as environment variable to execute python, a false value will use "python" as the environment variable. | Boolean (Default is true) |
571+
| UsePwsh | (Only valid for PowershellExecutor) A true value indicates use of "pwsh", a false value will use "powershell" as the command executor. | Boolean (Default is false) |
571572

572573
``` json
573574
Example:

0 commit comments

Comments
 (0)