Example #1
0
        public async Task GetDownloadResourceResultAsync_MultipleSources_PackageDownloadedWhenFoundInMultipleSources()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(new[]
            {
                TestSourceRepositoryUtility.V3PackageSource,
                TestSourceRepositoryUtility.V3PackageSource,
                new PackageSource("http://blah.com"),
                TestSourceRepositoryUtility.V2PackageSource,
                TestSourceRepositoryUtility.V2PackageSource,
            });

            var packageIdentity = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            // Act
            using (var cacheContext = new SourceCacheContext())
                using (var packagesDirectory = TestDirectory.Create())
                    using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                               sourceRepositoryProvider.GetRepositories(),
                               packageIdentity,
                               new PackageDownloadContext(cacheContext),
                               packagesDirectory,
                               NullLogger.Instance,
                               CancellationToken.None))
                    {
                        var targetPackageStream = downloadResult.PackageStream;

                        // Assert
                        Assert.True(targetPackageStream.CanSeek);
                    }
        }
        public async Task GetDownloadResourceResultAsync_V3_DownloadsPackage()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
            var v3sourceRepository       = sourceRepositoryProvider.GetRepositories().First();
            var packageIdentity          = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            // Act
            using (var cacheContext = new SourceCacheContext())
                using (var packagesDirectory = TestDirectory.Create())
                    using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                               v3sourceRepository,
                               packageIdentity,
                               new PackageDownloadContext(cacheContext),
                               packagesDirectory,
                               NullLogger.Instance,
                               CancellationToken.None))
                    {
                        var targetPackageStream = downloadResult.PackageStream;

                        // Assert
                        // jQuery.1.8.2 is of size 185476 bytes. Make sure the download is successful
                        Assert.Equal(185476, targetPackageStream.Length);
                        Assert.True(targetPackageStream.CanSeek);
                    }
        }
        public async Task GetDownloadResourceResultAsync_Source_ThrowsIfCancelled()
        {
            using (var test = new PackageDownloaderTest())
            {
                var resourceProvider = new Mock <INuGetResourceProvider>();

                resourceProvider.SetupGet(x => x.Name)
                .Returns(nameof(DownloadResource) + "Provider");
                resourceProvider.SetupGet(x => x.ResourceType)
                .Returns(typeof(DownloadResource));

                resourceProvider.Setup(x => x.TryCreate(
                                           It.IsNotNull <SourceRepository>(),
                                           It.IsAny <CancellationToken>()))
                .ReturnsAsync(new Tuple <bool, INuGetResource>(true, Mock.Of <DownloadResource>()));

                var sourceRepository = new SourceRepository(
                    test.SourceRepository.PackageSource,
                    new[] { resourceProvider.Object });

                await Assert.ThrowsAsync <OperationCanceledException>(
                    () => PackageDownloader.GetDownloadResourceResultAsync(
                        sourceRepository,
                        test.PackageIdentity,
                        test.Context,
                        globalPackagesFolder: "",
                        logger: NullLogger.Instance,
                        token: new CancellationToken(canceled: true)));
            }
        }
        public async Task GetDownloadResourceResultAsync_V3_ThrowIfPackageDoesNotExist()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
            var v3sourceRepository       = sourceRepositoryProvider.GetRepositories().First();
            var packageId       = Guid.NewGuid().ToString();
            var packageIdentity = new PackageIdentity(packageId, new NuGetVersion("1.0.0"));

            // Act
            Exception exception = null;

            try
            {
                using (var cacheContext = new SourceCacheContext())
                    using (var packagesDirectory = TestDirectory.Create())
                    {
                        await PackageDownloader.GetDownloadResourceResultAsync(
                            v3sourceRepository,
                            packageIdentity,
                            new PackageDownloadContext(cacheContext),
                            packagesDirectory,
                            NullLogger.Instance,
                            CancellationToken.None);
                    }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            // Assert
            Assert.NotNull(exception);
        }
        public async Task GetDownloadResourceResultAsync_MultipleSources_IncludesTaskStatusInException()
        {
            using (var test = new PackageDownloaderTest())
            {
                var resourceProvider = new Mock <INuGetResourceProvider>();
                var resource         = new Mock <DownloadResource>();

                resourceProvider.SetupGet(x => x.Name)
                .Returns(nameof(DownloadResource) + "Provider");
                resourceProvider.SetupGet(x => x.ResourceType)
                .Returns(typeof(DownloadResource));
                resourceProvider.Setup(x => x.TryCreate(
                                           It.IsNotNull <SourceRepository>(),
                                           It.IsAny <CancellationToken>()))
                .Throws(new OperationCanceledException());

                var sourceRepositories = new[]
                {
                    new SourceRepository(test.SourceRepository.PackageSource, new[] { resourceProvider.Object })
                };

                var exception = await Assert.ThrowsAsync <FatalProtocolException>(
                    () => PackageDownloader.GetDownloadResourceResultAsync(
                        sourceRepositories,
                        test.PackageIdentity,
                        test.Context,
                        globalPackagesFolder: "",
                        logger: NullLogger.Instance,
                        token: CancellationToken.None));

                Assert.Contains(": Canceled", exception.Message);
            }
        }
