Skip to content

Commit 3764562

Browse files
authored
Replace Format-Path function (#2084)
- SqlServerDsc.Common - Removed the function `Format-Path` in favor of the command with the same name in the module _DscResource.Common_.
1 parent a6c1159 commit 3764562

File tree

5 files changed

+8
-96
lines changed

5 files changed

+8
-96
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Removed
99

1010
- SqlServerDsc.Common
11-
- Removed the function `Get-RegistryPropertyValue` in favor of the command
12-
with the same name in the module _DscResource.Common_.
11+
- Removed the function `Get-RegistryPropertyValue` and `Format-Path` in
12+
favor of the commands with the same names in the module _DscResource.Common_.
1313

1414
### Added
1515

source/DSCResources/DSC_SqlRSSetup/DSC_SqlRSSetup.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,18 @@ function Set-TargetResource
307307

308308
$SourcePath = [Environment]::ExpandEnvironmentVariables($SourcePath)
309309

310-
$parametersToEvaluateTrailingSlash = @(
310+
$parametersToEvaluateDirectorySeparator = @(
311311
'SourcePath',
312312
'InstallFolder'
313313
)
314314

315315
# Making sure paths are correct.
316-
foreach ($parameterName in $parametersToEvaluateTrailingSlash)
316+
foreach ($parameterName in $parametersToEvaluateDirectorySeparator)
317317
{
318318
if ($PSBoundParameters.ContainsKey($parameterName))
319319
{
320320
$parameterValue = Get-Variable -Name $parameterName -ValueOnly
321-
$formattedPath = Format-Path -Path $parameterValue -TrailingSlash
321+
$formattedPath = Format-Path -Path $parameterValue -EnsureDriveLetterRoot -NoTrailingDirectorySeparator
322322
Set-Variable -Name $parameterName -Value $formattedPath
323323
}
324324
}

source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ function Set-TargetResource
10341034

10351035
$InstanceName = $InstanceName.ToUpper()
10361036

1037-
$parametersToEvaluateTrailingSlash = @(
1037+
$parametersToEvaluateDirectorySeparator = @(
10381038
'InstanceDir',
10391039
'InstallSharedDir',
10401040
'InstallSharedWOWDir',
@@ -1053,12 +1053,12 @@ function Set-TargetResource
10531053
)
10541054

10551055
# Making sure paths are correct.
1056-
foreach ($parameterName in $parametersToEvaluateTrailingSlash)
1056+
foreach ($parameterName in $parametersToEvaluateDirectorySeparator)
10571057
{
10581058
if ($PSBoundParameters.ContainsKey($parameterName))
10591059
{
10601060
$parameterValue = Get-Variable -Name $parameterName -ValueOnly
1061-
$formattedPath = Format-Path -Path $parameterValue -TrailingSlash
1061+
$formattedPath = Format-Path -Path $parameterValue -EnsureDriveLetterRoot -NoTrailingDirectorySeparator
10621062
Set-Variable -Name $parameterName -Value $formattedPath
10631063
}
10641064
}

source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,6 @@ Import-Module -Name $script:resourceHelperModulePath
44

55
$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
66

7-
<#
8-
.SYNOPSIS
9-
Returns the value of the provided in the Name parameter, at the registry
10-
location provided in the Path parameter.
11-
12-
.PARAMETER Path
13-
String containing the path in the registry to the property name.
14-
15-
.PARAMETER PropertyName
16-
String containing the name of the property for which the value is returned.
17-
#>
18-
function Format-Path
19-
{
20-
[CmdletBinding()]
21-
[OutputType([System.String])]
22-
param
23-
(
24-
[Parameter(Mandatory = $true)]
25-
[System.String]
26-
$Path,
27-
28-
[Parameter()]
29-
[System.Management.Automation.SwitchParameter]
30-
$TrailingSlash
31-
)
32-
33-
# Remove trailing slash ('\') from path.
34-
if ($TrailingSlash.IsPresent)
35-
{
36-
<#
37-
Trim backslash, but only if the path contains a full path and
38-
not just a qualifier.
39-
#>
40-
if ($Path -notmatch '^[a-zA-Z]:\\$')
41-
{
42-
$Path = $Path.TrimEnd('\')
43-
}
44-
45-
<#
46-
If the path only contains a qualifier but no backslash ('M:'),
47-
then a backslash is added ('M:\').
48-
#>
49-
if ($Path -match '^[a-zA-Z]:$')
50-
{
51-
$Path = '{0}\' -f $Path
52-
}
53-
}
54-
55-
return $Path
56-
}
57-
587
<#
598
.SYNOPSIS
609
Copy folder structure using Robocopy. Every file and folder, including empty ones are copied.

tests/Unit/SqlServerDsc.Common.Tests.ps1

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -83,43 +83,6 @@ AfterAll {
8383
Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force
8484
}
8585

86-
Describe 'SqlServerDsc.Common\Format-Path' -Tag 'FormatPath' {
87-
BeforeAll {
88-
$mockCorrectPath = 'C:\Correct\Path'
89-
$mockPathWithTrailingBackslash = 'C:\Correct\Path\'
90-
$mockPathWithOnlyQualifier = 'M:'
91-
$mockCorrectQualifierPath = 'M:\'
92-
}
93-
94-
Context 'When there is a path that is wrongly formatted, but now formatting was requested' {
95-
It 'Should return the same wrongly formatted path' {
96-
$result = Format-Path -Path $mockPathWithTrailingBackslash
97-
$result | Should -BeExactly $mockPathWithTrailingBackslash
98-
}
99-
}
100-
101-
Context 'When there is a path that is formatted correctly, and using TrailingSlash' {
102-
It 'Should return the same path' {
103-
$result = Format-Path -Path $mockCorrectPath -TrailingSlash
104-
$result | Should -BeExactly $mockCorrectPath
105-
}
106-
}
107-
108-
Context 'When there is a path that has a trailing backslash, and using TrailingSlash' {
109-
It 'Should return the path without trailing backslash' {
110-
$result = Format-Path -Path $mockPathWithTrailingBackslash -TrailingSlash
111-
$result | Should -BeExactly $mockCorrectPath
112-
}
113-
}
114-
115-
Context 'When there is a path that has only a qualifier, and using TrailingSlash' {
116-
It 'Should return the path with trailing backslash after the qualifier' {
117-
$result = Format-Path -Path $mockPathWithOnlyQualifier -TrailingSlash
118-
$result | Should -BeExactly $mockCorrectQualifierPath
119-
}
120-
}
121-
}
122-
12386
# Tests only the parts of the code that does not already get tested thru the other tests.
12487
Describe 'SqlServerDsc.Common\Copy-ItemWithRobocopy' -Tag 'CopyItemWithRobocopy' {
12588
BeforeAll {

0 commit comments

Comments
 (0)