From 78bc1a4853c37f4eb101a3d1cf76bf706a7fcfef Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 4 Apr 2018 21:06:07 +0100 Subject: [PATCH 1/5] move test code of appveyor build into its own function to expose test differences --- appveyor.yml | 22 +++------------------- tools/appveyor.psm1 | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index ef8d68836..2d653f9f9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -33,31 +33,15 @@ build_script: Invoke-AppveyorBuild -CheckoutPath $env:APPVEYOR_BUILD_FOLDER -BuildConfiguration $env:BuildConfiguration -BuildType 'NetStandard' } -# Test scripts are not in a module function because the tests behave differently for unknown reasons in AppVeyor test_script: - ps: | if ($env:PowerShellEdition -eq 'WindowsPowerShell') { - $modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1 - Copy-Item "${env:APPVEYOR_BUILD_FOLDER}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force - $testResultsFile = ".\TestResults.xml" - $testScripts = "${env:APPVEYOR_BUILD_FOLDER}\Tests\Engine","${env:APPVEYOR_BUILD_FOLDER}\Tests\Rules" - $testResults = Invoke-Pester -Script $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru - (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/${env:APPVEYOR_JOB_ID}", (Resolve-Path $testResultsFile)) - if ($testResults.FailedCount -gt 0) { - throw "$($testResults.FailedCount) tests failed." - } + Invoke-AppveyorTest -CheckoutPath $env:APPVEYOR_BUILD_FOLDER } - pwsh: | if ($env:PowerShellEdition -eq 'PowerShellCore') { - $modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1 - Copy-Item "${env:APPVEYOR_BUILD_FOLDER}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force - $testResultsFile = ".\TestResults.xml" - $testScripts = "${env:APPVEYOR_BUILD_FOLDER}\Tests\Engine","${env:APPVEYOR_BUILD_FOLDER}\Tests\Rules" - $testResults = Invoke-Pester -Script $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru - (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/${env:APPVEYOR_JOB_ID}", (Resolve-Path $testResultsFile)) - if ($testResults.FailedCount -gt 0) { - throw "$($testResults.FailedCount) tests failed." - } + Import-Module .\tools\appveyor.psm1 # Appveyor does not persist pwsh sessions like it does for ps + Invoke-AppveyorTest -CheckoutPath $env:APPVEYOR_BUILD_FOLDER } # Upload the project along with test results as a zip archive diff --git a/tools/appveyor.psm1 b/tools/appveyor.psm1 index bbac9a0a2..8883f8cb9 100644 --- a/tools/appveyor.psm1 +++ b/tools/appveyor.psm1 @@ -67,6 +67,25 @@ function Invoke-AppVeyorBuild { Pop-Location } +# Implements AppVeyor 'test_script' step +function Invoke-AppveyorTest { + Param( + [Parameter(Mandatory)] + [ValidateScript( {Test-Path $_})] + $CheckoutPath + ) + + $modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1 + Copy-Item "${CheckoutPath}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force + $testResultsFile = ".\TestResults.xml" + $testScripts = "${CheckoutPath}\Tests\Engine","${CheckoutPath}\Tests\Rules" + $testResults = Invoke-Pester -Script $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru + (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/${env:APPVEYOR_JOB_ID}", (Resolve-Path $testResultsFile)) + if ($testResults.FailedCount -gt 0) { + throw "$($testResults.FailedCount) tests failed." + } +} + # Implements AppVeyor 'on_finish' step function Invoke-AppveyorFinish { $stagingDirectory = (Resolve-Path ..).Path From a3ffec6c04b8792114ac0676f25f2a6d5ee32a65 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 7 Apr 2018 09:16:38 +0100 Subject: [PATCH 2/5] remove setting erroraction in appveyor module to have the same behaviour as in yaml --- tools/appveyor.psm1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/appveyor.psm1 b/tools/appveyor.psm1 index fca49b807..1f9b23030 100644 --- a/tools/appveyor.psm1 +++ b/tools/appveyor.psm1 @@ -1,8 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -$ErrorActionPreference = 'Stop' - # Implements the AppVeyor 'install' step and installs the required versions of Pester, platyPS and the .Net Core SDK if needed. function Invoke-AppVeyorInstall { $requiredPesterVersion = '4.3.1' From b9d4a36d1988f73b675a9b944d071729c087c934 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 7 Apr 2018 17:58:08 +0100 Subject: [PATCH 3/5] fix tests due to scoping bug in PowerShell --- Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 index 79a161d61..7f2e5e2ed 100644 --- a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 +++ b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 @@ -59,7 +59,8 @@ Describe "AvoidAssignmentToAutomaticVariables" { { try { - Set-Variable -Name $VariableName -Value 'foo' -ErrorVariable errorVariable -ErrorAction Stop + # Global scope has to be used due to a bug in PS. https://github.com/PowerShell/PowerShell/issues/6378 + Set-Variable -Name $VariableName -Value 'foo' -ErrorVariable errorVariable -ErrorAction Stop -Scope Global throw "Expected exception did not occur when assigning value to read-only variable '$VariableName'" } catch From e78122a9b38e5536e34c2232084b50b3fb6655ee Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 7 Apr 2018 18:22:32 +0100 Subject: [PATCH 4/5] fix test due to powershell bug wherby $error is not defined in the local scope when being executed inside a module --- Tests/Engine/GetScriptAnalyzerRule.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 index f8d80af43..4630cc8ff 100644 --- a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 +++ b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 @@ -142,7 +142,7 @@ Describe "Test RuleExtension" { } catch { - $Error[0].FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand" + $_.FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand" } } From c2c12e50153c5d0349b66feac18d5ef199494d98 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 7 Apr 2018 18:34:42 +0100 Subject: [PATCH 5/5] another similar test fix (same as before) and restore erroractionpreference in module again --- Tests/Engine/InvokeScriptAnalyzer.tests.ps1 | 2 +- tools/appveyor.psm1 | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index 31b3fdecf..c6e5b9b2a 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -477,7 +477,7 @@ Describe "Test CustomizedRulePath" { { if (-not $testingLibraryUsage) { - $Error[0].FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand" + $_.FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand" } } } diff --git a/tools/appveyor.psm1 b/tools/appveyor.psm1 index 1f9b23030..fca49b807 100644 --- a/tools/appveyor.psm1 +++ b/tools/appveyor.psm1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +$ErrorActionPreference = 'Stop' + # Implements the AppVeyor 'install' step and installs the required versions of Pester, platyPS and the .Net Core SDK if needed. function Invoke-AppVeyorInstall { $requiredPesterVersion = '4.3.1'