Skip to content

Commit 4226570

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 4226570

File tree

13 files changed

+468
-419
lines changed

13 files changed

+468
-419
lines changed

README-powershell-modules.md

Lines changed: 46 additions & 7 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,63 @@ echo "Failed Tests: $result"
3026
If you just need to test a single module, you could do this:
3127

3228
```powershell
29+
# Where MODULES_DIR is "stembuild/modules", or "modules"
3330
cd "$MODULES_DIR\BOSH.<module>"
3431
Invoke-Pester
3532
```
3633

37-
## Running a subset of tests on macOS
34+
35+
### Running tests via Concourse
36+
37+
```shell
38+
# Where MODULES_DIR is "stembuild/modules", or "modules"
39+
cd bosh-windows-stemcell-builder
40+
fly -t bosh-ecosystem execute \
41+
--tag=windows-nimbus \
42+
--config "./ci/tasks/test-units-bosh-psmodules/task.yml" \
43+
--inputs-from=windows-2019-stemcell/test-bosh-psmodules \
44+
--input=stemcell-builder="./"
45+
```
46+
47+
### Running a subset of tests on macOS
3848

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

4151
```shell
4252
cd ~/workspace
4353
brew install powershell
44-
git clone --depth 1 --branch 4.4.0 [email protected]:pester/Pester.git
54+
git clone --depth 1 [email protected]:pester/Pester.git
4555
pwsh
4656
Import-Module ./Pester/Pester.psm1
4757
cd stembuild/module/BOSH.<module>
4858
Invoke-Pester
4959
```
60+
61+
## Debugging
62+
63+
You can debug powershell scripts using VSCode. It has some dependencies:
64+
- dotnet runtime `brew install dotnet`
65+
- powershell binary `brew install powershell`
66+
- vscode extensions: `Powershell`, `C#` and `C# Dev Kit` (the latter may not be required)
67+
68+
You can create a launch.json file like:
69+
```json
70+
{
71+
"version": "0.2.0",
72+
"configurations": [
73+
74+
{
75+
"name": "PowerShell: Run Pester Tests",
76+
"type": "PowerShell",
77+
"request": "launch",
78+
"script": "Invoke-Pester",
79+
"createTemporaryIntegratedConsole": true,
80+
"attachDotnetDebugger": true,
81+
"cwd": "${file}"
82+
}
83+
]
84+
}
85+
```
86+
87+
And you should be able to run tests for a single file using the debug view. If you're missing extensions
88+
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)