Skip to content

Conversation

@sarithsutha
Copy link
Contributor

Closes #4702

Add -WhatIf switch to Start-Process cmdlet

@msftclas
Copy link

msftclas commented Sep 1, 2017

@sarithsutha,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by Microsoft. We will now review your pull request.
Thanks,
Microsoft Pull Request Bot


It "Should be able to use the whatif switch without error" {
{ Start-Process $pingCommand -ArgumentList $pingParam -WhatIf } | Should Not Throw
}
Copy link
Contributor

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.

Copy link
Contributor

@bergmeister bergmeister Sep 3, 2017

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
}

Copy link
Contributor Author

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.

@sarithsutha
Copy link
Contributor Author

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
}
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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
Copy link
Collaborator

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>
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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
Copy link
Collaborator

@iSazonov iSazonov Sep 5, 2017

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
Copy link
Contributor

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree.

Copy link
Contributor Author

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.

Copy link
Collaborator

@iSazonov iSazonov left a 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..

@sarithsutha
Copy link
Contributor Author

Thanks @iSazonov for reviewing and approving my changes. The minor comment is now incorporated.

Copy link
Contributor

@markekraus markekraus left a comment

Choose a reason for hiding this comment

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

LGTM

@iSazonov
Copy link
Collaborator

iSazonov commented Sep 6, 2017

@daxian-dbw Could you please review and approve?

startInfo.WindowStyle = _windowstyle;
}

string targetMessage = StringUtil.Format(ProcessResources.ProcessStartInfo, startInfo.FileName, startInfo.Arguments.Trim());
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

@sarithsutha Thanks!

@iSazonov iSazonov merged commit f4b075c into PowerShell:master Sep 8, 2017
@iSazonov
Copy link
Collaborator

iSazonov commented Sep 8, 2017

@sarithsutha Thanks for your contribution!

@sarithsutha sarithsutha deleted the start-process-enhancements branch September 8, 2017 13:42
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.

6 participants