Skip to content

Commit ac707f8

Browse files
bergmeisterJamesWTruher
authored andcommitted
Move common test code into AppVeyor module (#961)
* move test code of appveyor build into its own function to expose test differences * remove setting erroraction in appveyor module to have the same behaviour as in yaml * fix tests due to scoping bug in PowerShell * fix test due to powershell bug wherby $error is not defined in the local scope when being executed inside a module * another similar test fix (same as before) and restore erroractionpreference in module again
1 parent 83af8ed commit ac707f8

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Describe "Test RuleExtension" {
142142
}
143143
catch
144144
{
145-
$Error[0].FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand"
145+
$_.FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.GetScriptAnalyzerRuleCommand"
146146
}
147147
}
148148

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ Describe "Test CustomizedRulePath" {
477477
{
478478
if (-not $testingLibraryUsage)
479479
{
480-
$Error[0].FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand"
480+
$_.FullyQualifiedErrorId | Should -Match "PathNotFound,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand"
481481
}
482482
}
483483
}

Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ Describe "AvoidAssignmentToAutomaticVariables" {
5959
{
6060
try
6161
{
62-
Set-Variable -Name $VariableName -Value 'foo' -ErrorVariable errorVariable -ErrorAction Stop
62+
# Global scope has to be used due to a bug in PS. https://github.com/PowerShell/PowerShell/issues/6378
63+
Set-Variable -Name $VariableName -Value 'foo' -ErrorVariable errorVariable -ErrorAction Stop -Scope Global
6364
throw "Expected exception did not occur when assigning value to read-only variable '$VariableName'"
6465
}
6566
catch

appveyor.yml

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,15 @@ build_script:
4747
Invoke-AppveyorBuild -CheckoutPath $env:APPVEYOR_BUILD_FOLDER -BuildConfiguration $env:BuildConfiguration -BuildType 'NetStandard'
4848
}
4949
50-
# Test scripts are not in a module function because the tests behave differently for unknown reasons in AppVeyor
5150
test_script:
5251
- ps: |
5352
if ($env:PowerShellEdition -eq 'WindowsPowerShell') {
54-
$modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1
55-
Copy-Item "${env:APPVEYOR_BUILD_FOLDER}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force
56-
$testResultsFile = ".\TestResults.xml"
57-
$testScripts = "${env:APPVEYOR_BUILD_FOLDER}\Tests\Engine","${env:APPVEYOR_BUILD_FOLDER}\Tests\Rules"
58-
$testResults = Invoke-Pester -Script $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru
59-
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/${env:APPVEYOR_JOB_ID}", (Resolve-Path $testResultsFile))
60-
if ($testResults.FailedCount -gt 0) {
61-
throw "$($testResults.FailedCount) tests failed."
62-
}
53+
Invoke-AppveyorTest -CheckoutPath $env:APPVEYOR_BUILD_FOLDER
6354
}
6455
- pwsh: |
6556
if ($env:PowerShellEdition -eq 'PowerShellCore') {
66-
$modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1
67-
Copy-Item "${env:APPVEYOR_BUILD_FOLDER}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force
68-
$testResultsFile = ".\TestResults.xml"
69-
$testScripts = "${env:APPVEYOR_BUILD_FOLDER}\Tests\Engine","${env:APPVEYOR_BUILD_FOLDER}\Tests\Rules"
70-
$testResults = Invoke-Pester -Script $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru
71-
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/${env:APPVEYOR_JOB_ID}", (Resolve-Path $testResultsFile))
72-
if ($testResults.FailedCount -gt 0) {
73-
throw "$($testResults.FailedCount) tests failed."
74-
}
57+
Import-Module .\tools\appveyor.psm1 # Appveyor does not persist pwsh sessions like it does for ps
58+
Invoke-AppveyorTest -CheckoutPath $env:APPVEYOR_BUILD_FOLDER
7559
}
7660
7761
# Upload the project along with test results as a zip archive

tools/appveyor.psm1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ function Invoke-AppVeyorBuild {
6767
Pop-Location
6868
}
6969

70+
# Implements AppVeyor 'test_script' step
71+
function Invoke-AppveyorTest {
72+
Param(
73+
[Parameter(Mandatory)]
74+
[ValidateScript( {Test-Path $_})]
75+
$CheckoutPath
76+
)
77+
78+
$modulePath = $env:PSModulePath.Split([System.IO.Path]::PathSeparator) | Where-Object { Test-Path $_} | Select-Object -First 1
79+
Copy-Item "${CheckoutPath}\out\PSScriptAnalyzer" "$modulePath\" -Recurse -Force
80+
$testResultsFile = ".\TestResults.xml"
81+
$testScripts = "${CheckoutPath}\Tests\Engine","${CheckoutPath}\Tests\Rules"
82+
$testResults = Invoke-Pester -Script $testScripts -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru
83+
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/${env:APPVEYOR_JOB_ID}", (Resolve-Path $testResultsFile))
84+
if ($testResults.FailedCount -gt 0) {
85+
throw "$($testResults.FailedCount) tests failed."
86+
}
87+
}
88+
7089
# Implements AppVeyor 'on_finish' step
7190
function Invoke-AppveyorFinish {
7291
$stagingDirectory = (Resolve-Path ..).Path

0 commit comments

Comments
 (0)