Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3712,6 +3712,8 @@ internal void NewItem(
}

bool isSymbolicJunctionOrHardLink = false;
// Symbolic link targets are allowed to not exist on both Windows and Linux
bool allowNonexistingPath = false;

if(type != null)
{
Expand All @@ -3720,6 +3722,7 @@ internal void NewItem(
if (typeEvaluator.IsMatch("symboliclink") || typeEvaluator.IsMatch("junction") || typeEvaluator.IsMatch("hardlink"))
{
isSymbolicJunctionOrHardLink = true;
allowNonexistingPath = typeEvaluator.IsMatch("symboliclink");
}
}

Expand All @@ -3742,7 +3745,7 @@ internal void NewItem(

var globbedTarget = Globber.GetGlobbedProviderPathsFromMonadPath(
targetPath,
false,
allowNonexistingPath,
context,
out targetProvider,
out targetProviderInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2125,9 +2125,14 @@ protected override void NewItem(

bool exists = false;

// It is legal to create symbolic links to non-existing targets on
// both Windows and Linux. It is not legal to create hard links to
// non-existing targets on either Windows or Linux.
try
{
exists = CheckItemExists(strTargetPath, out isDirectory);
exists = (itemType == ItemType.SymbolicLink)
? true // pretend it exists if we're making a symbolic link
: CheckItemExists(strTargetPath, out isDirectory);
}
catch (Exception e)
{
Expand Down
15 changes: 13 additions & 2 deletions test/powershell/New-Item.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Describe "New-Item" {
Test-Path $FullyQualifiedFile | Should Be $false
}

It "Should create a symbolic link of a file without error" -Skip:$IsWindows {
It "Should create a symbolic link of a file without error" {
New-Item -Name $testfile -Path $tmpDirectory -ItemType file
Test-Path $FullyQualifiedFile | Should Be $true

Expand All @@ -116,7 +116,18 @@ Describe "New-Item" {
$fileInfo.LinkType | Should Be "SymbolicLink"
}

It "Should create a symbolic link from directory without error" -Skip:$IsWindows {
It "Should create a symbolic link to a non-existing file without error" {
$target = Join-Path $tmpDirectory "totallyBogusFile"
New-Item -ItemType SymbolicLink -Target $target -Name $testlink -Path $tmpDirectory
Test-Path $FullyQualifiedLink | Should Be $true

$fileInfo = Get-ChildItem $FullyQualifiedLink
$fileInfo.Target | Should Be $target
Test-Path $fileInfo.Target | Should be $false
$fileInfo.LinkType | Should Be "SymbolicLink"
}

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

Expand Down