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 @@ -3519,22 +3519,87 @@ protected override bool ProviderSupportsShouldProcess

#region Command code

private Collection<PathInfo> GetResolvedPaths(string path)
{
Collection<PathInfo> results = null;
try
{
results = SessionState.Path.GetResolvedPSPathFromPSPath(path, CmdletProviderContext);
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
}

return results;
}

/// <summary>
/// Moves the specified item to the specified destination
/// </summary>
protected override void ProcessRecord()
{
if (SuppressWildcardExpansion)
{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem all used methods use try-catch and we can remove the try.

RenameItem(Path, literalPath: true);
return;
}

Collection<PathInfo> resolvedPaths = GetResolvedPaths(Path);

if (resolvedPaths == null)
{
return;
}

if (resolvedPaths.Count == 1)
{
RenameItem(resolvedPaths[0].Path, literalPath: true);
}
else
{
RenameItem(WildcardPattern.Unescape(Path), literalPath: true);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the empty finally block.

}

private void RenameItem(string path, bool literalPath = false)
{
CmdletProviderContext currentContext = CmdletProviderContext;
currentContext.SuppressWildcardExpansion = literalPath;
Comment thread
SteveL-MSFT marked this conversation as resolved.

try
{
if (!InvokeProvider.Item.Exists(Path, currentContext))
if (!InvokeProvider.Item.Exists(path, currentContext))
{
PSInvalidOperationException invalidOperation =
(PSInvalidOperationException)
PSTraceSource.NewInvalidOperationException(
NavigationResources.RenameItemDoesntExist,
Path);
path);

WriteError(
new ErrorRecord(
Expand Down Expand Up @@ -3580,7 +3645,7 @@ protected override void ProcessRecord()
bool isCurrentLocationOrAncestor = false;
try
{
isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(_path, currentContext);
isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(path, currentContext);
}
catch (PSNotSupportedException notSupported)
{
Expand Down Expand Up @@ -3621,7 +3686,7 @@ protected override void ProcessRecord()
(PSInvalidOperationException)
PSTraceSource.NewInvalidOperationException(
NavigationResources.RenamedItemInUse,
Path);
path);

WriteError(
new ErrorRecord(
Expand All @@ -3635,12 +3700,12 @@ protected override void ProcessRecord()

currentContext.PassThru = PassThru;

tracer.WriteLine("Rename {0} to {1}", Path, NewName);
tracer.WriteLine("Rename {0} to {1}", path, NewName);

try
{
// Now do the rename
InvokeProvider.Item.Rename(Path, NewName, currentContext);
InvokeProvider.Item.Rename(path, NewName, currentContext);
}
catch (PSNotSupportedException notSupported)
{
Expand Down Expand Up @@ -3674,7 +3739,7 @@ protected override void ProcessRecord()
pathNotFound));
return;
}
} // ProcessRecord
}
#endregion Command code

} // RenameItemCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,13 @@ private Collection<PathInfo> ResolveDriveQualifiedPath(

s_pathResolutionTracer.WriteLine("Path is DRIVE-QUALIFIED");

string relativePath = GetDriveRootRelativePathFromPSPath(path, context, true, out drive, out providerInstance);
string relativePath =
GetDriveRootRelativePathFromPSPath(
path,
context,
!context.SuppressWildcardExpansion,
out drive,
out providerInstance);

Dbg.Diagnostics.Assert(
drive != null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Describe "Basic Function Provider Tests" -Tags "CI" {
}

It "Fails to rename not existing function" {
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand"
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.RenameItemCommand"
}

It "Fails to rename function which is Constant" {
Expand All @@ -113,8 +113,8 @@ Describe "Basic Function Provider Tests" -Tags "CI" {
}

It "Fails to rename function which is ReadOnly" {
Set-Item $nonExistingFunction -Options "ReadOnly"
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand"
Set-Item $nonExistingFunction -Options "ReadOnly" -Value $functionValue
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "CannotRenameFunction,Microsoft.PowerShell.Commands.RenameItemCommand"
}

It "Renames ReadOnly function when -Force parameter is on" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,56 @@
# Licensed under the MIT License.
Describe "Rename-Item tests" -Tag "CI" {
BeforeAll {
$content = "This is content"
Setup -f originalFile.txt -content "This is content"
$source = "$TESTDRIVE/originalFile.txt"
$target = "$TESTDRIVE/ItemWhichHasBeenRenamed.txt"
Setup -f [orig-file].txt -content "This is not content"
$sourceSp = "$TestDrive/``[orig-file``].txt"
$targetSpName = "ItemWhichHasBeen[Renamed].txt"
$targetSp = "$TestDrive/ItemWhichHasBeen``[Renamed``].txt"
Setup -Dir [test-dir]
$wdSp = "$TestDrive/``[test-dir``]"
}
It "Rename-Item will rename a file" {
Rename-Item $source $target
test-path $source | Should -BeFalse
test-path $target | Should -BeTrue
"$target" | Should -FileContentMatchExactly "This is content"
}
It "Rename-Item will rename a file when path contains special char" {
Rename-Item $sourceSp $targetSpName
$sourceSp | Should -Not -Exist
$targetSp | Should -Exist
$targetSp | Should -FileContentMatchExactly "This is not content"
}
It "Rename-Item will rename a file when -Path and CWD contains special char" {
$content = "This is content"
$oldSpName = "[orig]file.txt"
$oldSpBName = "``[orig``]file.txt"
$oldSp = "$wdSp/$oldSpBName"
$newSpName = "[renamed]file.txt"
$newSp = "$wdSp/``[renamed``]file.txt"
In $wdSp -Execute {
$null = New-Item -Name $oldSpName -ItemType File -Value $content -Force
Rename-Item -Path $oldSpBName $newSpName
}
$oldSp | Should -Not -Exist
$newSp | Should -Exist
$newSp | Should -FileContentMatchExactly $content
}
It "Rename-Item will rename a file when -LiteralPath and CWD contains special char" {
$content = "This is not content"
$oldSpName = "[orig]file2.txt"
$oldSpBName = "``[orig``]file2.txt"
$oldSp = "$wdSp/$oldSpBName"
$newSpName = "[renamed]file2.txt"
$newSp = "$wdSp/``[renamed``]file2.txt"
In $wdSp -Execute {
$null = New-Item -Name $oldSpName -ItemType File -Value $content -Force
Rename-Item -LiteralPath $oldSpName $newSpName
}
$oldSp | Should -Not -Exist
$newSp | Should -Exist
$newSp | Should -FileContentMatchExactly $content
}
}