8000 Change error message when using New-Item to create a symlink and the … · PowerShell/PowerShell@938e136 · GitHub
[go: up one dir, main page]

Skip to content

Commit 938e136

Browse files
jeffbidaxian-dbw
authored andcommitted
Change error message when using New-Item to create a symlink and the item exists (#3703)
1 parent d275cee commit 938e136

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/System.Management.Automation/namespaces/FileSystemProvider.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,8 @@ protected override void NewItem(
22082208
{
22092209
if (symLinkExists)
22102210
{
2211-
WriteError(new ErrorRecord(new IOException("NewItemIOError"), "NewItemIOError", ErrorCategory.ResourceExists, path));
2211+
string message = StringUtil.Format(FileSystemProviderStrings.SymlinkItemExists, path);
2212+
WriteError(new ErrorRecord(new IOException(message), "SymLinkExists", ErrorCategory.ResourceExists, path));
22122213
return;
22132214
}
22142215
}

src/System.Management.Automation/resources/FileSystemProviderStrings.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,7 @@
336336
<data name="DriveMaxSizeError" xml:space="preserve">
337337
<value>Maximum size for drive has been exceeded: {0}.</value>
338338
</data>
339-
</root>
339+
<data name="SymlinkItemExists" xml:space="preserve">
340+
<value>Cannot create symbolic link because the path {0} already exists.</value>
341+
</data>
342+
</root>

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

+3
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
344344
$link.LinkType | Should BeExactly "SymbolicLink"
345345
$link.Target | Should Be $nonFile
346346
}
347+
It "New-Item emits an error when path to symbolic link already exists." {
348+
{ New-Item -ItemType SymbolicLink -Path $realDir -Value $symLinkToDir -ErrorAction Stop } | ShouldBeErrorId "SymLinkExists,Microsoft.PowerShell.Commands.NewItemCommand"
349+
}
347350
It "New-Item can create a symbolic link to a directory" -Skip:($IsWindows) {
348351
New-Item -ItemType SymbolicLink -Path $symLinkToDir -Value $realDir
349352
Test-Path $symLinkToDir | Should Be $true

0 commit comments

Comments
 (0)
0