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
32 changes: 25 additions & 7 deletions src/System.Management.Automation/namespaces/LocationGlobber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,9 @@ internal string GetDriveRootRelativePathFromPSPath(
// Check to see if the path is relative or absolute
bool isPathForCurrentDrive = false;

// Check to see if the path is to the root of a drive
bool isPathForRootOfDrive = false;

if (IsAbsolutePath(path, out driveName))
{
Dbg.Diagnostics.Assert(
Expand Down Expand Up @@ -2080,6 +2083,12 @@ internal string GetDriveRootRelativePathFromPSPath(
// this is the default behavior for all windows drives, and all non-filesystem
// drives on non-windows
path = path.Substring(driveName.Length + 1);

if (String.IsNullOrEmpty(path))
{
// path was to the root of a drive such as 'c:'
isPathForRootOfDrive = true;
}
}
}
}
Expand Down Expand Up @@ -2111,14 +2120,23 @@ internal string GetDriveRootRelativePathFromPSPath(
// have access to it.
context.Drive = workingDriveForPath;

string relativePath =
GenerateRelativePath(
workingDriveForPath,
path,
escapeCurrentLocation,
providerInstance,
context);
string relativePath = String.Empty;

if (isPathForRootOfDrive)
{
relativePath = context.Drive.Root;
}
else
{
relativePath =
GenerateRelativePath(
workingDriveForPath,
path,
escapeCurrentLocation,
providerInstance,
context);
}

return relativePath;
}
catch (PSNotSupportedException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ Describe "New-Item" -Tags "CI" {
$fileInfo.Target | Should -BeNullOrEmpty
$fileInfo.LinkType | Should -BeExactly "HardLink"
}

It "Should create a file at the root of the drive while the current working directory is not the root" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The tests are wrong. They pass even without changes in this PR.

To repro this issue using "TestDrive:", you need to first Set-Location TestDrive:\, and then run New-Item in the context of the TestDrive:\. You cannot just use regular filesystem drive path to repro this.

Executing script .\testPR6600.ps1

  Describing New-Item with links
    [+] Should create a file at the root of the drive while the current working directory is not the root 397ms
    [+] Should create a folder at the root of the drive while the current working directory is not the root 80ms
Tests completed in 477ms
Tests Passed: 2, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0

@daxian-dbw Dongbo Wang (daxian-dbw) Apr 23, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use [Feature] when adding commits to fix the feedback.

@ghost Deleted user (ghost) Apr 24, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Given the previous feedback above, should I use Push-Location to move into the context of TestDrive:\ and then Pop-Location twice at the end of each test?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I believe that's how you can repro the issue with TestDrive:\. When using $TestDirve, you are actually using the real filesystem path, which is not a root path at all.
After you write new tests, please make sure that they fail as expected without your fix, but works with your fix.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you, I'll be more explicit about TestDrive:\ and will check with my new tests. I'll reply here when I confirm.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Executing script .\test\powershell\Modules\Microsoft.PowerShell.Management\New-Item.Tests.ps1
    Describing New-Item
    [-] Should create a file at the root of the drive while the current working directory is not the root 107ms
      Expected path 'C:\Users\mbobke\AppData\Local\Temp\42bafa09-bdae-4f1d-a18f-33def150c3a3\testfile.txt' to exist, but it did not exist.
      127:             $FullyQualifiedFile | Should -Exist
      at <ScriptBlock>, C:\Users\mbobke\Documents\GitKraken\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Management\New-Item.Tests.ps1: line 127

Confirm
The item at
C:\Users\mbobke\AppData\Local\Temp\42bafa09-bdae-4f1d-a18f-33def150c3a3\newDirectory has
children and the Recurse parameter was not specified. If you continue, all children will be
 removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): a
    [-] Should create a folder at the root of the drive while the current working directory is not the root 15.96s
      Expected path 'C:\Users\mbobke\AppData\Local\Temp\42bafa09-bdae-4f1d-a18f-33def150c3a3      ewDirectory2' to exist, but it did not exist.
      144:             $FullyQualifiedFolder2 | Should -Exist
      at <ScriptBlock>, C:\Users\mbobke\Documents\GitKraken\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Management\New-Item.Tests.ps1: line 144

Confirmed that these tests fail without my changes implemented. Waiting for CI/CD to finish with the changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @mcbobke !
It would be easier to review if you push new commit instead of rebasing the existing one, so that we know where we were and only need to look at the new commits.

Also, please add the [Feature] tag to your commit message so that CI runs extra tests for the file system code change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I will add the [Feature] tag for you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Understood, I was under the impression that I should try and keep it to a single commit at all times, noted for the future. Sorry about the confusion about the tag, I thought it needed to be in the first line of the body. Thanks for the help!

try {
New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
Push-Location -Path "TestDrive:\$testfolder"
New-Item -Name $testfile -Path "TestDrive:\" -ItemType file > $null
$FullyQualifiedFile | Should -Exist
}
finally {
Pop-Location
}
}

It "Should create a folder at the root of the drive while the current working directory is not the root" {
$testfolder2 = "newDirectory2"
$FullyQualifiedFolder2 = Join-Path -Path $tmpDirectory -ChildPath $testfolder2

try {
New-Item -Name $testfolder -Path "TestDrive:\" -ItemType directory > $null
Push-Location -Path "TestDrive:\$testfolder"
New-Item -Name $testfolder2 -Path "TestDrive:\" -ItemType directory > $null
$FullyQualifiedFolder2 | Should -Exist
}
finally {
Pop-Location
}
}
}

# More precisely these tests require SeCreateSymbolicLinkPrivilege.
Expand Down Expand Up @@ -186,7 +213,7 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') {
}

It "New-Item -ItemType SymbolicLink should understand directory path ending with slash" {
$folderName = [System.IO.Path]::GetRandomFileName()
$folderName = [System.IO.Path]::GetRandomFileName()
$symbolicLinkPath = New-Item -ItemType SymbolicLink -Path "$tmpDirectory/$folderName/" -Value "/bar/"
$symbolicLinkPath | Should -Not -BeNullOrEmpty
}
Expand Down