Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fe11afe
Refactor tests to use -ErrorAction 'Stop' for better error handling
johlju Oct 11, 2025
1fe27d0
Refactor test cases to suppress error output by assigning command res…
johlju Oct 11, 2025
0354042
Refactor tests to suppress error output by assigning command results …
johlju Oct 11, 2025
2ec83b4
Refactor tests to suppress exceptions on command execution
johlju Oct 11, 2025
8aca1f5
Refactor integration tests to remove unnecessary script blocks
johlju Oct 11, 2025
f7e16e8
Refactor tests to streamline parameter assignment and maintain error …
johlju Oct 12, 2025
ed7b55b
Refactor tests to remove usage of `Should -Not -Throw` and adopt Pest…
johlju Oct 12, 2025
921ca1b
Refactor tests to suppress error output by assigning command results …
johlju Oct 12, 2025
4205ed8
Refactor tests to improve clarity of test descriptions
johlju Oct 12, 2025
e89168f
Refactor tests to improve clarity of test descriptions for sql agent …
johlju Oct 12, 2025
5fc3605
Refactor tests to suppress error output by assigning command results …
johlju Oct 12, 2025
d6de9c0
Refactor Set-TargetResource calls to use -ErrorAction 'Stop' for impr…
johlju Oct 12, 2025
010bf07
Fix formatting of error action guideline to use quotes around 'Stop'
johlju Oct 12, 2025
c59fca7
Fix error action parameter formatting in build script calls
johlju Oct 12, 2025
9260584
Refactor Modify() tests to suppress error output by assigning command…
johlju Oct 12, 2025
0cce110
Refactor error handling in integration and unit tests to use string l…
johlju Oct 12, 2025
ed1851c
Refactor integration tests to eliminate redundant assignment of $null…
johlju Oct 12, 2025
ce6d9cb
Suppress error output in integration tests by assigning command resul…
johlju Oct 12, 2025
ac5eaec
Suppress error output in Modify() tests by assigning command results …
johlju Oct 12, 2025
2275c0f
Refactor error handling in AfterAll blocks to use 'Stop' for ErrorAct…
johlju Oct 12, 2025
aea1f2a
Refactor Remove-SqlDscDatabase call to use 'Stop' for ErrorAction par…
johlju Oct 12, 2025
36a290a
Refactor Get-TargetResource calls to use 'Stop' for ErrorAction param…
johlju Oct 12, 2025
e861d45
Suppress error output during audit cleanup by assigning command resul…
johlju Oct 12, 2025
f68b64b
Suppress error output in New-SqlDscAgentAlert and Invoke-SqlDscQuery …
johlju Oct 12, 2025
67a1b09
Suppress error output in New-SqlDscAgentOperator call by assigning co…
johlju Oct 12, 2025
81bbdbf
Remove unnecessary closing brace in Invoke-SqlDscQuery integration test
johlju Oct 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ applyTo: "tests/[iI]ntegration/**/*.[iI]ntegration.[tT]ests.ps1"
- Avoid `ExpectedMessage` for `Should -Throw` assertions
- Only run integration tests in CI unless explicitly instructed.
- Call commands with `-Force` parameter where applicable (avoids prompting).
- Use `-ErrorAction Stop` on commands so failures surface immediately
- Use `-ErrorAction 'Stop'` on commands so failures surface immediately

## Required Setup Block

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/copilot-setup-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ jobs:
Write-Host 'Resolving project dependencies...'

# Run dependency resolution
./build.ps1 -ResolveDependency -Tasks 'noop' -UseModuleFast -ErrorAction Stop
./build.ps1 -ResolveDependency -Tasks 'noop' -UseModuleFast -ErrorAction 'Stop'

Write-Host 'Dependencies resolved successfully.'

Expand All @@ -225,7 +225,7 @@ jobs:
Write-Host "Building $env:MODULE_NAME module..."

# Build the module
./build.ps1 -Tasks 'build' -ErrorAction Stop
./build.ps1 -Tasks 'build' -ErrorAction 'Stop'

