Skip to content

Commit

Permalink
(chocolatey#3174) Apply version number normalizing consistently
Browse files Browse the repository at this point in the history
Went through all references to `ToStringSafe` which were being called on
a NuGetVersion instance and changed them all to
`ToNormalizedStringChecked`.

Additionally, found a place in the upgrade dry-run code that was just
missing a call to this method and outputting a non-normalized version
string and added it.
  • Loading branch information
vexx32 authored and AdmiringWorm committed Jun 1, 2023
1 parent cff06ee commit 723611a
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 120 deletions.
72 changes: 36 additions & 36 deletions src/chocolatey.tests.integration/scenarios/InstallScenarios.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,8 @@ public class When_uninstalling_a_package_with_non_normalized_version : Scenarios
{
private PackageResult packageResult;

private string NonNormalizedVersion = "0004.0004.00005.00";
private const string NonNormalizedVersion = "0004.0004.00005.00";
private const string NormalizedVersion = "4.4.5";

public override void Context()
{
Expand Down Expand Up @@ -1550,15 +1551,15 @@ public void Config_should_match_package_result_name()
[Platform(Exclude = "Mono")]
public void Should_have_executed_chocolateyBeforeModify_script()
{
MockLogger.ContainsMessage("upgradepackage {0} Before Modification".FormatWith(NonNormalizedVersion), LogLevel.Info).ShouldBeTrue();
MockLogger.ContainsMessage("upgradepackage {0} Before Modification".FormatWith(NormalizedVersion), LogLevel.Info).ShouldBeTrue();
}

[Fact]
[WindowsOnly]
[Platform(Exclude = "Mono")]
public void Should_have_executed_chocolateyUninstall_script()
{
MockLogger.ContainsMessage("upgradepackage {0} Uninstalled".FormatWith(NonNormalizedVersion), LogLevel.Info).ShouldBeTrue();
MockLogger.ContainsMessage("upgradepackage {0} Uninstalled".FormatWith(NormalizedVersion), LogLevel.Info).ShouldBeTrue();
}
}

Expand Down
122 changes: 61 additions & 61 deletions src/chocolatey.tests.integration/scenarios/UpgradeScenarios.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void Run(PackageResult packageResult, ChocolateyConfiguration config)

foreach (var key in registryKeys.OrEmpty())
{
var packageCacheLocation = _fileSystem.CombinePaths(_fileSystem.GetFullPath(config.CacheLocation), package.Id, package.Version.ToStringSafe());
var packageCacheLocation = _fileSystem.CombinePaths(_fileSystem.GetFullPath(config.CacheLocation), package.Id, package.Version.ToNormalizedStringChecked());
Remove(key, config, packageResult, packageCacheLocation);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void Save(ChocolateyPackageInformation packageInformation)
{
var versionOverrideFile = _fileSystem.CombinePaths(pkgStorePath, VersionOverrideFile);
if (_fileSystem.FileExists(versionOverrideFile)) _fileSystem.DeleteFile(versionOverrideFile);
_fileSystem.WriteFile(versionOverrideFile, packageInformation.VersionOverride.ToStringSafe());
_fileSystem.WriteFile(versionOverrideFile, packageInformation.VersionOverride.ToNormalizedStringChecked());
}
else
{
Expand Down Expand Up @@ -266,6 +266,9 @@ private static string GetStorePath(IFileSystem fileSystem, string id, NuGetVersi
return preferredStorePath;
}

// Legacy handling for old package versions that was installed prior to v2.0.0.
// Do not remove the call to `ToStringSafe`.

var pkgStorePath = fileSystem.CombinePaths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".FormatWith(id, version.ToStringSafe()));

if (fileSystem.DirectoryExists(pkgStorePath))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2023 Chocolatey Software, Inc
// Copyright © 2017 - 2023 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -1312,11 +1312,6 @@ The recent package changes indicate a reboot is necessary.

public virtual void HandlePackageUninstall(PackageResult packageResult, ChocolateyConfiguration config)
{
if (!_fileSystem.DirectoryExists(packageResult.InstallLocation))
{
packageResult.InstallLocation += ".{0}".FormatWith(packageResult.PackageMetadata.Version.ToStringSafe());
}

//These items only apply to windows systems.
if (config.Information.PlatformType == PlatformType.Windows)
{
Expand Down
10 changes: 5 additions & 5 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
}
else
{
this.Log().Info("{0}|{1}|{2}|{3}".FormatWith(installedPackage.PackageMetadata.Id, installedPackage.Version, availablePackage.Identity.Version, isPinned.ToStringSafe().ToLowerSafe()));
this.Log().Info("{0}|{1}|{2}|{3}".FormatWith(installedPackage.PackageMetadata.Id, installedPackage.Version, availablePackage.Identity.Version.ToNormalizedStringChecked(), isPinned.ToStringSafe().ToLowerSafe()));
}
}

Expand Down Expand Up @@ -1858,7 +1858,7 @@ public virtual void BackupChangedFiles(string packageInstallPath, ChocolateyConf
{
if (packageInfo == null || packageInfo.Package == null) return;

var version = packageInfo.Package.Version.ToStringSafe();
var version = packageInfo.Package.Version.ToNormalizedStringChecked();

if (packageInfo.FilesSnapshot == null || packageInfo.FilesSnapshot.Files.Count == 0)
{
Expand Down Expand Up @@ -1923,7 +1923,7 @@ private void RemoveShimgenDirectors(IPackageMetadata installedPackage)
private void RemovePackageFromCache(ChocolateyConfiguration config, IPackageMetadata installedPackage)
{
this.Log().Debug(ChocolateyLoggers.Verbose, "Ensuring removal of package cache files.");
var cacheDirectory = _fileSystem.CombinePaths(config.CacheLocation, installedPackage.Id, installedPackage.Version.ToStringSafe());
var cacheDirectory = _fileSystem.CombinePaths(config.CacheLocation, installedPackage.Id, installedPackage.Version.ToNormalizedStringChecked());

if (!_fileSystem.DirectoryExists(cacheDirectory)) return;

Expand Down Expand Up @@ -1952,8 +1952,8 @@ protected void RemovePackageFromNugetCache(IPackageSearchMetadata installedPacka
FaultTolerance.TryCatchWithLoggingException(
() =>
{
var packageFolderPath = _fileSystem.CombinePaths(tempFolder, "{0}/{1}".FormatWith(installedPackage.Identity.Id, installedPackage.Identity.Version.ToStringSafe()));
var nugetCachedFile = _fileSystem.CombinePaths(packageFolderPath, "{0}.{1}.nupkg".FormatWith(installedPackage.Identity.Id, installedPackage.Identity.Version.ToStringSafe()));
var packageFolderPath = _fileSystem.CombinePaths(tempFolder, "{0}/{1}".FormatWith(installedPackage.Identity.Id, installedPackage.Identity.Version.ToNormalizedStringChecked()));
var nugetCachedFile = _fileSystem.CombinePaths(packageFolderPath, "{0}.{1}.nupkg".FormatWith(installedPackage.Identity.Id, installedPackage.Identity.Version.ToNormalizedStringChecked()));
var nupkgMetaDataFile = _fileSystem.CombinePaths(packageFolderPath, ".nupkg.metadata");
var nupkgShaFile = nugetCachedFile + ".sha512";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ public void PreparePowerShellEnvironment(IPackageSearchMetadata package, Chocola
Environment.SetEnvironmentVariable("packageName", package.Identity.Id);
Environment.SetEnvironmentVariable("chocolateyPackageTitle", package.Title);
Environment.SetEnvironmentVariable("packageTitle", package.Title);
Environment.SetEnvironmentVariable("chocolateyPackageVersion", package.Identity.Version.ToStringSafe());
Environment.SetEnvironmentVariable("packageVersion", package.Identity.Version.ToStringSafe());
Environment.SetEnvironmentVariable("chocolateyPackageVersion", package.Identity.Version.ToNormalizedStringChecked());
Environment.SetEnvironmentVariable("packageVersion", package.Identity.Version.ToNormalizedStringChecked());
Environment.SetEnvironmentVariable("chocolateyPackageVersionPrerelease", package.Identity.Version.Release.ToStringSafe());
Environment.SetEnvironmentVariable("chocolateyPackageVersionPackageRelease", package.Identity.Version.Version.ToStringSafe());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected void ListCustomTemplateInformation(ChocolateyConfiguration configurati

var templateInstalledViaPackage = (pkg != null);

var pkgVersion = templateInstalledViaPackage ? pkg.Identity.Version.ToStringSafe() : "0.0.0";
var pkgVersion = templateInstalledViaPackage ? pkg.Identity.Version.ToNormalizedStringChecked() : "0.0.0";
var pkgTitle = templateInstalledViaPackage ? pkg.Title : "{0} (Unmanaged)".FormatWith(configuration.TemplateCommand.Name);
var pkgSummary = templateInstalledViaPackage ?
(pkg.Summary != null && !string.IsNullOrWhiteSpace(pkg.Summary.ToStringSafe()) ? "{0}".FormatWith(pkg.Summary.EscapeCurlyBraces().ToStringSafe()) : string.Empty) : string.Empty;
Expand Down
8 changes: 4 additions & 4 deletions src/chocolatey/infrastructure/results/PackageResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ public void ResetMetadata(IPackageMetadata metadata, IPackageSearchMetadata sear
PackageMetadata = metadata;
SearchMetadata = search;
Name = metadata.Id;
Version = metadata.Version.ToStringSafe();
Version = metadata.Version.ToNormalizedStringChecked();
}

public PackageResult(IPackageMetadata packageMetadata, string installLocation, string source = null) : this(packageMetadata.Id, packageMetadata.Version.ToStringSafe(), installLocation)
public PackageResult(IPackageMetadata packageMetadata, string installLocation, string source = null) : this(packageMetadata.Id, packageMetadata.Version.ToNormalizedStringChecked(), installLocation)
{
PackageMetadata = packageMetadata;
Source = source;
}

public PackageResult(IPackageSearchMetadata packageSearch, string installLocation, string source = null) : this(packageSearch.Identity.Id, packageSearch.Identity.Version.ToStringSafe(), installLocation)
public PackageResult(IPackageSearchMetadata packageSearch, string installLocation, string source = null) : this(packageSearch.Identity.Id, packageSearch.Identity.Version.ToNormalizedStringChecked(), installLocation)
{
SearchMetadata = packageSearch;
Source = source;
Expand Down Expand Up @@ -103,7 +103,7 @@ public PackageResult(IPackageSearchMetadata packageSearch, string installLocatio
*/
}

public PackageResult(IPackageMetadata packageMetadata, IPackageSearchMetadata packageSearch, string installLocation, string source = null) : this(packageMetadata.Id, packageMetadata.Version.ToStringSafe(), installLocation)
public PackageResult(IPackageMetadata packageMetadata, IPackageSearchMetadata packageSearch, string installLocation, string source = null) : this(packageMetadata.Id, packageMetadata.Version.ToNormalizedStringChecked(), installLocation)
{
SearchMetadata = packageSearch;
PackageMetadata = packageMetadata;
Expand Down

0 comments on commit 723611a

Please sign in to comment.