Example #6
0
        public async Task GetDownloadResourceResultAsync_V3_DownloadsPackage()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider();
            var v3sourceRepository       = sourceRepositoryProvider.GetRepositories().First();
            var packageIdentity          = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            // Act
            using (var cacheContext = new SourceCacheContext())
                using (var packagesDirectory = TestDirectory.Create())
                    using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                               v3sourceRepository,
                               packageIdentity,
                               new PackageDownloadContext(cacheContext),
                               packagesDirectory,
                               NullLogger.Instance,
                               CancellationToken.None))
                    {
                        var targetPackageStream = downloadResult.PackageStream;

                        using (var packageArchiveReader = new PackageArchiveReader(targetPackageStream))
                        {
                            var contentHash = packageArchiveReader.GetContentHash(CancellationToken.None);

                            // Assert
                            Assert.Equal(_jQuery182ContentHash, contentHash);
                            Assert.True(targetPackageStream.CanSeek);
                        }
                    }
        }
 public async Task GetDownloadResourceResultAsync_Source_ThrowsIfNoDownloadResource()
 {
     using (var test = new PackageDownloaderTest())
     {
         await Assert.ThrowsAsync <InvalidOperationException>(
             () => PackageDownloader.GetDownloadResourceResultAsync(
                 test.SourceRepository,
                 test.PackageIdentity,
                 test.Context,
                 globalPackagesFolder: "",
                 logger: NullLogger.Instance,
                 token: CancellationToken.None));
     }
 }
 public async Task GetDownloadResourceResultAsync_Sources_ThrowsIfCancelled()
 {
     using (var test = new PackageDownloaderTest())
     {
         await Assert.ThrowsAsync <OperationCanceledException>(
             () => PackageDownloader.GetDownloadResourceResultAsync(
                 Enumerable.Empty <SourceRepository>(),
                 test.PackageIdentity,
                 test.Context,
                 globalPackagesFolder: "",
                 logger: NullLogger.Instance,
                 token: new CancellationToken(canceled: true)));
     }
 }
        public async Task GetDownloadResourceResultAsync_Sources_ThrowsForNullLogger()
        {
            using (var test = new PackageDownloaderTest())
            {
                var exception = await Assert.ThrowsAsync <ArgumentNullException>(
                    () => PackageDownloader.GetDownloadResourceResultAsync(
                        Enumerable.Empty <SourceRepository>(),
                        test.PackageIdentity,
                        test.Context,
                        globalPackagesFolder: "",
                        logger: null,
                        token: CancellationToken.None));

                Assert.Equal("logger", exception.ParamName);
            }
        }
        public async Task GetDownloadResourceResultAsync_Sources_ThrowsForNullSources()
        {
            using (var test = new PackageDownloaderTest())
            {
                var exception = await Assert.ThrowsAsync <ArgumentNullException>(
                    () => PackageDownloader.GetDownloadResourceResultAsync(
                        sources: null,
                        packageIdentity: test.PackageIdentity,
                        downloadContext: test.Context,
                        globalPackagesFolder: "",
                        logger: NullLogger.Instance,
                        token: CancellationToken.None));

                Assert.Equal("sources", exception.ParamName);
            }
        }
