From 1dcac3e649fa517a748fba420871d29b5d3a25b4 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Wed, 25 Jan 2023 23:08:28 -0600 Subject: [PATCH 1/2] (#1935) Add tests for minClientVersion These added tests ensure that a minClientVersion attribute that is higher than the Chocolatey and NuGet assembly versions does not prevent installation or packing. --- .../1.0.0/unsupportedelements.nuspec | 2 +- .../1.1.0/unsupportedelements.nuspec | 2 +- .../scenarios/PackScenarios.cs | 44 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/chocolatey.tests.integration/context/unsupportedelements/1.0.0/unsupportedelements.nuspec b/src/chocolatey.tests.integration/context/unsupportedelements/1.0.0/unsupportedelements.nuspec index 681e77fea1..68d7e8e42a 100644 --- a/src/chocolatey.tests.integration/context/unsupportedelements/1.0.0/unsupportedelements.nuspec +++ b/src/chocolatey.tests.integration/context/unsupportedelements/1.0.0/unsupportedelements.nuspec @@ -1,6 +1,6 @@ - + unsupportedelements 1.0.0 unsupportedelements diff --git a/src/chocolatey.tests.integration/context/unsupportedelements/1.1.0/unsupportedelements.nuspec b/src/chocolatey.tests.integration/context/unsupportedelements/1.1.0/unsupportedelements.nuspec index a0c1b0f35c..a86b86601a 100644 --- a/src/chocolatey.tests.integration/context/unsupportedelements/1.1.0/unsupportedelements.nuspec +++ b/src/chocolatey.tests.integration/context/unsupportedelements/1.1.0/unsupportedelements.nuspec @@ -1,6 +1,6 @@ - + unsupportedelements 1.1.0 unsupportedelements diff --git a/src/chocolatey.tests.integration/scenarios/PackScenarios.cs b/src/chocolatey.tests.integration/scenarios/PackScenarios.cs index 65c5006b31..b22a84e688 100644 --- a/src/chocolatey.tests.integration/scenarios/PackScenarios.cs +++ b/src/chocolatey.tests.integration/scenarios/PackScenarios.cs @@ -499,6 +499,27 @@ public void Should_throw_exception_on_icon_element() } } + public class when_packing_with_min_client_version : ScenariosBase + { + protected override string ExpectedNuspecVersion => "0.1.0"; + protected override string ExpectedSubDirectory => "PackageOutput"; + + // This high version is to ensure that pack does not throw, even if the min client version is well + // above both the Chocolatey and NuGet assembly versions. + private string MinClientVersion = "100.0.0"; + + public override void Because() + { + MockLogger.reset(); + Service.pack_run(Configuration); + } + + protected override string GetNuspecContent() + { + return NuspecContentWithFormatableMinClientVersion.format_with(MinClientVersion); + } + } + private const string NuspecContent = @" @@ -916,5 +937,28 @@ public void Should_throw_exception_on_icon_element() "; + + private const string NuspecContentWithFormatableMinClientVersion = @" + + + test-package + Test Package + 0.1.0 + package author + package owner + A brief summary + A big description + test admin + + http://apache.org/2 + false + + + + + + + +"; } } From 61cb649f4d9e788cf6d8955e658c75d2e76f9dad Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Wed, 25 Jan 2023 23:10:44 -0600 Subject: [PATCH 2/2] (#1935) Add User Agent test to ensure NuGet version is used This both ensures that the user agent is set, when NuGet sources are configured and that it uses the upstream version of NuGet that the NuGet assembles are based on. This same version is used in other areas as well, like minClientVersion, so it applies to multiple areas. --- .../scenarios/PackScenarios.cs | 10 +++++----- .../nuget/NugetCommonSpecs.cs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/chocolatey.tests.integration/scenarios/PackScenarios.cs b/src/chocolatey.tests.integration/scenarios/PackScenarios.cs index b22a84e688..196927d302 100644 --- a/src/chocolatey.tests.integration/scenarios/PackScenarios.cs +++ b/src/chocolatey.tests.integration/scenarios/PackScenarios.cs @@ -499,24 +499,24 @@ public void Should_throw_exception_on_icon_element() } } - public class when_packing_with_min_client_version : ScenariosBase + public class When_packing_with_min_client_version : ScenariosBase { protected override string ExpectedNuspecVersion => "0.1.0"; protected override string ExpectedSubDirectory => "PackageOutput"; // This high version is to ensure that pack does not throw, even if the min client version is well // above both the Chocolatey and NuGet assembly versions. - private string MinClientVersion = "100.0.0"; + private string _minClientVersion = "100.0.0"; public override void Because() { - MockLogger.reset(); - Service.pack_run(Configuration); + MockLogger.Reset(); + Service.Pack(Configuration); } protected override string GetNuspecContent() { - return NuspecContentWithFormatableMinClientVersion.format_with(MinClientVersion); + return NuspecContentWithFormatableMinClientVersion.FormatWith(_minClientVersion); } } diff --git a/src/chocolatey.tests/infrastructure.app/nuget/NugetCommonSpecs.cs b/src/chocolatey.tests/infrastructure.app/nuget/NugetCommonSpecs.cs index 35a54fa122..e1cc4baa8c 100644 --- a/src/chocolatey.tests/infrastructure.app/nuget/NugetCommonSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/nuget/NugetCommonSpecs.cs @@ -19,6 +19,7 @@ namespace chocolatey.tests.infrastructure.app.nuget using System; using System.Collections.Generic; using System.Linq; + using chocolatey.infrastructure.app; using chocolatey.infrastructure.app.configuration; using chocolatey.infrastructure.app.nuget; using chocolatey.infrastructure.filesystem; @@ -161,6 +162,22 @@ public void Should_parse_unc_source() packageRepositories.First().PackageSource.IsLocal.ShouldBeTrue(); packageRepositories.First().PackageSource.SourceUri.IsUnc.ShouldBeTrue(); } + + [Fact] + public void Should_set_user_agent_string() + { + Context(); + var source = "https://community.chocolatey.org/api/v2/"; + configuration.Sources = source; + configuration.Information.ChocolateyProductVersion = "vNext"; + + because(); + + // Change this when the NuGet version is updated. + string nugetClientVersion = "6.4.1"; + string expectedUserAgentString = "{0}/{1} via NuGet Client/{2}".FormatWith(ApplicationParameters.UserAgent, configuration.Information.ChocolateyProductVersion, nugetClientVersion); + UserAgent.UserAgentString.ShouldStartWith(expectedUserAgentString); + } } } }