Skip to content

Commit

Permalink
Merge pull request #2970 from TheCakeIsNaOH/version-normalization-tests
Browse files Browse the repository at this point in the history
(#1174)(#1610) Add tests for package versions with leading zeros
  • Loading branch information
gep13 committed Jan 16, 2023
2 parents ed5359e + de451cd commit b86f484
Show file tree
Hide file tree
Showing 8 changed files with 997 additions and 6 deletions.
52 changes: 51 additions & 1 deletion src/chocolatey.tests.integration/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ namespace chocolatey.tests.integration
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;

using System.Xml.Linq;
using chocolatey.infrastructure.app;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.domain;
Expand All @@ -29,6 +30,8 @@ namespace chocolatey.tests.integration
using chocolatey.infrastructure.filesystem;
using chocolatey.infrastructure.guards;
using chocolatey.infrastructure.platforms;
using NuGet.Configuration;
using NuGet.Packaging;

public class Scenario
{
Expand Down Expand Up @@ -201,6 +204,53 @@ public static void create_directory(string directoryPath)
_fileSystem.create_directory(directoryPath);
}

public static void add_changed_version_package_to_source_location(ChocolateyConfiguration config, string pattern, string newVersion)
{
_fileSystem.create_directory_if_not_exists(config.Sources);
var contextDir = _fileSystem.combine_paths(get_top_level(), "context");
var files = _fileSystem.get_files(contextDir, pattern, SearchOption.AllDirectories);

foreach (var file in files.or_empty_list_if_null())
{
var copyToPath = _fileSystem.combine_paths(config.Sources, _fileSystem.get_file_name(file));
_fileSystem.copy_file(_fileSystem.get_full_path(file), copyToPath, overwriteExisting: true);
change_package_version(copyToPath, newVersion);
}
}

public static void change_package_version(string existingPackagePath, string newVersion)
{
string packageId;
XDocument nuspecXml;

using (var packageStream = new FileStream(existingPackagePath, FileMode.Open, FileAccess.ReadWrite))
{
using (var packageReader = new PackageArchiveReader(packageStream, true))
{
nuspecXml = packageReader.NuspecReader.Xml;
var metadataNode = nuspecXml.Root.Elements().FirstOrDefault(e => StringComparer.Ordinal.Equals(e.Name.LocalName, "metadata"));
var metadataNamespace = metadataNode.GetDefaultNamespace().NamespaceName;
var node = metadataNode.Elements(XName.Get("version", metadataNamespace)).FirstOrDefault();
node.Value = newVersion;
packageId = packageReader.GetIdentity().Id;
}

using (var zipArchive = new ZipArchive(packageStream, ZipArchiveMode.Update))
{
var entry = zipArchive.GetEntry("{0}{1}".format_with(packageId, NuGetConstants.ManifestExtension));
using (var nuspecStream = entry.Open())
{
nuspecXml.Save(nuspecStream);
}
}
}

var renamedPath = _fileSystem.combine_paths(
_fileSystem.get_directory_name(existingPackagePath),
"{0}.{1}{2}".format_with(packageId, newVersion, NuGetConstants.PackageExtension));
_fileSystem.move_file(existingPackagePath, renamedPath);
}

private static ChocolateyConfiguration baseline_configuration()
{
delete_test_package_directories();
Expand Down
205 changes: 205 additions & 0 deletions src/chocolatey.tests.integration/scenarios/InfoScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,211 @@ public void should_log_package_count_as_warning()
}
}

public class when_searching_for_exact_package_with_version_specified : CommandScenariosBase
{
public override void Context()
{
base.Context();

Configuration.PackageNames = Configuration.Input = "installpackage";

Configuration.Sources = "PackageOutput";
Scenario.add_packages_to_source_location(Configuration, "installpackage.*" + NuGetConstants.PackageExtension);

Configuration.Version = "1.0.0";
}

[Fact]
public void should_log_standalone_header_with_package_name_and_version()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain("installpackage 1.0.0");
}

[Fact]
public void should_log_package_information()
{
var lastWriteDate = File.GetLastWriteTimeUtc(Path.Combine("PackageOutput", "installpackage.1.0.0" + NuGetConstants.PackageExtension))
.ToShortDateString();

MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain(" Title: installpackage | Published: {0}\r\n Number of Downloads: n/a | Downloads for this version: n/a\r\n Package url\r\n Chocolatey Package Source: n/a\r\n Tags: installpackage admin\r\n Software Site: n/a\r\n Software License: n/a\r\n Summary: __REPLACE__\r\n Description: __REPLACE__\r\n".format_with(lastWriteDate));
}

[Fact]
public void should_log_package_count_as_warning()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Warn.to_string());
MockLogger.Messages[LogLevel.Warn.to_string()].ShouldContain("1 packages found.");
}
}

public class when_searching_for_exact_package_with_non_normalized_version_specified : CommandScenariosBase
{
public override void Context()
{
base.Context();

Configuration.PackageNames = Configuration.Input = "installpackage";

Configuration.Sources = "PackageOutput";
Scenario.add_packages_to_source_location(Configuration, "installpackage.*" + NuGetConstants.PackageExtension);

Configuration.Version = "01.0.0.0";
}

[Fact]
public void should_log_standalone_header_with_package_name_and_version()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain("installpackage 1.0.0");
}

