From a2f3a3f80b9a1fc4c52717d723c877c3819d7f08 Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Wed, 5 Apr 2017 17:20:31 -0700 Subject: [PATCH] When SemVer 2.0.0 registration is disabled, put SemVer 2.0.0 packages in legacy registration (#154) --- .../Registration/RegistrationCollector.cs | 18 +++++++-- src/Ng/Jobs/LightningJob.cs | 4 +- tests/NgTests/RegistrationCollectorTests.cs | 39 ++++++++++++++++++- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/Catalog/Registration/RegistrationCollector.cs b/src/Catalog/Registration/RegistrationCollector.cs index 9d7a55051..49beaa7b7 100644 --- a/src/Catalog/Registration/RegistrationCollector.cs +++ b/src/Catalog/Registration/RegistrationCollector.cs @@ -21,6 +21,7 @@ public class RegistrationCollector : SortingGraphCollector private readonly StorageFactory _legacyStorageFactory; private readonly StorageFactory _semVer2StorageFactory; + private readonly ShouldIncludeRegistrationPackage _shouldIncludeSemVer2; public RegistrationCollector( Uri index, @@ -36,6 +37,7 @@ public RegistrationCollector( _legacyStorageFactory = legacyStorageFactory; _semVer2StorageFactory = semVer2StorageFactory; + _shouldIncludeSemVer2 = GetShouldIncludeRegistrationPackage(_semVer2StorageFactory); ContentBaseAddress = new Uri("http://tempuri.org"); } @@ -86,7 +88,7 @@ protected override async Task ProcessGraphs( await RegistrationMaker.Process( registrationKey: new RegistrationKey(sortedGraphs.Key), newItems: sortedGraphs.Value, - shouldInclude: IsNotSemVer2, + shouldInclude: _shouldIncludeSemVer2, storageFactory: _legacyStorageFactory, contentBaseAddress: ContentBaseAddress, partitionSize: PartitionSize, @@ -106,9 +108,19 @@ await RegistrationMaker.Process( } } - public static bool IsNotSemVer2(RegistrationEntryKey key, string resourceUri, IGraph graph) + public static ShouldIncludeRegistrationPackage GetShouldIncludeRegistrationPackage(StorageFactory semVer2StorageFactory) { - return !NuGetVersionUtility.IsGraphSemVer2(key.Version, resourceUri, graph); + // If SemVer 2.0.0 storage is disabled, put SemVer 2.0.0 registration in the legacy storage factory. In no + // case should a package be completely ignored. That is, if a package is SemVer 2.0.0 but SemVer 2.0.0 + // storage is not enabled, our only choice is to put SemVer 2.0.0 packages in the legacy storage. + if (semVer2StorageFactory == null) + { + return (k, u, g) => true; + } + else + { + return (k, u, g) => !NuGetVersionUtility.IsGraphSemVer2(k.Version, u, g); + } } } } diff --git a/src/Ng/Jobs/LightningJob.cs b/src/Ng/Jobs/LightningJob.cs index 2c640a208..3edad7ffe 100644 --- a/src/Ng/Jobs/LightningJob.cs +++ b/src/Ng/Jobs/LightningJob.cs @@ -124,6 +124,7 @@ public override string GetUsage() private TextWriter _log; private string _contentBaseAddress; private RegistrationStorageFactories _storageFactories; + private ShouldIncludeRegistrationPackage _shouldIncludeSemVer2; private IDictionary _arguments; #endregion @@ -157,6 +158,7 @@ protected override void Init(IDictionary arguments, Cancellation _log = _verbose ? Console.Out : new StringWriter(); _contentBaseAddress = arguments.GetOrThrow(Arguments.ContentBaseAddress); _storageFactories = CommandHelpers.CreateRegistrationStorageFactories(arguments, _verbose); + _shouldIncludeSemVer2 = RegistrationCollector.GetShouldIncludeRegistrationPackage(_storageFactories.SemVer2StorageFactory); // We save the arguments because the "prepare" command generates "strike" commands. Some of the arguments // used by "prepare" should be used when executing "strike". @@ -448,7 +450,7 @@ private async Task ProcessGraphsAsync(string packageId, IDictionary pair.Key.PathAndQuery.EndsWith("cursor.json")); + Assert.NotNull(legacyCursor.Key); + var legacyIndex = _legacyStorage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/testpackage.semver2/index.json")); + Assert.Null(legacyIndex.Key); + var legacyLeaf = _legacyStorage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/testpackage.semver2/1.0.0-alpha.1.json")); + Assert.Null(legacyLeaf.Key); Assert.Equal(1, _legacyStorage.Content.Count); + var semVer2Cursor = _semVer2Storage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("cursor.json")); + Assert.Null(semVer2Cursor.Key); + var semVer2Index = _semVer2Storage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/testpackage.semver2/index.json")); + Assert.NotNull(semVer2Index.Key); + var semVer2Leaf = _semVer2Storage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/testpackage.semver2/1.0.0-alpha.1.json")); + Assert.NotNull(semVer2Leaf.Key); + Assert.Equal(2, _semVer2Storage.Content.Count); + } + + [Fact] + public async Task PutsSemVer2PackagesInLegacyStorageWhenSemVer2IsDisabled() + { + // Arrange + SharedInit(useLegacy: true, useSemVer2: false); + + var catalogStorage = Catalogs.CreateTestCatalogWithSemVer2Package(); + await _mockServer.AddStorage(catalogStorage); + + var front = new DurableCursor(_legacyStorage.ResolveUri("cursor.json"), _legacyStorage, MemoryCursor.MinValue); + var back = MemoryCursor.CreateMax(); + + // Act + await _target.Run(front, back, CancellationToken.None); + + // Assert var legacyCursor = _legacyStorage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("cursor.json")); Assert.NotNull(legacyCursor.Key); + var legacyIndex = _legacyStorage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/testpackage.semver2/index.json")); + Assert.NotNull(legacyIndex.Key); + var legacyLeaf = _legacyStorage.Content.FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/testpackage.semver2/1.0.0-alpha.1.json")); + Assert.NotNull(legacyLeaf.Key); + Assert.Equal(3, _legacyStorage.Content.Count); } } } \ No newline at end of file