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" } } 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/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 diff --git a/appveyor.yml b/appveyor.yml index 5c6fde606..37beddc7e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -47,31 +47,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 70ee9ec10..fca49b807 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