Skip to content

Commit

Permalink
Merge & repatch from choco / Joel Francis:
Browse files Browse the repository at this point in the history
SHA-1: 74d7d39

* (chocolatey#2304) Fix list --exact -a for prereleases

Due to a prior fix in the logic for `list -e -a`, all packages with the
target ID were being returned, regardless of whether the user uses the
`--pre` flag or not. Since the same search without `--exact` does not
return prerelease versions unless the `--pre` option is also passed, the
behaviour for `list -e -a` should match this.

This fix modifies the search logic used when listing all package
versions with `--exact` to also check for and respect the `--pre` flag
from the configuration context, much like other list commands use the
setting in this file in other places.

Also includes a minor refactor to avoid doing a double query here; now
we only call the Search() function if we're actually going to use those
results. The code paths for AllVersions completely discard that already,
so this just removes the need to call the method unnecessarily.
  • Loading branch information
tapika committed Jan 9, 2022
1 parent 5b30410 commit 45a0f16
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>exactpackage</id>
<version>0.9.0</version>
<title>exactpackage</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<releaseNotes />
<tags>exactpackage admin</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
when running choco list exactpackage -e --all, this package should be in the resulting list.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>exactpackage</id>
<version>1.0.0-beta1</version>
<title>exactpackage</title>
<authors>__REPLACE_AUTHORS_OF_SOFTWARE__</authors>
<owners>__REPLACE_YOUR_NAME__</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>__REPLACE__</description>
<summary>__REPLACE__</summary>
<releaseNotes />
<tags>exactpackage admin</tags>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When running choco list exactpackage -a --all --pre, this package should be in the returned results.
99 changes: 99 additions & 0 deletions src/chocolatey.tests.integration/scenarios/ListScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,5 +553,104 @@ public void should_contain_debugging_messages()
MockLogger.contains_message("End of List", LogLevel.Debug).Should().BeTrue();
}
}

[Concern(typeof(ChocolateyListCommand))]
public class when_searching_for_all_packages_with_exact_id : ScenariosBase
{
public override void Context()
{
Configuration = Scenario.list();
Scenario.reset(Configuration);
Scenario.add_packages_to_source_location(Configuration, "exactpackage*" + Constants.PackageExtension);
Service = NUnitSetup.Container.GetInstance<IChocolateyPackageService>();

Configuration.ListCommand.Exact = true;
Configuration.AllVersions = true;
Configuration.Input = Configuration.PackageNames = "exactpackage";
}

public override void Because()
{
MockLogger.reset();
Results = Service.list_run(Configuration).ToList();
}

[Fact]
public void should_not_error()
{
// nothing necessary here
}

[Fact]
public void should_find_two_results()
{
Results.Count.Should().Be(2);
}

[Fact]
public void should_find_only_packages_with_exact_id()
{
Results[0].Package.Id.Should().Be("exactpackage");
Results[1].Package.Id.Should().Be("exactpackage");
}

[Fact]
public void should_find_all_non_prerelease_versions_in_descending_order()
{
Results[0].Package.Version.ToNormalizedString().Should().Be("1.0.0");
Results[1].Package.Version.ToNormalizedString().Should().Be("0.9.0");
}
}

[Concern(typeof(ChocolateyListCommand))]
public class when_searching_for_all_packages_including_prerelease_with_exact_id : ScenariosBase
{
public override void Context()
{
Configuration = Scenario.list();
Scenario.reset(Configuration);
Scenario.add_packages_to_source_location(Configuration, "exactpackage*" + Constants.PackageExtension);
Service = NUnitSetup.Container.GetInstance<IChocolateyPackageService>();

Configuration.ListCommand.Exact = true;
Configuration.AllVersions = true;
Configuration.Prerelease = true;
Configuration.Input = Configuration.PackageNames = "exactpackage";
}

public override void Because()
{
MockLogger.reset();
Results = Service.list_run(Configuration).ToList();
}

[Fact]
public void should_not_error()
{
// nothing necessary here
}

[Fact]
public void should_find_three_results()
{
Results.Count.Should().Be(3);
}

[Fact]
public void should_find_only_packages_with_exact_id()
{
Results[0].Package.Id.Should().Be("exactpackage");
Results[1].Package.Id.Should().Be("exactpackage");
Results[2].Package.Id.Should().Be("exactpackage");
}

[Fact]
public void should_find_all_versions_in_descending_order()
{
Results[0].Package.Version.ToNormalizedString().Should().Be("1.0.0");
Results[1].Package.Version.ToNormalizedString().Should().Be("1.0.0-beta1");
Results[2].Package.Version.ToNormalizedString().Should().Be("0.9.0");
}
}
}
}
14 changes: 10 additions & 4 deletions src/chocolatey/infrastructure.app/nuget/NugetList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,23 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
isServiceBased = packageRepository is IServiceBasedRepository;
}

IQueryable<IPackage> results = packageRepository.Search(searchTermLower, configuration.Prerelease);

SemanticVersion version = !string.IsNullOrWhiteSpace(configuration.Version) ? new SemanticVersion(configuration.Version) : null;
IQueryable<IPackage> results;

if (configuration.ListCommand.Exact)
if (!configuration.ListCommand.Exact)
{
results = packageRepository.Search(searchTermLower, configuration.Prerelease);
}
else
{
if (configuration.AllVersions)
{
// convert from a search to getting packages by id.
// search based on lower case id - similar to PackageRepositoryExtensions.FindPackagesByIdCore()
results = packageRepository.GetPackages().Where(x => x.Id.ToLower() == searchTermLower);
results = packageRepository.GetPackages().Where(p => p.Id.ToLower() == searchTermLower)
.AsEnumerable()
.Where(p => configuration.Prerelease || p.IsReleaseVersion())
.AsQueryable();
}
else
{
Expand Down

0 comments on commit 45a0f16

Please sign in to comment.