Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@gep13
Copy link
Member

@gep13 gep13 commented Jun 10, 2025

Description Of Changes

This commit adds the ability to output the previous package version, during say a choco upgrade command, as an environment variable. Prior to this commit, it was only the package version that was currently being installed/upgraded that was presented as an environment variable.

The method that is responsible for writing the environment variables is the PreparePowerShellEnvironment method, which is defined by the IPowershellService interface. Rather than make a change to the interface to pass in this information, it was decided to add a property to the ChocolateyConfiguration class, which is currently being passed through to this method. This means that there is some maintainability in place, where additional information can be passed through as environment variables in the future, should the need arise. This would be done by simply adding another item into the Dictionary.

Pester tests have been updated to allow the testing for this new environment variable during the process of upgrading a package version.

Motivation and Context

This is needed since there are times that during an upgrade you want to do something based on which version is being upgraded from. For example, you might have to do "something" when upgrading from version 1.0.0 and "something else" when upgrading from version 2.0.0. Being able to inspect the previous package version as an environment variable, in the same way as you can the package version being currently installed is very useful, and also consistent with how package maintainers get other information.

Testing

  1. Clone this repository, and check out PR branch
  2. Open project in Visual Studio, and set a breakpoint on line 1719 of the NugetService.cs class
  3. Run install vscode --version 1.100.2 -n
  4. The above breakpoint should not be hit, as the install is a different code path
  5. Run upgrade vscode -n
  6. The above breakpoint should be hit for first the vscode.install package, with the previous version of 1.100.2 and the current version 1.100.3
  7. Continue code execution
  8. The above breakpoint should be hit for first the vscode package, with the previous version of 1.100.2 and the current version 1.100.3
  9. Each time, the config.Information.EnvironmentVariables should either have a Count of 0, or have a null value for the ChocolateyPreviousPackageVersion entry.

Operating Systems Testing

  • Windows 11

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

Fixes #1713

@gep13 gep13 requested a review from AdmiringWorm June 10, 2025 10:51
@gep13
Copy link
Member Author

gep13 commented Jun 10, 2025

@AdmiringWorm as a reminder here, for both you, and me, we need to update the docs page with the new environment variable that is being created here.

gep13 and others added 2 commits June 11, 2025 12:03
This commit adds the ability to output the previous package version,
during say a `choco upgrade` command, as an environment variable.
Prior to this commit, it was only the package version that was
currently being installed/upgraded that was presented as an environment
variable.

The method that is responsible for writing the environment variables is
the PreparePowerShellEnvironment method, which is defined by the
IPowershellService interface.  Rather than make a change to the
interface to pass in this information, it was decided to add a property
to the ChocolateyConfiguration class, which is currently being passed
through to this method.  This means that there is some maintainability
in place, where additional information can be passed through as
environment variables in the future, should the need arise.  This would
be done by simply adding another item into the Dictionary.

Pester tests have been updated to allow the testing for this new
environment variable during the process of upgrading a package version.

This is needed since there are times that during an upgrade you want to
do something based on which version is being upgraded from. For
example, you might have to do "something" when upgrading from version
1.0.0 and "something else" when upgrading from version 2.0.0.  Being
able to inspect the previous package version as an environment
variable, in the same way as you can the package version being
currently installed is _very_ useful, and also consistent with how
package maintainers get other information.
Move clearing of ChocolateyPreviousPackageVersion environment variable
to a finally block to ensure it always runs after package operations. This
prevents version "pollution" between package installs or upgrades, especially
during `choco upgrade all` or when upgrading packages with dependencies.
Moving to a finally block also prevents the environment variable from being
reset before we run the Chocolatey Install script.
Copy link
Member

@AdmiringWorm AdmiringWorm left a comment

Choose a reason for hiding this comment

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

LGTM, and passed all expected tests internally.

@AdmiringWorm AdmiringWorm merged commit b8c0117 into chocolatey:develop Jun 11, 2025
5 checks passed
@AdmiringWorm
Copy link
Member

@gep13 great work on getting this added 👍

@gep13 gep13 deleted the issue1713 branch June 11, 2025 13:45
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.

Add ability for package maintainers to know the package version of previously installed package version during choco upgrade command

2 participants