Example #11
0
        private static async Task VerifyDirectDownloadSkipsGlobalPackagesFolderAsync(
            SourceRepositoryProvider sourceRepositoryProvider)
        {
            // Arrange
            var sourceRepository = sourceRepositoryProvider.GetRepositories().First();
            var packageIdentity  = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            using (var packagesDirectory = TestDirectory.Create())
                using (var directDownloadDirectory = TestDirectory.Create())
                    using (var cacheContext = new SourceCacheContext())
                    {
                        var downloadContext = new PackageDownloadContext(
                            cacheContext,
                            directDownloadDirectory,
                            directDownload: true);

                        // Act
                        using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                                   sourceRepository,
                                   packageIdentity,
                                   downloadContext,
                                   packagesDirectory,
                                   NullLogger.Instance,
                                   CancellationToken.None))
                        {
                            var targetPackageStream = downloadResult.PackageStream;

                            using (var packageArchiveReader = new PackageArchiveReader(targetPackageStream))
                            {
                                var contentHash = packageArchiveReader.GetContentHash(CancellationToken.None);

                                // Assert
                                Assert.Equal(_jQuery182ContentHash, contentHash);
                                Assert.True(targetPackageStream.CanSeek);
                            }
                        }

                        // Verify that the direct download directory is empty. The package should be downloaded to a temporary
                        // file opened with DeleteOnClose.
                        Assert.Equal(0, Directory.EnumerateFileSystemEntries(directDownloadDirectory).Count());

                        // Verify that the package was not cached in the Global Packages Folder
                        var globalPackage = GlobalPackagesFolderUtility.GetPackage(packageIdentity, packagesDirectory);
                        Assert.Null(globalPackage);
                    }
        }
Example #12
0
        public async Task TestDownloadPackage_MultipleSources_NotFound()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(new[]
            {
                new Configuration.PackageSource("https://www.myget.org/F/aspnetvnext/api/v2/"),
                new Configuration.PackageSource("http://blah.com"),
            });

            var packageIdentity = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            await Assert.ThrowsAsync <FatalProtocolException>(async() => await PackageDownloader.GetDownloadResourceResultAsync(sourceRepositoryProvider.GetRepositories(),
                                                                                                                                packageIdentity,
                                                                                                                                Configuration.NullSettings.Instance,
                                                                                                                                Common.NullLogger.Instance,
                                                                                                                                CancellationToken.None));
        }
        public async Task GetDownloadResourceResultAsync_SupportsDownloadResultWithoutPackageStream()
        {
            using (var test = new PackageDownloaderTest())
                using (var stream = new MemoryStream())
                    using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create))
                        using (var packageReader = new PackageArchiveReader(zipArchive))
                        {
                            var resourceProvider = new Mock <INuGetResourceProvider>();
                            var resource         = new Mock <DownloadResource>();
                            var expectedResult   = new DownloadResourceResult(
                                packageReader,
                                test.SourceRepository.PackageSource.Source);

                            resource.Setup(x => x.GetDownloadResourceResultAsync(
                                               It.IsNotNull <PackageIdentity>(),
                                               It.IsNotNull <PackageDownloadContext>(),
                                               It.IsAny <string>(),
                                               It.IsNotNull <ILogger>(),
                                               It.IsAny <CancellationToken>()))
                            .ReturnsAsync(expectedResult);

                            resourceProvider.SetupGet(x => x.Name)
                            .Returns(nameof(DownloadResource) + "Provider");
                            resourceProvider.SetupGet(x => x.ResourceType)
                            .Returns(typeof(DownloadResource));
                            resourceProvider.Setup(x => x.TryCreate(
                                                       It.IsNotNull <SourceRepository>(),
                                                       It.IsAny <CancellationToken>()))
                            .ReturnsAsync(new Tuple <bool, INuGetResource>(true, resource.Object));

                            var sourceRepository = new SourceRepository(
                                test.SourceRepository.PackageSource,
                                new[] { resourceProvider.Object });

                            var actualResult = await PackageDownloader.GetDownloadResourceResultAsync(
                                sourceRepository,
                                test.PackageIdentity,
                                test.Context,
                                globalPackagesFolder : "",
                                logger : NullLogger.Instance,
                                token : CancellationToken.None);

                            Assert.Equal(DownloadResourceResultStatus.AvailableWithoutStream, actualResult.Status);
                            Assert.Same(expectedResult, actualResult);
                        }
        }
        public async Task GetDownloadResourceResultAsync_MultipleSources_PackageDownloadedWhenFoundInMultipleSources()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(new[]
            {
                TestSourceRepositoryUtility.V3PackageSource,
                TestSourceRepositoryUtility.V3PackageSource,
                new PackageSource("http://unit.test"),
                TestSourceRepositoryUtility.V2PackageSource,
                TestSourceRepositoryUtility.V2PackageSource,
            });

            var packageIdentity = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            // Act
            using (var cacheContext = new SourceCacheContext())
            {
                var downloadContext = new PackageDownloadContext(cacheContext)
                {
                    ExtractionContext = new PackageExtractionContext(
                        PackageSaveMode.Defaultv3,
                        PackageExtractionBehavior.XmlDocFileSaveMode,
                        NullLogger.Instance,
                        signedPackageVerifier: null,
                        signedPackageVerifierSettings: null)
                };

                using (var packagesDirectory = TestDirectory.Create())
                    using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                               sourceRepositoryProvider.GetRepositories(),
                               packageIdentity,
                               downloadContext,
                               packagesDirectory,
                               NullLogger.Instance,
                               CancellationToken.None))
                    {
                        var targetPackageStream = downloadResult.PackageStream;

                        // Assert
                        Assert.True(targetPackageStream.CanSeek);
                    }
            }
        }
        public async Task GetDownloadResourceResultAsync_MultipleSources_DownloadsPackage()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(new[]
            {
                new PackageSource("https://www.myget.org/F/aspnetvnext/api/v2/"),
                TestSourceRepositoryUtility.V3PackageSource,
                new PackageSource("http://unit.test"),
            });

            var packageIdentity = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            // Act
            using (var cacheContext = new SourceCacheContext())
            {
                var downloadContext = new PackageDownloadContext(cacheContext)
                {
                    ExtractionContext = new PackageExtractionContext(
                        PackageSaveMode.Defaultv3,
                        PackageExtractionBehavior.XmlDocFileSaveMode,
                        clientPolicyContext: null,
                        logger: NullLogger.Instance)
                };

                using (var packagesDirectory = TestDirectory.Create())
                    using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                               sourceRepositoryProvider.GetRepositories(),
                               packageIdentity,
                               downloadContext,
                               packagesDirectory,
                               NullLogger.Instance,
                               CancellationToken.None))
                    {
                        var targetPackageStream = downloadResult.PackageStream;

                        // Assert
                        Assert.True(targetPackageStream.CanSeek);
                    }
            }
        }
