From 889d62cbf8bec419bc3083de1a85296c0c3db47a Mon Sep 17 00:00:00 2001 From: alx9r Date: Sat, 7 Jan 2017 19:36:03 -0800 Subject: [PATCH 1/2] 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 --- Functions/path.Tests.ps1 | 10 +++++++--- Functions/path.ps1 | 2 +- Functions/shellLibraryFolder.ps1 | 10 +++++++++- IntegrationTests/shellLibraryFolder.Tests.ps1 | 6 ++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Functions/path.Tests.ps1 b/Functions/path.Tests.ps1 index 5abd365..32e2760 100644 --- a/Functions/path.Tests.ps1 +++ b/Functions/path.Tests.ps1 @@ -8,12 +8,14 @@ Describe Test-FolderPathsAreEqual { @('c:\folder1', 'c:\folder2', $false), @('c:\folder\', 'c:/folder', $true), @('c:/folder', 'c:\folder', $true), - @('c:/folder1', 'c:/folder2', $false) + @('c:/folder1', 'c:/folder2', $false), + @('\\server.net\share','\\server.net\share\', $true), + @('file://server.net/share','\\server.net\share', $true) ) foreach ( $value in $values ) { $a,$b,$expected = $value - It "$b -eq $b is $expected" { + It "$a -eq $b is $expected" { $r = Test-FolderPathsAreEqual $a $b $r | Should be $expected } @@ -25,7 +27,9 @@ Describe ConvertTo-WindowsShellFolderPathFormat { @('c:\folder', 'c:\folder'), @('c:/folder', 'c:\folder'), @('c:\folder\','c:\folder'), - @('c:\\folder','c:\folder') + @('c:\\folder','c:\folder'), + @('\\server.net\share\','\\server.net\share'), + @('file://server.net/share','\\server.net\share') ) foreach ( $value in $values ) { diff --git a/Functions/path.ps1 b/Functions/path.ps1 index 1a8cc5c..229544e 100644 --- a/Functions/path.ps1 +++ b/Functions/path.ps1 @@ -34,7 +34,7 @@ function ConvertTo-WindowsShellFolderPathFormat process { $splat = @{ - FilePathType = 'Windows' + Scheme = 'plain' TrailingSlash = $false } return $InputPath | diff --git a/Functions/shellLibraryFolder.ps1 b/Functions/shellLibraryFolder.ps1 index 939b8bb..4c31766 100644 --- a/Functions/shellLibraryFolder.ps1 +++ b/Functions/shellLibraryFolder.ps1 @@ -26,7 +26,15 @@ function Test-ShellLibraryFolder # load the library $l = [Microsoft.WindowsAPICodePack.Shell.ShellLibrary]::Load($LibraryName,$true) - if ( -not ($FolderPath | Test-Path -PathType Container -ea Stop ) ) + # safely test if the path exists + try + { + # Test-Path can throw an exception for UNC paths + $folderPathExists = $FolderPath | Test-Path -PathType Container + } + catch {} + + if ( -not ( $folderPathExists ) ) { # the file system folder does not exist # search through the list of folders diff --git a/IntegrationTests/shellLibraryFolder.Tests.ps1 b/IntegrationTests/shellLibraryFolder.Tests.ps1 index b4d6ed1..5357eac 100644 --- a/IntegrationTests/shellLibraryFolder.Tests.ps1 +++ b/IntegrationTests/shellLibraryFolder.Tests.ps1 @@ -78,6 +78,12 @@ Describe Test-ShellLibraryFolder { $r | Should be $false } } + Context 'the folder does not exist and neither does the file system folder (UNC)' { + It 'returns false' { + $r = '\\serverc41f5bdb.net\path' | Test-ShellLibraryFolder $libraryName + $r | Should be $false + } + } Context 'library doesn''t exist' { It 'remove the library' { Invoke-ProcessShellLibrary Set Absent $libraryName From 046d0394e9a4c1575e031a3db03f784a21e9b7da Mon Sep 17 00:00:00 2001 From: alx9r Date: Sat, 7 Jan 2017 19:41:22 -0800 Subject: [PATCH 2/2] add missing external dependency --- External/domainName-4282008.ps1 | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 External/domainName-4282008.ps1 diff --git a/External/domainName-4282008.ps1 b/External/domainName-4282008.ps1 new file mode 100644 index 0000000..364b140 --- /dev/null +++ b/External/domainName-4282008.ps1 @@ -0,0 +1,50 @@ +function Test-ValidDomainName +{ +<# +.SYNOPSIS +Tests whether a string is a valid domain name. + +.DESCRIPTION +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 + +.OUTPUTS +Returns true when DomainName is a valid domain name. Returns false otherwise. + +.EXAMPLE + 'as-d.jkl' | Test-ValidDomainName + # true + +as-d.jkl is a valid domain name. + +.EXAMPLE + '-asd.jkl' | Test-ValidDomainName + # false + +Labels cannot start or end with hyphen. + +.LINK +http://stackoverflow.com/a/20204811/1404637 +#> + [CmdletBinding()] + param + ( + # The domain name to test for validity. + [Parameter(Mandatory = $true, + Position = 1, + ValueFromPipeline = $true)] + [AllowEmptyString()] + [string] + $DomainName + ) + process + { + # http://stackoverflow.com/a/20204811/1404637 + + if ( $DomainName -notmatch '(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?