From bfc5b7e8361f4f5010663cb857e4212f656e2d82 Mon Sep 17 00:00:00 2001 From: "Jason Shirk (POWERSHELL)" Date: Fri, 24 Jun 2016 11:58:25 -0700 Subject: [PATCH 1/2] Revert Unix specific Split-Path -Qualifier code The -Qualifier code for Split-Path made some incorrect assumptions: * That a path only referred to the file system * That drives cannot be used on Unix systems These assumptions were both wrong, so I've reverted the change so we do not have any Unix specific code in Split-Path now. Fixes #1176 --- .../commands/management/ParsePathCommand.cs | 12 +-- .../Split-Path.Tests.ps1 | 88 ++++++------------- 2 files changed, 30 insertions(+), 70 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs index 341d713ac..2ac293852 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs @@ -272,7 +272,6 @@ namespace Microsoft.PowerShell.Commands #region Command code - private static string volumeSeparatorCharAsString = System.IO.Path.VolumeSeparatorChar.ToString(); /// /// Parses the specified path and returns the portion determined by the /// boolean parameters. @@ -396,7 +395,7 @@ namespace Microsoft.PowerShell.Commands continue; case qualifierSet : - int separatorIndex = pathsToParse[index].IndexOf(volumeSeparatorCharAsString, StringComparison.CurrentCulture); + int separatorIndex = pathsToParse[index].IndexOf(":", StringComparison.CurrentCulture); if (separatorIndex < 0) { @@ -538,12 +537,7 @@ namespace Microsoft.PowerShell.Commands string result = path; - // Platform notes: On single root fileystems, there is no such thing - // as a drive qualifier, and so this is a noop - if (Platform.HasSingleRootFilesystem()) - { - } - else if (SessionState.Path.IsProviderQualified(path)) + if (SessionState.Path.IsProviderQualified(path)) { int index = path.IndexOf("::", StringComparison.CurrentCulture); @@ -559,8 +553,8 @@ namespace Microsoft.PowerShell.Commands if (SessionState.Path.IsPSAbsolute(path, out driveName)) { - // Remove the drive name and colon + result = path.Substring(driveName.Length + 1); } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 index 75a53ad5a..ff3fa343d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Split-Path.Tests.ps1 @@ -1,12 +1,4 @@ Describe "Split-Path" { - if ($IsWindows) - { - $qualifier = "C:" - } - else - { - $qualifier = "/" - } It "Should return a string object when invoked" { ( Split-Path . ).GetType().Name | Should Be "String" @@ -15,47 +7,25 @@ Describe "Split-Path" { } It "Should return the name of the drive when the qualifier switch is used" { - Split-Path $qualifier -Qualifier | Should Be "$qualifier" - Split-Path ${qualifier}usr/bin -Qualifier | Should Be "$qualifier" + Split-Path -Qualifier env: | Should Be "env:" + Split-Path -Qualifier env:PATH | Should Be "env:" } - It "Should error when using the qualifier switch for a Windows path while on a non-Windows machine" { - # ErrorAction SilentlyContinue merely suppresses the error from the console. - # Throwing exceptions still seen by Pester. - - if ($qualifier -eq "/") - { - Split-Path "C:\Users" -Qualifier -ErrorAction SilentlyContinue | Should Throw - } - else - { - Split-Path "/Users" -Qualifier -ErrorAction SilentlyContinue | Should Throw - - } + It "Should error when using the qualifier switch and no qualifier in the path" { + { Split-Path -Qualifier -ErrorAction Stop /Users } | Should Throw + { Split-Path -Qualifier -ErrorAction Stop abcdef } | Should Throw } - It "Should error when no directory separator characters are used with a qualifier" { - Split-Path "abadTest" -Qualifier -ErrorAction SilentlyContinue | Should Throw - } - - It "Should return the path when the noqualifier switch is used on a Linux system" { - { Split-Path ${qualifier}usr/bin -NoQualifier } | Should Not Throw - if ($IsWindows) - { - Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "usr/bin" - } - else - { - Split-Path ${qualifier}usr/bin -NoQualifier | Should Be "/usr/bin" - } + It "Should return the path when the noqualifier switch is used" { + Split-Path env:PATH -NoQualifier | Should Be "PATH" } It "Should return the base name when the leaf switch is used" { - Split-Path ${qualifier}usr/bin -Leaf | Should be "bin" - Split-Path ${qualifier}usr/local/bin -Leaf | Should be "bin" - Split-Path usr/bin -Leaf | Should be "bin" - Split-Path ./bin -Leaf | Should be "bin" - Split-Path bin -Leaf | Should be "bin" + Split-Path -Leaf /usr/bin | Should be "bin" + Split-Path -Leaf fs:/usr/local/bin | Should be "bin" + Split-Path -Leaf usr/bin | Should be "bin" + Split-Path -Leaf ./bin | Should be "bin" + Split-Path -Leaf bin | Should be "bin" } It "Should be able to accept regular expression input and output an array for multiple objects" { @@ -77,30 +47,26 @@ Describe "Split-Path" { } It "Should be able to tell if a given path is an absolute path" { - ( Split-Path ${qualifier}usr/bin -IsAbsolute ) | Should be $true - ( Split-Path .. -IsAbsolute ) | Should be $false - ( Split-Path ${qualifier}usr/.. -IsAbsolute ) | Should be $true - ( Split-Path ${qualifier}usr/../ -IsAbsolute ) | Should be $true - ( Split-Path ../ -IsAbsolute ) | Should be $false - ( Split-Path . -IsAbsolute ) | Should be $false - ( Split-Path ~/ -IsAbsolute ) | Should be $false - ( Split-Path ~/.. -IsAbsolute ) | Should be $false - ( Split-Path ~/../.. -IsAbsolute ) | Should be $false - + Split-Path -IsAbsolute fs:/usr/bin | Should Be $true + Split-Path -IsAbsolute .. | Should Be $false + Split-Path -IsAbsolute /usr/.. | Should Be (!$IsWindows) + Split-Path -IsAbsolute fs:/usr/../ | Should Be $true + Split-Path -IsAbsolute ../ | Should Be $false + Split-Path -IsAbsolute . | Should Be $false + Split-Path -IsAbsolute ~/ | Should Be $false + Split-Path -IsAbsolute ~/.. | Should Be $false + Split-Path -IsAbsolute ~/../.. | Should Be $false } It "Should support piping" { - $path = "${qualifier}usr/bin" - ( $path | Split-Path ) | Should Be "${qualifier}usr" + "usr/bin" | Split-Path | Should Be "usr" } It "Should return the path up to the parent of the directory when Parent switch is used" { - Split-Path "${qualifier}usr/bin" -Parent | Should Be "${qualifier}usr" - Split-Path "${qualifier}usr/local/bin" -Parent | Should Be $(Join-Path "${qualifier}usr" -ChildPath "local") - Split-Path "usr/local/bin" -Parent | Should Be $(Join-Path "usr" -ChildPath "local") - } - - It "Should throw if a parameterSetName is incorrect" { - { Split-Path "${qualifier}usr/bin/" -Parentaoeu } | Should Throw "A parameter cannot be found that matches parameter name" + $dirSep = [string]([System.IO.Path]::DirectorySeparatorChar) + Split-Path -Parent "fs:/usr/bin" | Should Be "fs:${dirSep}usr" + Split-Path -Parent "/usr/bin" | Should Be "${dirSep}usr" + Split-Path -Parent "/usr/local/bin" | Should Be "${dirSep}usr${dirSep}local" + Split-Path -Parent "usr/local/bin" | Should Be "usr${dirSep}local" } } From 4269398379ddaa8027e7241aad1edae540b237d8 Mon Sep 17 00:00:00 2001 From: "Jason Shirk (POWERSHELL)" Date: Thu, 30 Jun 2016 12:58:53 -0700 Subject: [PATCH 2/2] Fix Split-Path -NoQualifier for Unix paths --- .../commands/management/ParsePathCommand.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs index 2ac293852..09128a973 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ParsePathCommand.cs @@ -553,9 +553,12 @@ namespace Microsoft.PowerShell.Commands if (SessionState.Path.IsPSAbsolute(path, out driveName)) { - // Remove the drive name and colon - - result = path.Substring(driveName.Length + 1); + var driveNameLength = driveName.Length; + if (path.Length > (driveNameLength + 1) && path[driveNameLength] == ':') + { + // Remove the drive name and colon + result = path.Substring(driveNameLength + 1); + } } }