From 89ebce93834792d1587872310dce521903455325 Mon Sep 17 00:00:00 2001 From: Nikita Bykov <49442273+nikita-bykov@users.noreply.github.com> Date: Tue, 8 Sep 2020 11:39:22 +0300 Subject: [PATCH 1/6] Fix win-vs-env.psm1 and pester-extensions.psm1 (#15) --- pester-extensions.psm1 | 15 ++++++++++----- win-vs-env.psm1 | 6 ++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pester-extensions.psm1 b/pester-extensions.psm1 index 6cec822..b5ab964 100644 --- a/pester-extensions.psm1 +++ b/pester-extensions.psm1 @@ -6,23 +6,28 @@ Pester extension that allows to run command and validate exit code #> function Get-CommandResult { - param ( + Param ( [Parameter(Mandatory=$true)] [string] $Command, [switch] $Multiline ) - # Bash trick to suppress and show error output because some commands write to stderr (for example, "python --version") - $stdout = & bash -c "$Command 2>&1" + # CMD trick to suppress and show error output because some commands write to stderr (for example, "python --version") + If ($IsWindows) { + [string[]]$output = & $env:comspec /c "$Command 2>&1" + } else { + $output = & bash -c "$Command 2>&1" + } $exitCode = $LASTEXITCODE + return @{ - Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout } + Output = If ($Multiline -eq $true) { $output } else { [string]$output } ExitCode = $exitCode } } function ShouldReturnZeroExitCode { Param( - [String] $ActualValue, + [string] $ActualValue, [switch] $Negate, [string] $Because # This parameter is unused by we need it to match Pester asserts signature ) diff --git a/win-vs-env.psm1 b/win-vs-env.psm1 index 1eb9093..e9f3d1a 100644 --- a/win-vs-env.psm1 +++ b/win-vs-env.psm1 @@ -3,7 +3,7 @@ ### function Get-VSWhere { - $vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"; + $vswhere = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" if (-not (Test-Path $vswhere )) { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 @@ -34,9 +34,11 @@ function Invoke-Environment } function Get-VSInstallationPath { + Write-Host "ProgramFiles(x86) - ${env:ProgramFiles(x86)}" $vswhere = Get-VSWhere + Write-Host "vswhere - $vswhere" $installationPath = & $vswhere -prerelease -legacy -latest -property installationPath - + Write-Host "installationPath - $installationPath" return $installationPath } From 94d8a3af833b23635cc9bc20b26729905122d6d8 Mon Sep 17 00:00:00 2001 From: Nikita Bykov Date: Tue, 8 Sep 2020 13:27:15 +0300 Subject: [PATCH 2/6] removed changes --- win-vs-env.psm1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/win-vs-env.psm1 b/win-vs-env.psm1 index e9f3d1a..1eb9093 100644 --- a/win-vs-env.psm1 +++ b/win-vs-env.psm1 @@ -3,7 +3,7 @@ ### function Get-VSWhere { - $vswhere = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" + $vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"; if (-not (Test-Path $vswhere )) { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 @@ -34,11 +34,9 @@ function Invoke-Environment } function Get-VSInstallationPath { - Write-Host "ProgramFiles(x86) - ${env:ProgramFiles(x86)}" $vswhere = Get-VSWhere - Write-Host "vswhere - $vswhere" $installationPath = & $vswhere -prerelease -legacy -latest -property installationPath - Write-Host "installationPath - $installationPath" + return $installationPath } From 34c6f7f0ec5312e8386f86b01e16438a6babbe77 Mon Sep 17 00:00:00 2001 From: Nikita Bykov Date: Tue, 8 Sep 2020 13:59:03 +0300 Subject: [PATCH 3/6] Resolved comments --- pester-extensions.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pester-extensions.psm1 b/pester-extensions.psm1 index b5ab964..b7607bd 100644 --- a/pester-extensions.psm1 +++ b/pester-extensions.psm1 @@ -11,9 +11,9 @@ function Get-CommandResult { [string] $Command, [switch] $Multiline ) - # CMD trick to suppress and show error output because some commands write to stderr (for example, "python --version") + # CMD and bash trick to suppress and show error output because some commands write to stderr (for example, "python --version") If ($IsWindows) { - [string[]]$output = & $env:comspec /c "$Command 2>&1" + $output = & $env:comspec /c "$Command 2>&1" } else { $output = & bash -c "$Command 2>&1" } @@ -27,7 +27,7 @@ function Get-CommandResult { function ShouldReturnZeroExitCode { Param( - [string] $ActualValue, + [String] $ActualValue, [switch] $Negate, [string] $Because # This parameter is unused by we need it to match Pester asserts signature ) From 7f90ceb82c058f9f2833cdeb37538ea360e54896 Mon Sep 17 00:00:00 2001 From: Nikita Bykov Date: Tue, 8 Sep 2020 14:00:33 +0300 Subject: [PATCH 4/6] fixed naming --- pester-extensions.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pester-extensions.psm1 b/pester-extensions.psm1 index b7607bd..1284581 100644 --- a/pester-extensions.psm1 +++ b/pester-extensions.psm1 @@ -13,14 +13,14 @@ function Get-CommandResult { ) # CMD and bash trick to suppress and show error output because some commands write to stderr (for example, "python --version") If ($IsWindows) { - $output = & $env:comspec /c "$Command 2>&1" + $stdout = & $env:comspec /c "$Command 2>&1" } else { - $output = & bash -c "$Command 2>&1" + $stdout = & bash -c "$Command 2>&1" } $exitCode = $LASTEXITCODE return @{ - Output = If ($Multiline -eq $true) { $output } else { [string]$output } + Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout } ExitCode = $exitCode } } From 3f2b84426737832b5ca2f6e4b29871391f527bb9 Mon Sep 17 00:00:00 2001 From: Nikita Bykov Date: Wed, 9 Sep 2020 12:18:45 +0300 Subject: [PATCH 5/6] added the function GetToolDirectory --- common-helpers.psm1 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/common-helpers.psm1 b/common-helpers.psm1 index 4d3d3f1..e3e27d5 100644 --- a/common-helpers.psm1 +++ b/common-helpers.psm1 @@ -83,3 +83,28 @@ function IsNixPlatform { return ($Platform -match "macos") -or ($Platform -match "darwin") -or ($Platform -match "ubuntu") -or ($Platform -match "linux") } + +<# +.SYNOPSIS +Get root directory of selected tool +#> +function GetToolDirectory { + param( + [Parameter(Mandatory=$true)] + [String]$ToolName, + [Parameter(Mandatory=$true)] + [String]$Version, + [Parameter(Mandatory=$true)] + [String]$Architecture + ) + $targetPath = $env:AGENT_TOOLSDIRECTORY + if ([string]::IsNullOrEmpty($targetPath)) { + # GitHub Windows images don't have `AGENT_TOOLSDIRECTORY` variable + $targetPath = $env:RUNNER_TOOL_CACHE + } + $ToolcachePath = Join-Path -Path $targetPath -ChildPath $ToolName + $ToolcacheVersionPath = Join-Path -Path $ToolcachePath -ChildPath $Version + $toolDirectory = Join-Path $ToolcacheVersionPath $Architecture + + return $toolDirectory +} From 5bcb80c333356d72c36b329c31d460d9403af56b Mon Sep 17 00:00:00 2001 From: Nikita Bykov Date: Wed, 9 Sep 2020 12:28:21 +0300 Subject: [PATCH 6/6] refactored function --- common-helpers.psm1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/common-helpers.psm1 b/common-helpers.psm1 index e3e27d5..fa54723 100644 --- a/common-helpers.psm1 +++ b/common-helpers.psm1 @@ -104,7 +104,5 @@ function GetToolDirectory { } $ToolcachePath = Join-Path -Path $targetPath -ChildPath $ToolName $ToolcacheVersionPath = Join-Path -Path $ToolcachePath -ChildPath $Version - $toolDirectory = Join-Path $ToolcacheVersionPath $Architecture - - return $toolDirectory + return Join-Path $ToolcacheVersionPath $Architecture }