コード例 #1
0
        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())
                using (var packagesDirectory = TestDirectory.Create())
                    using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(
                               v2sourceRepository,
                               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);
                        }
                    }
        }
コード例 #2
0
 private string GetContentHashFromNupkg(string filePath, CancellationToken token)
 {
     using (var reader = new PackageArchiveReader(filePath))
     {
         var hash = reader.GetContentHash(token);
         return(hash);
     }
 }
コード例 #3
0
        public async Task RestorePackagesConfig_WithExistingLockFile_LockedMode_Succeeds()
        {
            // Arrange
            using (var pathContext = new SimpleTestPathContext())
            {
                // Set up solution, project, and packages
                var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

                var net461 = NuGetFramework.Parse("net461");

                var projectA = SimpleTestProjectContext.CreateLegacyPackageReference(
                    "a",
                    pathContext.SolutionRoot,
                    net461);

                var packageX = new SimpleTestPackageContext()
                {
                    Id      = "x",
                    Version = "1.0.0"
                };
                packageX.Files.Clear();
                packageX.AddFile("lib/net461/x.dll");

                solution.Projects.Add(projectA);
                solution.Create(pathContext.SolutionRoot);
                Util.CreateFile(Path.GetDirectoryName(projectA.ProjectPath), "packages.config",
                                @"<packages>
  <package id=""x"" version=""1.0.0"" targetFramework=""net461"" />
</packages>");

                await SimpleTestPackageUtility.CreateFolderFeedV3Async(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                // Preconditions, regular restore
                var result = RunRestore(pathContext, _successExitCode);
                result.Success.Should().BeTrue(because: result.AllOutput);
                new FileInfo(projectA.NuGetLockFileOutputPath).Exists.Should().BeFalse();

                // Write expected lock file
                var packagePath = LocalFolderUtility.GetPackagesV3(pathContext.PackageSource, NullLogger.Instance).Single().Path;

                string contentHash = null;
                using (var reader = new PackageArchiveReader(packagePath))
                {
                    contentHash = reader.GetContentHash(CancellationToken.None);
                }

                var expectedLockFile = GetResource("NuGet.CommandLine.FuncTest.compiler.resources.pc.packages.lock.json").Replace("TEMPLATE", contentHash);
                File.WriteAllText(projectA.NuGetLockFileOutputPath, expectedLockFile);

                // Run lockedmode restore.
                result = RunRestore(pathContext, _successExitCode, "-LockedMode");
                result.Success.Should().BeTrue(because: result.AllOutput);
                new FileInfo(projectA.NuGetLockFileOutputPath).Exists.Should().BeTrue();
            }
        }
コード例 #4
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);
                    }
        }
コード例 #5
0
        public static void GenerateNupkgMetadataFile(string nupkgPath, string installPath, string hashPath, string nupkgMetadataPath)
        {
            ConcurrencyUtilities.ExecuteWithFileLocked(nupkgPath,
                                                       action: () =>
            {
                // make sure new hash file doesn't exists within File lock before actually creating it.
                if (!File.Exists(nupkgMetadataPath))
                {
                    var tempNupkgMetadataFilePath = Path.Combine(installPath, Path.GetRandomFileName());
                    using (var stream = File.Open(nupkgPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        using (var packageReader = new PackageArchiveReader(stream))
                        {
                            // get hash of unsigned content of signed package
                            var packageHash = packageReader.GetContentHash(
                                CancellationToken.None,
                                GetUnsignedPackageHash:
                                () =>
                            {
                                if (!string.IsNullOrEmpty(hashPath) && File.Exists(hashPath))
                                {
                                    return(File.ReadAllText(hashPath));
                                }

                                return(null);
                            });

                            // write the new hash file
                            var hashFile = new NupkgMetadataFile()
                            {
                                ContentHash = packageHash
                            };

                            NupkgMetadataFileFormat.Write(tempNupkgMetadataFilePath, hashFile);
                            File.Move(tempNupkgMetadataFilePath, nupkgMetadataPath);
                        }
                }
            });
        }