diff --git a/Tasks/AzurePowerShellV4/AzurePowerShell.ps1 b/Tasks/AzurePowerShellV4/AzurePowerShell.ps1 index a94665d36b07..318d59c03c60 100644 --- a/Tasks/AzurePowerShellV4/AzurePowerShell.ps1 +++ b/Tasks/AzurePowerShellV4/AzurePowerShell.ps1 @@ -10,7 +10,7 @@ $__vsts_input_errorActionPreference = Get-VstsInput -Name errorActionPreference $__vsts_input_failOnStandardError = Get-VstsInput -Name FailOnStandardError $targetAzurePs = Get-VstsInput -Name TargetAzurePs $customTargetAzurePs = Get-VstsInput -Name CustomTargetAzurePs -$input_pwsh = Get-VstsInput -Name 'pwsh' -AsBool +$input_pwsh = Get-VstsInput -Name 'pwsh' $input_workingDirectory = Get-VstsInput -Name 'workingDirectory' -Require # Validate the script path and args do not contains new-lines. Otherwise, it will diff --git a/Tasks/AzurePowerShellV4/Tests/ChecksForPowerShellCore.ps1 b/Tasks/AzurePowerShellV4/Tests/ChecksForPowerShellCore.ps1 new file mode 100644 index 000000000000..4ed3376b5ac6 --- /dev/null +++ b/Tasks/AzurePowerShellV4/Tests/ChecksForPowerShellCore.ps1 @@ -0,0 +1,28 @@ +[CmdletBinding()] +param() + +# Arrange. +. $PSScriptRoot\..\..\..\Tests\lib\Initialize-Test.ps1 +$targetAzurePs = "4.1.0" +Register-Mock Get-VstsInput { "FilePath" } -- -Name ScriptType -Require +Register-Mock Get-VstsInput { "$PSScriptRoot/PerformsBasicFlow_TargetScript.ps1" } -- -Name ScriptPath +Register-Mock Get-VstsInput { $targetAzurePs } -- -Name TargetAzurePs +Register-Mock Get-VstsInput { 'arg1 arg2' } -- -Name ScriptArguments +Register-Mock Get-VstsInput { "continue" } -- -Name errorActionPreference +Register-Mock Get-VstsInput { $true } -- -Name FailOnStandardError +Register-Mock Get-VstsInput { $true } -- -Name pwsh +Register-Mock Get-VstsInput { ([System.IO.Path]::GetTempPath()) } -- -Name workingDirectory +Register-Mock Update-PSModulePathForHostedAgent +Register-Mock Get-Module +Register-Mock Initialize-AzModule +Register-Mock Get-VstsEndpoint { @{auth = @{ scheme = "ServicePrincipal" }} } +Register-Mock Remove-EndpointSecrets +Register-Mock Disconnect-AzureAndClearContext +Register-Mock Assert-VstsPath +Register-Mock Invoke-VstsTool { } + +# Act. +$actual = & $PSScriptRoot\..\AzurePowerShell.ps1 + +Assert-WasCalled Invoke-VstsTool -Times 1 +Assert-WasCalled Invoke-VstsTool -ArgumentsEvaluator {$args.count -eq 6 -and $args[3] -like '*pwsh.exe*'} \ No newline at end of file diff --git a/Tasks/AzurePowerShellV4/Tests/L0.ts b/Tasks/AzurePowerShellV4/Tests/L0.ts index 52f4c31bb086..6f7546d09e32 100644 --- a/Tasks/AzurePowerShellV4/Tests/L0.ts +++ b/Tasks/AzurePowerShellV4/Tests/L0.ts @@ -27,12 +27,12 @@ describe('AzurePowerShell Suite', function () { }); if (psm.testSupported()) { + it('checks for powershell core', (done) => { + psr.run(path.join(__dirname, 'ChecksForPowerShellCore.ps1'), done); + }) it('performs basic flow', (done) => { psr.run(path.join(__dirname, 'PerformsBasicFlow.ps1'), done); }) - it('removes functions and variables', (done) => { - psr.run(path.join(__dirname, 'RemovesFunctionsAndVariables.ps1'), done); - }) it('throws when otherversion is specified in a wrong format', (done) => { psr.run(path.join(__dirname, 'ThrowsForInvalidVersion.ps1'), done); }) diff --git a/Tasks/AzurePowerShellV4/Tests/RemovesFunctionsAndVariables.ps1 b/Tasks/AzurePowerShellV4/Tests/RemovesFunctionsAndVariables.ps1 deleted file mode 100644 index 876235588bc7..000000000000 --- a/Tasks/AzurePowerShellV4/Tests/RemovesFunctionsAndVariables.ps1 +++ /dev/null @@ -1,41 +0,0 @@ -[CmdletBinding()] -param() - -# Arrange. -. $PSScriptRoot\..\..\..\Tests\lib\Initialize-Test.ps1 - -# Arrange the task inputs. -$targetAzurePs = "4.1.0" -Register-Mock Get-VstsInput { "FilePath" } -- -Name ScriptType -Require -Register-Mock Get-VstsInput { "$PSScriptRoot/RemovesFunctionsAndVariables_TargetScript.ps1" } -- -Name ScriptPath -Register-Mock Get-VstsInput { $targetAzurePs } -- -Name TargetAzurePs -Register-Mock Get-VstsInput { "continue" } -- -Name errorActionPreference -Register-Mock Get-VstsInput { $true } -- -Name FailOnStandardError -Register-Mock Update-PSModulePathForHostedAgent -Register-Mock Remove-EndpointSecrets -Register-Mock Disconnect-AzureAndClearContext -Register-Mock Get-VstsEndpoint -Register-Mock Assert-VstsPath -Register-Mock Invoke-VstsTool { } - -# Arrange the mock task SDK module. -New-Module -Name VstsTaskSdk -ScriptBlock { - function SomeVstsTaskSdkFunction1 { } - function SomeVstsTaskSdkFunction2 { } - function Out-Default { } -} -function Invoke-VstsTaskScript { } # Detached from the task SDK module -$null = Get-Item function:SomeVstsTaskSdkFunction1 # Sanity check to verify the function was imported. - -# Arrange the mock Azure helpers module. -Register-Mock Initialize-AzModule -Register-Mock Initialize-AzureRMModule -New-Module -Name VstsAzureHelpers_ -ScriptBlock { - function SomeAzureHelpersFunction1 { } - function SomeAzureHelpersFunction2 { } -} -$null = Get-Item function:SomeAzureHelpersFunction1 # Sanity check to verify the function was imported. - -# Act. -$actual = & $PSScriptRoot\..\AzurePowerShell.ps1 -$global:ErrorActionPreference = 'Stop' # Reset to stop. diff --git a/Tasks/AzurePowerShellV4/Tests/RemovesFunctionsAndVariables_TargetScript.ps1 b/Tasks/AzurePowerShellV4/Tests/RemovesFunctionsAndVariables_TargetScript.ps1 deleted file mode 100644 index c88c45cfcc2d..000000000000 --- a/Tasks/AzurePowerShellV4/Tests/RemovesFunctionsAndVariables_TargetScript.ps1 +++ /dev/null @@ -1,14 +0,0 @@ -# Return key information. -$result = New-Object psobject -Property @{ - FunctionNames = @{ } - VariableNames = @{ } -} -foreach ($function in (Get-ChildItem function:)) { - $result.FunctionNames[$function.Name] = $function.Name -} - -foreach ($variable in (Get-ChildItem variable:)) { - $result.VariableNames[$variable.Name] = $variable.Name -} - -$result \ No newline at end of file