Skip to content

Commit

Permalink
update instructions to be a little shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Oct 25, 2024
1 parent 8b93039 commit c4bcd7b
Showing 1 changed file with 61 additions and 57 deletions.
118 changes: 61 additions & 57 deletions .aider/prompts/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,6 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
}
```

## TestCases
Use the `-ForEach` parameter in `It` blocks for multiple test cases:

```powershell
It "Should calculate correctly" -ForEach @(
@{ Input = 1; Expected = 2 }
@{ Input = 2; Expected = 4 }
@{ Input = 3; Expected = 6 }
) {
$result = Get-Double -Number $Input
$result | Should -Be $Expected
}
```

## Style Guidelines
- Use double quotes for strings (we're a SQL Server module)
- Array declarations should be on multiple lines:
Expand All @@ -72,7 +58,6 @@ $array = @(
- Skip conditions must evaluate to `$true` or `$false`, not strings
- Use `$global:` instead of `$script:` for test configuration variables when required for Pester v5 scoping
- Avoid script blocks in Where-Object when possible:

```powershell
# Good - direct property comparison
$master = $databases | Where-Object Name -eq "master"
Expand All @@ -82,11 +67,56 @@ $systemDbs = $databases | Where-Object Name -in "master", "model", "msdb", "temp
$actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
```

## DO NOT
- DO NOT use `$MyInvocation.MyCommand.Name` to get command names
- DO NOT use the old `knownParameters` validation approach
- DO NOT include loose code outside of proper test blocks
- DO NOT remove comments like "#TestConfig.instance3" or "#$TestConfig.instance2 for appveyor"
### Parameter & Variable Naming Rules
- Use direct parameters for 1-2 parameters
- Use `$splat<Purpose>` for 3+ parameters (never plain `$splat`)

```powershell
# Direct parameters
$ag = Get-DbaLogin -SqlInstance $instance -Login $loginName
# Splat with purpose suffix
$splatPrimary = @{
Primary = $TestConfig.instance3
Name = $primaryAgName
ClusterType = "None"
FailoverMode = "Manual"
Certificate = "dbatoolsci_AGCert"
Confirm = $false
}
$primaryAg = New-DbaAvailabilityGroup @splatPrimary
```

### Unique names across scopes

- Use unique, descriptive variable names across scopes to avoid collisions
- Play particlar attention to variable names in the BeforeAll

```powershell
Describe "Add-DbaAgReplica" -Tag "IntegrationTests" {
BeforeAll {
$primaryAgName = "dbatoolsci_agroup"
$splatPrimary = @{
Primary = $TestConfig.instance3
Name = $primaryAgName
...
}
$ag = New-DbaAvailabilityGroup @splatPrimary
}
Context "When adding AG replicas" {
BeforeAll {
$replicaAgName = "dbatoolsci_add_replicagroup"
$splatRepAg = @{
Primary = $TestConfig.instance3
Name = $replicaAgName
...
}
$replicaAg = New-DbaAvailabilityGroup @splatRepAg
}
}
}
```

## Examples

Expand All @@ -106,14 +136,14 @@ Describe "Get-DbaDatabase" -Tag "UnitTests" {
)
}
It "Should have exactly the expected parameters" {
$actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty
}
It "Has parameter: <_>" -ForEach $expectedParameters {
$command | Should -HaveParameter $PSItem
}
It "Should have exactly the number of expected parameters" {
$actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty
}
}
}
```
Expand All @@ -139,35 +169,9 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
}
```

### Parameter & Variable Naming Rules
1. Use direct parameters for 1-3 parameters
2. Use `$splat<Purpose>` for 4+ parameters (never plain `$splat`)
3. Use unique, descriptive variable names across scopes

```powershell
# Direct parameters
$ag = New-DbaLogin -SqlInstance $instance -Login $loginName -Password $password
# Splat with purpose suffix
$splatPrimary = @{
Primary = $TestConfig.instance3
Name = $primaryAgName # Descriptive variable name
ClusterType = "None"
FailoverMode = "Manual"
Certificate = "dbatoolsci_AGCert"
Confirm = $false
}
$primaryAg = New-DbaAvailabilityGroup @splatPrimary
# Unique names across scopes
Describe "New-DbaAvailabilityGroup" {
BeforeAll {
$primaryAgName = "primaryAG"
}
Context "Adding replica" {
BeforeAll {
$replicaAgName = "replicaAG"
}
}
}
```
## DO NOT
- DO NOT use `$MyInvocation.MyCommand.Name` to get command names
- DO NOT use the old `knownParameters` validation approach
- DO NOT include loose code outside of proper test blocks
- DO NOT remove comments like "#TestConfig.instance3" or "#$TestConfig.instance2 for appveyor"
- DO NOT use $_ DO use $PSItem instead

0 comments on commit c4bcd7b

Please sign in to comment.