From 1fb8c5d6a187c1b2ac276faecf0766f3210d0b58 Mon Sep 17 00:00:00 2001 From: Mikhail Timofeev <48208649+miketimofeev@users.noreply.github.com> Date: Mon, 21 Jun 2021 14:09:51 +0300 Subject: [PATCH] [Windows] Improve Pester tests coverage for MSYS2 (#3595) * Add ShouldReturnZeroExitCodeWithParam function and a new set of tests for MSYS2 tools * Change get-command -> Get-Command * Remove elevated user, return original path after msys installation and tests, resolve nitpicks * Add BeforeEach and AfterEach blocks These blocks change the path before each test and return the original path after each test * Change get-command -> Get-Command --- .../win/scripts/ImageHelpers/TestsHelpers.ps1 | 37 +++++++++ .../win/scripts/Installers/Install-Msys2.ps1 | 1 + images/win/scripts/Tests/MSYS2.Tests.ps1 | 81 ++++++++++++++++--- images/win/windows2016.json | 2 - images/win/windows2019.json | 2 - 5 files changed, 106 insertions(+), 17 deletions(-) diff --git a/images/win/scripts/ImageHelpers/TestsHelpers.ps1 b/images/win/scripts/ImageHelpers/TestsHelpers.ps1 index ec71faec846e..9cbfb1badb80 100644 --- a/images/win/scripts/ImageHelpers/TestsHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/TestsHelpers.ps1 @@ -96,6 +96,42 @@ function ShouldReturnZeroExitCode { } } +# Pester Assert to check exit code of command with given parameter, the assertion performed up to 3 checks (without '-', with 1 and 2 '-') until succeeded +function ShouldReturnZeroExitCodeWithParam { + param ( + [Parameter(Mandatory)] [string] $ActualValue, + [switch] $Negate, + [string] $CallParameter = "version", + [string] $CallerSessionState + ) + + $delimiterCharacter = "" + + while ($delimiterCharacter.Length -le 2) + { + $callParameterWithDelimeter = $delimiterCharacter + $CallParameter + $commandToCheck = "$ActualValue $callParameterWithDelimeter" + [bool]$succeeded = (ShouldReturnZeroExitCode -ActualValue $commandToCheck).Succeeded + + if ($succeeded) + { + break + } + $delimiterCharacter += '-' + } + if ($Negate) { $succeeded = -not $succeeded } + + if (-not $succeeded) + { + $failureMessage = "Tool '$ActualValue' has not returned 0 exit code for any of these flags: '$CallParameter' or '-$CallParameter' or '--$CallParameter'" + } + + return [PSCustomObject] @{ + Succeeded = $succeeded + FailureMessage = $failureMessage + } +} + # Pester Assert to match output of command function ShouldMatchCommandOutput { Param( @@ -130,5 +166,6 @@ function ShouldMatchCommandOutput { If (Get-Command -Name Add-ShouldOperator -ErrorAction SilentlyContinue) { Add-ShouldOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode} + Add-ShouldOperator -Name ReturnZeroExitCodeWithParam -InternalName ShouldReturnZeroExitCodeWithParam -Test ${function:ShouldReturnZeroExitCodeWithParam} Add-ShouldOperator -Name MatchCommandOutput -InternalName ShouldMatchCommandOutput -Test ${function:ShouldMatchCommandOutput} } \ No newline at end of file diff --git a/images/win/scripts/Installers/Install-Msys2.ps1 b/images/win/scripts/Installers/Install-Msys2.ps1 index 6aded0e1f07e..894653acc1a6 100644 --- a/images/win/scripts/Installers/Install-Msys2.ps1 +++ b/images/win/scripts/Installers/Install-Msys2.ps1 @@ -88,6 +88,7 @@ foreach ($arch in $archs) pacman.exe -Q | grep ^${arch}- } +$env:PATH = $origPath Write-Host "`nMSYS2 installation completed" Invoke-PesterTests -TestFile "MSYS2" diff --git a/images/win/scripts/Tests/MSYS2.Tests.ps1 b/images/win/scripts/Tests/MSYS2.Tests.ps1 index e0c6486d0f6f..1290dd1606c1 100644 --- a/images/win/scripts/Tests/MSYS2.Tests.ps1 +++ b/images/win/scripts/Tests/MSYS2.Tests.ps1 @@ -1,23 +1,78 @@ +$toolsetContent = (Get-ToolsetContent).MsysPackages +$archs = $toolsetContent.mingw.arch + BeforeAll { - $msys2BinDir = "C:\msys64\usr\bin" - $msys2mingwDir = "C:\msys64\mingw64\bin" + $msys2Dir = "C:\msys64\usr\bin" + $originalPath = $env:PATH } -Describe "MSYS2" { - It "" -TestCases @( +Describe "MSYS2 packages" { + BeforeEach { + $env:PATH = "$msys2Dir;$env:PATH" + } + + It "msys2Dir exists" { + $msys2Dir | Should -Exist + } + + $TestCases = @( @{ ToolName = "bash.exe" } @{ ToolName = "tar.exe" } @{ ToolName = "make.exe" } - ) { - Join-Path $msys2BinDir $ToolName | Should -Exist + ) + + It " is installed in " -TestCases $TestCases { + (Get-Command "$ToolName").Source | Should -BeLike "$msys2Dir*" + } + + It " is avaialable" -TestCases $TestCases { + "$ToolName" | Should -ReturnZeroExitCodeWithParam } - It "" -TestCases @( - @{ ToolName = "gcc.exe" } - @{ ToolName = "cmake.exe" } - @{ ToolName = "g++.exe" } - @{ ToolName = "clang-tidy.exe" } - ) { - Join-Path $msys2mingwDir $ToolName | Should -Exist + AfterEach { + $env:PATH = $originalPath + } +} + +foreach ($arch in $archs) { + Describe "$arch arch packages" { + $archPackages = $toolsetContent.mingw | Where-Object { $_.arch -eq $arch } + $tools = $archPackages.runtime_packages.name + + if ($arch -eq "mingw-w64-i686") + { + $ExecDir = "C:\msys64\mingw32\bin" + } + else + { + $ExecDir = "C:\msys64\mingw64\bin" + } + + foreach ($tool in $tools) { + Context "$tool package"{ + $executables = ($archPackages.runtime_packages | Where-Object { $_.name -eq $tool }).executables | ForEach-Object { + @{ + ExecName = $_ + ExecDir = $ExecDir + } + } + + BeforeEach { + $env:PATH = "$ExecDir;$env:PATH" + } + + It " is installed in " -TestCases $executables { + (Get-Command "$ExecName").Source | Should -BeLike "$ExecDir*" + } + + It " is available" -TestCases $executables { + "$ExecName" | Should -ReturnZeroExitCodeWithParam + } + + AfterEach { + $env:PATH = $originalPath + } + } + } } } \ No newline at end of file diff --git a/images/win/windows2016.json b/images/win/windows2016.json index 4c183f6ee86a..4259124d9d29 100644 --- a/images/win/windows2016.json +++ b/images/win/windows2016.json @@ -303,8 +303,6 @@ "environment_vars": [ "TOOLSET_JSON_PATH={{user `toolset_json_path`}}" ], - "elevated_user": "SYSTEM", - "elevated_password": "", "scripts": [ "{{ template_dir }}/scripts/Installers/Install-Msys2.ps1" ] diff --git a/images/win/windows2019.json b/images/win/windows2019.json index 8bb66f535b33..5c4a775871ac 100644 --- a/images/win/windows2019.json +++ b/images/win/windows2019.json @@ -274,8 +274,6 @@ "environment_vars": [ "TOOLSET_JSON_PATH={{user `toolset_json_path`}}" ], - "elevated_user": "SYSTEM", - "elevated_password": "", "scripts": [ "{{ template_dir }}/scripts/Installers/Install-Msys2.ps1" ]