Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Rewrite New-TemporaryFile in C# #2732#2786

Merged
lzybkr merged 1 commit into
PowerShell:masterPowerShell/PowerShell:masterfrom
kittholland:New-TemporaryFilekittholland/PowerShell:New-TemporaryFileCopy head branch name to clipboard
Dec 7, 2016
Merged

Rewrite New-TemporaryFile in C# #2732#2786
lzybkr merged 1 commit into
PowerShell:masterPowerShell/PowerShell:masterfrom
kittholland:New-TemporaryFilekittholland/PowerShell:New-TemporaryFileCopy head branch name to clipboard

Conversation

@kittholland

Copy link
Copy Markdown
Contributor

Literal translation of New-TemporaryFile to C# cmdlet.

@msftclas

Copy link
Copy Markdown

Hi @kittholland, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

@powercode

Copy link
Copy Markdown
Collaborator

And the command to the UNIX version of the psd1 to fix the build error.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add new line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please correct indentation.
filePath can be null here and raise exception.
Original cmdlet wrap this:

if (filePath)
{
   FileInfo file = new FileInfo(filePath);
   WriteObject(file); 
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I wasn't sure what condition could fall into that, that would not trip the catch return. Thanks for clarifying.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@PowerShellTeam Is it correct? What is a policy about copyright?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@kittholland :-) My question was not addressed to you. Please don't rush to remove.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Repeating code. Please use a variable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed duplicate GetEnvironmentVariable call. Thanks

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We usually use a space after //.
And the comment is obvious and likely unnecessary.

@iSazonov

Copy link
Copy Markdown
Collaborator

@kittholland Thanks for your contribution!

My question about the copyright header was not addressed to you. Please don't rush to remove it.
With this in mind, the PR is LGTM. Please wait review from PG.

@PowerShellTeam PowerShellTeam added the Review - Needed The PR is being reviewed label Nov 28, 2016

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.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

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

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

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.

Yep, totally understood. Thanks!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This implies that we need to open a new Issue with analysing what cmdlets must be corrected.

@JamesWTruher

Copy link
Copy Markdown
Collaborator

Are the tests in test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 sufficient, or should there be more?

@iSazonov

iSazonov commented Dec 1, 2016

Copy link
Copy Markdown
Collaborator

It would be nice to test i/o on the file.

@kittholland

Copy link
Copy Markdown
Contributor Author

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.

@iSazonov

iSazonov commented Dec 1, 2016

Copy link
Copy Markdown
Collaborator

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

@lzybkr lzybkr self-assigned this Dec 3, 2016
@lzybkr lzybkr added Review - Waiting on Author and removed Review - Needed The PR is being reviewed labels Dec 3, 2016
@lzybkr

lzybkr commented Dec 3, 2016

Copy link
Copy Markdown
Contributor

We can merge as soon as the conflicts are resolved.

@kittholland

Copy link
Copy Markdown
Contributor Author

Conflicts have been resolved, thanks.

@PowerShellTeam PowerShellTeam added Review - Needed The PR is being reviewed and removed Review - Waiting on Author labels Dec 5, 2016
@lzybkr

lzybkr commented Dec 5, 2016

Copy link
Copy Markdown
Contributor

@kittholland - can you rebase? The PR now has a bunch of commits it shouldn't.

@lzybkr lzybkr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some unicode characters got messed up, please fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Revert this change please.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Revert this change please.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Revert this change please.

Literal translation of New-TemporaryFile function to a C# cmdlet,
other than moving logic from Begin to End.
@lzybkr lzybkr merged commit e568fda into PowerShell:master Dec 7, 2016
@iSazonov

iSazonov commented Dec 7, 2016

Copy link
Copy Markdown
Collaborator

@kittholland Thanks for your contribution!

@iSazonov iSazonov mentioned this pull request Nov 22, 2025
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.