Skip to content

Commit f2d3146

Browse files
Pester migration - Second batch (#9530)
1 parent 81cd54f commit f2d3146

35 files changed

+1866
-1049
lines changed

.aider/aider.psm1

Lines changed: 291 additions & 187 deletions
Large diffs are not rendered by default.

.aider/prompts/conventions.md

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
## Core Requirements
44
```powershell
55
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
6-
param($ModuleName = "dbatools")
7-
$global:TestConfig = Get-TestConfig
6+
param(
7+
$ModuleName = "dbatools",
8+
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
9+
)
810
```
9-
These three lines must start every test file.
11+
These lines must start every test file.
1012

1113
## Test Structure
1214

@@ -45,20 +47,6 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
4547
}
4648
```
4749

48-
## TestCases
49-
Use the `-ForEach` parameter in `It` blocks for multiple test cases:
50-
51-
```powershell
52-
It "Should calculate correctly" -ForEach @(
53-
@{ Input = 1; Expected = 2 }
54-
@{ Input = 2; Expected = 4 }
55-
@{ Input = 3; Expected = 6 }
56-
) {
57-
$result = Get-Double -Number $Input
58-
$result | Should -Be $Expected
59-
}
60-
```
61-
6250
## Style Guidelines
6351
- Use double quotes for strings (we're a SQL Server module)
6452
- Array declarations should be on multiple lines:
@@ -72,21 +60,65 @@ $array = @(
7260
- Skip conditions must evaluate to `$true` or `$false`, not strings
7361
- Use `$global:` instead of `$script:` for test configuration variables when required for Pester v5 scoping
7462
- Avoid script blocks in Where-Object when possible:
75-
7663
```powershell
7764
# Good - direct property comparison
7865
$master = $databases | Where-Object Name -eq "master"
7966
$systemDbs = $databases | Where-Object Name -in "master", "model", "msdb", "tempdb"
8067
8168
# Required - script block for Parameters.Keys
82-
$actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
69+
$newParameters = $command.Parameters.Values.Name | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
8370
```
8471

85-
## DO NOT
86-
- DO NOT use `$MyInvocation.MyCommand.Name` to get command names
87-
- DO NOT use the old `knownParameters` validation approach
88-
- DO NOT include loose code outside of proper test blocks
89-
- DO NOT remove comments like "#TestConfig.instance3" or "#$TestConfig.instance2 for appveyor"
72+
### Parameter & Variable Naming Rules
73+
- Use direct parameters for 1-2 parameters
74+
- Use `$splat<Purpose>` for 3+ parameters (never plain `$splat`)
75+
76+
```powershell
77+
# Direct parameters
78+
$ag = Get-DbaLogin -SqlInstance $instance -Login $loginName
79+
80+
# Splat with purpose suffix
81+
$splatPrimary = @{
82+
Primary = $TestConfig.instance3
83+
Name = $primaryAgName
84+
ClusterType = "None"
85+
FailoverMode = "Manual"
86+
Certificate = "dbatoolsci_AGCert"
87+
Confirm = $false
88+
}
89+
$primaryAg = New-DbaAvailabilityGroup @splatPrimary
90+
```
91+
92+
### Unique names across scopes
93+
94+
- Use unique, descriptive variable names across scopes to avoid collisions
95+
- Play particlar attention to variable names in the BeforeAll
96+
97+
```powershell
98+
Describe "Add-DbaAgReplica" -Tag "IntegrationTests" {
99+
BeforeAll {
100+
$primaryAgName = "dbatoolsci_agroup"
101+
$splatPrimary = @{
102+
Primary = $TestConfig.instance3
103+
Name = $primaryAgName
104+
...
105+
}
106+
$ag = New-DbaAvailabilityGroup @splatPrimary
107+
}
108+
109+
Context "When adding AG replicas" {
110+
BeforeAll {
111+
$replicaAgName = "dbatoolsci_add_replicagroup"
112+
$splatRepAg = @{
113+
Primary = $TestConfig.instance3
114+
Name = $replicaAgName
115+
...
116+
}
117+
$replicaAg = New-DbaAvailabilityGroup @splatRepAg
118+
}
119+
}
120+
}
121+
```
90122

91123
## Examples
92124

@@ -97,22 +129,23 @@ Describe "Get-DbaDatabase" -Tag "UnitTests" {
97129
Context "Parameter validation" {
98130
BeforeAll {
99131
$command = Get-Command Get-DbaDatabase
100-
$expectedParameters = $TestConfig.CommonParameters
101-
102-
$expectedParameters += @(
132+
$expected = $TestConfig.CommonParameters
133+
$expected += @(
103134
"SqlInstance",
104135
"SqlCredential",
105-
"Database"
136+
"Database",
137+
"Confirm",
138+
"WhatIf"
106139
)
107140
}
108141
109-
It "Should have exactly the expected parameters" {
110-
$actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
111-
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty
142+
It "Has parameter: <_>" -ForEach $expected {
143+
$command | Should -HaveParameter $PSItem
112144
}
113145
114-
It "Has parameter: <_>" -ForEach $expectedParameters {
115-
$command | Should -HaveParameter $PSItem
146+
It "Should have exactly the number of expected parameters ($($expected.Count))" {
147+
$hasparms = $command.Parameters.Values.Name
148+
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
116149
}
117150
}
118151
}
@@ -139,35 +172,11 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
139172
}
140173
```
141174

