Repository navigation
Fixing JSON property accessor to attempt user settings directory creation on the first write operation - #2387
Conversation
|
Hi Mike Richmond (@mirichmo), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
TTYL, MSBOT; |
5d4a5bf to
0373f1e
Compare
|
Hi Mike Richmond (@mirichmo), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
TTYL, MSBOT; |
41b2fe9 to
79a50e2
Compare
|
Dongbo Wang (@daxian-dbw) and Andy Jordan (@andschwa) Please review |
Andy Jordan (andyleejordan)
left a comment
There was a problem hiding this comment.
Looks good to me, thanks!
Jason Shirk (lzybkr)
left a comment
There was a problem hiding this comment.
The filename PropertyAccessor.Tests.ps1 is too generic, I'd assume that tests something else.
There was a problem hiding this comment.
There is a race condition here - another process could create the directory after the Exists test fails. You should probably catch the appropriate exception from CreateDirectory and ignore it if that happens.
There was a problem hiding this comment.
CreateDirectory doesn't throw an exception if it already exists. It just returns the directory.
https://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx
Now the question becomes... is the check if it exists necessary? It feels like it is slightly faster to check first in scenarios where the directory already exists.
There was a problem hiding this comment.
Indeed - CreateDirectory will check if the directory exists, so we can remove our redundant check.
There was a problem hiding this comment.
Maybe $IsNotSkipped = $IsWindows?
Or even better, why not just use $IsWindows without $IsNotSkipped? I don't feel this variable helps readability.
There was a problem hiding this comment.
$shouldSkipTest is not used in this test.
There was a problem hiding this comment.
Assuming the first module path is the user module path is fragile. This may not work when a dev is running the test with a profile. I think it's safer to construct the path to the user property.json file. That should be easy given that the test only runs on win-platform.
There was a problem hiding this comment.
Minor question: is it really necessary to create the file when it's requested to remove a property? If the file doesn't exist, then no action needed, and the removal will be sure successful.
There was a problem hiding this comment.
Good point. I removed it
There was a problem hiding this comment.
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$IsNotSkipped = ($IsWindows -eq $true);
$PSDefaultParameterValues["it:skip"] = !$IsNotSkipped
$ShouldSkipTest = !$IsNotSkipped
I think all these can be moved to BeforeAll { } and $global:PSDefaultParameterValues = $originalDefaultParameterValues can be moved to AfterAll { }.
Some examples:
PowerShell\test\powershell\Language\Scripting\I18n.Tests.ps1
PowerShell\test\powershell\Modules\Microsoft.PowerShell.Diagnostics\Get-WinEvent.Tests.ps1
There was a problem hiding this comment.
This is the preferred technique because it results in less duplication when there are multiple Describe blocks within the try-finally.
For background, see the discussion in PR #2335
There was a problem hiding this comment.
Thanks for the link to the discussion. I think it depends on how many Describe sections you expect to be in a file. If there is just one Describe, then it's cleaner to have it in BeforeAlll and AfterAll. But I'm fine keep the try/finally as it's possible that we will add more Describe sections later. #Closed
There was a problem hiding this comment.
original the
Should be the original
There was a problem hiding this comment.
Use the powershell way to do it: if (-not $backupPropertiesFile)
There was a problem hiding this comment.
Do we want to create the directory C:\Users\dongbow\Documents when it already exists?
Probably it should be
elseif (-not (Test-Path $userSettingsDir))
{
$null = New-Item -Type Directory -Path $userSettingsDir -Force -ErrorAction SilentlyContinue
}There was a problem hiding this comment.
Maybe add one more check to verify that read operation should not create the file.
There was a problem hiding this comment.
this line seems not needed given the same command will run in BeforeEach { }
8e36415 to
a82ec2b
Compare
There was a problem hiding this comment.
Thanks for taking Nano/IoT PS into account. Now it makes sense to have $skipTest = !$IsWindows -or $IsInbox, because the if ($IsWindows) below should actually be if (!skipTest).
There was a problem hiding this comment.
You know what, we should exclude the FullCLR powershell as well 😄 Ah, I guess $isInbox already covers that.
There was a problem hiding this comment.
It should be if (!skipTest)
There was a problem hiding this comment.
It should be if (!skipTest)
There was a problem hiding this comment.
It should be if (!skipTest)
…tion on the first write operation
a82ec2b to
e7030dc
Compare
|
Jason Shirk (@lzybkr) do you still have concerns on this PR? If not, I will merge it. |
Fixes #2037 and #2205
I addressed the issue by delaying user settings directory creation until a write operation takes place. At that point, the console should be up and running, so it is safe to let errors propagate for eventual display to the user. This should address all startup scenarios regardless of hosting technique.
I also wrote some new tests to validate basic file operations on the user settings PowerShellProperties.json file.