From 26a58670c1dc82303736ea2e2f12458f34f2bf58 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 7 Feb 2019 11:44:23 -0800 Subject: [PATCH] Enable creating relative symlinks on Windows with `New-Item` (#8783) --- .../engine/SessionStateContainer.cs | 7 ++++++- .../FileSystem.Tests.ps1 | 2 -- .../New-Item.Tests.ps1 | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/SessionStateContainer.cs b/src/System.Management.Automation/engine/SessionStateContainer.cs index b94597b541..546965c83c 100644 --- a/src/System.Management.Automation/engine/SessionStateContainer.cs +++ b/src/System.Management.Automation/engine/SessionStateContainer.cs @@ -3535,7 +3535,12 @@ namespace System.Management.Automation throw PSTraceSource.NewInvalidOperationException(SessionStateStrings.PathNotFound, targetPath); } - content = globbedTarget[0]; + // If the original target was a relative path, we want to leave it as relative if it did not require + // globbing to resolve. + if (WildcardPattern.ContainsWildcardCharacters(targetPath)) + { + content = globbedTarget[0]; + } } NewItemPrivate(providerInstance, composedPath, type, content, context); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index f974e370eb..67369f9953 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -500,7 +500,6 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows" Context "Get-ChildItem and symbolic links" { BeforeAll { - $TestDrive = "TestDrive:" $alphaDir = Join-Path $TestDrive "sub-alpha" $alphaLink = Join-Path $TestDrive "link-alpha" $alphaFile1 = Join-Path $alphaDir "AlphaFile1.txt" @@ -671,7 +670,6 @@ Describe "Copy-Item can avoid copying an item onto itself" -Tags "CI", "RequireA # attempt is made to copy an item onto itself. $selfCopyKey = "SelfCopy" - $TestDrive = "TestDrive:" $subDir = "$TestDrive/sub" $otherSubDir = "$TestDrive/other-sub" $fileName = "file.txt" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 index 7f49225dd5..a9a2599891 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 @@ -254,6 +254,18 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') { $symbolicLinkPath = New-Item -ItemType SymbolicLink -Path "$tmpDirectory/$folderName/" -Value "/bar/" $symbolicLinkPath | Should -Not -BeNullOrEmpty } + + It "New-Item -ItemType SymbolicLink should be able to create a relative link" -Skip:(!$IsWindows) { + try { + Push-Location $TestDrive + $relativeFilePath = Join-Path -Path . -ChildPath "relativefile.txt" + $file = New-Item -ItemType File -Path $relativeFilePath + $link = New-Item -ItemType SymbolicLink -Path ./link -Target $relativeFilePath + $link.Target | Should -BeExactly $relativeFilePath + } finally { + Pop-Location + } + } } Describe "New-Item with links fails for non elevated user if developer mode not enabled on Windows." -Tags "CI" {