# Verify build output
$builtPath = "output/builtModule/$env:MODULE_NAME"
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
from managed computer and instance objects.
- Enhanced to optionally return all protocols when ProtocolName parameter
is not specified.
- All tests was changed to no longer use `Should -Not -Throw` for testing
that commands do not throw errors. Instead, tests now simply call the command
and will fail if an error is thrown, following best practices (follows
Pester 6 syntax).

### Changed

Expand Down
6 changes: 3 additions & 3 deletions source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function Set-TargetResource
$script:localizedData.RemoveAvailabilityGroup -f $Name, $InstanceName
)

Remove-SqlAvailabilityGroup -InputObject $availabilityGroup -ErrorAction Stop
Remove-SqlAvailabilityGroup -InputObject $availabilityGroup -ErrorAction 'Stop'
}
catch
{
Expand Down Expand Up @@ -368,7 +368,7 @@ function Set-TargetResource
$script:localizedData.CreateAvailabilityGroupReplica -f $newReplicaParams.Name, $Name, $InstanceName
)

$primaryReplica = New-SqlAvailabilityReplica @newReplicaParams -ErrorAction Stop
$primaryReplica = New-SqlAvailabilityReplica @newReplicaParams -ErrorAction 'Stop'
}
catch
{
Expand Down Expand Up @@ -412,7 +412,7 @@ function Set-TargetResource
$script:localizedData.CreateAvailabilityGroup -f $Name, $InstanceName
)

New-SqlAvailabilityGroup @newAvailabilityGroupParams -ErrorAction Stop
New-SqlAvailabilityGroup @newAvailabilityGroupParams -ErrorAction 'Stop'
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function Set-TargetResource
Write-Verbose -Message $script:localizedData.SetAvailabilityGroupListenerDhcpDefaultSubnet
}

New-SqlAvailabilityGroupListener @newListenerParams -ErrorAction Stop | Out-Null
New-SqlAvailabilityGroupListener @newListenerParams -ErrorAction 'Stop' | Out-Null
}
else
{
Expand Down Expand Up @@ -355,7 +355,7 @@ function Set-TargetResource
Port = $Port
}

Set-SqlAvailabilityGroupListener @setListenerParams -ErrorAction Stop | Out-Null
Set-SqlAvailabilityGroupListener @setListenerParams -ErrorAction 'Stop' | Out-Null
}

if (-not $ipAddressEqual)
Expand All @@ -379,7 +379,7 @@ function Set-TargetResource
StaticIp = $newIpAddress
}

Add-SqlAvailabilityGroupListenerStaticIp @setListenerParams -ErrorAction Stop | Out-Null
Add-SqlAvailabilityGroupListenerStaticIp @setListenerParams -ErrorAction 'Stop' | Out-Null
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ function Set-TargetResource
$script:localizedData.RemoveAvailabilityReplica -f $Name, $AvailabilityGroupName, $InstanceName
)

Remove-SqlAvailabilityReplica -InputObject $availabilityGroupReplica -Confirm:$false -ErrorAction Stop
Remove-SqlAvailabilityReplica -InputObject $availabilityGroupReplica -Confirm:$false -ErrorAction 'Stop'
}
catch
{
Expand Down
4 changes: 2 additions & 2 deletions source/DSCResources/DSC_SqlLogin/DSC_SqlLogin.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function Set-TargetResource
$LoginCreateOptions = [Microsoft.SqlServer.Management.Smo.LoginCreateOptions]::None
}

New-SQLServerLogin -Login $login -LoginCreateOptions $LoginCreateOptions -SecureString $LoginCredential.Password -ErrorAction Stop
New-SQLServerLogin -Login $login -LoginCreateOptions $LoginCreateOptions -SecureString $LoginCredential.Password -ErrorAction 'Stop'
}

default
Expand Down Expand Up @@ -674,7 +674,7 @@ function Update-SQLServerLogin
The SecureString object that contains the password for a SQL login.

.EXAMPLE
CreateLogin -Login $login -LoginCreateOptions $LoginCreateOptions -SecureString $LoginCredential.Password -ErrorAction Stop
CreateLogin -Login $login -LoginCreateOptions $LoginCreateOptions -SecureString $LoginCredential.Password -ErrorAction 'Stop'

