diff --git a/src/NuGetGallery/Services/PackageService.cs b/src/NuGetGallery/Services/PackageService.cs index 6c412e802b..7cc99dd241 100644 --- a/src/NuGetGallery/Services/PackageService.cs +++ b/src/NuGetGallery/Services/PackageService.cs @@ -793,7 +793,7 @@ public async Task UpdateIsLatestAsync(PackageRegistration packageRegistration, b // TODO: improve setting the latest bit; this is horrible. Trigger maybe? var currentUtcTime = DateTime.UtcNow; - foreach (var pv in packageRegistration.Packages.Where(p => p.IsLatest || p.IsLatestStable)) + foreach (var pv in packageRegistration.Packages.Where(p => p.IsLatest || p.IsLatestStable || p.IsLatestSemVer2 || p.IsLatestStableSemVer2)) { pv.IsLatest = false; pv.IsLatestStable = false; diff --git a/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs b/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs index 547a839d78..dc535e30a5 100644 --- a/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs +++ b/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs @@ -1052,6 +1052,50 @@ public async Task CommitIfCommitChangesIsTrue() packageRepository.Verify(r => r.CommitChangesAsync(), Times.Once()); } + [Fact] + public async Task ResetsCurrentLatestPackageVersionsBeforeUpdate() + { + // Arrange + var packageRegistration = new PackageRegistration(); + + var previousLatestStable = new Package { PackageRegistration = packageRegistration, Version = "1.0.0", IsLatestStable = true }; + packageRegistration.Packages.Add(previousLatestStable); + var previousLatest = new Package { PackageRegistration = packageRegistration, Version = "1.0.0-alpha", IsLatest = true, IsPrerelease = true }; + packageRegistration.Packages.Add(previousLatest); + var previousLatestStableSemVer2 = new Package { PackageRegistration = packageRegistration, Version = "1.0.1+metadata", IsLatestStableSemVer2 = true, SemVerLevelKey = SemVerLevelKey.SemVer2 }; + packageRegistration.Packages.Add(previousLatestStableSemVer2); + var previousLatestSemVer2 = new Package { PackageRegistration = packageRegistration, Version = "1.0.1-alpha.1", IsLatestSemVer2 = true, IsPrerelease = true, SemVerLevelKey = SemVerLevelKey.SemVer2 }; + packageRegistration.Packages.Add(previousLatestSemVer2); + + // Simulates adding newer versions, to ensure the previous latest are no longer latest at end of test. + var newLatestStable = new Package { PackageRegistration = packageRegistration, Version = "1.0.1", IsLatestStable = true }; + packageRegistration.Packages.Add(newLatestStable); + var newLatest = new Package { PackageRegistration = packageRegistration, Version = "1.0.2-alpha", IsLatest = true, IsPrerelease = true }; + packageRegistration.Packages.Add(newLatest); + var newLatestStableSemVer2 = new Package { PackageRegistration = packageRegistration, Version = "1.0.2+metadata", IsLatestStableSemVer2 = true, SemVerLevelKey = SemVerLevelKey.SemVer2 }; + packageRegistration.Packages.Add(newLatestStableSemVer2); + var newLatestSemVer2 = new Package { PackageRegistration = packageRegistration, Version = "1.0.3-alpha.1", IsLatestSemVer2 = true, IsPrerelease = true, SemVerLevelKey = SemVerLevelKey.SemVer2 }; + packageRegistration.Packages.Add(newLatestSemVer2); + + var packageRepository = new Mock>(); + + var service = CreateService(packageRepository: packageRepository); + + // Act + await service.UpdateIsLatestAsync(packageRegistration, commitChanges: true); + + // Assert + Assert.False(previousLatestStable.IsLatestStable); + Assert.False(previousLatest.IsLatest); + Assert.False(previousLatestSemVer2.IsLatestSemVer2); + Assert.False(previousLatestStableSemVer2.IsLatestStableSemVer2); + + Assert.True(newLatestStable.IsLatestStable); + Assert.True(newLatest.IsLatest); + Assert.True(newLatestSemVer2.IsLatestSemVer2); + Assert.True(newLatestStableSemVer2.IsLatestStableSemVer2); + } + [Fact] public async Task UpdateIsLatestScenarioForPrereleaseAsAbsoluteLatest() {