diff --git a/CHANGELOG.md b/CHANGELOG.md index 8136df93b1..7a18d0d121 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Refactored integration tests to remove `finally` blocks from `It`-blocks and + use Pester `BeforeEach`/`AfterEach` blocks instead, following DSC Community + coding guidelines. This improves test cleanup reliability and maintainability + across the following test files ([issue #2288](https://github.com/dsccommunity/SqlServerDsc/issues/2288)): + - `New-SqlDscLogin.Integration.Tests.ps1` + - `New-SqlDscAudit.Integration.Tests.ps1` + - `Get-SqlDscDatabasePermission.Integration.Tests.ps1` + - `Get-SqlDscPreferredModule.Integration.Tests.ps1` + - `New-SqlDscDatabase.Integration.Tests.ps1` +- Refactored unit tests to remove `finally` blocks from `It`-blocks and + use Pester `AfterEach` blocks instead, following DSC Community coding + guidelines. This improves test cleanup reliability and maintainability + across the following test files ([issue #2288](https://github.com/dsccommunity/SqlServerDsc/issues/2288)): + - `Set-SqlDscConfigurationOption.Tests.ps1` + - `Get-SqlDscConfigurationOption.Tests.ps1` + - `Test-SqlDscConfigurationOption.Tests.ps1` - `Test-SqlDscIsDatabasePrincipal` and `Get-SqlDscDatabasePermission` - Added `Refresh` parameter to refresh SMO collections before checking database principals, addressing issues with custom database roles created diff --git a/tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1 b/tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1 index ee0a88e9e0..e643a54286 100644 --- a/tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1 +++ b/tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1 @@ -163,6 +163,19 @@ Describe 'Get-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integrati } Context 'When working with built-in database roles' { + BeforeEach { + $script:customRoleName = $null + } + + AfterEach { + # Clean up the custom role if it was created + if ($script:customRoleName) + { + $dropRoleSql = "USE [$($script:testDatabaseName)]; DROP ROLE [$script:customRoleName];" + Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $dropRoleSql -Force -ErrorAction 'SilentlyContinue' + } + } + It 'Should return permissions for db_datareader role' { # Note: The command excludes fixed roles by default, so this should return null or empty $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' -ErrorAction 'SilentlyContinue' @@ -173,33 +186,24 @@ Describe 'Get-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integrati It 'Should work with non-fixed database roles when they exist' { # Create a custom database role for testing - $customRoleName = 'TestRole_' + (Get-Random) - $createRoleSql = "USE [$($script:testDatabaseName)]; CREATE ROLE [$customRoleName];" + $script:customRoleName = 'TestRole_' + (Get-Random) + $createRoleSql = "USE [$($script:testDatabaseName)]; CREATE ROLE [$script:customRoleName];" Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $createRoleSql -Force -ErrorAction 'Stop' - try - { - # Grant a permission to the custom role - $grantRolePermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT TO [$customRoleName];" - Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantRolePermissionSql -Force -ErrorAction 'Stop' + # Grant a permission to the custom role + $grantRolePermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT TO [$script:customRoleName];" + Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantRolePermissionSql -Force -ErrorAction 'Stop' - # Test getting permissions for the custom role - $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $customRoleName -Refresh + # Test getting permissions for the custom role + $result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:customRoleName -Refresh - $result | Should -Not -BeNullOrEmpty - $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo] + $result | Should -Not -BeNullOrEmpty + $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo] - # Verify the Connect permission we granted is present - $connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true } - $connectPermission | Should -Not -BeNullOrEmpty - $connectPermission.PermissionState | Should -Be 'Grant' - } - finally - { - # Clean up the custom role - $dropRoleSql = "USE [$($script:testDatabaseName)]; DROP ROLE [$customRoleName];" - Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $dropRoleSql -Force -ErrorAction 'SilentlyContinue' - } + # Verify the Connect permission we granted is present + $connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true } + $connectPermission | Should -Not -BeNullOrEmpty + $connectPermission.PermissionState | Should -Be 'Grant' } } } diff --git a/tests/Integration/Commands/Get-SqlDscPreferredModule.Integration.Tests.ps1 b/tests/Integration/Commands/Get-SqlDscPreferredModule.Integration.Tests.ps1 index 9ddb85d47d..6d1cab2e9e 100644 --- a/tests/Integration/Commands/Get-SqlDscPreferredModule.Integration.Tests.ps1 +++ b/tests/Integration/Commands/Get-SqlDscPreferredModule.Integration.Tests.ps1 @@ -210,25 +210,28 @@ Describe 'Get-SqlDscPreferredModule' -Tag @('Integration_SQL2017', 'Integration_ } } - It 'Should throw an error when specified version does not exist' { - # Backup original environment variable - $originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion - - try { - # Set to a non-existent version - $env:SMODefaultModuleVersion = '999.999.999' - - { Get-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule' + Context 'When testing error handling' { + BeforeEach { + # Capture the current environment variable value + $script:originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion } - finally { - # Restore original environment variable - if ($originalSMODefaultModuleVersion) { - $env:SMODefaultModuleVersion = $originalSMODefaultModuleVersion + + AfterEach { + # Restore the environment variable + if ($script:originalSMODefaultModuleVersion) { + $env:SMODefaultModuleVersion = $script:originalSMODefaultModuleVersion } else { Remove-Item -Path 'env:SMODefaultModuleVersion' -ErrorAction 'SilentlyContinue' } } + + It 'Should throw an error when specified version does not exist' { + # Set to a non-existent version + $env:SMODefaultModuleVersion = '999.999.999' + + { Get-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule' + } } } } diff --git a/tests/Integration/Commands/New-SqlDscAudit.Integration.Tests.ps1 b/tests/Integration/Commands/New-SqlDscAudit.Integration.Tests.ps1 index a612d5ab33..5b163a8e85 100644 --- a/tests/Integration/Commands/New-SqlDscAudit.Integration.Tests.ps1 +++ b/tests/Integration/Commands/New-SqlDscAudit.Integration.Tests.ps1 @@ -105,30 +105,18 @@ Describe 'New-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019', } It 'Should create a security log audit successfully' { - $securityLogAuditName = 'SqlDscTestAudit_SecLog_' + (Get-Random) + $script:testAuditName = 'SqlDscTestAudit_SecLog_' + (Get-Random) - try - { - $result = New-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -LogType 'SecurityLog' -PassThru -Force -ErrorAction Stop + $result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'SecurityLog' -PassThru -Force -ErrorAction Stop - $result | Should -Not -BeNullOrEmpty - $result.Name | Should -Be $securityLogAuditName - $result.DestinationType | Should -Be 'SecurityLog' + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be $script:testAuditName + $result.DestinationType | Should -Be 'SecurityLog' - # Verify the audit exists in the server - $createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction Stop - $createdAudit | Should -Not -BeNullOrEmpty - $createdAudit.DestinationType | Should -Be 'SecurityLog' - } - finally - { - # Clean up - $auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction 'SilentlyContinue' - if ($auditToRemove) - { - Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue' - } - } + # Verify the audit exists in the server + $createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop + $createdAudit | Should -Not -BeNullOrEmpty + $createdAudit.DestinationType | Should -Be 'SecurityLog' } It 'Should support PassThru parameter' { diff --git a/tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1 b/tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1 index 3083c90b6f..c0146e2a32 100644 --- a/tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1 +++ b/tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1 @@ -96,25 +96,29 @@ Describe 'New-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019 } Context 'When using the Refresh parameter' { - It 'Should refresh the database collection before creating' { - $uniqueName = 'SqlDscTestRefresh_' + (Get-Random) - - try - { - $result = New-SqlDscDatabase -ServerObject $script:serverObject -Name $uniqueName -Refresh -Force -ErrorAction Stop + BeforeEach { + $script:refreshTestDbName = $null + } - $result | Should -Not -BeNullOrEmpty - $result.Name | Should -Be $uniqueName - } - finally + AfterEach { + # Clean up the refresh test database if it was created + if ($script:refreshTestDbName) { - # Clean up - $dbToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $uniqueName -ErrorAction 'SilentlyContinue' + $dbToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:refreshTestDbName -ErrorAction 'SilentlyContinue' if ($dbToRemove) { $null = Remove-SqlDscDatabase -DatabaseObject $dbToRemove -Force } } } + + It 'Should refresh the database collection before creating' { + $script:refreshTestDbName = 'SqlDscTestRefresh_' + (Get-Random) + + $result = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:refreshTestDbName -Refresh -Force -ErrorAction Stop + + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be $script:refreshTestDbName + } } } diff --git a/tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1 b/tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1 index df0473a833..877d2d00e0 100644 --- a/tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1 +++ b/tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1 @@ -63,116 +63,107 @@ Describe 'New-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', $script:testPassword = ConvertTo-SecureString -String 'P@ssw0rd123!' -AsPlainText -Force } - It 'Should create a SQL Server login without error' { - $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testSqlLoginName -SqlLogin -SecurePassword $script:testPassword -Force + BeforeEach { + $script:testLoginName = $null + $script:isPersistentLogin = $false } - It 'Should verify the SQL Server login was created' { - Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testSqlLoginName | Should -BeTrue + AfterEach { + # Clean up any login created in the test (but not persistent logins) + if ($script:testLoginName -and -not $script:isPersistentLogin -and (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testLoginName)) + { + $script:serverObject.Logins[$script:testLoginName].Drop() + } } - It 'Should verify the login type is SqlLogin' { - $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testSqlLoginName + It 'Should create a SQL Server login and verify it was created with correct properties' { + $script:testLoginName = $script:testSqlLoginName + $script:isPersistentLogin = $true # This is a persistent login for other tests + + # Only create if it doesn't already exist + if (-not (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testLoginName)) + { + $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -SqlLogin -SecurePassword $script:testPassword -Force + } + + # Verify the login was created + Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testLoginName | Should -BeTrue + + # Verify the login type + $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName $loginObject.LoginType | Should -Be 'SqlLogin' - } - It 'Should verify the default database is set correctly' { - $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testSqlLoginName + # Verify the default database $loginObject.DefaultDatabase | Should -Be 'master' } It 'Should create a SQL Server login with custom default database' { - $customLoginName = 'IntegrationTestCustomDb' + $script:testLoginName = 'IntegrationTestCustomDb' - try - { - $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $customLoginName -SqlLogin -SecurePassword $script:testPassword -DefaultDatabase 'tempdb' -Force + $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -SqlLogin -SecurePassword $script:testPassword -DefaultDatabase 'tempdb' -Force - $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $customLoginName - $loginObject.DefaultDatabase | Should -Be 'tempdb' - } - finally - { - # Clean up - if (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $customLoginName) - { - $script:serverObject.Logins[$customLoginName].Drop() - } - } + $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName + $loginObject.DefaultDatabase | Should -Be 'tempdb' } It 'Should create a SQL Server login with PassThru parameter' { - $passthroughLoginName = 'IntegrationTestPassThru' + $script:testLoginName = 'IntegrationTestPassThru' - try - { - $result = New-SqlDscLogin -ServerObject $script:serverObject -Name $passthroughLoginName -SqlLogin -SecurePassword $script:testPassword -PassThru -Force + $result = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -SqlLogin -SecurePassword $script:testPassword -PassThru -Force - $result | Should -Not -BeNullOrEmpty - $result.Name | Should -Be $passthroughLoginName - $result.LoginType | Should -Be 'SqlLogin' - } - finally - { - # Clean up - if (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $passthroughLoginName) - { - $script:serverObject.Logins[$passthroughLoginName].Drop() - } - } + $result | Should -Not -BeNullOrEmpty + $result.Name | Should -Be $script:testLoginName + $result.LoginType | Should -Be 'SqlLogin' } It 'Should create a disabled SQL Server login' { - $disabledLoginName = 'IntegrationTestDisabled' + $script:testLoginName = 'IntegrationTestDisabled' - try - { - $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $disabledLoginName -SqlLogin -SecurePassword $script:testPassword -Disabled -Force + $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -SqlLogin -SecurePassword $script:testPassword -Disabled -Force - $loginObject = $script:serverObject.Logins[$disabledLoginName] - $loginObject.IsDisabled | Should -BeTrue - } - finally - { - # Clean up - if (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $disabledLoginName) - { - $script:serverObject.Logins[$disabledLoginName].Drop() - } - } + $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName + $loginObject.IsDisabled | Should -BeTrue } It 'Should throw an error when trying to create a login that already exists' { - { New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testSqlLoginName -SqlLogin -SecurePassword $script:testPassword -Force } | Should -Throw + $script:testLoginName = 'IntegrationTestDuplicate' + + # First create the login + $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -SqlLogin -SecurePassword $script:testPassword -Force + + # Then try to create it again, which should throw + { New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testLoginName -SqlLogin -SecurePassword $script:testPassword -Force } | Should -Throw } } Context 'When creating a Windows user login' { - It 'Should create a Windows user login without error' { - try + AfterEach { + # Clean up Windows user login + if (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName) { - # Using the SqlIntegrationTest user created by Prerequisites integration test - $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName -WindowsUser -Force + $script:serverObject.Logins[$script:testWindowsUserName].Drop() + } + } + + It 'Should create a Windows user login without error' { + # Using the SqlIntegrationTest user created by Prerequisites integration test + $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName -WindowsUser -Force - Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName | Should -BeTrue + Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName | Should -BeTrue - $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName - $loginObject.LoginType | Should -Be 'WindowsUser' - } - finally - { - # Clean up - if (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName) - { - $script:serverObject.Logins[$script:testWindowsUserName].Drop() - } - } + $loginObject = Get-SqlDscLogin -ServerObject $script:serverObject -Name $script:testWindowsUserName + $loginObject.LoginType | Should -Be 'WindowsUser' } } Context 'When creating a Windows group login' { It 'Should create a Windows group login without error' { - $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testWindowsGroupName -WindowsGroup -Force + # This is a persistent login for other tests + # Only create if it doesn't already exist + if (-not (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testWindowsGroupName)) + { + $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:testWindowsGroupName -WindowsGroup -Force + } Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:testWindowsGroupName | Should -BeTrue @@ -182,24 +173,23 @@ Describe 'New-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', } Context 'When using Force parameter' { - It 'Should create a login with Force parameter without confirmation prompt' { - $forceLoginName = 'IntegrationTestForce' - - try - { - $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $forceLoginName -SqlLogin -SecurePassword $script:testPassword -Force + BeforeEach { + $script:forceLoginName = 'IntegrationTestForce' + } - Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $forceLoginName | Should -BeTrue - } - finally + AfterEach { + # Clean up Force test login + if ($script:forceLoginName -and (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:forceLoginName)) { - # Clean up - if (Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $forceLoginName) - { - $script:serverObject.Logins[$forceLoginName].Drop() - } + $script:serverObject.Logins[$script:forceLoginName].Drop() } } + + It 'Should create a login with Force parameter without confirmation prompt' { + $null = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:forceLoginName -SqlLogin -SecurePassword $script:testPassword -Force + + Test-SqlDscIsLogin -ServerObject $script:serverObject -Name $script:forceLoginName | Should -BeTrue + } } Context 'When running with WhatIf' { diff --git a/tests/Integration/Commands/README.md b/tests/Integration/Commands/README.md index 3c8e88081f..ba1a27f433 100644 --- a/tests/Integration/Commands/README.md +++ b/tests/Integration/Commands/README.md @@ -63,23 +63,23 @@ Get-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTE Set-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Set-SqlDscStartupParameter | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Set-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - -Disable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - -Enable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Disable-SqlDscLogin | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Enable-SqlDscLogin | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Enable-SqlDscAudit | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - -Test-SqlDscIsLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - -Test-SqlDscIsLoginEnabled | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Test-SqlDscIsLogin | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Test-SqlDscIsLoginEnabled | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - New-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | SqlDscIntegrationTestRole_Persistent role Get-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Get-SqlDscStartupParameter | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Test-SqlDscIsRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Test-SqlDscIsDatabasePrincipal | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test database and database principals -Grant-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Grants CreateEndpoint permission to role -Get-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - -Set-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Grant-SqlDscServerPermission | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Grants CreateEndpoint permission to role +Get-SqlDscServerPermission | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Set-SqlDscServerPermission | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - ConvertFrom-SqlDscServerPermission | 2 | 0 (Prerequisites) | - | - -Test-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - -Deny-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Denies AlterTrace permission to login (persistent) -Revoke-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Test-SqlDscServerPermission | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Deny-SqlDscServerPermission | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Denies AlterTrace permission to login (persistent) +Revoke-SqlDscServerPermission | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Get-SqlDscDatabase | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - ConvertFrom-SqlDscDatabasePermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - New-SqlDscDatabase | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test databases @@ -88,7 +88,7 @@ Test-SqlDscDatabase | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTE Get-SqlDscDatabasePermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test database, Test user Invoke-SqlDscQuery | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test database and table ConvertTo-SqlDscDatabasePermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - -Set-SqlDscDatabasePermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Set-SqlDscDatabasePermission | 2 | 2 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Get-SqlDscAgentAlert | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - New-SqlDscAgentAlert | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test alerts New-SqlDscAudit | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test audits diff --git a/tests/Unit/Public/Get-SqlDscConfigurationOption.Tests.ps1 b/tests/Unit/Public/Get-SqlDscConfigurationOption.Tests.ps1 index c0a0a9402e..fd87d52910 100644 --- a/tests/Unit/Public/Get-SqlDscConfigurationOption.Tests.ps1 +++ b/tests/Unit/Public/Get-SqlDscConfigurationOption.Tests.ps1 @@ -407,6 +407,19 @@ Describe 'Get-SqlDscConfigurationOption' -Tag 'Public' { $completions[1].CompletionText | Should -Be "'max degree of parallelism'" } + AfterAll { + # Clean up global variables created in error handling tests + if (Get-Variable -Name 'BadTestServerObject' -Scope Global -ErrorAction SilentlyContinue) + { + Remove-Variable -Name 'BadTestServerObject' -Scope Global -Force + } + + if (Get-Variable -Name 'InvalidTestServerObject' -Scope Global -ErrorAction SilentlyContinue) + { + Remove-Variable -Name 'InvalidTestServerObject' -Scope Global -Force + } + } + It 'Should handle tab completion errors gracefully' { # Create a server object that will cause an error $badServer = [Microsoft.SqlServer.Management.Smo.Server]::CreateTypeInstance() @@ -416,20 +429,15 @@ Describe 'Get-SqlDscConfigurationOption' -Tag 'Public' { } -Force $global:BadTestServerObject = $badServer - try { - $inputScript = 'Get-SqlDscConfigurationOption -ServerObject $global:BadTestServerObject -Name test' - $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length + $inputScript = 'Get-SqlDscConfigurationOption -ServerObject $global:BadTestServerObject -Name test' + $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length - <# - Should not throw an error and should return empty from argument completer, but then - TabExpansion2 itself returns some default completions (like filesystem paths). - #> - $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) - $result.CompletionMatches.ListItemText | Should -Be 'tests' - } - finally { - Remove-Variable -Name 'BadTestServerObject' -Scope Global -Force -ErrorAction SilentlyContinue - } + <# + Should not throw an error and should return empty from argument completer, but then + TabExpansion2 itself returns some default completions (like filesystem paths). + #> + $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) + $result.CompletionMatches.ListItemText | Should -Be 'tests' } It 'Should handle missing ServerObject in tab completion gracefully' { @@ -450,20 +458,15 @@ Describe 'Get-SqlDscConfigurationOption' -Tag 'Public' { $invalidServer = 'Not a server object' $global:InvalidTestServerObject = $invalidServer - try { - $inputScript = 'Get-SqlDscConfigurationOption -ServerObject $global:InvalidTestServerObject -Name test' - $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length + $inputScript = 'Get-SqlDscConfigurationOption -ServerObject $global:InvalidTestServerObject -Name test' + $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length - <# - Should not throw an error and should return empty from argument completer, but then - TabExpansion2 itself returns some default completions (like filesystem paths). - #> - $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) - $result.CompletionMatches.ListItemText | Should -Be 'tests' - } - finally { - Remove-Variable -Name 'InvalidTestServerObject' -Scope Global -Force -ErrorAction SilentlyContinue - } + <# + Should not throw an error and should return empty from argument completer, but then + TabExpansion2 itself returns some default completions (like filesystem paths). + #> + $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) + $result.CompletionMatches.ListItemText | Should -Be 'tests' } } } diff --git a/tests/Unit/Public/Set-SqlDscConfigurationOption.Tests.ps1 b/tests/Unit/Public/Set-SqlDscConfigurationOption.Tests.ps1 index 1ac12c194c..15b98b18ed 100644 --- a/tests/Unit/Public/Set-SqlDscConfigurationOption.Tests.ps1 +++ b/tests/Unit/Public/Set-SqlDscConfigurationOption.Tests.ps1 @@ -523,6 +523,11 @@ Describe 'Set-SqlDscConfigurationOption' -Tag 'Public' { } } + AfterEach { + # Clean up any global test variables created during tab completion tests + Remove-Variable -Name 'BadTestServerObject' -Scope Global -Force -ErrorAction SilentlyContinue + } + It 'Should provide Name parameter completions through TabExpansion2' { # This actually exercises the argument completer code through PowerShell's tab completion system $inputScript = 'Set-SqlDscConfigurationOption -ServerObject $global:TestServerObject -Name max' @@ -602,20 +607,15 @@ Describe 'Set-SqlDscConfigurationOption' -Tag 'Public' { } -Force $global:BadTestServerObject = $badServer - try { - $inputScript = 'Set-SqlDscConfigurationOption -ServerObject $global:BadTestServerObject -Name test' - $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length - - <# - Should not throw an error and should return empty from argument completer, but then - TabExpansion2 itself returns some default completions (like filesystem paths). - #> - $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) - $result.CompletionMatches.ListItemText | Should -Be 'tests' - } - finally { - Remove-Variable -Name 'BadTestServerObject' -Scope Global -Force -ErrorAction SilentlyContinue - } + $inputScript = 'Set-SqlDscConfigurationOption -ServerObject $global:BadTestServerObject -Name test' + $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length + + <# + Should not throw an error and should return empty from argument completer, but then + TabExpansion2 itself returns some default completions (like filesystem paths). + #> + $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) + $result.CompletionMatches.ListItemText | Should -Be 'tests' } It 'Should handle missing ServerObject in tab completion gracefully' { diff --git a/tests/Unit/Public/Test-SqlDscConfigurationOption.Tests.ps1 b/tests/Unit/Public/Test-SqlDscConfigurationOption.Tests.ps1 index 217dc2d7cb..49c4f77dba 100644 --- a/tests/Unit/Public/Test-SqlDscConfigurationOption.Tests.ps1 +++ b/tests/Unit/Public/Test-SqlDscConfigurationOption.Tests.ps1 @@ -339,6 +339,11 @@ Describe 'Test-SqlDscConfigurationOption' -Tag 'Public' { } } + AfterEach { + # Clean up any global test variables created during tab completion tests + Remove-Variable -Name 'BadTestServerObject' -Scope Global -Force -ErrorAction SilentlyContinue + } + It 'Should provide Name parameter completions through TabExpansion2' { # This actually exercises the argument completer code through PowerShell's tab completion system $inputScript = 'Test-SqlDscConfigurationOption -ServerObject $global:TestServerObject -Name max' @@ -418,20 +423,15 @@ Describe 'Test-SqlDscConfigurationOption' -Tag 'Public' { } -Force $global:BadTestServerObject = $badServer - try { - $inputScript = 'Test-SqlDscConfigurationOption -ServerObject $global:BadTestServerObject -Name test' - $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length + $inputScript = 'Test-SqlDscConfigurationOption -ServerObject $global:BadTestServerObject -Name test' + $result = TabExpansion2 -inputScript $inputScript -cursorColumn $inputScript.Length - <# - Should not throw an error and should return empty from argument completer, but then - TabExpansion2 itself returns some default completions (like filesystem paths). - #> - $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) - $result.CompletionMatches.ListItemText | Should -Be 'tests' - } - finally { - Remove-Variable -Name 'BadTestServerObject' -Scope Global -Force -ErrorAction SilentlyContinue - } + <# + Should not throw an error and should return empty from argument completer, but then + TabExpansion2 itself returns some default completions (like filesystem paths). + #> + $result | Should -BeOfType ([System.Management.Automation.CommandCompletion]) + $result.CompletionMatches.ListItemText | Should -Be 'tests' } It 'Should handle missing ServerObject in tab completion gracefully' {