Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix New-Item to create correct symlink type (#2915) (#3509)
Now `New-Item` can create a file symlink to a file target or to a non-existent target. It can also create a directory symlink to a directory target on Windows.
  • Loading branch information
jeffbi authored and daxian-dbw committed Apr 14, 2017
1 parent 2851f7e commit 06020f3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Expand Up @@ -2138,9 +2138,9 @@ public static string Mode(PSObject instance)
// non-existing targets on either Windows or Linux.
try
{
exists = (itemType == ItemType.SymbolicLink)
? true // pretend it exists if we're making a symbolic link
: CheckItemExists(strTargetPath, out isDirectory);
exists = CheckItemExists(strTargetPath, out isDirectory);
if (itemType == ItemType.SymbolicLink)
exists = true; // pretend the target exists if we're making a symbolic link
}
catch (Exception e)
{
Expand Down
Expand Up @@ -129,6 +129,8 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
$FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile
$FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder
$FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink
$SymLinkMask = [System.IO.FileAttributes]::ReparsePoint
$DirLinkMask = $SymLinkMask -bor [System.IO.FileAttributes]::Directory

BeforeEach {
Clean-State
Expand All @@ -144,6 +146,7 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
$fileInfo = Get-ChildItem $FullyQualifiedLink
$fileInfo.Target | Should Match ([regex]::Escape($FullyQualifiedFile))
$fileInfo.LinkType | Should Be "SymbolicLink"
$fileInfo.Attributes -band $DirLinkMask | Should Be $SymLinkMask
}

It "Should create a symbolic link to a non-existing file without error" {
Expand All @@ -155,18 +158,20 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
$fileInfo.Target | Should Be $target
Test-Path $fileInfo.Target | Should be $false
$fileInfo.LinkType | Should Be "SymbolicLink"
$fileInfo.Attributes -band $DirLinkMask | Should Be $SymLinkMask
}

It "Should create a symbolic link from directory without error" {
It "Should create a symbolic link to directory without error" {
New-Item -Name $testFolder -Path $tmpDirectory -ItemType directory
Test-Path $FullyQualifiedFolder | Should Be $true

New-Item -ItemType SymbolicLink -Target $FullyQualifiedFolder -Name $testlink -Path $tmpDirectory
Test-Path $FullyQualifiedLink | Should Be $true

$fileInfo = Get-ChildItem $FullyQualifiedLink
$fileInfo = Get-Item $FullyQualifiedLink
$fileInfo.Target | Should Match ([regex]::Escape($FullyQualifiedFolder))
$fileInfo.LinkType | Should Be "SymbolicLink"
$fileInfo.Attributes -band $DirLinkMask | Should Be $DirLinkMask

# Remove the link explicitly to avoid broken symlink issue
Remove-Item $FullyQualifiedLink -Force
Expand Down

0 comments on commit 06020f3

Please sign in to comment.