From b810acb88ee741e5be292b2d7969d584b92d5bdc 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. --- src/chocolatey/StringResources.cs | 60 +++++++++++++++++++ src/chocolatey/chocolatey.csproj | 1 + .../services/PowershellService.cs | 4 ++ .../templates/ChocolateyReadMeTemplate.cs | 2 +- .../commands/choco-install.Tests.ps1 | 33 ++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/chocolatey/StringResources.cs diff --git a/src/chocolatey/StringResources.cs b/src/chocolatey/StringResources.cs new file mode 100644 index 0000000000..4bbf0f6c99 --- /dev/null +++ b/src/chocolatey/StringResources.cs @@ -0,0 +1,60 @@ +// Copyright © 2023-Present Chocolatey Software, Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey +{ + using System.ComponentModel; + + public static class StringResources + { + /// + /// Resources for the names of available environment variables + /// that will be created or used as part of executing + /// Chocolatey CLI. + /// + /// + /// DEV NOTICE: Mark anything that is not meant for public consumption as + /// internal constants and not browsable, even if used in other projects. + /// + public static class EnvironmentVariables + { + /// + /// The version of the package that is being handled as it is defined in the embedded + /// nuspec file. + /// + /// + /// Will be sets during package installs, upgrades and uninstalls. + /// Environment variable is only for internal uses. + /// + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Browsable(false)] + internal const string ChocolateyPackageNuspecVersion = "chocolateyPackageNuspecVersion"; + + /// + /// The version of the package that is being handled as it is defined in the embedded + /// nuspec file. + /// + /// + /// Will be sets during package installs, upgrades and uninstalls. + /// Environment variable is only for internal uses. + /// + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Browsable(false)] + internal const string PackageNuspecVersion = "packageNuspecVersion"; + } + } +} \ No newline at end of file diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index cad4959a51..eb291d883c 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -475,6 +475,7 @@ + diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index b0b60b5ef7..b80dce5ba8 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(StringResources.EnvironmentVariables.ChocolateyPackageNuspecVersion, package.Identity.Version.ToStringSafe()); + Environment.SetEnvironmentVariable(StringResources.EnvironmentVariables.PackageNuspecVersion, 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..c5257a4d49 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 = 'chocolateyPackageNuspecVersion' + Value= '0.9' + } + @{ + Name = 'packageNuspecVersion' + 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