Skip to content
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

(#3477) Add ShouldProcess support to rewritten helper cmdlets #3532

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

vexx32
Copy link
Member

@vexx32 vexx32 commented Oct 21, 2024

Description Of Changes

  • Add ShouldProcess support to Set-EnvironmentVariable, Install-ChocolateyPath, Uninstall-ChocolateyPath, and Update-SessionEnvironment
  • Add unit tests verifying just the base logic of the commands, that the ShouldProcess lines get hit when you use -WhatIf in the appropriate circumstances.
  • Tag potentially destructive / system-state-modifying tests with VMOnly so they do not run by default in typical dev environments

Motivation and Context

In discussion with @gep13 we realised there is value in having the ability to non-destructively verify the logic paths taken in the code here. As such, I have augmented the existing commands and associated helper classes with ShouldProcess semantics to make this workable.

Testing

  1. Run build.ps1
  2. Run ./Invoke-Tests.ps1 -Tags WhatIf

Operating Systems Testing

WIn10

Change Types Made

  • Bug fix (non-breaking change).
  • Feature / Enhancement (non-breaking change).
  • Breaking change (fix or feature that could cause existing functionality to change).
  • Documentation changes.
  • PowerShell code changes.

Change Checklist

  • Requires a change to the documentation -- will submit a docs PR to update these cmdlet docs as well, ShouldProcess will add -Confirm and -WhatIf to the documentation list iirc
  • Documentation has been updated.
  • Tests to cover my changes, have been added.
  • All new and existing tests passed?
  • PowerShell code changes: PowerShell v3 compatibility checked?

Related Issue

Part of #3477

Docs PR: chocolatey/docs#1086

Copy link
Member

@gep13 gep13 left a comment

Choose a reason for hiding this comment

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

This is a great addition, and I don't see any reason that we can't pull this into 2.4.0. I have left one minor comment, but other than that, this looks great!

It 'skips removing the value if it is not present' {
$targetPathEntry = [Environment]::GetEnvironmentVariable('PATH', $Scope) -split ';' | Select-Object -First 1
$Command = [scriptblock]::Create("Uninstall-ChocolateyPath -Path 'C:\ThisShouldNotBePresent' -Scope $Scope -WhatIf")
Get-WhatIfResult -Preamble $Preamble -Command $Command | Should -BeNullOrEmpty -Because 'we should skip adding values that already exist'
Copy link
Member

Choose a reason for hiding this comment

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

Should the Because here not be something like we should skip uninstalling a value that doesn't exist?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks!

@gep13
Copy link
Member

gep13 commented Oct 25, 2024

Also, this is tagged against #3477 but should it not actually be tagged against #3318? I realise that this has already shipped, but this change is related to those cmdlets, and not the new ones that will be converted as part of #3477.

Some cmdlets should support ShouldProcess, this change ensures that they
do correctly support it, along with adding checks to the underlying
methods to make it work as users will expect.

As Chocolatey CLI already supports --dry-run itself, this is primarily
going to be useful for effectively unit testing the commands.
Update-SessionEnvironment, Set-EnvironmentVariable, and
(Un)Install-ChocolateyPath implement ShouldProcess, so we can use
-WhatIf to verify their behaviour with unit tests.
@vexx32
Copy link
Member Author

vexx32 commented Oct 25, 2024

@gep13 I think I've addressed both of those, thanks! Also rebased on develop.

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.

2 participants