Skip to content

Commit 2f12561

Browse files
authored
fix problems handling UNC paths (#5)
correctly handle conversion from FileUri paths correctly handle UNC paths catch exceptions thrown by Test-Path for UNC paths to non-existent servers resolve #4 add external dependency
1 parent d14fe10 commit 2f12561

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

External/domainName-4282008.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function Test-ValidDomainName
2+
{
3+
<#
4+
.SYNOPSIS
5+
Tests whether a string is a valid domain name.
6+
7+
.DESCRIPTION
8+
Test-ValidDomainName uses a regular expression match to test whether DomainName is a valid domain name. The match pattern is the generally-accepted regular expression from http://stackoverflow.com/a/20204811/1404637
9+
10+
.OUTPUTS
11+
Returns true when DomainName is a valid domain name. Returns false otherwise.
12+
13+
.EXAMPLE
14+
'as-d.jkl' | Test-ValidDomainName
15+
# true
16+
17+
as-d.jkl is a valid domain name.
18+
19+
.EXAMPLE
20+
'-asd.jkl' | Test-ValidDomainName
21+
# false
22+
23+
Labels cannot start or end with hyphen.
24+
25+
.LINK
26+
http://stackoverflow.com/a/20204811/1404637
27+
#>
28+
[CmdletBinding()]
29+
param
30+
(
31+
# The domain name to test for validity.
32+
[Parameter(Mandatory = $true,
33+
Position = 1,
34+
ValueFromPipeline = $true)]
35+
[AllowEmptyString()]
36+
[string]
37+
$DomainName
38+
)
39+
process
40+
{
41+
# http://stackoverflow.com/a/20204811/1404637
42+
43+
if ( $DomainName -notmatch '(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)' )
44+
{
45+
&(Publish-Failure "$DomainName is not a valid domain name.",'DomainName' ([System.ArgumentException]))
46+
return $false
47+
}
48+
return $true
49+
}
50+
}

Functions/path.Tests.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ Describe Test-FolderPathsAreEqual {
88
@('c:\folder1', 'c:\folder2', $false),
99
@('c:\folder\', 'c:/folder', $true),
1010
@('c:/folder', 'c:\folder', $true),
11-
@('c:/folder1', 'c:/folder2', $false)
11+
@('c:/folder1', 'c:/folder2', $false),
12+
@('\\server.net\share','\\server.net\share\', $true),
13+
@('file://server.net/share','\\server.net\share', $true)
1214
)
1315
foreach ( $value in $values )
1416
{
1517
$a,$b,$expected = $value
16-
It "$b -eq $b is $expected" {
18+
It "$a -eq $b is $expected" {
1719
$r = Test-FolderPathsAreEqual $a $b
1820
$r | Should be $expected
1921
}
@@ -25,7 +27,9 @@ Describe ConvertTo-WindowsShellFolderPathFormat {
2527
@('c:\folder', 'c:\folder'),
2628
@('c:/folder', 'c:\folder'),
2729
@('c:\folder\','c:\folder'),
28-
@('c:\\folder','c:\folder')
30+
@('c:\\folder','c:\folder'),
31+
@('\\server.net\share\','\\server.net\share'),
32+
@('file://server.net/share','\\server.net\share')
2933
)
3034
foreach ( $value in $values )
3135
{

Functions/path.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function ConvertTo-WindowsShellFolderPathFormat
3434
process
3535
{
3636
$splat = @{
37-
FilePathType = 'Windows'
37+
Scheme = 'plain'
3838
TrailingSlash = $false
3939
}
4040
return $InputPath |

Functions/shellLibraryFolder.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ function Test-ShellLibraryFolder
2626
# load the library
2727
$l = [Microsoft.WindowsAPICodePack.Shell.ShellLibrary]::Load($LibraryName,$true)
2828

29-
if ( -not ($FolderPath | Test-Path -PathType Container -ea Stop ) )
29+
# safely test if the path exists
30+
try
31+
{
32+
# Test-Path can throw an exception for UNC paths
33+
$folderPathExists = $FolderPath | Test-Path -PathType Container
34+
}
35+
catch {}
36+
37+
if ( -not ( $folderPathExists ) )
3038
{
3139
# the file system folder does not exist
3240
# search through the list of folders

IntegrationTests/shellLibraryFolder.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ Describe Test-ShellLibraryFolder {
7878
$r | Should be $false
7979
}
8080
}
81+
Context 'the folder does not exist and neither does the file system folder (UNC)' {
82+
It 'returns false' {
83+
$r = '\\serverc41f5bdb.net\path' | Test-ShellLibraryFolder $libraryName
84+
$r | Should be $false
85+
}
86+
}
8187
Context 'library doesn''t exist' {
8288
It 'remove the library' {
8389
Invoke-ProcessShellLibrary Set Absent $libraryName

0 commit comments

Comments
 (0)