Skip to content

Commit

Permalink
(chocolatey#3292) Ensure pre-release flag is used for parent lookup
Browse files Browse the repository at this point in the history
These changes makes sure that we pass along any pre-release flag
when there is a pre-release package that is a parent of one of the
packages that we are installing, and to verify that the results is not
null before we attempt to use it.

This ensures that pre-release only packages can be found when we
look up the parent, and if by some reason the package can not be
found we do not throw an undesired null exception in this case.
  • Loading branch information
AdmiringWorm authored and gep13 committed Aug 4, 2023
1 parent 680c1eb commit 3d10959
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,21 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
}
}

var configPrerelease = config.Prerelease;
config.Prerelease = parentPackage.Version.IsPrerelease;

var availablePackageDependency = NugetList.FindPackage(parentPackage.Id, config, _nugetLogger, sourceCacheContext, remoteEndpoints);
NugetCommon.GetPackageDependencies(availablePackageDependency.Identity, NuGetFramework.AnyFramework, sourceCacheContext, _nugetLogger, remoteEndpoints, sourcePackageDependencyInfos, sourceDependencyCache, config).GetAwaiter().GetResult();

if (availablePackageDependency != null)
{
NugetCommon.GetPackageDependencies(availablePackageDependency.Identity, NuGetFramework.AnyFramework, sourceCacheContext, _nugetLogger, remoteEndpoints, sourcePackageDependencyInfos, sourceDependencyCache, config).GetAwaiter().GetResult();
}
else
{
this.Log().Warn("Unable to find the parent package '{0}'.", parentPackage.Id);
}

config.Prerelease = configPrerelease;
}

var removedSources = RemovePinnedSourceDependencies(sourcePackageDependencyInfos, allLocalPackages);
Expand Down
27 changes: 27 additions & 0 deletions tests/chocolatey-tests/commands/choco-upgrade.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,33 @@ To upgrade a local, or remote file, you may use:
}
}

Context 'Upgrading a package where parent only contain pre-releases' -Tag Testing {
BeforeAll {
Restore-ChocolateyInstallSnapshot

$null = Invoke-Choco install isdependency --version 1.0.0
$null = Invoke-Choco install hasstabledependency

$Output = Invoke-Choco upgrade isdependency --version 2.0.0
}

It "Should exit with sucess (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Should report successful upgrade" {
$Output.Lines | Should -Contain "isdependency v2.0.0" -Because $Output.String
$Output.Lines | Should -Contain "Chocolatey upgraded 1/1 packages." -Because $Output.String
}

It "Should have upgraded the correct files" {
$ExpectedFile = "${env:ChocolateyInstall}/lib/isdependency/isdependency"
"$ExpectedFile.nupkg" | Should -Exist
$NuspecContent = [xml](Get-Content "$ExpectedFile.nuspec")
$NuspecContent.package.metadata.version | Should -Be "2.0.0"
}
}

# This needs to be (almost) the last test in this block, to ensure NuGet configurations aren't being created.
# Any tests after this block are expected to generate the configuration as they're explicitly using the NuGet CLI
Test-NuGetPaths
Expand Down

0 comments on commit 3d10959

Please sign in to comment.