Rewrite New-TemporaryFile in C# #2732#2786
Rewrite New-TemporaryFile in C# #2732#2786lzybkr merged 1 commit intoPowerShell:masterPowerShell/PowerShell:masterfrom kittholland:New-TemporaryFilekittholland/PowerShell:New-TemporaryFileCopy head branch name to clipboard
Conversation
|
Hi @kittholland, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! The agreement was validated by Microsoft and real humans are currently evaluating your PR. TTYL, MSBOT; |
|
And the command to the UNIX version of the psd1 to fix the build error. |
There was a problem hiding this comment.
Please correct indentation.
filePath can be null here and raise exception.
Original cmdlet wrap this:
if (filePath)
{
FileInfo file = new FileInfo(filePath);
WriteObject(file);
}There was a problem hiding this comment.
Thanks, I wasn't sure what condition could fall into that, that would not trip the catch return. Thanks for clarifying.
There was a problem hiding this comment.
@PowerShellTeam Is it correct? What is a policy about copyright?
There was a problem hiding this comment.
First cmdlet I've writte and I saw that at the top of every other file, wasn't really sure what the protocol was. It's probably wrong. Removing now.
There was a problem hiding this comment.
@kittholland :-) My question was not addressed to you. Please don't rush to remove.
There was a problem hiding this comment.
Repeating code. Please use a variable.
There was a problem hiding this comment.
Removed duplicate GetEnvironmentVariable call. Thanks
There was a problem hiding this comment.
We usually use a space after //.
And the comment is obvious and likely unnecessary.
|
@kittholland Thanks for your contribution! My question about the copyright header was not addressed to you. Please don't rush to remove it. |
There was a problem hiding this comment.
@lzybkr I found some powershell built in cmdlets put the main logic in BeginProcessing when the logic is simple, such as New-Guid, New-TemporaryFile, and Write-Information before @iSazonov adding pipeline support to it. Why did we choose to put main logic in BeginProcess instead of EndProcess for simple cmdlets like this one? I don't see any obvious performance difference.
There was a problem hiding this comment.
Putting the logic of a simple cmdlet in the end block does protect against stupid pipelines, e.g.
function f { begin { write-host 'in begin f'}}
function g { end { write-host 'in end g'}}
g | fThis will print
in begin f
in end g
which is a little weird that the logic for a downstream cmdlet finishes before the upstream command (note no objects are being piped).
In others, you could more easily write some obfuscated (as in, hard to figure out the real logic) pipelines if you use begin instead of end.
There was a problem hiding this comment.
Thanks @lzybkr, this is a great example. So it looks the guideline should be using EndProcessing (or End block in script) for simple logic that doesn't require pipeline input.
There was a problem hiding this comment.
I think the reason we have those cmdlets with main logic in Begin/BeginProcessing is that the pattern was copied from other existing cmdlets, without knowing why it should be put in Begin/BeginProcessing.
@kittholland could you please move the main logic to EndProcessing method given the discussion here? Thanks!
There was a problem hiding this comment.
Yes I will move it.
I left as is because this is an existing cmdlet and did not want to potentially alter any existing implementations (and I did not feel that I could fully comprehend what those possible scenarios would have been).
There was a problem hiding this comment.
Yep, totally understood. Thanks!
There was a problem hiding this comment.
This implies that we need to open a new Issue with analysing what cmdlets must be corrected.
|
Are the tests in |
|
It would be nice to test i/o on the file. |
|
I am happy to add an additional test regarding IO but I am uncertain if testing IO after the new file was generated is within the scope of this command. I could validate additional attributes of the resulting file, for example that the extension is tmp, that the length is 0, and that it was created during the span of testing, but I'm not certain there are any conditions that would ever cause those tests to fail. |
|
I believe that there is no need to examine the underlying level. We need to check only the cmdlet. $tmp=New-TemporaryFile
Get-Content -Path $tmp.FullName | Should Be $null
$teststring ="any test string"
$teststring | Out-File -Path $tmp.FullName
Get-Content -Path $tmp.FullName | Should Be $teststring
Remove-Item $tmp.FullName -Force |
|
We can merge as soon as the conflicts are resolved. |
|
Conflicts have been resolved, thanks. |
|
@kittholland - can you rebase? The PR now has a bunch of commits it shouldn't. |
lzybkr
left a comment
There was a problem hiding this comment.
Some unicode characters got messed up, please fix.
Literal translation of New-TemporaryFile function to a C# cmdlet, other than moving logic from Begin to End.
973e057 to
b1cf784
Compare
|
@kittholland Thanks for your contribution! |
Literal translation of New-TemporaryFile to C# cmdlet.