Skip to content

Commit b3b0521

Browse files
committed
Bump to Pester 5
This is super painful since pester 5 had a lot of small, subtle and otherwise small cahgnes which break back compat, like scoping, error handling and a variety of other small things. I also did some cleanup to standardise on calling conventions and pulled in all the missed Pester 3 -> 4 changes as well. It will also no longer try test directories with no tests, since that throws up a really ugly error.
1 parent cd355ad commit b3b0521

File tree

13 files changed

+478
-421
lines changed

13 files changed

+478
-421
lines changed

README-powershell-modules.md

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ The test suite for each module currently assumes that the tests are being run wi
99

1010
This requires iterating through the module directories to run all the tests:
1111

12-
Two copies of BOSH Powershell Modules exist in this repo:
13-
- `stembuild/modules`
14-
- `modules`
15-
1612
```powershell
17-
# Where MODULES_DIR is ""ci/tasks/delete-vms/delete-vms.iml, or "modules"
13+
# Where MODULES_DIR is "stembuild/modules", or "modules"
1814
cd $MODULES_DIR
1915
foreach ($module in (Get-ChildItem "./modules").Name) {
2016
Push-Location "modules/$module"
@@ -30,20 +26,71 @@ echo "Failed Tests: $result"
3026
If you just need to test a single module, you could do this:
3127

3228
```powershell
33-
cd "$MODULES_DIR\BOSH.<module>"
29+
# Where MODULES_DIR is "stembuild/modules", or "modules"
30+
cd "${MODULES_DIR}/BOSH.<module>"
31+
Install-Module -Name Pester -Force
3432
Invoke-Pester
3533
```
3634

37-
## Running a subset of tests on macOS
35+
### Running tests via Concourse
36+
37+
```shell
38+
# Where MODULES_DIR is "stembuild/modules", or "modules"
39+
export MODULES_DIR="${MODULES_DIR}"
40+
41+
WINDOWS_STEMCELL_BUILDER=${WINDOWS_STEMCELL_BUILDER:-~/workspace/bosh-windows-stemcell-builder}
42+
43+
fly -t "${CONCOURSE_TARGET:-bosh-ecosystem}" \
44+
--tag="${WINDOWS_TAG:-windows-nimbus}" \
45+
--input=stemcell-builder="${WINDOWS_STEMCELL_BUILDER}" \
46+
--input-mapping="bosh-windows-stemcell-builder-ci=stemcell-builder" \
47+
--inputs-from=stemcells-windows-2019/test-bosh-psmodules \
48+
--config "${WINDOWS_STEMCELL_BUILDER}/ci/tasks/test-units-bosh-psmodules/task.yml"
49+
```
50+
51+
### Running a subset of tests on macOS
3852

3953
You can use Powershell and macOS to run the tests that do not require Windows system calls:
4054

4155
```shell
4256
cd ~/workspace
4357
brew install powershell
44-
git clone --depth 1 --branch 4.4.0 [email protected]:pester/Pester.git
4558
pwsh
46-
Import-Module ./Pester/Pester.psm1
59+
```
60+
61+
Then from within `pwsh`:
62+
63+
```powershell
64+
Install-Module -Name Pester -Force
4765
cd stembuild/module/BOSH.<module>
4866
Invoke-Pester
4967
```
68+
69+
## Debugging
70+
71+
You can debug powershell scripts using VSCode. It has some dependencies:
72+
- dotnet runtime `brew install dotnet`
73+
- powershell binary `brew install powershell`
74+
- vscode extensions: `Powershell`, `C#` and `C# Dev Kit` (the latter may not be required)
75+
76+
You can create a launch.json file like:
77+
```json
78+
{
79+
"version": "0.2.0",
80+
"configurations": [
81+
82+
{
83+
"name": "PowerShell: Run Pester Tests",
84+
"type": "PowerShell",
85+
"request": "launch",
86+
"script": "Invoke-Pester",
87+
"createTemporaryIntegratedConsole": true,
88+
"attachDotnetDebugger": true,
89+
"cwd": "${file}"
90+
}
91+
]
92+
}
93+
```
94+
95+
And you should be able to run tests for a single file using the debug view. If you're missing extensions
96+
you'll see odd failures such as the wrong CWD leading to import failures.
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
Remove-Module -Name BOSH.Account -ErrorAction Ignore
2-
Import-Module ./BOSH.Account.psm1
1+
BeforeAll {
2+
Remove-Module -Name BOSH.Account -ErrorAction Ignore
3+
Import-Module ./BOSH.Account.psm1
34

4-
Remove-Module -Name BOSH.Utils -ErrorAction Ignore
5-
Import-Module ../BOSH.Utils/BOSH.Utils.psm1
5+
Remove-Module -Name BOSH.Utils -ErrorAction Ignore
6+
Import-Module ../BOSH.Utils/BOSH.Utils.psm1
7+
}
68

79
Describe "Account" {
8-
910
Context "when username is not provided" {
1011
It "throws" {
11-
{ Add-Account } | Should Throw "Provide a user name"
12+
{ Add-Account } | Should -Throw "Provide a user name"
1213
}
1314
}
1415

1516
Context "when password is not provided" {
1617
It "throws" {
17-
{ Add-Account -User hello } | Should Throw "Provide a password"
18+
{ Add-Account -User hello } | Should -Throw "Provide a password"
1819
}
1920
}
2021

2122
Context "when the username and password are valid" {
22-
$timestamp=(get-date -UFormat "%s" -Millisecond 0)
23-
$user = "TestUser_$timestamp"
24-
$password = "Password123!"
23+
BeforeAll {
24+
$timestamp=(get-date -UFormat "%s" -Millisecond 0)
25+
$user = "TestUser_$timestamp"
26+
$password = "Password123!"
27+
}
2528

2629
BeforeEach {
2730
$userExists = !!(Get-LocalUser | Where {$_.Name -eq $user})
@@ -34,14 +37,11 @@ Describe "Account" {
3437
Add-Account -User $user -Password $password
3538
mkdir "C:\Users\$user" -ErrorAction Ignore
3639
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
37-
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
38-
$existing | Should Not Be $null
40+
$existing = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
41+
$existing | Should -Not -Be $null
3942
Remove-Account -User $user
40-
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
41-
$existing | Should Be $null
43+
$existing = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'user' -and $_.Name -eq $user }
44+
$existing | Should -Be $null
4245
}
4346
}
4447
}
45-
46-
Remove-Module -Name BOSH.Account -ErrorAction Ignore
47-
Remove-Module -Name BOSH.Utils -ErrorAction Ignore

0 commit comments

Comments
 (0)