|
1 | | -# Publish-Symbols Tests |
| 1 | +# OneBranch Script Tests |
2 | 2 |
|
3 | | -Pester tests for the `publish-symbols.ps1` script used by the symbol publishing pipeline step. |
| 3 | +This directory contains [Pester](https://pester.dev/) tests for PowerShell |
| 4 | +scripts used by the OneBranch pipelines. |
4 | 5 |
|
5 | 6 | ## Prerequisites |
6 | 7 |
|
7 | | -- PowerShell 5.1+ or PowerShell 7+ |
8 | | -- [Pester v5](https://pester.dev/) (`Install-Module Pester -MinimumVersion 5.0 -Scope CurrentUser`) |
| 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` | |
9 | 12 |
|
10 | | -## Running the Tests |
| 13 | +## Running tests |
11 | 14 |
|
12 | | -From this directory: |
| 15 | +From the repository root: |
13 | 16 |
|
14 | 17 | ```powershell |
15 | | -Invoke-Pester ./publish-symbols.Tests.ps1 |
| 18 | +pwsh -c "Invoke-Pester ./eng/pipelines/onebranch/scripts/tests/ -Output Detailed" |
16 | 19 | ``` |
17 | 20 |
|
18 | | -Or from the repository root: |
| 21 | +Run a single test file: |
19 | 22 |
|
20 | 23 | ```powershell |
21 | | -Invoke-Pester ./eng/pipelines/onebranch/scripts/tests/ |
| 24 | +pwsh -c "Invoke-Pester ./eng/pipelines/onebranch/scripts/tests/publish-symbols.Tests.ps1 -Output Detailed" |
| 25 | +pwsh -c "Invoke-Pester ./eng/pipelines/onebranch/scripts/tests/validate-symbols.Tests.ps1 -Output Detailed" |
22 | 26 | ``` |
23 | 27 |
|
24 | | -For detailed output: |
| 28 | +## Writing tests |
| 29 | + |
| 30 | +### File naming |
| 31 | + |
| 32 | +Test files must follow Pester naming conventions: |
| 33 | + |
| 34 | +```text |
| 35 | +<ScriptUnderTest>.Tests.ps1 |
| 36 | +``` |
| 37 | + |
| 38 | +### Locating the script under test |
| 39 | + |
| 40 | +When scripts and tests are siblings under `scripts/` and `scripts/tests/`, |
| 41 | +reference scripts relative to `$PSScriptRoot`: |
| 42 | + |
| 43 | +```powershell |
| 44 | +BeforeAll { |
| 45 | + $Script:ScriptPath = Join-Path $PSScriptRoot '..' 'my-script.ps1' |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Testing scripts that use `exit` |
| 50 | + |
| 51 | +Pipeline scripts commonly use `exit` for control flow. To validate exit codes, |
| 52 | +run scripts as child processes with `Start-Process`: |
25 | 53 |
|
26 | 54 | ```powershell |
27 | | -Invoke-Pester ./publish-symbols.Tests.ps1 -Output Detailed |
| 55 | +$proc = Start-Process -FilePath 'pwsh' ` |
| 56 | + -ArgumentList @('-NoProfile', '-NonInteractive', '-File', $scriptPath, <args...>) ` |
| 57 | + -NoNewWindow -Wait -PassThru ` |
| 58 | + -RedirectStandardOutput $stdoutFile ` |
| 59 | + -RedirectStandardError $stderrFile |
| 60 | +
|
| 61 | +$proc.ExitCode | Should -Be 0 |
28 | 62 | ``` |
29 | 63 |
|
30 | | -## Test Coverage |
| 64 | +### Mocking external tools |
| 65 | + |
| 66 | +When a script calls external tools (for example `symchk.exe`, `az`, or |
| 67 | +`Invoke-RestMethod`), mock those calls in tests. See |
| 68 | +`validate-symbols.Tests.ps1` and `publish-symbols.Tests.ps1`. |
| 69 | + |
| 70 | +## Test inventory |
31 | 71 |
|
32 | | -| Area | What's tested | |
33 | | -| --------------------- | ---------------------------------------------------------------- | |
34 | | -| Parameter validation | Empty strings rejected for all mandatory parameters | |
35 | | -| URL construction | Base URL, register URL, request URL built from parameters | |
36 | | -| Request bodies | Registration body, default publish flags, flag overrides | |
37 | | -| Error handling | Token failure, registration failure, publish failure, status failure — all verify expanded URI in error message | |
38 | | -| Status validation | Detects Failed/Cancelled results, respects PublishToInternal/PublishToPublic flags, passes on Succeeded/Pending | |
| 72 | +| Test file | Script under test | What it covers | |
| 73 | +| --------- | ----------------- | -------------- | |
| 74 | +| `publish-symbols.Tests.ps1` | `scripts/publish-symbols.ps1` | Parameter validation, URL construction, request bodies, status validation, error handling | |
| 75 | +| `validate-symbols.Tests.ps1` | `scripts/validate-symbols.ps1` | Syntax validation, package discovery/extraction, symchk detection, retry logic | |
39 | 76 |
|
40 | 77 | ## Notes |
41 | 78 |
|
42 | | -- All external calls (`az`, `Invoke-RestMethod`) are mocked — no network access or Azure credentials are required. |
43 | | -- Tests validate the script at `../publish-symbols.ps1` relative to this directory. |
| 79 | +- Tests for `publish-symbols.ps1` mock all external calls (`az`, `Invoke-RestMethod`), so no network access or Azure credentials are required. |
0 commit comments