Skip to content

Refactor and enhance Pester tests batch #9759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8e40775
Refactor and modernize Pester tests for multiple commands
potatoqualitee Aug 9, 2025
1f17830
Enhance formatter to preserve alignment and avoid unnecessary writes
potatoqualitee Aug 9, 2025
b07ac96
Improve file handling in Invoke-DbatoolsFormatter
potatoqualitee Aug 9, 2025
4b4c3b5
Improve formatting comparison in Invoke-DbatoolsFormatter
potatoqualitee Aug 9, 2025
05eb897
Refactor Invoke-DbatoolsFormatter for improved formatting
potatoqualitee Aug 9, 2025
21da25c
Add progress reporting to Invoke-DbatoolsFormatter
potatoqualitee Aug 9, 2025
1e8e4fe
Merge branch 'formatfix' into b4
potatoqualitee Aug 9, 2025
5dec387
Normalize whitespace in test scripts
potatoqualitee Aug 9, 2025
2d8168e
Merge branch 'development' into b4
potatoqualitee Aug 10, 2025
e68c4a8
Add Pattern parameter to filter test files
potatoqualitee Aug 10, 2025
5fc846c
Merge branch 'development' into b4
potatoqualitee Aug 10, 2025
ce0098b
Refactor and enhance Get-DbaCredential and Get-DbaDatabase tests
potatoqualitee Aug 10, 2025
3a36a65
Database, Build, BackupHistory cant be automatically converted
potatoqualitee Aug 11, 2025
d131087
Let's try with Sonnet
potatoqualitee Aug 11, 2025
36bfc6d
These can't be auto-converted
potatoqualitee Aug 11, 2025
9c9416b
Add -IgnoreFileChecks to Backup-DbaDatabase test
potatoqualitee Aug 11, 2025
e38d760
Wrong branch
potatoqualitee Aug 11, 2025
6bfbfbc
Improve certificate backup test assertions
potatoqualitee Aug 11, 2025
79174f2
Wrong branch again wth
potatoqualitee Aug 11, 2025
3097d67
Improve certificate backup test assertions
potatoqualitee Aug 11, 2025
628ead0
okay no idea
potatoqualitee Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions .aitools/module/Repair-PullRequestTest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function Repair-PullRequestTest {
from the development branch to the current branch, without running Update-PesterTest
or committing any changes.
.PARAMETER Pattern
Pattern to filter test files. Only files matching this pattern will be processed.
Supports wildcards (e.g., "*Login*" to match files containing "Login").
.NOTES
Tags: Testing, Pester, PullRequest, CI
Author: dbatools team
Expand Down Expand Up @@ -53,7 +57,8 @@ function Repair-PullRequestTest {
[switch]$AutoCommit,
[int]$MaxPRs = 5,
[int]$BuildNumber,
[switch]$CopyOnly
[switch]$CopyOnly,
[string]$Pattern
)

begin {
Expand Down Expand Up @@ -269,7 +274,14 @@ function Repair-PullRequestTest {
$fileErrorPath = @()
$testdirectory = Join-Path $script:ModulePath "tests"

foreach ($test in $allFailedTestsAcrossPRs) {
# Apply Pattern filter first if specified
$filteredTests = if ($Pattern) {
$allFailedTestsAcrossPRs | Where-Object { [System.IO.Path]::GetFileName($_.TestFile) -match $Pattern }
} else {
$allFailedTestsAcrossPRs
}

foreach ($test in $filteredTests) {
$fileName = [System.IO.Path]::GetFileName($test.TestFile)
# ONLY include files that are actually in the PR changes
if ($allRelevantTestFiles.Count -eq 0 -or $fileName -in $allRelevantTestFiles) {
Expand All @@ -284,7 +296,11 @@ function Repair-PullRequestTest {
}
}
$fileErrorPath = $fileErrorPath | Sort-Object -Unique
Write-Verbose "Found failures in $($fileErrorMap.Keys.Count) unique test files (filtered to PR changes only)"
$filterMessage = "filtered to PR changes only"
if ($Pattern) {
$filterMessage += " and pattern '$Pattern'"
}
Write-Verbose "Found failures in $($fileErrorMap.Keys.Count) unique test files ($filterMessage)"
foreach ($fileName in $fileErrorMap.Keys) {
Write-Verbose " ${fileName} - $($fileErrorMap[$fileName].Count) failures"
Write-Verbose " Paths: $fileErrorPath"
Expand Down
39 changes: 28 additions & 11 deletions tests/Get-DbaClientAlias.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-DbaClientAlias",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe $CommandName -Tag UnitTests {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'Credential', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"ComputerName",
"Credential",
"EnableException"
)
}

It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
$newalias = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias -Verbose:$false
$newAlias = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias -Verbose:$false
}

AfterAll {
$newalias | Remove-DbaClientAlias
$newAlias | Remove-DbaClientAlias -ErrorAction SilentlyContinue
}

Context "gets the alias" {
$results = Get-DbaClientAlias
BeforeAll {
$results = Get-DbaClientAlias
}

It "returns accurate information" {
$results.AliasName -contains 'dbatoolscialias' | Should Be $true
$results.AliasName -contains "dbatoolscialias" | Should -Be $true
}
}
}
40 changes: 28 additions & 12 deletions tests/Get-DbaClientProtocol.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-DbaClientProtocol",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'Credential', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"ComputerName",
"Credential",
"EnableException"
)
}

