From cce75a3fee59e0e973889fb7aad83fbfde91a54e Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Wed, 24 Apr 2019 13:50:33 +0200 Subject: [PATCH] SqlScript/SqlScriptQuery: Added localization (#1324) - Changes to SqlScript - Added en-US localization (issue #624). - Added additional unit tests for code coverage. - Changes to SqlScriptQuery - Added en-US localization. - Added additional unit tests for code coverage. --- CHANGELOG.md | 4 ++ .../MSFT_SqlScript/MSFT_SqlScript.psm1 | 26 +++++++ .../en-US/MSFT_SqlScript.strings.psd1 | 8 +++ .../MSFT_SqlScriptQuery.psm1 | 26 +++++++ .../en-US/MSFT_SqlScriptQuery.strings.psd1 | 8 +++ Tests/Unit/MSFT_SqlScript.Tests.ps1 | 71 ++++++++++++------- Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 | 70 +++++++++++------- 7 files changed, 159 insertions(+), 54 deletions(-) create mode 100644 DSCResources/MSFT_SqlScript/en-US/MSFT_SqlScript.strings.psd1 create mode 100644 DSCResources/MSFT_SqlScriptQuery/en-US/MSFT_SqlScriptQuery.strings.psd1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e5696dba..d43701bc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,11 +11,15 @@ `Invoke-SqlScript` so that `PRINT` statements is outputted correctly when verbose output is requested, e.g `Start-DscConfiguration -Verbose`. + - Added en-US localization ([issue #624](https://github.com/PowerShell/SqlServerDsc/issues/624)). + - Added additional unit tests for code coverage. - Changes to SqlScriptQuery - Correctly passes the `$VerbosePreference` to the helper function `Invoke-SqlScript` so that `PRINT` statements is outputted correctly when verbose output is requested, e.g `Start-DscConfiguration -Verbose`. + - Added en-US localization. + - Added additional unit tests for code coverage. - Changes to SqlSetup - Concatenated Robocopy localization strings ([issue #694](https://github.com/PowerShell/SqlServerDsc/issues/694)). - Changes to SqlWaitForAG diff --git a/DSCResources/MSFT_SqlScript/MSFT_SqlScript.psm1 b/DSCResources/MSFT_SqlScript/MSFT_SqlScript.psm1 index 3197d99bd..bc2b8e4be 100644 --- a/DSCResources/MSFT_SqlScript/MSFT_SqlScript.psm1 +++ b/DSCResources/MSFT_SqlScript/MSFT_SqlScript.psm1 @@ -7,6 +7,8 @@ Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath ' $script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common' Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1') +$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlScript' + <# .SYNOPSIS Returns the current state of the SQL Server features. @@ -90,6 +92,10 @@ function Get-TargetResource ErrorAction = 'Stop' } + Write-Verbose -Message ( + $script:localizedData.ExecutingGetScript -f $GetFilePath, $ServerInstance + ) + $result = Invoke-SqlScript @invokeParameters $getResult = Out-String -InputObject $result @@ -177,6 +183,10 @@ function Set-TargetResource $Variable ) + Write-Verbose -Message ( + $script:localizedData.ExecutingSetScript -f $SetFilePath, $ServerInstance + ) + $invokeParameters = @{ ServerInstance = $ServerInstance InputFile = $SetFilePath @@ -261,8 +271,16 @@ function Test-TargetResource $Variable ) + Write-Verbose -Message ( + $script:localizedData.TestingConfiguration + ) + try { + Write-Verbose -Message ( + $script:localizedData.ExecutingTestScript -f $TestFilePath, $ServerInstance + ) + $invokeParameters = @{ ServerInstance = $ServerInstance InputFile = $TestFilePath @@ -277,10 +295,18 @@ function Test-TargetResource if ($null -eq $result) { + Write-Verbose -Message ( + $script:localizedData.InDesiredState + ) + return $true } else { + Write-Verbose -Message ( + $script:localizedData.NotInDesiredState + ) + return $false } } diff --git a/DSCResources/MSFT_SqlScript/en-US/MSFT_SqlScript.strings.psd1 b/DSCResources/MSFT_SqlScript/en-US/MSFT_SqlScript.strings.psd1 new file mode 100644 index 000000000..dbc267805 --- /dev/null +++ b/DSCResources/MSFT_SqlScript/en-US/MSFT_SqlScript.strings.psd1 @@ -0,0 +1,8 @@ +ConvertFrom-StringData @' + ExecutingGetScript = Executing the Get script from the file path '{0}' on the instance '{1}'. + ExecutingSetScript = Executing the Set script from the file path '{0}' on the instance '{1}'. + ExecutingTestScript = Executing the Test script from the file path '{0}' on the instance '{1}'. + TestingConfiguration = Determines if the configuration in the Set script is in desired state. + InDesiredState = The configuration is in desired state. + NotInDesiredState = The configuration is not in desired state. +'@ diff --git a/DSCResources/MSFT_SqlScriptQuery/MSFT_SqlScriptQuery.psm1 b/DSCResources/MSFT_SqlScriptQuery/MSFT_SqlScriptQuery.psm1 index 709b4ec16..0a55000d1 100644 --- a/DSCResources/MSFT_SqlScriptQuery/MSFT_SqlScriptQuery.psm1 +++ b/DSCResources/MSFT_SqlScriptQuery/MSFT_SqlScriptQuery.psm1 @@ -7,6 +7,8 @@ Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath ' $script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common' Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1') +$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlScriptQuery' + <# .SYNOPSIS Returns the current state of the SQL Server features. @@ -80,6 +82,10 @@ function Get-TargetResource $Variable ) + Write-Verbose -Message ( + $script:localizedData.ExecutingGetQuery -f $ServerInstance + ) + $invokeParameters = @{ Query = $GetQuery ServerInstance = $ServerInstance @@ -177,6 +183,10 @@ function Set-TargetResource $Variable ) + Write-Verbose -Message ( + $script:localizedData.ExecutingSetQuery -f $ServerInstance + ) + $invokeParameters = @{ Query = $SetQuery ServerInstance = $ServerInstance @@ -261,8 +271,16 @@ function Test-TargetResource $Variable ) + Write-Verbose -Message ( + $script:localizedData.TestingConfiguration + ) + try { + Write-Verbose -Message ( + $script:localizedData.ExecutingTestQuery -f $ServerInstance + ) + $invokeParameters = @{ Query = $TestQuery ServerInstance = $ServerInstance @@ -277,10 +295,18 @@ function Test-TargetResource if ($null -eq $result) { + Write-Verbose -Message ( + $script:localizedData.InDesiredState + ) + return $true } else { + Write-Verbose -Message ( + $script:localizedData.NotInDesiredState + ) + return $false } } diff --git a/DSCResources/MSFT_SqlScriptQuery/en-US/MSFT_SqlScriptQuery.strings.psd1 b/DSCResources/MSFT_SqlScriptQuery/en-US/MSFT_SqlScriptQuery.strings.psd1 new file mode 100644 index 000000000..7b2f46709 --- /dev/null +++ b/DSCResources/MSFT_SqlScriptQuery/en-US/MSFT_SqlScriptQuery.strings.psd1 @@ -0,0 +1,8 @@ +ConvertFrom-StringData @' + ExecutingGetQuery = Executing the Get query on the instance '{0}'. + ExecutingSetQuery = Executing the Set query on the instance '{0}'. + ExecutingTestQuery = Executing the Test query on the instance '{0}'. + TestingConfiguration = Determines if the configuration in the Set query is in desired state. + InDesiredState = The configuration is in desired state. + NotInDesiredState = The configuration is not in desired state. +'@ diff --git a/Tests/Unit/MSFT_SqlScript.Tests.ps1 b/Tests/Unit/MSFT_SqlScript.Tests.ps1 index cb560a952..a961049ad 100644 --- a/Tests/Unit/MSFT_SqlScript.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlScript.Tests.ps1 @@ -41,12 +41,14 @@ $TestEnvironment = Initialize-TestEnvironment ` #endregion HEADER -function Invoke-TestSetup { +function Invoke-TestSetup +{ # Loading mocked classes Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs') -ChildPath 'SqlPowerShellSqlExecutionException.cs') } -function Invoke-TestCleanup { +function Invoke-TestCleanup +{ Restore-TestEnvironment -TestEnvironment $TestEnvironment } @@ -156,44 +158,59 @@ try } Describe 'MSFT_SqlScript\Test-TargetResource' { - Context 'When Test-TargetResource runs script without issue' { - Mock -CommandName Invoke-SqlScript + Context 'When the system is in the desired state' { + Context 'When Test-TargetResource runs script without issue' { + Mock -CommandName Invoke-SqlScript - It 'Should return true' { - $result = Test-TargetResource @testParameters - $result | Should -Be $true + It 'Should return true' { + $result = Test-TargetResource @testParameters + $result | Should -Be $true + } } - } - Context 'When Test-TargetResource runs script without issue with timeout' { - Mock -CommandName Invoke-SqlScript + Context 'When Test-TargetResource runs script without issue with timeout' { + Mock -CommandName Invoke-SqlScript - It 'Should return true' { - $result = Test-TargetResource @testParametersTimeout - $result | Should -Be $true + It 'Should return true' { + $result = Test-TargetResource @testParametersTimeout + $result | Should -Be $true + } } } - Context 'When Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' { - Mock -CommandName Invoke-SqlScript -MockWith { - throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException - } + Context 'When the system is not in the desired state' { + Context 'When Invoke-SqlScript returns an SQL error code from the script that was ran' { + Mock -CommandName Invoke-SqlScript -MockWith { + return 1 + } - It 'Should return false' { - $result = Test-TargetResource @testParameters - $result | Should -Be $false + It 'Should return false' { + $result = Test-TargetResource @testParametersTimeout + $result | Should -Be $false + } } - } - Context 'When Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' { - $errorMessage = "Failed to run SQL Script" + Context 'When Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' { + Mock -CommandName Invoke-SqlScript -MockWith { + throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException + } - Mock -CommandName Invoke-SqlScript -MockWith { - throw $errorMessage + It 'Should return false' { + $result = Test-TargetResource @testParameters + $result | Should -Be $false + } } - It 'Should throw the correct error from Invoke-Sqlcmd' { - { Test-TargetResource @testParameters } | Should -Throw $errorMessage + Context 'When Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' { + $errorMessage = "Failed to run SQL Script" + + Mock -CommandName Invoke-SqlScript -MockWith { + throw $errorMessage + } + + It 'Should throw the correct error from Invoke-Sqlcmd' { + { Test-TargetResource @testParameters } | Should -Throw $errorMessage + } } } } diff --git a/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 b/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 index 9eeb4d83f..8c3fd3d7b 100644 --- a/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 @@ -41,7 +41,8 @@ $TestEnvironment = Initialize-TestEnvironment ` #endregion HEADER -function Invoke-TestSetup { +function Invoke-TestSetup +{ # Loading mocked classes Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs') -ChildPath 'SqlPowerShellSqlExecutionException.cs') @@ -49,7 +50,8 @@ function Invoke-TestSetup { Import-Module -Name (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs') -ChildPath 'SQLPSStub.psm1') -Force -Global } -function Invoke-TestCleanup { +function Invoke-TestCleanup +{ Restore-TestEnvironment -TestEnvironment $TestEnvironment } @@ -159,44 +161,58 @@ try } Describe 'MSFT_SqlScriptQuery\Test-TargetResource' { - Context 'Test-TargetResource runs script without issue' { - Mock -CommandName Invoke-SqlScript + Context 'When the system is in the desired state' { + Context 'Test-TargetResource runs script without issue' { + Mock -CommandName Invoke-SqlScript - It 'Should return true' { - $result = Test-TargetResource @testParameters - $result | Should -Be $true + It 'Should return true' { + $result = Test-TargetResource @testParameters + $result | Should -Be $true + } } - } - Context 'Test-TargetResource runs script without issue with timeout' { - Mock -CommandName Invoke-SqlScript + Context 'Test-TargetResource runs script without issue with timeout' { + Mock -CommandName Invoke-SqlScript - It 'Should return true' { - $result = Test-TargetResource @testParametersTimeout - $result | Should -Be $true + It 'Should return true' { + $result = Test-TargetResource @testParametersTimeout + $result | Should -Be $true + } } } - Context 'Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' { - Mock -CommandName Invoke-SqlScript -MockWith { - throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException + Context 'When the system is not in the desired state' { + Context 'When Invoke-SqlScript returns an SQL error code from the query that was ran' { + Mock -CommandName Invoke-SqlScript -MockWith { + return 1 + } + It 'Should return false' { + $result = Test-TargetResource @testParametersTimeout + $result | Should -Be $false + } } - It 'Should return false' { - $result = Test-TargetResource @testParameters - $result | Should -Be $false + Context 'Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' { + Mock -CommandName Invoke-SqlScript -MockWith { + throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException + } + + It 'Should return false' { + $result = Test-TargetResource @testParameters + $result | Should -Be $false + } } - } - Context 'Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' { - $errorMessage = "Failed to run SQL Script" + Context 'Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' { + $errorMessage = "Failed to run SQL Script" - Mock -CommandName Invoke-SqlScript -MockWith { - throw $errorMessage - } + Mock -CommandName Invoke-SqlScript -MockWith { + throw $errorMessage + } - It 'Should throw the correct error from Invoke-Sqlcmd' { - { Test-TargetResource @testParameters } | Should -Throw $errorMessage + It 'Should throw the correct error from Invoke-Sqlcmd' { + { Test-TargetResource @testParameters } | Should -Throw $errorMessage + } } } }