Esempio n. 1
0
        public void EnsurePackageStorePackageInMemoryIfMachineCacheIsNotAvailable()
        {
            // Arrange
            var uri            = new Uri("http://nuget.org");
            var mockRepository = new Mock <MockPackageRepository>().As <IPackageCacheRepository>();

            mockRepository.Setup(s => s.InvokeOnPackage(It.IsAny <string>(), It.IsAny <SemanticVersion>(), It.IsAny <Action <Stream> >())).Returns(false);

            var packageDownloader = new Mock <PackageDownloader>();

            packageDownloader.Setup(d => d.DownloadPackage(uri, It.IsAny <IPackageMetadata>(), It.IsAny <Stream>()))
            .Callback(new Action <Uri, IPackageMetadata, Stream>(
                          (url, metadata, stream) => PackageUtility.CreateSimplePackageStream("A", "1.2").CopyTo(stream)))
            .Verifiable();
            var hashProvider = new Mock <IHashProvider>(MockBehavior.Strict);

            byte[] hash1 = new byte[] { 1, 2, 3, 4 };
            hashProvider.Setup(h => h.CalculateHash(It.IsAny <Stream>())).Returns <Stream>((stream) => hash1);

            var context = new Mock <IDataServiceContext>();

            context.Setup(c => c.GetReadStreamUri(It.IsAny <object>())).Returns(uri).Verifiable();

            var servicePackage = new DataServicePackage
            {
                Id           = "A",
                Version      = "1.2",
                PackageHash  = Convert.ToBase64String(hash1),
                Downloader   = packageDownloader.Object,
                HashProvider = hashProvider.Object,
                Context      = context.Object
            };

            // Act
            servicePackage.EnsurePackage(mockRepository.Object);

            // Assert
            context.Verify();
            packageDownloader.Verify();

            var foundPackage = servicePackage._package;

            Assert.NotNull(foundPackage);
            Assert.True(foundPackage is ZipPackage);
        }
        public void GetPackagesDoesNotReturnDuplicatedPackagesIfBothNuspecAndNupkgFilesArePresent()
        {
            // Arrange
            var packageStream = PackageUtility.CreateSimplePackageStream("one", "1.0.0-alpha");

            var fileSystem = new MockFileSystem("x:\\root");

            fileSystem.AddFile("one.1.0.0-alpha\\one.1.0.0-alpha.nupkg", packageStream);
            fileSystem.AddFile("one.1.0.0-alpha\\one.1.0.0-alpha.nuspec", "rubbish".AsStream());

            var configFileSystem = new MockFileSystem();
            var repository       = new SharedPackageRepository(new DefaultPackagePathResolver(fileSystem), fileSystem, configFileSystem);

            // Act
            var packages = repository.GetPackages().ToList();

            // Assert
            Assert.Equal(1, packages.Count);
            Assert.True(packages[0] is OptimizedZipPackage);
            Assert.Equal("one", packages[0].Id);
            Assert.Equal(new SemanticVersion("1.0.0-alpha"), packages[0].Version);
        }
        public void FindPackageReturnOptimizedZipPackageObject()
        {
            // Arrange
            var packageStream = PackageUtility.CreateSimplePackageStream("one", "1.0.0-alpha");

            var fileSystem = new MockFileSystem("x:\\root");

            fileSystem.AddFile("one.1.0.0-alpha\\one.1.0.0-alpha.nupkg", packageStream);
            fileSystem.AddFile("one.1.0.0-alpha\\one.1.0.0-alpha.nuspace", "rubbish".AsStream());

            var configFileSystem = new MockFileSystem();
            var repository       = new SharedPackageRepository(new DefaultPackagePathResolver(fileSystem), fileSystem, configFileSystem);

            // Act
            IPackage package = repository.FindPackage("one", new SemanticVersion("1.0.0-alpha"));

            // Assert
            Assert.True(package is OptimizedZipPackage);
            Assert.Equal("one", package.Id);
            Assert.Equal(new SemanticVersion("1.0.0-alpha"), package.Version);
            Assert.Equal("Test description", package.Description);
        }
        public void FindPackagesByIdAlwaysReturnsOptimizedZipPackageEvenIfNuspecIfPresent()
        {
            // Arrange
            string manifestContent = @"<?xml version=""1.0""?>
<package xmlns=""http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"">
  <metadata>
    <id>One</id>
    <version>1.0.0-alpha</version>
    <authors>test</authors>
    <description>My package description.</description>
  </metadata>
  <files />
</package>";

            var fileSystem = new MockFileSystem("x:\\root");

            fileSystem.AddFile("one.1.0.0-alpha\\one.1.0.0-alpha.nuspec", manifestContent.AsStream());

            var packageStream = PackageUtility.CreateSimplePackageStream("One", "1.0.0-alpha");

            fileSystem.AddFile("one.1.0.0-alpha\\one.1.0.0-alpha.nupkg", packageStream);

            var configFileSystem = new MockFileSystem();
            var repository       = new SharedPackageRepository(new DefaultPackagePathResolver(fileSystem), fileSystem, configFileSystem);

            // Act
            var packages = repository.FindPackagesById("one").ToList();

            // Assert
            Assert.Equal(1, packages.Count);
            var package = packages[0];

            Assert.True(package is OptimizedZipPackage);
            Assert.Equal("One", package.Id);
            Assert.Equal(new SemanticVersion("1.0.0-alpha"), package.Version);
        }