Fix for files/folders not created when given path is drive root (#5228)#6600
Conversation
|
@iSazonov new PR with branch workon5228. |
|
@adityapatwardhan and @anmenaga, can you please review this PR? |
There was a problem hiding this comment.
Please redirect to null using > $null to avoid output of New-Item on console.
There was a problem hiding this comment.
Use Should -Exist instead
There was a problem hiding this comment.
Please use Push-Location and Pop-Location to restore $pwd. Also, use a try .. finally pattern to ensure Pop-Location is applied even if the test fails. Currently if the assert for Should -True fails then Set-Location -Path $oldLocation is never called.
There was a problem hiding this comment.
Do not need to do this. As it is on TestDrive:\. It is cleared automatically after the Describe is finished.
There was a problem hiding this comment.
Please use String.IsNullOrEmpty(path) instead.
There was a problem hiding this comment.
The path has to be like c: for this to happen not "c:". Please update comment.
7a75d44 to
3354aff
Compare
|
Changes have been made. |
|
This change can affect a lot of scenarios, please add the |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Please use [Feature] when adding commits to fix the feedback.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thank you, I'll be more explicit about TestDrive:\ and will check with my new tests. I'll reply here when I confirm.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I will add the [Feature] tag for you.
There was a problem hiding this comment.
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!
[Feature] Checks to see if the root of a PSDrive was given as the Path argument to a parameter that supports it. If so, returns the current context's Drive's Root.
|
LGTM. @mcbobke Thanks for your contribution! |
|
Thank you all for the help and the merge! |
…and $PWD is a sub folder of the drive root. (PowerShell#6600)" This reverts commit 35d8de9. See issue PowerShell#6749.
PR Summary
If the
Pathargument toNew-Itemis the root of a drive such asC:\, the LocationGlobber attempts to generate a drive root relative path from the pathC:.GetDriveRootRelativePathFromPSPathdetermines that this is an absolute path, and strips the drive name and colon from the path; originally, this would result inGenerateRelativePathdetermining that the current working directory should be given as the relative path. Now, a check is made on whether stripping the drive name and colon results in an empty string; if it does, the root of the current PSDrivecontext.Drive.Rootis returned as the relative path.This fixes #5228.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests