Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Commit

Permalink
When SemVer 2.0.0 registration is disabled, put SemVer 2.0.0 packages…
Browse files Browse the repository at this point in the history
… in legacy registration (#154)
  • Loading branch information
joelverhagen committed Apr 6, 2017
1 parent 713bfde commit a2f3a3f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
18 changes: 15 additions & 3 deletions src/Catalog/Registration/RegistrationCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class RegistrationCollector : SortingGraphCollector

private readonly StorageFactory _legacyStorageFactory;
private readonly StorageFactory _semVer2StorageFactory;
private readonly ShouldIncludeRegistrationPackage _shouldIncludeSemVer2;

public RegistrationCollector(
Uri index,
Expand All @@ -36,6 +37,7 @@ public RegistrationCollector(

_legacyStorageFactory = legacyStorageFactory;
_semVer2StorageFactory = semVer2StorageFactory;
_shouldIncludeSemVer2 = GetShouldIncludeRegistrationPackage(_semVer2StorageFactory);

ContentBaseAddress = new Uri("http://tempuri.org");
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}
}
}
4 changes: 3 additions & 1 deletion src/Ng/Jobs/LightningJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public override string GetUsage()
private TextWriter _log;
private string _contentBaseAddress;
private RegistrationStorageFactories _storageFactories;
private ShouldIncludeRegistrationPackage _shouldIncludeSemVer2;
private IDictionary<string, string> _arguments;
#endregion

Expand Down Expand Up @@ -157,6 +158,7 @@ protected override void Init(IDictionary<string, string> arguments, Cancellation
_log = _verbose ? Console.Out : new StringWriter();
_contentBaseAddress = arguments.GetOrThrow<string>(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".
Expand Down Expand Up @@ -448,7 +450,7 @@ private async Task ProcessGraphsAsync(string packageId, IDictionary<string, IGra
await RegistrationMaker.Process(
new RegistrationKey(packageId),
sortedGraphs,
RegistrationCollector.IsNotSemVer2,
_shouldIncludeSemVer2,
_storageFactories.LegacyStorageFactory,
new Uri(_contentBaseAddress),
RegistrationCollector.PartitionSize,
Expand Down
39 changes: 37 additions & 2 deletions tests/NgTests/RegistrationCollectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public async Task CreatesRegistrationsWithSemVer2()
}

[Fact]
public async Task IgnoresSemVer2Packages()
public async Task IgnoresSemVer2PackagesInLegacyStorageWhenSemVer2IsEnabled()
{
// Arrange
SharedInit(useLegacy: true, useSemVer2: true);
Expand All @@ -306,11 +306,46 @@ public async Task IgnoresSemVer2Packages()
await _target.Run(front, back, CancellationToken.None);

// Assert
// Verify the contents of the SemVer 1.0.0 storage
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.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);
}
}
}

0 comments on commit a2f3a3f

Please sign in to comment.