142-
### Parameter & Variable Naming Rules
143-
1. Use direct parameters for 1-3 parameters
144-
2. Use `$splat<Purpose>` for 4+ parameters (never plain `$splat`)
145-
3. Use unique, descriptive variable names across scopes
146-
147-
```powershell
148-
# Direct parameters
149-
$ag = New-DbaLogin -SqlInstance $instance -Login $loginName -Password $password
175+
## Additional instructions
150176

151-
# Splat with purpose suffix
152-
$splatPrimary = @{
153-
Primary = $TestConfig.instance3
154-
Name = $primaryAgName # Descriptive variable name
155-
ClusterType = "None"
156-
FailoverMode = "Manual"
157-
Certificate = "dbatoolsci_AGCert"
158-
Confirm = $false
159-
}
160-
$primaryAg = New-DbaAvailabilityGroup @splatPrimary
161-
162-
# Unique names across scopes
163-
Describe "New-DbaAvailabilityGroup" {
164-
BeforeAll {
165-
$primaryAgName = "primaryAG"
166-
}
167-
Context "Adding replica" {
168-
BeforeAll {
169-
$replicaAgName = "replicaAG"
170-
}
171-
}
172-
}
173-
```
177+
- DO NOT use `$MyInvocation.MyCommand.Name` to get command names
178+
- DO NOT use the old `knownParameters` validation approach
179+
- DO NOT include loose code outside of proper test blocks
180+
- DO NOT remove comments like "#TestConfig.instance3" or "#$TestConfig.instance2 for appveyor"
181+
- DO NOT use $_ DO use $PSItem instead
182+
- Parameter validation is ALWAYS tagged as a Unit Test

.aider/prompts/template.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
You are an AI assistant created by Anthropic to help migrate Pester tests for the **dbatools PowerShell module** from version 4 to version 5. Analyze and update the file `/workspace/tests/--CMDNAME--.Tests.ps1` according to the instructions in conventions.md.
44

5-
Required parameters for this command:
6-
--PARMZ--
7-
85
Command name:
96
--CMDNAME--
107

8+
Parameters for this command:
9+
--PARMZ--
10+
1111
Before responding, verify that your answer adheres to the specified coding and migration guidelines.

private/testing/Get-TestConfig.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ function Get-TestConfig {
88
Write-Host "Tests will use local constants file: tests\constants.local.ps1." -ForegroundColor Cyan
99
. $LocalConfigPath
1010
# Note: Local constants are sourced but not explicitly added to $config
11-
} elseif ($env:CODESPACES -and ($env:TERM_PROGRAM -eq 'vscode' -and $env:REMOTE_CONTAINERS)) {
11+
} elseif ($env:CODESPACES -or ($env:TERM_PROGRAM -eq 'vscode' -and $env:REMOTE_CONTAINERS)) {
12+
$null = Set-DbatoolsInsecureConnection
1213
$config['Instance1'] = "dbatools1"
1314
$config['Instance2'] = "dbatools2"
1415
$config['Instance3'] = "dbatools3"
1516
$config['Instances'] = @($config['Instance1'], $config['Instance2'])
1617

1718
$config['SqlCred'] = [PSCredential]::new('sa', (ConvertTo-SecureString $env:SA_PASSWORD -AsPlainText -Force))
18-
$config['PSDefaultParameterValues'] = @{
19+
$config['Defaults'] = [System.Management.Automation.DefaultParameterDictionary]@{
1920
"*:SqlCredential" = $config['SqlCred']
21+
"*:SourceSqlCredential" = $config['SqlCred']
22+
"*:DestinationSqlCredential" = $config['SqlCred']
2023
}
2124
} elseif ($env:GITHUB_WORKSPACE) {
2225
$config['DbaToolsCi_Computer'] = "localhost"
@@ -53,7 +56,7 @@ function Get-TestConfig {
5356
}
5457

5558
if ($env:appveyor) {
56-
$config['PSDefaultParameterValues'] = @{
59+
$config['Defaults'] = [System.Management.Automation.DefaultParameterDictionary]@{
5760
'*:WarningAction' = 'SilentlyContinue'
5861
}
5962
}

0 commit comments

Comments
 (0)