Example #16
0
        public async Task TestDownloadPackage_InV2()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV2OnlySourceRepositoryProvider();
            var v2sourceRepository       = sourceRepositoryProvider.GetRepositories().First();
            var packageIdentity          = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            // Act
            using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(v2sourceRepository,
                                                                                               packageIdentity,
                                                                                               Configuration.NullSettings.Instance,
                                                                                               Common.NullLogger.Instance,
                                                                                               CancellationToken.None))
            {
                var targetPackageStream = downloadResult.PackageStream;

                // Assert
                // jQuery.1.8.2 is of size 185476 bytes. Make sure the download is successful
                Assert.Equal(185476, targetPackageStream.Length);
                Assert.True(targetPackageStream.CanSeek);
            }
        }
        public async Task GetDownloadResourceResultAsync_V2_DownloadsPackage()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV2OnlySourceRepositoryProvider();
            var v2sourceRepository       = sourceRepositoryProvider.GetRepositories().First();
            var packageIdentity          = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            // Act
            using (var cacheContext = new SourceCacheContext())
            {
                var downloadContext = new PackageDownloadContext(cacheContext)
                {
                    ExtractionContext = new PackageExtractionContext(
                        PackageSaveMode.Defaultv3,
                        PackageExtractionBehavior.XmlDocFileSaveMode,
                        NullLogger.Instance,
                        signedPackageVerifier: null,
                        signedPackageVerifierSettings: null)
                };

                using (var packagesDirectory = TestDirectory.Create())
                    using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                               v2sourceRepository,
                               packageIdentity,
                               downloadContext,
                               packagesDirectory,
                               NullLogger.Instance,
                               CancellationToken.None))
                    {
                        var targetPackageStream = downloadResult.PackageStream;

                        // Assert
                        // jQuery.1.8.2 is of size 185476 bytes. Make sure the download is successful
                        Assert.Equal(185476, targetPackageStream.Length);
                        Assert.True(targetPackageStream.CanSeek);
                    }
            }
        }
        public async Task GetDownloadResourceResultAsync_MultipleSources_PackageNotFound()
        {
            // Arrange
            var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateSourceRepositoryProvider(new[]
            {
                new PackageSource("https://www.myget.org/F/aspnetvnext/api/v2/"),
                new PackageSource("http://unit.test"),
            });

            var packageIdentity = new PackageIdentity("jQuery", new NuGetVersion("1.8.2"));

            using (var packagesDirectory = TestDirectory.Create())
                using (var cacheContext = new SourceCacheContext())
                {
                    await Assert.ThrowsAsync <FatalProtocolException>(
                        async() => await PackageDownloader.GetDownloadResourceResultAsync(
                            sourceRepositoryProvider.GetRepositories(),
                            packageIdentity,
                            new PackageDownloadContext(cacheContext),
                            packagesDirectory,
                            NullLogger.Instance,
                            CancellationToken.None));
                }
        }
