Skip to content

Commit d1d82d0

Browse files
author
Sergei Vorobev
committed
Make Invoke-AppVeyorTest run elevated and non-elevated tests separately
- Add RequireAdminOnWindows to the list of blessed tags - Fix a bug in Get-PesterTag where we accepted 'Slow' as a proper priority - Fix missed comma in Start-PSPester parameters - Add documentation about new Pester tag - Fix finishing logic for Start-PSPester -Unelevate
1 parent 339c7d8 commit d1d82d0

3 files changed

Lines changed: 54 additions & 26 deletions

File tree

build.psm1

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -545,13 +545,19 @@ function Get-PesterTag {
545545
$warnings += "TAGS must be static strings, error in ${fullname}, line $lineno"
546546
}
547547
$values = $vAst.FindAll({$args[0] -is "System.Management.Automation.Language.StringConstantExpressionAst"},$true).Value
548-
$values | %{
549-
if ( $_ -notmatch "CI|FEATURE|SCENARIO|SLOW" ) {
548+
$values | % {
549+
if (@('REQUIREADMINONWINDOWS', 'SLOW') -contains $_) {
550+
# These are valid tags also, but they are not the priority tags
551+
}
552+
elseif (@('CI', 'FEATURE', 'SCENARIO') -contains $_) {
553+
$foundTag = $true
554+
}
555+
else {
550556
$warnings += "${fullname} includes improper tag '$_', line '$lineno'"
551557
}
558+
552559
$alltags[$_]++
553-
}
554-
$foundTag = $true
560+
}
555561
}
556562
}
557563
if ( ! $foundTag ) {
@@ -598,7 +604,7 @@ function Start-PSPester {
598604
[switch]$FullCLR,
599605
[string]$binDir = (Split-Path (New-PSOptions -FullCLR:$FullCLR).Output),
600606
[string]$powershell = (Join-Path $binDir 'powershell'),
601-
[string]$Pester = ([IO.Path]::Combine($binDir, "Modules", "Pester"))
607+
[string]$Pester = ([IO.Path]::Combine($binDir, "Modules", "Pester")),
602608
[switch]$Unelevate
603609
)
604610

@@ -662,7 +668,7 @@ function Start-PSPester {
662668
$Command += "'" + $Path + "'"
663669
if ($Unelevate)
664670
{
665-
$Command += "; Stop-Transcript; '__THE_END__' > $outputBufferFilePath"
671+
$Command += "; Stop-Transcript; '__UNELEVATED_TESTS_THE_END__' >> $outputBufferFilePath"
666672
}
667673

668674
Write-Verbose $Command
@@ -682,15 +688,13 @@ function Start-PSPester {
682688
while ($true)
683689
{
684690
$lines = Get-Content $outputBufferFilePath | Select-Object -Skip $currentLines
685-
$count = ($lines | measure-object).Count
686-
# Write-Verbose "Read $count lines"
687-
$line = $lines | Select-Object -Last 1
688-
if ($line -eq '__THE_END__')
691+
$lines | Write-Host
692+
if ($lines | ? { $_ -eq '__UNELEVATED_TESTS_THE_END__'})
689693
{
690694
break
691695
}
692696

693-
$lines | Write-Host
697+
$count = ($lines | measure-object).Count
694698
if ($count -eq 0)
695699
{
696700
sleep 1

docs/testing-guidelines/WritingPesterTests.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ Provides logical grouping of It blocks within a single Describe block. Any Mocks
8282
#### It
8383
The It block is intended to be used inside of a Describe or Context Block. If you are familiar with the AAA pattern (Arrange-Act-Assert), the body of the It block is the appropriate location for an assert. The convention is to assert a single expectation for each It block. The code inside of the It block should throw a terminating error if the expectation of the test is not met and thus cause the test to fail. The name of the It block should expressively state the expectation of the test.
8484

85+
### Admin privileges in tests
86+
Tests that require admin privileges **on windows** should be additionally marked with 'RequireAdminOnWindows' Pester tag.
87+
In the AppVeyor CI, we run two different passes:
88+
89+
- The pass with exclusion of tests with 'RequireAdminOnWindows' tag
90+
- The pass where we run only 'RequireAdminOnWindows' tests
91+
92+
In each case, tests are executed with appropriate privileges.
93+
8594
### Selected Features
8695

8796
#### Test Drive

tools/appveyor.psm1

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,48 +173,63 @@ function Invoke-AppVeyorTest
173173

174174
$env:CoreOutput = Split-Path -Parent (Get-PSOutput -Options (New-PSOptions -Publish -Configuration $buildConfiguration))
175175
Write-Host -Foreground Green 'Run CoreCLR tests'
176-
$testResultsFile = "$pwd\TestsResults.xml"
176+
$testResultsNonAdminFile = "$pwd\TestsResultsNonAdmin.xml"
177+
$testResultsAdminFile = "$pwd\TestsResultsAdmin.xml"
178+
$testResultsFileFullCLR = "$pwd\TestsResults.FullCLR.xml"
177179
if(!(Test-Path "$env:CoreOutput\powershell.exe"))
178180
{
179181
throw "CoreCLR PowerShell.exe was not built"
180182
}
181183

182184
$coreClrTestParams = @{
183-
Tag = @('CI')
184-
ExcludeTag = @('Slow')
185+
Tag = @()
186+
ExcludeTag = @()
187+
Unelevate = $true
185188
}
186189

187-
if(Test-DailyBuild)
190+
if(-not (Test-DailyBuild))
188191
{
189-
Write-Host -Foreground Green 'Running all CorCLR tests..'
190-
$coreClrTestParams.Tag = $null
191-
$coreClrTestParams.ExcludeTag = $null
192+
# Pester doesn't allow Invoke-Pester -TagAll@('CI', 'RequireAdminOnWindows') currently
193+
# https://github.com/pester/Pester/issues/608
194+
# To work-around it, we exlude all categories, but 'CI' from the list
195+
$coreClrTestParams.ExcludeTag += @('Slow', 'Feature', 'Scenario')
196+
Write-Host -Foreground Green 'Running "CI" CoreCLR tests..'
192197
}
193198
else
194199
{
195-
Write-Host -Foreground Green 'Running "CI" CorCLR tests..'
200+
Write-Host -Foreground Green 'Running all CoreCLR tests..'
196201
}
197202

198-
Start-PSPester -bindir $env:CoreOutput -outputFile $testResultsFile @coreClrTestParams
199-
Write-Host -Foreground Green 'Upload CoreCLR test results'
200-
Update-AppVeyorTestResults -resultsFile $testResultsFile
203+
Start-PSPester -bindir $env:CoreOutput -outputFile $testResultsNonAdminFile @coreClrTestParams
204+
Write-Host -Foreground Green 'Upload CoreCLR Non-Admin test results'
205+
Update-AppVeyorTestResults -resultsFile $testResultsNonAdminFile
206+
207+
$coreClrTestParams.Tag += 'RequireAdminOnWindows'
208+
$coreClrTestParams.Unelevate = $false
209+
210+
Start-PSPester -bindir $env:CoreOutput -outputFile $testResultsAdminFile @coreClrTestParams
211+
Write-Host -Foreground Green 'Upload CoreCLR Admin test results'
212+
Update-AppVeyorTestResults -resultsFile $testResultsAdminFile
201213

202214
#
203215
# FullCLR
204216
$env:FullOutput = Split-Path -Parent (Get-PSOutput -Options (New-PSOptions -FullCLR))
205217
Write-Host -Foreground Green 'Run FullCLR tests'
206-
$testResultsFileFullCLR = "$pwd\TestsResults.FullCLR.xml"
207218
Start-PSPester -FullCLR -bindir $env:FullOutput -outputFile $testResultsFileFullCLR -Tag $null -path 'test/fullCLR'
208219

209220
Write-Host -Foreground Green 'Upload FullCLR test results'
210221
Update-AppVeyorTestResults -resultsFile $testResultsFileFullCLR
211222

212-
213223
#
214224
# Fail the build, if tests failed
215-
Test-PSPesterResults -TestResultsFile $testResultsFile
225+
@(
226+
$testResultsNonAdminFile,
227+
$testResultsAdminFile,
228+
$testResultsFileFullCLR
229+
) | % {
230+
Test-PSPesterResults -TestResultsFile $_
231+
}
216232

217-
Test-PSPesterResults -TestResultsFile $testResultsFileFullCLR
218233
Set-BuildVariable -Name TestPassed -Value True
219234
}
220235

0 commit comments

Comments
 (0)