Skip to content

Refactor and enhance Pester tests batch #9760

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 19 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
80 changes: 64 additions & 16 deletions tests/Export-DbaXESessionTemplate.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,79 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Export-DbaXESessionTemplate",
$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 = 'SqlInstance', 'SqlCredential', 'Session', 'Path', 'FilePath', 'InputObject', '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 += @(
"SqlInstance",
"SqlCredential",
"Session",
"Path",
"FilePath",
"InputObject",
"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 {
BeforeAll {
$null = Get-DbaXESession -SqlInstance $TestConfig.instance2 -Session 'Profiler TSQL Duration' | Remove-DbaXESession
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true

# For all the backups that we want to clean up after the test, we create a directory that we can delete at the end.
# Other files can be written there as well, maybe we change the name of that variable later. But for now we focus on backups.
$tempPath = "$($TestConfig.Temp)\$CommandName-$(Get-Random)"
$null = New-Item -Path $tempPath -ItemType Directory

# Explain what needs to be set up for the test:
# We need to ensure that any existing 'Profiler TSQL Duration' session is removed before we start

# Set variables. They are available in all the It blocks.
$sessionName = "Profiler TSQL Duration"

# Clean up any existing session
$null = Get-DbaXESession -SqlInstance $TestConfig.instance2 -Session $sessionName | Remove-DbaXESession

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
}

AfterAll {
$null = Get-DbaXESession -SqlInstance $TestConfig.instance2 -Session 'Profiler TSQL Duration' | Remove-DbaXESession
Remove-Item -Path 'C:\windows\temp\Profiler TSQL Duration.xml' -ErrorAction SilentlyContinue
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true

# Cleanup all created objects.
$null = Get-DbaXESession -SqlInstance $TestConfig.instance2 -Session $sessionName | Remove-DbaXESession

# Remove the temporary directory and any exported files.
Remove-Item -Path $tempPath -Recurse -ErrorAction SilentlyContinue

# As this is the last block we do not need to reset the $PSDefaultParameterValues.
}

Context "Test Importing Session Template" {
$session = Import-DbaXESessionTemplate -SqlInstance $TestConfig.instance2 -Template 'Profiler TSQL Duration'
$results = $session | Export-DbaXESessionTemplate -Path C:\windows\temp
BeforeAll {
$session = Import-DbaXESessionTemplate -SqlInstance $TestConfig.instance2 -Template "Profiler TSQL Duration"
$results = $session | Export-DbaXESessionTemplate -Path $tempPath
}

It "session exports to disk" {
$results.Name | Should Be 'Profiler TSQL Duration.xml'
$results.Name | Should -Be "$sessionName.xml"
}
}
}
}
Loading
Loading