.EXAMPLE
CreateLogin -Login $login
Expand Down
6 changes: 3 additions & 3 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Copy-ItemWithRobocopy

$quotedPath = '"{0}"' -f $Path
$quotedDestinationPath = '"{0}"' -f $DestinationPath
$robocopyExecutable = Get-Command -Name 'Robocopy.exe' -ErrorAction Stop
$robocopyExecutable = Get-Command -Name 'Robocopy.exe' -ErrorAction 'Stop'

$robocopyArgumentSilent = '/njh /njs /ndl /nc /ns /nfl'
$robocopyArgumentCopySubDirectoriesIncludingEmpty = '/e'
Expand Down Expand Up @@ -284,11 +284,11 @@ function Start-SqlSetupProcess
ArgumentList = $ArgumentList
}

$sqlSetupProcess = Start-Process @startProcessParameters -PassThru -NoNewWindow -ErrorAction Stop
$sqlSetupProcess = Start-Process @startProcessParameters -PassThru -NoNewWindow -ErrorAction 'Stop'

Write-Verbose -Message ($script:localizedData.StartSetupProcess -f $sqlSetupProcess.Id, $startProcessParameters.FilePath, $Timeout) -Verbose

Wait-Process -InputObject $sqlSetupProcess -Timeout $Timeout -ErrorAction Stop
Wait-Process -InputObject $sqlSetupProcess -Timeout $Timeout -ErrorAction 'Stop'

return $sqlSetupProcess.ExitCode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ Describe 'Add-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL201

