Exemplo n.º 1
0
        public async Task PackageExpander_CleansExtraFiles()
        {
            // Arrange
            using (var package = TestPackages.GetNearestReferenceFilteringPackage())
            {
                var version  = new NuGetVersion(package.Version);
                var identity = new PackageIdentity(package.Id, version);

                var packagesDir  = TestFileSystemUtility.CreateRandomTestFolder();
                var pathResolver = new VersionFolderPathResolver(packagesDir);

                var token  = CancellationToken.None;
                var logger = NullLogger.Instance;
                var versionFolderPathContext = new VersionFolderPathContext(
                    identity,
                    packagesDir,
                    logger,
                    packageSaveMode: PackageSaveMode.Defaultv3,
                    xmlDocFileSaveMode: XmlDocFileSaveMode.None);

                var packageDir = pathResolver.GetInstallPath(package.Id, identity.Version);

                var randomFile = Path.Combine(packageDir, package.Id + "." + package.Version + ".random");

                Directory.CreateDirectory(packageDir);
                File.WriteAllBytes(randomFile, new byte[] { });

                var randomFolder = Path.Combine(packageDir, "random");
                Directory.CreateDirectory(randomFolder);

                Assert.True(File.Exists(randomFile), randomFile + " does not exist");
                AssertDirectoryExists(randomFolder);

                // Act
                using (var stream = package.File.OpenRead())
                {
                    await PackageExtractor.InstallFromSourceAsync(async (d) => await stream.CopyToAsync(d),
                                                                  versionFolderPathContext,
                                                                  token);
                }

                // Assert
                AssertDirectoryExists(packageDir, packageDir + " does not exist");

                var filePath = pathResolver.GetPackageFilePath(package.Id, identity.Version);
                Assert.True(File.Exists(filePath), filePath + " does not exist");

                var dllPath = Path.Combine(packageDir, "lib", "net40", "one.dll");
                Assert.True(File.Exists(dllPath), dllPath + " does not exist");

                Assert.False(File.Exists(randomFile), randomFile + " does exist");
                Assert.False(Directory.Exists(randomFolder), randomFolder + " does exist");
            }
        }
Exemplo n.º 2
0
        public async Task PackageExpander_Recovers_WhenStreamIsCorrupt()
        {
            // Arrange
            using (var package = TestPackages.GetNearestReferenceFilteringPackage())
            {
                var version  = new NuGetVersion(package.Version);
                var identity = new PackageIdentity(package.Id, version);

                var packagesDir  = TestFileSystemUtility.CreateRandomTestFolder();
                var pathResolver = new VersionFolderPathResolver(packagesDir);

                var token  = CancellationToken.None;
                var logger = NullLogger.Instance;
                var versionFolderPathContext = new VersionFolderPathContext(
                    identity,
                    packagesDir,
                    logger,
                    packageSaveMode: PackageSaveMode.Defaultv3,
                    xmlDocFileSaveMode: XmlDocFileSaveMode.None);

                var packageDir = pathResolver.GetInstallPath(package.Id, identity.Version);
                Assert.False(Directory.Exists(packageDir), packageDir + " exist");

                // Act
                using (var stream = package.File.OpenRead())
                {
                    await Assert.ThrowsAnyAsync <CorruptionException>(async() =>
                                                                      await PackageExtractor.InstallFromSourceAsync(
                                                                          async(d) => await new CorruptStreamWrapper(stream).CopyToAsync(d),
                                                                          versionFolderPathContext,
                                                                          token));
                }

                AssertDirectoryExists(packageDir, packageDir + " does not exist");

                Assert.NotEmpty(Directory.EnumerateFiles(packageDir));

                using (var stream = package.File.OpenRead())
                {
                    await PackageExtractor.InstallFromSourceAsync(async (d) => await stream.CopyToAsync(d),
                                                                  versionFolderPathContext,
                                                                  token);
                }

                // Assert
                var filePath = pathResolver.GetPackageFilePath(package.Id, identity.Version);
                Assert.True(File.Exists(filePath), filePath + " does not exist");

                var dllPath = Path.Combine(packageDir, "lib", "net40", "one.dll");
                Assert.True(File.Exists(dllPath), dllPath + " does not exist");
            }
        }