It "Should have the expected parameters" {
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
Context "Get some client protocols" {
$results = Get-DbaClientProtocol
BeforeAll {
$results = @(Get-DbaClientProtocol)
}

It "Should return some protocols" {
$results.Count | Should BeGreaterThan 1
$results | Where-Object { $_.ProtocolDisplayName -eq 'TCP/IP' } | Should Not Be $null
$results.Status.Count | Should -BeGreaterThan 1
$results | Where-Object ProtocolDisplayName -eq "TCP/IP" | Should -Not -BeNullOrEmpty
}
}
}
53 changes: 36 additions & 17 deletions tests/Get-DbaCmConnection.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'UserName', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-DbaCmConnection",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"ComputerName",
"UserName",
"EnableException"
)
}

It "Should have the expected parameters" {
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
New-DbaCmConnection -ComputerName $env:COMPUTERNAME
}

AfterAll {
Remove-DbaCmConnection -ComputerName $env:COMPUTERNAME -Confirm:$False
Remove-DbaCmConnection -ComputerName $env:COMPUTERNAME -Confirm:$false
}

Context "Returns DbaCmConnection" {
$Results = Get-DbaCMConnection -ComputerName $env:COMPUTERNAME
BeforeAll {
$cmConnectionResults = Get-DbaCmConnection -ComputerName $env:COMPUTERNAME
}

It "Results are not Empty" {
$Results | should not be $null
$cmConnectionResults | Should -Not -BeNullOrEmpty
}
}

Context "Returns DbaCmConnection for User" {
$Results = Get-DbaCMConnection -ComputerName $env:COMPUTERNAME -UserName *
BeforeAll {
$userConnectionResults = Get-DbaCmConnection -ComputerName $env:COMPUTERNAME -UserName *
}

It "Results are not Empty" {
$Results | should not be $null
$userConnectionResults | Should -Not -BeNullOrEmpty
}
}
}
43 changes: 31 additions & 12 deletions tests/Get-DbaCmObject.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-DbaCmObject",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ClassName', 'Query', 'ComputerName', 'Credential', 'Namespace', 'DoNotUse', 'Force', 'SilentlyContinue', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"ClassName",
"Query",
"ComputerName",
"Credential",
"Namespace",
"DoNotUse",
"Force",
"SilentlyContinue",
"EnableException"
)
}

It "Should have the expected parameters" {
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Context "returns proper information" {
It "returns a bias that's an int" {
(Get-DbaCmObject -ClassName Win32_TimeZone).Bias -is [int]
Describe $CommandName -Tag IntegrationTests {
Context "Returns proper information" {
It "Returns a bias that's an int" {
(Get-DbaCmObject -ClassName Win32_TimeZone).Bias | Should -BeOfType [int]
}
}
}
56 changes: 38 additions & 18 deletions tests/Get-DbaComputerCertificate.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-DbaComputerCertificate",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'Credential', 'Store', 'Folder', 'Path', 'Thumbprint', 'EnableException', 'Type'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"ComputerName",
"Credential",
"Store",
"Folder",
"Path",
"Thumbprint",
"EnableException",
"Type"
)
}

It "Should have the expected parameters" {
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
Context "Can get a certificate" {
BeforeAll {
$null = Add-DbaComputerCertificate -Path "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt" -Confirm:$false
$thumbprint = "29C469578D6C6211076A09CEE5C5797EEA0C2713"

# Get all certificates once for testing
$allCertificates = Get-DbaComputerCertificate
$specificCertificate = Get-DbaComputerCertificate -Thumbprint $thumbprint
}

AfterAll {
Remove-DbaComputerCertificate -Thumbprint $thumbprint -Confirm:$false
Remove-DbaComputerCertificate -Thumbprint $thumbprint -Confirm:$false -ErrorAction SilentlyContinue
}

$cert = Get-DbaComputerCertificate -Thumbprint $thumbprint

It "returns a single certificate with a specific thumbprint" {
$cert.Thumbprint | Should Be $thumbprint
$specificCertificate.Thumbprint | Should -Be $thumbprint
}

$cert = Get-DbaComputerCertificate

It "returns all certificates and at least one has the specified thumbprint" {
"$($cert.Thumbprint)" -match $thumbprint | Should Be $true
"$($allCertificates.Thumbprint)" -match $thumbprint | Should -Be $true
}

It "returns all certificates and at least one has the specified EnhancedKeyUsageList" {
"$($cert.EnhancedKeyUsageList)" -match '1\.3\.6\.1\.5\.5\.7\.3\.1' | Should Be $true
"$($allCertificates.EnhancedKeyUsageList)" -match "1\.3\.6\.1\.5\.5\.7\.3\.1" | Should -Be $true
}
}
}
}
Loading
Loading