Skip to content

Commit 5fccaeb

Browse files
committed
Sync-DbaLoginPermission.Tests - Spell master in lower case and capture the warning for real
The setup batch says USE Master, which a case sensitive instance rejects ("Database 'Master' does not exist"), so the whole batch is dropped, the login never exists on the source and the sync finds nothing. The setup now runs with EnableException like the other files, so a failing setup fails the file instead of two tests later on. The test also passed -WarningVariable $warn, an empty variable name, which hid that warning and made the warning assertion vacuous. (do Sync-DbaLoginPermission)
1 parent d245590 commit 5fccaeb

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

tests/Sync-DbaLoginPermission.Tests.ps1

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ Describe $CommandName -Tag UnitTests {
2626

2727
Describe $CommandName -Tag IntegrationTests {
2828
BeforeAll {
29+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
30+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
31+
2932
$tempguid = [guid]::newguid()
3033
$DBUserName = "dbatoolssci_$($tempguid.guid)"
3134
$CreateTestUser = @"
3235
CREATE LOGIN [$DBUserName]
3336
WITH PASSWORD = '$($tempguid.guid)';
34-
USE Master;
37+
USE master;
3538
CREATE USER [$DBUserName] FOR LOGIN [$DBUserName]
3639
WITH DEFAULT_SCHEMA = dbo;
3740
GRANT VIEW ANY DEFINITION to [$DBUserName];
@@ -43,10 +46,18 @@ GRANT VIEW ANY DEFINITION to [$DBUserName];
4346
CREATE LOGIN [$DBUserName]
4447
WITH PASSWORD = '$($tempguid.guid)';
4548
"@
49+
50+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
51+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
4652
}
4753
AfterAll {
54+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
55+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
56+
4857
$DropTestUser = "DROP LOGIN [$DBUserName]"
4958
Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti1, $TestConfig.InstanceMulti2 -Query $DropTestUser -Database master
59+
60+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
5061
}
5162

5263
Context "Command execution and functionality" {
@@ -59,7 +70,7 @@ CREATE LOGIN [$DBUserName]
5970
It "Should execute against active nodes" {
6071
# Creates the user on
6172
Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti2 -Query $CreateTestLogin
62-
$results = Sync-DbaLoginPermission -Source $TestConfig.InstanceMulti1 -Destination $TestConfig.InstanceMulti2 -Login $DBUserName -ExcludeLogin 'NotaLogin' -WarningVariable $warn
73+
$results = Sync-DbaLoginPermission -Source $TestConfig.InstanceMulti1 -Destination $TestConfig.InstanceMulti2 -Login $DBUserName -ExcludeLogin 'NotaLogin' -WarningVariable warn
6374
$results.Status | Should -Be 'Successful'
6475
$warn | Should -BeNullOrEmpty
6576
}

0 commit comments

Comments
 (0)