From 252f8176ce78a42c9044df67f3ddd1d2775ef726 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 22:38:15 +0200 Subject: [PATCH 01/12] first pester5 test (do Invoke-DbaQuery) --- tests/Invoke-DbaQuery.Tests.ps1 | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/Invoke-DbaQuery.Tests.ps1 b/tests/Invoke-DbaQuery.Tests.ps1 index 361252dd42..46bb384a28 100644 --- a/tests/Invoke-DbaQuery.Tests.ps1 +++ b/tests/Invoke-DbaQuery.Tests.ps1 @@ -1,6 +1,11 @@ -$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") -Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan -$global:TestConfig = Get-TestConfig +#pester5 + +BeforeAll { + $CommandName = (Get-Item $PSCommandPath).Name.Replace(".Tests.ps1", "") + Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan + $global:TestConfig = Get-TestConfig +} + Describe "$CommandName Unit Tests" -Tag 'UnitTests' { Context "Validate parameters" { @@ -65,7 +70,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" { } It "stops when piped databases and -Database" { $dbs = Get-DbaDatabase -SqlInstance $TestConfig.instance2, $TestConfig.instance3 - { $dbs | Invoke-DbaQuery -Query "Select 'hello' as TestColumn, DB_NAME() as dbname" -Database tempdb -EnableException } | Should Throw "You can't" + { $dbs | Invoke-DbaQuery -Query "Select 'hello' as TestColumn, DB_NAME() as dbname" -Database tempdb -EnableException } | Should -Throw "You can't*" } It "supports reading files" { $testPath = "TestDrive:\dbasqlquerytest.txt" From ecc9600a4b1bc81c32e05add5cae61775ae0e8c5 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 22:48:31 +0200 Subject: [PATCH 02/12] pester5 needs HaveParameter. more or less. --- tests/Invoke-DbaQuery.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Invoke-DbaQuery.Tests.ps1 b/tests/Invoke-DbaQuery.Tests.ps1 index 46bb384a28..bc15939b0a 100644 --- a/tests/Invoke-DbaQuery.Tests.ps1 +++ b/tests/Invoke-DbaQuery.Tests.ps1 @@ -1,4 +1,4 @@ -#pester5 +#HaveParameter - yeah, I know. BeforeAll { $CommandName = (Get-Item $PSCommandPath).Name.Replace(".Tests.ps1", "") From ad134f06ca8c222e5c7d41fb71dba380d2a38029 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 22:52:21 +0200 Subject: [PATCH 03/12] whoopsie (do Invoke-DbaQuery) --- tests/Invoke-DbaQuery.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Invoke-DbaQuery.Tests.ps1 b/tests/Invoke-DbaQuery.Tests.ps1 index bc15939b0a..923256d85e 100644 --- a/tests/Invoke-DbaQuery.Tests.ps1 +++ b/tests/Invoke-DbaQuery.Tests.ps1 @@ -351,6 +351,5 @@ SELECT 2 $results.Column1 | Should -Be "NULL" } - } From 29de0e4bcead44d4eebcf88c71260ec6ac61ec2e Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 23:17:53 +0200 Subject: [PATCH 04/12] old-style param check (do Invoke-DbaQuery) --- tests/Invoke-DbaQuery.Tests.ps1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/Invoke-DbaQuery.Tests.ps1 b/tests/Invoke-DbaQuery.Tests.ps1 index 923256d85e..fb1b242bde 100644 --- a/tests/Invoke-DbaQuery.Tests.ps1 +++ b/tests/Invoke-DbaQuery.Tests.ps1 @@ -9,11 +9,14 @@ BeforeAll { 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', 'Database', 'Query', 'QueryTimeout', 'File', 'SqlObject', 'As', 'SqlParameter', 'AppendServerInstance', 'MessagesToOutput', 'InputObject', 'ReadOnly', 'EnableException', 'CommandType', 'NoExec' - $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters + BeforeAll { + $command = Get-Command Invoke-DbaQuery + } It "Should only contain our specific parameters" { - (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0 + [object[]]$params = $command.Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') } + [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'Query', 'QueryTimeout', 'File', 'SqlObject', 'As', 'SqlParameter', 'AppendServerInstance', 'MessagesToOutput', 'InputObject', 'ReadOnly', 'EnableException', 'CommandType', 'NoExec' + $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters + (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should -Be 0 } } Context "Validate alias" { From c0f21b94c384f5c1fad5bad01985116367e49c99 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 23:32:20 +0200 Subject: [PATCH 05/12] adjust runner and test (do Invoke-DbaQuery) --- tests/Invoke-DbaQuery.Tests.ps1 | 6 +++--- tests/appveyor.pester.ps1 | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Invoke-DbaQuery.Tests.ps1 b/tests/Invoke-DbaQuery.Tests.ps1 index fb1b242bde..df4ca7e79b 100644 --- a/tests/Invoke-DbaQuery.Tests.ps1 +++ b/tests/Invoke-DbaQuery.Tests.ps1 @@ -116,7 +116,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" { $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance3 -Database tempdb -SqlObject $smoobj $check = "SELECT name FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CommandLog]') AND type in (N'U')" $results = Invoke-DbaQuery -SqlInstance $TestConfig.instance3 -Database tempdb -Query $check - $results.Name | Should Be 'CommandLog' + $results.Name | Should -Be 'CommandLog' $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2, $TestConfig.instance3 -Database tempdb -Query $cleanup } <# @@ -195,7 +195,7 @@ SELECT @@servername as dbname } It "Executes stored procedures with parameters" { $results = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "dbatoolsci_procedure_example" -SqlParameters @{p1 = 1 } -CommandType StoredProcedure - $results.TestColumn | Should Be 1 + $results.TestColumn | Should -Be 1 } It "Executes script file with a relative path (see #6184)" { Set-Content -Path ".\hellorelative.sql" -Value "Select 'hello' as TestColumn, DB_NAME() as dbname" @@ -332,7 +332,7 @@ SELECT 2 $result = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query $q -NoExec $result | Should -BeNullOrEmpty - { Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "SELEC p FROM c" -NoExec -EnableException } | Should -Throw "Incorrect syntax near 'selec'" + { Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "SELEC p FROM c" -NoExec -EnableException } | Should -Throw "Incorrect syntax near 'SELEC'" } It "supports dropping temp objects (#8472)" { diff --git a/tests/appveyor.pester.ps1 b/tests/appveyor.pester.ps1 index 20df69244d..609bfbaee4 100644 --- a/tests/appveyor.pester.ps1 +++ b/tests/appveyor.pester.ps1 @@ -359,6 +359,7 @@ if (-not $Finalize) { Write-Warning "Failed tests summary (pester 5):" $faileditems | ForEach-Object { $name = $_.Name + $failedCount += 1 [pscustomobject]@{ Path = $_.Path -Join '/' Name = "It $name" From 8735d8e0c8dc72d7b7e269fdc11b7fd0fd6dd528 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 23:35:02 +0200 Subject: [PATCH 06/12] update failure counter --- tests/appveyor.pester.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/appveyor.pester.ps1 b/tests/appveyor.pester.ps1 index 609bfbaee4..fbff87e38f 100644 --- a/tests/appveyor.pester.ps1 +++ b/tests/appveyor.pester.ps1 @@ -331,7 +331,8 @@ if (-not $Finalize) { Get-ChildItem $ModuleBase\dbatools_messages_and_errors.xml.zip | ForEach-Object { Push-AppveyorArtifact $_.FullName -FileName $_.Name } } #$totalcount = $results | Select-Object -ExpandProperty TotalCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum - $failedcount = $results | Select-Object -ExpandProperty FailedCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum + $failedcount = 0 + $failedcount += $results | Select-Object -ExpandProperty FailedCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum if ($failedcount -gt 0) { # pester 4 output $faileditems = $results | Select-Object -ExpandProperty TestResult | Where-Object { $_.Passed -notlike $True } @@ -353,13 +354,13 @@ if (-not $Finalize) { $results5 = @(Get-ChildItem -Path "$ModuleBase\Pester5Results*.xml" | Import-Clixml) + $failedcount += $results | Select-Object -ExpandProperty FailedCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum # pester 5 output $faileditems = $results5 | Select-Object -ExpandProperty Tests | Where-Object { $_.Passed -notlike $True } if ($faileditems) { Write-Warning "Failed tests summary (pester 5):" $faileditems | ForEach-Object { $name = $_.Name - $failedCount += 1 [pscustomobject]@{ Path = $_.Path -Join '/' Name = "It $name" From c284f1a4a414cdf4ffef1c5bf2c0ec44aef86f9c Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 23:39:05 +0200 Subject: [PATCH 07/12] whoopsie (do Invoke-DbaQuery) --- tests/Invoke-DbaQuery.Tests.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Invoke-DbaQuery.Tests.ps1 b/tests/Invoke-DbaQuery.Tests.ps1 index df4ca7e79b..3c94138c0b 100644 --- a/tests/Invoke-DbaQuery.Tests.ps1 +++ b/tests/Invoke-DbaQuery.Tests.ps1 @@ -353,6 +353,4 @@ SELECT 2 $results = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query "select cast(null as hierarchyid)" $results.Column1 | Should -Be "NULL" } - } - From f681e78def92ff8d27348ce78cd198ef508f6277 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Mon, 21 Oct 2024 23:50:02 +0200 Subject: [PATCH 08/12] exceptions checks are stricter (do Invoke-DbaQuery) --- tests/Invoke-DbaQuery.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Invoke-DbaQuery.Tests.ps1 b/tests/Invoke-DbaQuery.Tests.ps1 index 3c94138c0b..9636656cbc 100644 --- a/tests/Invoke-DbaQuery.Tests.ps1 +++ b/tests/Invoke-DbaQuery.Tests.ps1 @@ -332,7 +332,7 @@ SELECT 2 $result = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query $q -NoExec $result | Should -BeNullOrEmpty - { Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "SELEC p FROM c" -NoExec -EnableException } | Should -Throw "Incorrect syntax near 'SELEC'" + { Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "SELEC p FROM c" -NoExec -EnableException } | Should -Throw "Incorrect syntax near 'SELEC'." } It "supports dropping temp objects (#8472)" { From 1b6d2812d79c538390f782d2ff98f5caa71d2577 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Tue, 22 Oct 2024 00:04:06 +0200 Subject: [PATCH 09/12] try detect coverage files for pester5 (do Invoke-DbaQuery) --- tests/appveyor.post.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/appveyor.post.ps1 b/tests/appveyor.post.ps1 index 7d695a4560..c515652e45 100644 --- a/tests/appveyor.post.ps1 +++ b/tests/appveyor.post.ps1 @@ -5,11 +5,12 @@ Push-AppveyorArtifact PesterResultsCoverage.json -FileName "PesterResultsCoverag codecov -f PesterResultsCoverage.json --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null Write-Host -Object "appveyor.post: Sending coverage data (pester 5)" -ForeGroundColor DarkGreen -$ProjectRoot = $env:APPVEYOR_BUILD_FOLDER, -$ModuleBase = $ProjectRoot, +$ProjectRoot = $env:APPVEYOR_BUILD_FOLDER +$ModuleBase = $ProjectRoot $pester5CoverageFiles = Get-ChildItem -Path "$ModuleBase\Pester5Coverage*.xml" foreach($coverageFile in $pester5CoverageFiles) { + Write-Host -Object "appveyor.post: Sending $($coverageFile.FullName)" -ForeGroundColor DarkGreen Push-AppveyorArtifact $coverageFile.FullName -FileName $coverageFile.Name codecov -f $coverageFile.FullName --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null } From 389a334a27dfbde559ee2055af6c31202efa517f Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Tue, 22 Oct 2024 00:07:53 +0200 Subject: [PATCH 10/12] flag pester5 explicitely --- tests/appveyor.post.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/appveyor.post.ps1 b/tests/appveyor.post.ps1 index c515652e45..b8efc5eed4 100644 --- a/tests/appveyor.post.ps1 +++ b/tests/appveyor.post.ps1 @@ -12,7 +12,7 @@ foreach($coverageFile in $pester5CoverageFiles) { Write-Host -Object "appveyor.post: Sending $($coverageFile.FullName)" -ForeGroundColor DarkGreen Push-AppveyorArtifact $coverageFile.FullName -FileName $coverageFile.Name - codecov -f $coverageFile.FullName --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null + codecov -f $coverageFile.FullName --flag "ps,pester5,$($env:SCENARIO.ToLowerInvariant())" | Out-Null } $sw.Stop() From 6047758a8ef61a861377a46e099ac06d2f4fcc68 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Tue, 22 Oct 2024 00:27:47 +0200 Subject: [PATCH 11/12] yet another pester5 small difference --- tests/appveyor.pester.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/appveyor.pester.ps1 b/tests/appveyor.pester.ps1 index fbff87e38f..038cd00a01 100644 --- a/tests/appveyor.pester.ps1 +++ b/tests/appveyor.pester.ps1 @@ -277,9 +277,9 @@ if (-not $Finalize) { $outcome = "Passed" if ($PesterRun.FailedCount -gt 0) { $trialno += 1 - Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Failed" -Duration $PesterRun.Time.TotalMilliseconds + Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Failed" -Duration $PesterRun.Duration.TotalMilliseconds } else { - Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Passed" -Duration $PesterRun.Time.TotalMilliseconds + Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Passed" -Duration $PesterRun.Duration.TotalMilliseconds break } } From 2694422869211f7597ae6a5e4afdc4b0d6363ec6 Mon Sep 17 00:00:00 2001 From: Simone Bizzotto Date: Tue, 22 Oct 2024 00:29:54 +0200 Subject: [PATCH 12/12] multiple flags are now discouraged in codecov --- tests/appveyor.post.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/appveyor.post.ps1 b/tests/appveyor.post.ps1 index b8efc5eed4..688d7fe675 100644 --- a/tests/appveyor.post.ps1 +++ b/tests/appveyor.post.ps1 @@ -2,7 +2,7 @@ Add-AppveyorTest -Name "appveyor.post" -Framework NUnit -FileName "appveyor.post $sw = [system.diagnostics.stopwatch]::startNew() Write-Host -Object "appveyor.post: Sending coverage data (pester 4)" -ForeGroundColor DarkGreen Push-AppveyorArtifact PesterResultsCoverage.json -FileName "PesterResultsCoverage" -codecov -f PesterResultsCoverage.json --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null +codecov -f PesterResultsCoverage.json --flag "pester4_$($env:SCENARIO.ToLowerInvariant())" | Out-Null Write-Host -Object "appveyor.post: Sending coverage data (pester 5)" -ForeGroundColor DarkGreen $ProjectRoot = $env:APPVEYOR_BUILD_FOLDER @@ -12,7 +12,7 @@ foreach($coverageFile in $pester5CoverageFiles) { Write-Host -Object "appveyor.post: Sending $($coverageFile.FullName)" -ForeGroundColor DarkGreen Push-AppveyorArtifact $coverageFile.FullName -FileName $coverageFile.Name - codecov -f $coverageFile.FullName --flag "ps,pester5,$($env:SCENARIO.ToLowerInvariant())" | Out-Null + codecov -f $coverageFile.FullName --flag "pester5_$($env:SCENARIO.ToLowerInvariant())" | Out-Null } $sw.Stop()