Example #19
0
        /// <summary>
        /// Download all needed packages for install actions.
        /// </summary>
        public static async Task <Dictionary <PackageIdentity, PackagePreFetcherResult> > GetPackagesAsync(
            IEnumerable <NuGetProjectAction> actions,
            FolderNuGetProject packagesFolder,
            Configuration.ISettings settings,
            Common.ILogger logger,
            CancellationToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (packagesFolder == null)
            {
                throw new ArgumentNullException(nameof(packagesFolder));
            }

            if (actions == null)
            {
                throw new ArgumentNullException(nameof(actions));
            }

            var result           = new Dictionary <PackageIdentity, PackagePreFetcherResult>();
            var maxParallelTasks = PackageManagementConstants.DefaultMaxDegreeOfParallelism;
            var toDownload       = new Queue <NuGetProjectAction>();
            var seen             = new HashSet <PackageIdentity>();

            // Find all uninstalled packages
            var uninstalledPackages = new HashSet <PackageIdentity>(
                actions.Where(action => action.NuGetProjectActionType == NuGetProjectActionType.Uninstall)
                .Select(action => action.PackageIdentity));

            // Check the packages folder for each package
            // If the package is not found mark it for download
            // These actions need to stay in order!
            foreach (var action in actions)
            {
                // Ignore uninstalls here
                // Avoid duplicate downloads
                if (action.NuGetProjectActionType == NuGetProjectActionType.Install &&
                    seen.Add(action.PackageIdentity))
                {
                    string installPath = null;

                    // Packages that are also being uninstalled cannot come from the
                    // packages folder since it will be gone. This is true for reinstalls.
                    if (!uninstalledPackages.Contains(action.PackageIdentity))
                    {
                        // Check the packages folder for the id and version
                        installPath = packagesFolder.GetInstalledPackageFilePath(action.PackageIdentity);

                        // Verify the nupkg exists
                        if (!File.Exists(installPath))
                        {
                            installPath = null;
                        }
                    }

                    // installPath will contain the full path of the already installed nupkg if it
                    // exists. If the path is empty it will need to be downloaded.
                    if (!string.IsNullOrEmpty(installPath))
                    {
                        // Create a download result using the already installed package
                        var downloadResult = new PackagePreFetcherResult(installPath, action.PackageIdentity);
                        result.Add(action.PackageIdentity, downloadResult);
                    }
                    else
                    {
                        // Download this package
                        toDownload.Enqueue(action);
                    }
                }
            }

            // Check if any packages are not already in the packages folder
            if (toDownload.Count > 0)
            {
                var downloadResults = new List <PackagePreFetcherResult>(maxParallelTasks);

                while (toDownload.Count > 0)
                {
                    // Throttle tasks
                    if (downloadResults.Count == maxParallelTasks)
                    {
                        // Wait for a task to complete
                        // This will not throw, exceptions are stored in the result
                        await Task.WhenAny(downloadResults.Select(e => e.EnsureResultAsync()));

                        // Remove all completed tasks
                        downloadResults.RemoveAll(e => e.IsComplete);
                    }

                    var action = toDownload.Dequeue();

                    // Download the package if it does not exist in the packages folder already
                    // Start the download task
                    var task = Task.Run(async() => await PackageDownloader.GetDownloadResourceResultAsync(
                                            action.SourceRepository,
                                            action.PackageIdentity,
                                            settings,
                                            logger,
                                            token));

                    var downloadResult = new PackagePreFetcherResult(
                        task,
                        action.PackageIdentity,
                        action.SourceRepository.PackageSource);

                    downloadResults.Add(downloadResult);
                    result.Add(action.PackageIdentity, downloadResult);
                }
            }

            // Do not wait for the remaining tasks to finish, these will download
            // in the background while other operations such as uninstall run first.
            return(result);
        }