Skip to content

Commit 1cd6aab

Browse files
committed
Fixed legacy PowerShell versus modern pwsh problems.
1 parent 30c749d commit 1cd6aab

5 files changed

Lines changed: 536 additions & 8 deletions

File tree

eng/pipelines/common/templates/steps/override-sni-version.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ steps:
1515
- task: PowerShell@2
1616
displayName: Add SNI Validation Feed in Nuget.config
1717
inputs:
18+
pwsh: true
1819
targetType: inline
1920
script: |
2021
Write-Host "SNI validation feed to use = ${{parameters.SNIValidationFeed}}"
2122
2223
# define file to update
2324
$NugetCfg = Join-Path -Path '.' -ChildPath 'NuGet.config'
24-
type $NugetCfg
25+
Get-Content $NugetCfg
2526
2627
# load content of xml from file defined above
2728
$xml = New-Object XML
@@ -44,10 +45,11 @@ steps:
4445
4546
# save the xml file
4647
$xml.Save($NugetCfg)
47-
type $NugetCfg
48+
Get-Content $NugetCfg
4849
- task: PowerShell@2
4950
displayName: Update SNI Version in Versions.props
5051
inputs:
52+
pwsh: true
5153
targetType: inline
5254
# TODO(https://sqlclientdrivers.visualstudio.com/ADO.Net/_workitems/edit/42204):
5355
# Package dependency versions have moved to Directory.Packages.props, so the below script no
@@ -57,7 +59,7 @@ steps:
5759
5860
# define file to update
5961
$PropsPath = Join-Path -Path '.' -ChildPath 'tools\props\Versions.props'
60-
type $PropsPath
62+
Get-Content $PropsPath
6163
6264
# load content of xml from file defined above
6365
$xml = New-Object XML
@@ -79,6 +81,6 @@ steps:
7981
8082
# save the xml file
8183
$xml.Save($PropsPath)
82-
type $PropsPath
84+
Get-Content $PropsPath
8385
- task: NuGetAuthenticate@1
8486
displayName: 'NuGet Authenticate with SNI Validation Feed'

eng/pipelines/onebranch/jobs/validate-symbols-job.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
- task: PowerShell@2
6464
displayName: Verify ${{ pkg.packageName }} on ${{ server.name }}
6565
inputs:
66+
pwsh: true
6667
filePath: eng/pipelines/onebranch/jobs/validate-symbols.ps1
6768
arguments: >
6869
-ArtifactPath "$(Pipeline.Workspace)\${{ pkg.artifactName }}"

eng/pipelines/onebranch/jobs/validate-symbols.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ param(
8080
Set-StrictMode -Version Latest
8181
$ErrorActionPreference = 'Stop'
8282

83-
# ── Extract the package (skip if already done) ────────────────────────────────
83+
# -- Extract the package (skip if already done) --------------------------------
8484

8585
$dllFullPath = Join-Path $ExtractPath $DllPath
8686

@@ -111,7 +111,7 @@ if (-not (Test-Path $dllFullPath)) {
111111
exit 1
112112
}
113113

114-
# ── Locate symchk.exe ────────────────────────────────────────────────────────
114+
# -- Locate symchk.exe ---------------------------------------------------------
115115

116116
$symchkCandidates = @(
117117
"${env:ProgramFiles(x86)}\Windows Kits\10\Debuggers\x64\symchk.exe"
@@ -131,7 +131,7 @@ if (-not $symchkPath) {
131131
exit 1
132132
}
133133

134-
# ── Verify symbols (with retries for publishing latency) ──────────────────────
134+
# -- Verify symbols (with retries for publishing latency) ----------------------
135135

136136
$dllLeaf = Split-Path $dllFullPath -Leaf
137137

@@ -146,7 +146,7 @@ $symchkArgs = @(
146146
)
147147

148148
for ($attempt = 1; $attempt -le $MaxRetries; $attempt++) {
149-
Write-Host "Attempt $attempt of $MaxRetries running: symchk $($symchkArgs -join ' ')"
149+
Write-Host "Attempt $attempt of $MaxRetries -- running: symchk $($symchkArgs -join ' ')"
150150
$output = & $symchkPath @symchkArgs 2>&1 | Out-String
151151
$symchkExit = $LASTEXITCODE
152152

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# OneBranch Pipeline Tests
2+
3+
This directory contains [Pester](https://pester.dev/) tests for the
4+
PowerShell scripts used by the OneBranch build and validation pipelines.
5+
6+
## Prerequisites
7+
8+
| Tool | Version | Install |
9+
| ---- | ------- | ------- |
10+
| PowerShell (pwsh) | 7.2+ | [Install PowerShell](https://learn.microsoft.com/powershell/scripting/install/installing-powershell) |
11+
| Pester | 5.x | `Install-Module Pester -Force -Scope CurrentUser -SkipPublisherCheck` |
12+
13+
## Running tests
14+
15+
From the repository root:
16+
17+
```powershell
18+
pwsh -c "Invoke-Pester ./eng/pipelines/onebranch/tests/ -Output Detailed"
19+
```
20+
21+
Or run a single test file:
22+
23+
```powershell
24+
pwsh -c "Invoke-Pester ./eng/pipelines/onebranch/tests/validate-symbols.Tests.ps1 -Output Detailed"
25+
```
26+
27+
## Writing tests
28+
29+
### File naming
30+
31+
Test files must follow Pester's naming convention:
32+
33+
```text
34+
<ScriptUnderTest>.Tests.ps1
35+
```
36+
37+
For example, tests for `jobs/validate-symbols.ps1` live in
38+
`tests/validate-symbols.Tests.ps1`.
39+
40+
### Locating the script under test
41+
42+
Since scripts live in sibling directories (`jobs/`, `steps/`, etc.),
43+
reference them relative to `$PSScriptRoot`:
44+
45+
```powershell
46+
BeforeAll {
47+
$Script:ScriptPath = Join-Path $PSScriptRoot '..' 'jobs' 'my-script.ps1'
48+
}
49+
```
50+
51+
### Testing scripts that use `exit`
52+
53+
Pipeline scripts typically use `exit` for control flow, which terminates
54+
the PowerShell host. To test exit codes, run the script as a **child
55+
process** via `Start-Process`:
56+
57+
```powershell
58+
$proc = Start-Process -FilePath 'pwsh' `
59+
-ArgumentList @('-NoProfile', '-NonInteractive', '-File', $scriptPath, <args...>) `
60+
-NoNewWindow -Wait -PassThru `
61+
-RedirectStandardOutput $stdoutFile `
62+
-RedirectStandardError $stderrFile
63+
64+
$proc.ExitCode | Should -Be 0
65+
```
66+
67+
### Mocking external tools
68+
69+
When a script calls an external tool (e.g. `symchk.exe`), create a
70+
patched copy of the script that replaces the tool path with a mock `.ps1`
71+
script. See `validate-symbols.Tests.ps1` for an example of this pattern.
72+
73+
## Test inventory
74+
75+
| Test file | Script under test | What it covers |
76+
| --------- | ----------------- | -------------- |
77+
| `validate-symbols.Tests.ps1` | `jobs/validate-symbols.ps1` | Syntax validation, package discovery/extraction, symchk detection, retry logic |

0 commit comments

Comments
 (0)