[Fact]
public void should_log_package_information()
{
var lastWriteDate = File.GetLastWriteTimeUtc(Path.Combine("PackageOutput", "installpackage.1.0.0" + NuGetConstants.PackageExtension))
.ToShortDateString();

MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain(" Title: installpackage | Published: {0}\r\n Number of Downloads: n/a | Downloads for this version: n/a\r\n Package url\r\n Chocolatey Package Source: n/a\r\n Tags: installpackage admin\r\n Software Site: n/a\r\n Software License: n/a\r\n Summary: __REPLACE__\r\n Description: __REPLACE__\r\n".format_with(lastWriteDate));
}

[Fact]
public void should_log_package_count_as_warning()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Warn.to_string());
MockLogger.Messages[LogLevel.Warn.to_string()].ShouldContain("1 packages found.");
}
}

public class when_searching_for_non_normalized_exact_package : CommandScenariosBase
{
private string NonNormalizedVersion = "004.0.01.0";
private string NormalizedVersion = "4.0.1";

public override void Context()
{
base.Context();

Configuration.PackageNames = Configuration.Input = "installpackage";

Configuration.Sources = "PackageOutput";

Scenario.add_changed_version_package_to_source_location(Configuration, "installpackage.1.0.0" + NuGetConstants.PackageExtension, NonNormalizedVersion);
}

[Fact]
public void should_log_standalone_header_with_package_name_and_version()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain("installpackage {0}".format_with(NormalizedVersion));
}

[Fact]
public void should_log_package_information()
{
var lastWriteDate = File.GetLastWriteTimeUtc(Path.Combine("PackageOutput", "installpackage.{0}".format_with(NonNormalizedVersion) + NuGetConstants.PackageExtension))
.ToShortDateString();

MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain(" Title: installpackage | Published: {0}\r\n Number of Downloads: n/a | Downloads for this version: n/a\r\n Package url\r\n Chocolatey Package Source: n/a\r\n Tags: installpackage admin\r\n Software Site: n/a\r\n Software License: n/a\r\n Summary: __REPLACE__\r\n Description: __REPLACE__\r\n".format_with(lastWriteDate));
}

[Fact]
public void should_log_package_count_as_warning()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Warn.to_string());
MockLogger.Messages[LogLevel.Warn.to_string()].ShouldContain("1 packages found.");
}
}

public class when_searching_for_non_normalized_exact_package_with_version_specified : CommandScenariosBase
{
private string NonNormalizedVersion = "004.0.01.0";
private string NormalizedVersion = "4.0.1";

public override void Context()
{
base.Context();

Configuration.PackageNames = Configuration.Input = "installpackage";

Configuration.Sources = "PackageOutput";

Scenario.add_changed_version_package_to_source_location(Configuration, "installpackage.1.0.0" + NuGetConstants.PackageExtension, NonNormalizedVersion);

Configuration.Version = "4.0.1";
}

[Fact]
public void should_log_standalone_header_with_package_name_and_version()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain("installpackage {0}".format_with(NormalizedVersion));
}

[Fact]
public void should_log_package_information()
{
var lastWriteDate = File.GetLastWriteTimeUtc(Path.Combine("PackageOutput", "installpackage.{0}".format_with(NonNormalizedVersion) + NuGetConstants.PackageExtension))
.ToShortDateString();

MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain(" Title: installpackage | Published: {0}\r\n Number of Downloads: n/a | Downloads for this version: n/a\r\n Package url\r\n Chocolatey Package Source: n/a\r\n Tags: installpackage admin\r\n Software Site: n/a\r\n Software License: n/a\r\n Summary: __REPLACE__\r\n Description: __REPLACE__\r\n".format_with(lastWriteDate));
}

[Fact]
public void should_log_package_count_as_warning()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Warn.to_string());
MockLogger.Messages[LogLevel.Warn.to_string()].ShouldContain("1 packages found.");
}
}

public class when_searching_for_non_normalized_exact_package_with_non_normalized_version_specified : CommandScenariosBase
{
private string NonNormalizedVersion = "004.0.01.0";
private string NormalizedVersion = "4.0.1";

public override void Context()
{
base.Context();

Configuration.PackageNames = Configuration.Input = "installpackage";

Configuration.Sources = "PackageOutput";

Scenario.add_changed_version_package_to_source_location(Configuration, "installpackage.1.0.0" + NuGetConstants.PackageExtension, NonNormalizedVersion);

Configuration.Version = NonNormalizedVersion;
}

[Fact]
public void should_log_standalone_header_with_package_name_and_version()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain("installpackage {0}".format_with(NormalizedVersion));
}

[Fact]
public void should_log_package_information()
{
var lastWriteDate = File.GetLastWriteTimeUtc(Path.Combine("PackageOutput", "installpackage.{0}".format_with(NonNormalizedVersion) + NuGetConstants.PackageExtension))
.ToShortDateString();

MockLogger.Messages.Keys.ShouldContain(LogLevel.Info.to_string());
MockLogger.Messages[LogLevel.Info.to_string()].ShouldContain(" Title: installpackage | Published: {0}\r\n Number of Downloads: n/a | Downloads for this version: n/a\r\n Package url\r\n Chocolatey Package Source: n/a\r\n Tags: installpackage admin\r\n Software Site: n/a\r\n Software License: n/a\r\n Summary: __REPLACE__\r\n Description: __REPLACE__\r\n".format_with(lastWriteDate));
}

[Fact]
public void should_log_package_count_as_warning()
{
MockLogger.Messages.Keys.ShouldContain(LogLevel.Warn.to_string());
MockLogger.Messages[LogLevel.Warn.to_string()].ShouldContain("1 packages found.");
}
}

public class when_searching_for_exact_package_with_dot_relative_path_source : when_searching_for_exact_package_through_command
{
public override void Context()
Expand Down
Loading

0 comments on commit b86f484

Please sign in to comment.