Skip to content

Fix for files/folders not created when given path is drive root (#5228)#6600

Merged
daxian-dbw merged 2 commits into
masterfrom
unknown repository
Apr 25, 2018
Merged

Fix for files/folders not created when given path is drive root (#5228)#6600
daxian-dbw merged 2 commits into
masterfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Apr 9, 2018

PR Summary

If the Path argument to New-Item is the root of a drive such as C:\, the LocationGlobber attempts to generate a drive root relative path from the path C:. GetDriveRootRelativePathFromPSPath determines that this is an absolute path, and strips the drive name and colon from the path; originally, this would result in GenerateRelativePath determining 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 PSDrive context.Drive.Root is returned as the relative path.

This fixes #5228.

PR Checklist

@ghost ghost requested review from BrucePay and anmenaga as code owners April 9, 2018 05:47
@ghost
Copy link
Copy Markdown
Author

ghost commented Apr 9, 2018

@iSazonov new PR with branch workon5228.

@daxian-dbw daxian-dbw requested review from adityapatwardhan and removed request for BrucePay April 18, 2018 10:34
@daxian-dbw
Copy link
Copy Markdown
Member

@adityapatwardhan and @anmenaga, can you please review this PR?

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 redirect to null using > $null to avoid output of New-Item on console.

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.

Same as above.

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.

Use Should -Exist instead

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 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.

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.

Same as above.

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.

| Should -Exist

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.

Do not need to do this. As it is on TestDrive:\. It is cleared automatically after the Describe is finished.

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 String.IsNullOrEmpty(path) instead.

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.

use String.Empty

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 path has to be like c: for this to happen not "c:". Please update comment.

@ghost ghost force-pushed the workon5228 branch 2 times, most recently from 7a75d44 to 3354aff Compare April 18, 2018 19:53
@ghost
Copy link
Copy Markdown
Author

ghost commented Apr 18, 2018

Changes have been made.

@adityapatwardhan
Copy link
Copy Markdown
Member

This change can affect a lot of scenarios, please add the [Feature] tag to the last commit so additional tests can be run. https://github.com/PowerShell/PowerShell/blob/6ef94c1dfe912ce18f93c05ce5362bbce2284291/docs/testing-guidelines/testing-guidelines.md#requesting-additional-tests-for-a-pr

@ghost ghost force-pushed the workon5228 branch from 3354aff to 3a2adde Compare April 18, 2018 20:40
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

Copy link
Copy Markdown
Member

@daxian-dbw daxian-dbw Apr 23, 2018

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.

Copy link
Copy Markdown
Author

@ghost ghost Apr 24, 2018

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!

[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.
@ghost ghost force-pushed the workon5228 branch from 0589d30 to b101d52 Compare April 24, 2018 23:29
@iSazonov
Copy link
Copy Markdown
Collaborator

LGTM.

@mcbobke Thanks for your contribution!

@daxian-dbw daxian-dbw merged commit 35d8de9 into PowerShell:master Apr 25, 2018
@ghost
Copy link
Copy Markdown
Author

ghost commented Apr 26, 2018

Thank you all for the help and the merge!

rjmholt added a commit that referenced this pull request Apr 27, 2018
…and $PWD is a sub folder of the drive root. (#6600)"

This reverts commit 35d8de9.
rjmholt added a commit to rjmholt/PowerShell that referenced this pull request Apr 30, 2018
…and $PWD is a sub folder of the drive root. (PowerShell#6600)"

This reverts commit 35d8de9.
See issue PowerShell#6749.
daxian-dbw pushed a commit that referenced this pull request Apr 30, 2018
…or (#6751)

This reverts the commit 35d8de9, which caused a regression in drive behavior where drive paths were not restored. See issue #6749.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New-Item ignore's Path param if Name param is specified

3 participants