From bc0bd9d3bfd5e4f9041e46cb39d52690a19f6b2c Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Mon, 26 Jun 2023 19:25:45 +0200 Subject: [PATCH] (#3174) Store non-normalized package version In a previous change we normalized all versions that was being used in Chocolatey CLI. This caused certain packages that uses non-normalized version numbers to start failing to look up additional information in other products. To work around this problem we need to store the non-normalized version of the package when scripts are being ran. This is done by adding a new environment variable that can be used, this environment variable is considered private and is not for public consumption. --- .../services/PowershellService.cs | 4 +++ .../templates/ChocolateyReadMeTemplate.cs | 2 +- .../commands/choco-install.Tests.ps1 | 33 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index b0b60b5ef7..3958007c7b 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -446,6 +446,10 @@ public void PreparePowerShellEnvironment(IPackageSearchMetadata package, Chocola Environment.SetEnvironmentVariable("packageTitle", package.Title); Environment.SetEnvironmentVariable("chocolateyPackageVersion", package.Identity.Version.ToNormalizedStringChecked()); Environment.SetEnvironmentVariable("packageVersion", package.Identity.Version.ToNormalizedStringChecked()); + // We use ToStringSafe on purpose here. There is a need for the version + // the package specified, not the normalized version we want users to use. + Environment.SetEnvironmentVariable("chocolateyPackageVersionOriginal", package.Identity.Version.ToStringSafe()); + Environment.SetEnvironmentVariable("packageVersionOriginal", package.Identity.Version.ToStringSafe()); Environment.SetEnvironmentVariable("chocolateyPackageVersionPrerelease", package.Identity.Version.Release.ToStringSafe()); Environment.SetEnvironmentVariable("chocolateyPackageFolder", packageDirectory); diff --git a/src/chocolatey/infrastructure.app/templates/ChocolateyReadMeTemplate.cs b/src/chocolatey/infrastructure.app/templates/ChocolateyReadMeTemplate.cs index 7dac594a22..fd17ae41c0 100644 --- a/src/chocolatey/infrastructure.app/templates/ChocolateyReadMeTemplate.cs +++ b/src/chocolatey/infrastructure.app/templates/ChocolateyReadMeTemplate.cs @@ -84,7 +84,7 @@ Chocolatey makes a number of environment variables available (You can access any * ChocolateyInstall - Top level folder where Chocolatey is installed * ChocolateyPackageName - The name of the package, equivalent to the `` field in the nuspec * ChocolateyPackageTitle - The title of the package, equivalent to the `` field in the nuspec - * ChocolateyPackageVersion - The version of the package, equivalent to the `<version />` field in the nuspec + * ChocolateyPackageVersion - The normalized version of the package, equivalent to a normalized edition of the `<version />` field in the nuspec * ChocolateyPackageFolder - The top level location of the package folder - the folder where Chocolatey has downloaded and extracted the NuGet package, typically `C:\ProgramData\chocolatey\lib\packageName`. #### Advanced Environment Variables diff --git a/tests/chocolatey-tests/commands/choco-install.Tests.ps1 b/tests/chocolatey-tests/commands/choco-install.Tests.ps1 index e0664cff38..59fb883e13 100644 --- a/tests/chocolatey-tests/commands/choco-install.Tests.ps1 +++ b/tests/chocolatey-tests/commands/choco-install.Tests.ps1 @@ -1863,6 +1863,39 @@ To install a local, or remote file, you may use: } } + # Tagged as Internal as this package needs to be packaged by an older version of Chocolatey CLI to have the nuspec version + # not be normalized. + Context 'Installing non-normalized package outputting all environment variables' -Tag Internal { + BeforeAll { + Restore-ChocolateyInstallSnapshot + + $Output = Invoke-Choco install test-environment --version 0.9 --confirm + } + + It 'Exits with Success (0)' { + $Output.ExitCode | Should -Be 0 -Because $Output.String + } + + It 'Outputs <Name> as <Value>' -ForEach @(@{ + Name = 'chocolateyPackageVersion' + Value= '0.9.0' + } + @{ + Name = 'packageVersion' + Value= '0.9.0' + } + @{ + Name = 'chocolateyPackageVersionOriginal' + Value= '0.9' + } + @{ + Name = 'packageVersionOriginal' + Value= '0.9' + }) { + $Output.Lines | Should -Contain "$Name=$Value" + } + } + # This needs to be the last test in this block, to ensure NuGet configurations aren't being created. # Any tests after this block are expected to generate the configuration as they're explicitly using the NuGet CLI Test-NuGetPaths