It 'Should add a single trace flag without error' {
# Act - Add the trace flag
{
Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:singleTestTraceFlag -Force -ErrorAction 'Stop'
} | Should -Not -Throw
$null = Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:singleTestTraceFlag -Force -ErrorAction 'Stop'

# Assert - Verify the trace flag was added
$currentTraceFlags = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
Expand All @@ -74,9 +72,7 @@ Describe 'Add-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL201

It 'Should add multiple trace flags without error' {
# Act - Add the trace flags
{
Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:testTraceFlags -Force -ErrorAction 'Stop'
} | Should -Not -Throw
$null = Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:testTraceFlags -Force -ErrorAction 'Stop'

# Assert - Verify the trace flags were added
$currentTraceFlags = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
Expand All @@ -94,9 +90,7 @@ Describe 'Add-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL201
$beforeCount = ($beforeAddTraceFlags | Where-Object { $_ -eq $script:singleTestTraceFlag }).Count

# Act - Try to add the same trace flag again
{
Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:singleTestTraceFlag -Force -ErrorAction 'Stop'
} | Should -Not -Throw
$null = Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:singleTestTraceFlag -Force -ErrorAction 'Stop'

# Assert - Verify no duplicate was created
$afterAddTraceFlags = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
Expand All @@ -111,9 +105,7 @@ Describe 'Add-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL201
Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:singleTestTraceFlag -Force -ErrorAction 'Stop'

# Act - Add additional trace flags
{
Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:additionalTestTraceFlag -Force -ErrorAction 'Stop'
} | Should -Not -Throw
$null = Add-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -TraceFlag $script:additionalTestTraceFlag -Force -ErrorAction 'Stop'

# Assert - Verify both old and new trace flags exist
$currentTraceFlags = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
Expand Down Expand Up @@ -158,9 +150,7 @@ Describe 'Add-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL201

It 'Should add a single trace flag using ServiceObject parameter' {
# Act - Add the trace flag
{
Add-SqlDscTraceFlag -ServiceObject $script:serviceObject -TraceFlag $script:singleTestTraceFlag -Force -ErrorAction 'Stop'
} | Should -Not -Throw
$null = Add-SqlDscTraceFlag -ServiceObject $script:serviceObject -TraceFlag $script:singleTestTraceFlag -Force -ErrorAction 'Stop'

# Assert - Verify the trace flag was added
$currentTraceFlags = Get-SqlDscTraceFlag -ServiceObject $script:serviceObject -ErrorAction 'Stop'
Expand All @@ -169,9 +159,7 @@ Describe 'Add-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL201

It 'Should add multiple trace flags using ServiceObject parameter' {
# Act - Add the trace flags
{
Add-SqlDscTraceFlag -ServiceObject $script:serviceObject -TraceFlag $script:testTraceFlags -Force -ErrorAction 'Stop'
} | Should -Not -Throw
$null = Add-SqlDscTraceFlag -ServiceObject $script:serviceObject -TraceFlag $script:testTraceFlags -Force -ErrorAction 'Stop'

# Assert - Verify the trace flags were added
$currentTraceFlags = Get-SqlDscTraceFlag -ServiceObject $script:serviceObject -ErrorAction 'Stop'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ BeforeAll {
}

AfterAll {
Remove-Item -Path 'Env:\SqlServerDscCI' -ErrorAction 'SilentlyContinue'
$null = Remove-Item -Path 'Env:\SqlServerDscCI' -ErrorAction 'Stop'

# Unload the module being tested so that it doesn't impact any other tests.
Get-Module -Name $script:moduleName -All | Remove-Module -Force
Expand All @@ -52,34 +52,34 @@ Describe 'Assert-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_S
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:sqlServerInstance -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'

# Create a test operator for assertion tests
$script:sqlServerObject | New-SqlDscAgentOperator -Name 'IntegrationTest_AssertOperator' -EmailAddress '[email protected]' -ErrorAction Stop
$null = $script:sqlServerObject | New-SqlDscAgentOperator -Name 'IntegrationTest_AssertOperator' -EmailAddress '[email protected]' -ErrorAction 'Stop'
}

AfterAll {
$script:sqlServerObject | Remove-SqlDscAgentOperator -Name 'IntegrationTest_AssertOperator' -Force -ErrorAction 'SilentlyContinue'
$null = $script:sqlServerObject | Remove-SqlDscAgentOperator -Name 'IntegrationTest_AssertOperator' -Force -ErrorAction 'Stop'

# Disconnect from the SQL Server
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
}

Context 'When operator exists' {
It 'Should not throw when asserting existing operator' {
$null = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'IntegrationTest_AssertOperator' -ErrorAction Stop
$null = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'IntegrationTest_AssertOperator' -ErrorAction 'Stop'
}

It 'Should not throw when asserting persistent operator created by New-SqlDscAgentOperator' {
$null = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'SqlDscIntegrationTestOperator_Persistent' -ErrorAction Stop
$null = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'SqlDscIntegrationTestOperator_Persistent' -ErrorAction 'Stop'
}

It 'Should not return anything when operator exists' {
$result = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'IntegrationTest_AssertOperator' -ErrorAction Stop
$result = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'IntegrationTest_AssertOperator' -ErrorAction 'Stop'
$result | Should -BeNullOrEmpty
}
}

Context 'When operator does not exist' {
It 'Should throw terminating error when operator does not exist' {
{ Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'NonExistentOperator' -ErrorAction Stop } | Should -Throw
{ Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'NonExistentOperator' -ErrorAction 'Stop' } | Should -Throw
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ Describe 'Assert-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019

Context 'When a login exists' {
It 'Should not throw an error for sa login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'sa' } | Should -Not -Throw
$null = Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'sa' -ErrorAction 'Stop'
}

It 'Should not throw an error when using pipeline' {
{ $script:serverObject | Assert-SqlDscLogin -Name 'sa' } | Should -Not -Throw
$null = $script:serverObject | Assert-SqlDscLogin -Name 'sa' -ErrorAction 'Stop'
}

It 'Should not throw an error for NT AUTHORITY\SYSTEM login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'NT AUTHORITY\SYSTEM' } | Should -Not -Throw
$null = Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'NT AUTHORITY\SYSTEM' -ErrorAction 'Stop'
}

It 'Should not throw an error for SqlAdmin login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name ('{0}\SqlAdmin' -f $script:computerName) } | Should -Not -Throw
$null = Assert-SqlDscLogin -ServerObject $script:serverObject -Name ('{0}\SqlAdmin' -f $script:computerName) -ErrorAction 'Stop'
}
}

Expand Down
Loading
Loading