Skip to content

Commit

Permalink
(chocolatey#2304) Fix list --exact -a for prereleases
Browse files Browse the repository at this point in the history
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
vexx32 committed Aug 10, 2021
1 parent af3e8c9 commit fcc7f84
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 7 deletions.
16 changes: 15 additions & 1 deletion Scenarios.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
* should not have inconclusive package result
* should not have warning package result

### ChocolateyListCommand [ 11 Scenario(s), 48 Observation(s) ]
### ChocolateyListCommand [ 13 Scenario(s), 56 Observation(s) ]

#### when listing local packages

Expand Down Expand Up @@ -448,6 +448,20 @@
* should contain packages and versions with a space between them
* should not contain packages that do not match

#### when searching for all packages including prerelease with exact id

* should find all versions in descending order
* should find only packages with exact id
* should find three results
* should not error

#### when searching for all packages with exact id

* should find all non prerelease versions in descending order
* should find only packages with exact id
* should find two results
* should not error

#### when searching for an exact package

* should contain a summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@
<None Include="context\exactpackage\exactpackage.dontfind\1.0.0\exactpackage.dontfind.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\exactpackage\exactpackage\0.9.0\exactpackage.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\exactpackage\exactpackage\1.0.0-beta1\exactpackage.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="context\exactpackage\exactpackage\1.0.0\exactpackage.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -479,6 +485,12 @@
<Content Include="context\exactpackage\exactpackage.dontfind\1.0.0\tools\purpose.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="context\exactpackage\exactpackage\0.9.0\tools\purpose.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="context\exactpackage\exactpackage\1.0.0-beta1\tools\purpose.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="context\exactpackage\exactpackage\1.0.0\tools\purpose.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
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.
103 changes: 101 additions & 2 deletions src/chocolatey.tests.integration/scenarios/ListScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ public void should_contain_debugging_messages()
MockLogger.contains_message("Start of List", LogLevel.Debug).ShouldBeTrue();
MockLogger.contains_message("End of List", LogLevel.Debug).ShouldBeTrue();
}
}
}

[Concern(typeof(ChocolateyListCommand))]
public class when_searching_for_an_exact_package_with_zero_results : ScenariosBase
{
Expand Down Expand Up @@ -553,5 +553,104 @@ public void should_contain_debugging_messages()
MockLogger.contains_message("End of List", LogLevel.Debug).ShouldBeTrue();
}
}

[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.ShouldEqual(2);
}

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

[Fact]
public void should_find_all_non_prerelease_versions_in_descending_order()
{
Results[0].Package.Version.ToNormalizedString().ShouldEqual("1.0.0");
Results[1].Package.Version.ToNormalizedString().ShouldEqual("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.ShouldEqual(3);
}

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

[Fact]
public void should_find_all_versions_in_descending_order()
{
Results[0].Package.Version.ToNormalizedString().ShouldEqual("1.0.0");
Results[1].Package.Version.ToNormalizedString().ShouldEqual("1.0.0-beta1");
Results[2].Package.Version.ToNormalizedString().ShouldEqual("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 fcc7f84

Please sign in to comment.