-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add -WhatIf switch to Start-Process cmdlet #4735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add -WhatIf switch to Start-Process cmdlet #4735
Conversation
|
@sarithsutha, |
|
|
||
| It "Should be able to use the whatif switch without error" { | ||
| { Start-Process $pingCommand -ArgumentList $pingParam -WhatIf } | Should Not Throw | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add a test that would normally perform an action and ensure that -WhatIf prevented the action?
For example, have a Start-Process command to write a string to a file and then test to ensure the file was not written to with -WhatIf is supplied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also a test that using the -PassThru switch, no object gets returned (which is also a good additional test that the command was not executed)?
It "returns null when using -WhatIf switch with `PassThru`" {
{ Start-Process $pingCommand -ArgumentList $pingParam -PassThru -WhatIf } | Should Be $null
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion @bergmeister . I've added the test that you described.
… from being performed.
|
Thanks for your suggestion @markekraus . I've added that test. |
|
|
||
| It "Should be able to use the whatif switch without error" { | ||
| { Start-Process $pingCommand -ArgumentList $pingParam -WhatIf } | Should Not Throw | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the test does not need - next do the check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I did not get which - were you referring to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant next It block.
| It "Should be able to use the whatif switch without performing the actual action" { | ||
| $pingOutput = Join-Path $TestDrive "pingOutput.txt" | ||
| Start-Process -Wait $pingCommand -ArgumentList $pingParam -RedirectStandardOutput $pingOutput -WhatIf | ||
| Test-Path $pingOutput | Should Be $false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe:
$pingOutput | Should Not Exist | </data> | ||
| <data name="ProcessStartInfo" xml:space="preserve"> | ||
| <value>{0} {1}</value> | ||
| </data> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We re-use ProcessNameForConfirmation in Stop-Process and Debug-Process so I believe we can use the same in Start-process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original request was to output the exact command being executed. By re-using the ProcessNameForConfirmation, the format of the output will be changed slightly, the arguments would appear in the parentheses. i.e. C:\WINDOWS\system32\PING.EXE (-n 2 localhost) instead of C:\WINDOWS\system32\PING.EXE -n 2 localhost.
If that is acceptable, I'm happy to make the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarify.
Closed.
|
|
||
| It "Should be able to use the whatif switch without performing the actual action" { | ||
| $pingOutput = Join-Path $TestDrive "pingOutput.txt" | ||
| Start-Process -Wait $pingCommand -ArgumentList $pingParam -RedirectStandardOutput $pingOutput -WhatIf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove next It and do the check here.
| It "Should be able to use the -WhatIf switch without performing the actual action" { | ||
| $pingOutput = Join-Path $TestDrive "pingOutput.txt" | ||
| Start-Process -Wait $pingCommand -ArgumentList $pingParam -RedirectStandardOutput $pingOutput -WhatIf | ||
| { Start-Process -Wait $pingCommand -ArgumentList $pingParam -RedirectStandardOutput $pingOutput -WhatIf} | Should Not Throw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe -ErrorAction Stop is required or it will never actually throw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @markekraus for pointing this out. Not sure how I missed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AppVeyor failed on other tests so the PR LGTM with one minor comment..
|
Thanks @iSazonov for reviewing and approving my changes. The minor comment is now incorporated. |
markekraus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@daxian-dbw Could you please review and approve? |
| startInfo.WindowStyle = _windowstyle; | ||
| } | ||
|
|
||
| string targetMessage = StringUtil.Format(ProcessResources.ProcessStartInfo, startInfo.FileName, startInfo.Arguments.Trim()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resource string id ProcessStartInfo should be changed to StartProcessTarget.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw Renamed the resource string as suggested.
daxian-dbw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sarithsutha Thanks!
|
@sarithsutha Thanks for your contribution! |
Closes #4702
Add -WhatIf switch to Start-Process cmdlet