From b12e5bbc3b26ec912506a689bd4349ef68ad10cc Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Mon, 10 Apr 2023 15:17:18 +0200 Subject: [PATCH] `Get-SqlDscPreferredModule`: Using command `Get-PSModulePath` (#1905) - `Get-SqlDscPreferredModule` - Re-using the command `Get-PSModulePath` from the module DscResource.Common. --- CHANGELOG.md | 1 + source/Public/Get-SqlDscPreferredModule.ps1 | 23 +------------------ .../Get-SqlDscPreferredModule.Tests.ps1 | 4 ++++ 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 311105dea..b90637fb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New public command: - `Get-SqlDscPreferredModule` - Returns the name of the first available preferred module ([issue #1879](https://github.com/dsccommunity/SqlServerDsc/issues/1879)). + - Re-using the command `Get-PSModulePath` from the module DscResource.Common. - SqlSecureConnection - Added new parameter `ServerName` that will be used as the host name when restarting the SQL Server instance. The specified value should be the same diff --git a/source/Public/Get-SqlDscPreferredModule.ps1 b/source/Public/Get-SqlDscPreferredModule.ps1 index 98408fb64..e2c0b98ab 100644 --- a/source/Public/Get-SqlDscPreferredModule.ps1 +++ b/source/Public/Get-SqlDscPreferredModule.ps1 @@ -70,28 +70,7 @@ function Get-SqlDscPreferredModule contains all paths. #> - <# - TODO: This should be replaced by Get-PSModulePath that is in - PR https://github.com/dsccommunity/DscResource.Common/pull/104. - #> - - <# - Get the environment variables from all targets session, user and machine. - Casts the value to System.String to convert $null values to empty string. - #> - $modulePathSession = [System.String] [System.Environment]::GetEnvironmentVariable('PSModulePath') - $modulePathUser = [System.String] [System.Environment]::GetEnvironmentVariable('PSModulePath', 'User') - $modulePathMachine = [System.String] [System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') - - $modulePath = $modulePathSession, $modulePathUser, $modulePathMachine -join ';' - - $modulePathArray = $modulePath -split ';' | - Where-Object -FilterScript { - -not [System.String]::IsNullOrEmpty($_) - } | - Sort-Object -Unique - - $modulePath = $modulePathArray -join ';' + $modulePath = Get-PSModulePath -FromTarget 'Session', 'User', 'Machine' Set-PSModulePath -Path $modulePath } diff --git a/tests/Unit/Public/Get-SqlDscPreferredModule.Tests.ps1 b/tests/Unit/Public/Get-SqlDscPreferredModule.Tests.ps1 index 0b590b070..d25405309 100644 --- a/tests/Unit/Public/Get-SqlDscPreferredModule.Tests.ps1 +++ b/tests/Unit/Public/Get-SqlDscPreferredModule.Tests.ps1 @@ -406,6 +406,10 @@ Describe 'Get-SqlDscPreferredModule' -Tag 'Public' { Context 'When specifying the parameter Refresh' { BeforeAll { Mock -CommandName Set-PSModulePath + Mock -CommandName Get-PSModulePath -MockWith { + return 'MockPath' + } + Mock -CommandName Get-Module -MockWith { return @{ Name = 'SqlServer'