private LocalV3FindPackageByIdResourceTest(
     LocalV3FindPackageByIdResource resource,
     FileInfo package,
     PackageIdentity packageIdentity,
     TestDirectory testDirectory)
 {
     Resource           = resource;
     Package            = package;
     PackageIdentity    = packageIdentity;
     _testDirectory     = testDirectory;
     SourceCacheContext = new SourceCacheContext();
 }
            internal static LocalV3FindPackageByIdResourceTest Create(bool createFiles = false)
            {
                var packageIdentity  = new PackageIdentity(id: "a", version: NuGetVersion.Parse("1.0.0"));
                var testDirectory    = TestDirectory.Create();
                var packageDirectory = Directory.CreateDirectory(
                    Path.Combine(
                        testDirectory.Path,
                        packageIdentity.Id,
                        packageIdentity.Version.ToNormalizedString()));
                var      packageSource = new PackageSource(testDirectory.Path);
                FileInfo package       = null;

                // Only a few tests have outcomes that rely on the presence of these files,
                // so only create them if necessary.
                if (createFiles)
                {
                    package = SimpleTestPackageUtility.CreateFullPackage(
                        packageDirectory.FullName,
                        packageIdentity.Id,
                        packageIdentity.Version.ToNormalizedString());

                    File.WriteAllText(
                        Path.Combine(packageDirectory.FullName, $"{packageIdentity.Id}.nuspec"),
                        $@"<?xml version=""1.0"" encoding=""utf-8""?>
                        <package>
                            <metadata>
                                <id>{packageIdentity.Id}</id>
                                <version>{packageIdentity.Version.ToNormalizedString()}</version>
                                <title />
                                <frameworkAssemblies>
                                    <frameworkAssembly assemblyName=""System.Runtime"" />
                                </frameworkAssemblies>
                            </metadata>
                        </package>");

                    File.WriteAllText(
                        Path.Combine(
                            packageDirectory.FullName,
                            $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}.nupkg.sha512"),
                        string.Empty);
                }

                var resource = new LocalV3FindPackageByIdResource(packageSource);

                return(new LocalV3FindPackageByIdResourceTest(
                           resource,
                           package,
                           packageIdentity,
                           testDirectory));
            }
示例#3
0
        public async Task FindPackageByIdResource_V2V3Compare()
        {
            using (var rootV3 = TestFileSystemUtility.CreateRandomTestFolder())
                using (var rootV2 = TestFileSystemUtility.CreateRandomTestFolder())
                {
                    // Arrange
                    var testLogger = new TestLogger();

                    var a1 = new PackageIdentity("a", NuGetVersion.Parse("1.0.0-alpha.1.2"));
                    var a2 = new PackageIdentity("a", NuGetVersion.Parse("1.0.0+server.2"));
                    var b  = new PackageIdentity("b", NuGetVersion.Parse("1.0.0.0"));

                    SimpleTestPackageUtility.CreateFolderFeedV2(rootV2, a1, a2, b);
                    await SimpleTestPackageUtility.CreateFolderFeedV3(rootV3, a1, a2, b);

                    var resourceV2 = new LocalV2FindPackageByIdResource(new PackageSource(rootV2));
                    var resourceV3 = new LocalV3FindPackageByIdResource(new PackageSource(rootV3));

                    resourceV2.Logger       = testLogger;
                    resourceV3.Logger       = testLogger;
                    resourceV2.CacheContext = new SourceCacheContext();
                    resourceV3.CacheContext = new SourceCacheContext();

                    var bNonNorm = new PackageIdentity("B", NuGetVersion.Parse("1.0"));

                    // Act
                    var versionsV2 = new HashSet <NuGetVersion>(await resourceV2.GetAllVersionsAsync("A", CancellationToken.None));
                    var versionsV3 = new HashSet <NuGetVersion>(await resourceV3.GetAllVersionsAsync("A", CancellationToken.None));

                    var emptyV2 = (await resourceV2.GetAllVersionsAsync("c", CancellationToken.None))
                                  .ToList();

                    var emptyV3 = (await resourceV3.GetAllVersionsAsync("c", CancellationToken.None))
                                  .ToList();

                    var v2CanSeek = false;
                    var v3CanSeek = false;

                    using (var stream = await resourceV2.GetNupkgStreamAsync(bNonNorm.Id, bNonNorm.Version, CancellationToken.None))
                    {
                        v2CanSeek = stream.CanSeek;
                    }

                    using (var stream = await resourceV3.GetNupkgStreamAsync(bNonNorm.Id, bNonNorm.Version, CancellationToken.None))
                    {
                        v3CanSeek = stream.CanSeek;
                    }

                    var depV2 = await resourceV2.GetDependencyInfoAsync(bNonNorm.Id, bNonNorm.Version, CancellationToken.None);

                    var depV3 = await resourceV3.GetDependencyInfoAsync(bNonNorm.Id, bNonNorm.Version, CancellationToken.None);

                    var depEmptyV2 = await resourceV2.GetDependencyInfoAsync(bNonNorm.Id, NuGetVersion.Parse("2.9"), CancellationToken.None);

                    var depEmptyV3 = await resourceV3.GetDependencyInfoAsync(bNonNorm.Id, NuGetVersion.Parse("2.9"), CancellationToken.None);

                    // Assert
                    Assert.True(versionsV2.SetEquals(versionsV3));
                    Assert.Equal(0, emptyV2.Count);
                    Assert.Equal(0, emptyV3.Count);
                    Assert.True(v2CanSeek);
                    Assert.True(v3CanSeek);
                    Assert.Equal(0, depV2.DependencyGroups.Count);
                    Assert.Equal(0, depV3.DependencyGroups.Count);
                    Assert.Null(depEmptyV2);
                    Assert.Null(depEmptyV3);
                }
        }
示例#4
0
        public async Task FindPackageByIdResource_V2V3Compare()
        {
            using (var rootV3 = TestDirectory.Create())
                using (var rootV2 = TestDirectory.Create())
                {
                    // Arrange
                    var testLogger = new TestLogger();

                    var a1 = new PackageIdentity("a", NuGetVersion.Parse("1.0.0-alpha.1.2"));
                    var a2 = new PackageIdentity("a", NuGetVersion.Parse("1.0.0+server.2"));
                    var b  = new PackageIdentity("b", NuGetVersion.Parse("1.0.0.0"));

                    SimpleTestPackageUtility.CreateFolderFeedV2(rootV2, a1, a2, b);
                    await SimpleTestPackageUtility.CreateFolderFeedV3(rootV3, a1, a2, b);

                    var resourceV2 = new LocalV2FindPackageByIdResource(new PackageSource(rootV2));
                    var resourceV3 = new LocalV3FindPackageByIdResource(new PackageSource(rootV3));

                    using (var cacheContext = new SourceCacheContext())
                    {
                        var bNonNorm = new PackageIdentity("B", NuGetVersion.Parse("1.0"));

                        // Act
                        var versionsV2 = new HashSet <NuGetVersion>(await resourceV2.GetAllVersionsAsync(
                                                                        "A",
                                                                        cacheContext,
                                                                        testLogger,
                                                                        CancellationToken.None));

                        var versionsV3 = new HashSet <NuGetVersion>(await resourceV3.GetAllVersionsAsync(
                                                                        "A",
                                                                        cacheContext,
                                                                        testLogger,
                                                                        CancellationToken.None));

                        var emptyV2 = (await resourceV2.GetAllVersionsAsync(
                                           "c",
                                           cacheContext,
                                           testLogger,
                                           CancellationToken.None))
                                      .ToList();

                        var emptyV3 = (await resourceV3.GetAllVersionsAsync("c",
                                                                            cacheContext,
                                                                            testLogger,
                                                                            CancellationToken.None))
                                      .ToList();

                        var v2Stream = new MemoryStream();
                        await resourceV2.CopyNupkgToStreamAsync(
                            bNonNorm.Id,
                            bNonNorm.Version,
                            v2Stream,
                            cacheContext,
                            testLogger,
                            CancellationToken.None);

                        var v3Stream = new MemoryStream();
                        await resourceV3.CopyNupkgToStreamAsync(
                            bNonNorm.Id,
                            bNonNorm.Version,
                            v3Stream,
                            cacheContext,
                            testLogger,
                            CancellationToken.None);

                        var depV2 = await resourceV2.GetDependencyInfoAsync(
                            bNonNorm.Id,
                            bNonNorm.Version,
                            cacheContext,
                            testLogger,
                            CancellationToken.None);

                        var depV3 = await resourceV3.GetDependencyInfoAsync(
                            bNonNorm.Id,
                            bNonNorm.Version,
                            cacheContext,
                            testLogger,
                            CancellationToken.None);

                        var depEmptyV2 = await resourceV2.GetDependencyInfoAsync(
                            bNonNorm.Id,
                            NuGetVersion.Parse("2.9"),
                            cacheContext,
                            testLogger,
                            CancellationToken.None);

                        var depEmptyV3 = await resourceV3.GetDependencyInfoAsync(
                            bNonNorm.Id,
                            NuGetVersion.Parse("2.9"),
                            cacheContext,
                            testLogger,
                            CancellationToken.None);

                        // Assert
                        Assert.True(versionsV2.SetEquals(versionsV3));
                        Assert.Equal(0, emptyV2.Count);
                        Assert.Equal(0, emptyV3.Count);
                        Assert.True(v2Stream.Length > 0);
                        Assert.True(v3Stream.Length > 0);
                        Assert.Equal(0, depV2.DependencyGroups.Count);
                        Assert.Equal(0, depV3.DependencyGroups.Count);
                        Assert.Null(depEmptyV2);
                        Assert.Null(depEmptyV3);
                    }
                }
        }