Exemplo n.º 3
0
        public async Task PackageExpander_ExpandsPackage_SkipsIfShaIsThere()
        {
            // Arrange
            using (var package = TestPackages.GetNearestReferenceFilteringPackage())
            {
                var version  = new NuGetVersion(package.Version);
                var identity = new PackageIdentity(package.Id, version);

                var packagesDir  = TestDirectory.Create();
                var pathResolver = new VersionFolderPathResolver(packagesDir);

                var token  = CancellationToken.None;
                var logger = NullLogger.Instance;
                var versionFolderPathContext = new VersionFolderPathContext(
                    identity,
                    packagesDir,
                    logger,
                    packageSaveMode: PackageSaveMode.Defaultv3,
                    xmlDocFileSaveMode: XmlDocFileSaveMode.None);

                var packageDir = pathResolver.GetInstallPath(package.Id, identity.Version);

                Directory.CreateDirectory(packageDir);

                var nupkgPath = pathResolver.GetPackageFilePath(package.Id, identity.Version);
                var shaPath   = pathResolver.GetHashPath(package.Id, identity.Version);

                File.WriteAllBytes(shaPath, new byte[] { });

                Assert.True(File.Exists(shaPath));

                // Act
                using (var stream = package.File.OpenRead())
                {
                    await PackageExtractor.InstallFromSourceAsync(async (d) => await stream.CopyToAsync(d),
                                                                  versionFolderPathContext,
                                                                  token);
                }

                // Assert
                AssertDirectoryExists(packageDir, packageDir + " does not exist");

                Assert.False(File.Exists(nupkgPath), nupkgPath + " does not exist");

                var dllPath = Path.Combine(packageDir, "lib", "net40", "one.dll");
                Assert.False(File.Exists(dllPath), dllPath + " does not exist");

                Assert.Equal(1, Directory.EnumerateFiles(packageDir).Count());
            }
        }
Exemplo n.º 4
0
        public void PackageReader_RespectReferencesAccordingToDifferentFrameworks()
        {
            // Copy of the InstallPackageRespectReferencesAccordingToDifferentFrameworks functional test

            // Arrange
            var zip    = TestPackages.GetZip(TestPackages.GetNearestReferenceFilteringPackage());
            var reader = new PackageReader(zip);

            // Act
            var references = reader.GetReferenceItems();
            var netResult  = NuGetFrameworkUtility.GetNearest <FrameworkSpecificGroup>(references, NuGetFramework.Parse("net45"));
            var slResult   = NuGetFrameworkUtility.GetNearest <FrameworkSpecificGroup>(references, NuGetFramework.Parse("sl5"));

            // Assert
            Assert.Equal(2, netResult.Items.Count());
            Assert.Equal(1, slResult.Items.Count());
            Assert.Equal("lib/sl40/a.dll", slResult.Items.First());
            Assert.Equal("lib/net40/one.dll", netResult.Items.First());
            Assert.Equal("lib/net40/three.dll", netResult.Items.Skip(1).First());
        }
Exemplo n.º 5
0
        public async Task PackageExpander_Recovers_WhenFileIsLocked()
        {
            // Arrange
            using (var package = TestPackages.GetNearestReferenceFilteringPackage())
            {
                var version  = new NuGetVersion(package.Version);
                var identity = new PackageIdentity(package.Id, version);

                var packagesDir  = TestFileSystemUtility.CreateRandomTestFolder();
                var pathResolver = new VersionFolderPathResolver(packagesDir);

                var token  = CancellationToken.None;
                var logger = NullLogger.Instance;
                var versionFolderPathContext = new VersionFolderPathContext(
                    identity,
                    packagesDir,
                    logger,
                    packageSaveMode: PackageSaveMode.Defaultv3,
                    xmlDocFileSaveMode: XmlDocFileSaveMode.None);

                var packageDir = pathResolver.GetInstallPath(package.Id, identity.Version);
                Assert.False(Directory.Exists(packageDir), packageDir + " exist");

                string filePathToLock = Path.Combine(packageDir, "lib", "net40", "two.dll");

                // Act
                using (var stream = package.File.OpenRead())
                {
                    var cts = new CancellationTokenSource(DefaultTimeOut);

                    Func <CancellationToken, Task <bool> > action = (ct) => {
                        Assert.ThrowsAnyAsync <IOException>(async() =>
                                                            await PackageExtractor.InstallFromSourceAsync(
                                                                str => stream.CopyToAsync(stream, bufferSize: 8192, cancellationToken: token),
                                                                versionFolderPathContext,
                                                                token));

                        return(Task.FromResult(true));
                    };

                    await ConcurrencyUtilities.ExecuteWithFileLockedAsync(filePathToLock, action, cts.Token);
                }

                AssertDirectoryExists(packageDir, packageDir + " does not exist");

                Assert.NotEmpty(Directory.EnumerateFiles(packageDir));

                using (var stream = package.File.OpenRead())
                {
                    await PackageExtractor.InstallFromSourceAsync(async (d) => await stream.CopyToAsync(d),
                                                                  versionFolderPathContext,
                                                                  token);
                }

                // Assert
                var filePath = pathResolver.GetPackageFilePath(package.Id, identity.Version);
                Assert.True(File.Exists(filePath), filePath + " does not exist");

                var dllPath = Path.Combine(packageDir, "lib", "net40", "one.dll");
                Assert.True(File.Exists(dllPath), dllPath + " does not exist");

                Assert.True(File.Exists(filePathToLock));

                // Make sure the actual file from the zip was extracted
                Assert.Equal(new byte[] { 0 }, File.ReadAllBytes(filePathToLock));
            }
        }