Skip to content

Commit

Permalink
(chocolatey#3174) Store non-normalized package version
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AdmiringWorm committed Jun 27, 2023
1 parent 10a91ef commit 851dd44
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/chocolatey/StringResources.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Resources for the names of available environment variables
/// that will be created or used as part of executing
/// Chocolatey CLI.
/// </summary>
/// <remarks>
/// DEV NOTICE: Mark anything that is not meant for public consumption as
/// internal constants and not browsable, even if used in other projects.
/// </remarks>
public static class EnvironmentVariables
{
/// <summary>
/// The version of the package that is being handled as it is defined in the embedded
/// nuspec file.
/// </summary>
/// <remarks>
/// Will be sets during package installs, upgrades and uninstalls.
/// Environment variable is only for internal uses.
/// </remarks>
/// <seealso cref="PackageNuspecVersion" />
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
internal const string ChocolateyPackageNuspecVersion = "chocolateyPackageNuspecVersion";

/// <summary>
/// The version of the package that is being handled as it is defined in the embedded
/// nuspec file.
/// </summary>
/// <remarks>
/// Will be sets during package installs, upgrades and uninstalls.
/// Environment variable is only for internal uses.
/// </remarks>
/// <seealso cref="ChocolateyPackageNuspecVersion" />
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
internal const string PackageNuspecVersion = "packageNuspecVersion";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class ChocolateyReadMeTemplate
* ChocolateyInstall - Top level folder where Chocolatey is installed
* ChocolateyPackageName - The name of the package, equivalent to the `<id />` field in the nuspec
* ChocolateyPackageTitle - The title of the package, equivalent to the `<title />` 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
Expand Down
33 changes: 33 additions & 0 deletions tests/chocolatey-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 851dd44

Please sign in to comment.