Пример #1
0
        public async Task GetInstalledSatelliteFilesAsync_ReturnsNullRuntimePackageDirectoryIfRuntimePackageDoesNotExist()
        {
            using (var test = PackageHelperTest.Create(TestPackagesCore.GetPackageContentReaderTestPackage()))
            {
                using (var testDirectory = TestDirectory.Create())
                {
                    var packageIdentity      = new PackageIdentity("A", new NuGetVersion("1.2.3"));
                    var packagePathResolver  = new PackagePathResolver(testDirectory.Path);
                    var satellitePackageInfo = await TestPackagesCore.GetSatellitePackageAsync(
                        testDirectory.Path,
                        packageIdentity.Id,
                        packageIdentity.Version.ToNormalizedString(),
                        language : "fr");

                    using (var packageReader = new PackageArchiveReader(File.OpenRead(satellitePackageInfo.FullName)))
                    {
                        var result = await PackageHelper.GetInstalledSatelliteFilesAsync(
                            packageReader,
                            packagePathResolver,
                            PackageSaveMode.Defaultv3,
                            CancellationToken.None);

                        var runtimePackageDirectory = result.Item1;
                        var satelliteFiles          = result.Item2;

                        Assert.Null(runtimePackageDirectory);
                        Assert.Empty(satelliteFiles);
                    }
                }
            }
        }
Пример #2
0
        public async Task GetInstalledSatelliteFilesAsync_ReturnsInstalledSatelliteFiles()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var packageIdentity     = new PackageIdentity("A", new NuGetVersion("1.2.3"));
                var packagePathResolver = new PackagePathResolver(testDirectory.Path);
                var packageFileInfo     = await TestPackagesCore.GetRuntimePackageAsync(
                    testDirectory.Path,
                    packageIdentity.Id,
                    packageIdentity.Version.ToNormalizedString());

                var satellitePackageInfo = await TestPackagesCore.GetSatellitePackageAsync(
                    testDirectory.Path,
                    packageIdentity.Id,
                    packageIdentity.Version.ToNormalizedString(),
                    language : "fr");

                // Install runtime package
                using (var packageReader = new PackageArchiveReader(File.OpenRead(packageFileInfo.FullName)))
                    using (var packageStream = File.OpenRead(packageFileInfo.FullName))
                    {
                        await PackageExtractor.ExtractPackageAsync(
                            packageReader,
                            packageStream,
                            packagePathResolver,
                            new PackageExtractionContext(NullLogger.Instance),
                            CancellationToken.None);
                    }

                // Install satellite package
                using (var packageReader = new PackageArchiveReader(File.OpenRead(satellitePackageInfo.FullName)))
                    using (var packageStream = File.OpenRead(satellitePackageInfo.FullName))
                    {
                        await PackageExtractor.ExtractPackageAsync(
                            packageReader,
                            packageStream,
                            packagePathResolver,
                            new PackageExtractionContext(NullLogger.Instance),
                            CancellationToken.None);
                    }

                using (var packageReader = new PackageArchiveReader(File.OpenRead(satellitePackageInfo.FullName)))
                {
                    var result = await PackageHelper.GetInstalledSatelliteFilesAsync(
                        packageReader,
                        packagePathResolver,
                        PackageSaveMode.Defaultv3,
                        CancellationToken.None);

                    var runtimePackageDirectory = result.Item1;
                    var satelliteFiles          = result.Item2.ToArray();
                    var packageDirectoryName    = $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}";

                    Assert.Equal(Path.Combine(testDirectory.Path, packageDirectoryName), runtimePackageDirectory);
                    Assert.Equal(1, satelliteFiles.Length);
                    Assert.Equal(Path.Combine(testDirectory.Path, packageDirectoryName, "lib/net45/fr/A.resources.dll"), satelliteFiles[0].FileFullPath);
                }
            }
        }
Пример #3
0
        public async Task GetInstalledSatelliteFilesAsync_ReturnsEmptyEnumerableForNoInstalledSatelliteFiles()
        {
            using (var test = PackageHelperTest.Create(TestPackagesCore.GetPackageContentReaderTestPackage()))
            {
                using (var testDirectory = TestDirectory.Create())
                {
                    var packageIdentity     = new PackageIdentity("A", new NuGetVersion("1.2.3"));
                    var packagePathResolver = new PackagePathResolver(testDirectory.Path);
                    var packageFileInfo     = await TestPackagesCore.GetRuntimePackageAsync(
                        testDirectory.Path,
                        packageIdentity.Id,
                        packageIdentity.Version.ToNormalizedString());

                    var satellitePackageInfo = await TestPackagesCore.GetSatellitePackageAsync(
                        testDirectory.Path,
                        packageIdentity.Id,
                        packageIdentity.Version.ToNormalizedString(),
                        language : "fr");

                    // Install runtime package
                    using (var packageReader = new PackageArchiveReader(File.OpenRead(packageFileInfo.FullName)))
                        using (var packageStream = File.OpenRead(packageFileInfo.FullName))
                        {
                            await PackageExtractor.ExtractPackageAsync(
                                test.Root,
                                packageReader,
                                packageStream,
                                packagePathResolver,
                                new PackageExtractionContext(
                                    PackageSaveMode.Defaultv2,
                                    PackageExtractionBehavior.XmlDocFileSaveMode,
                                    NullLogger.Instance,
                                    signedPackageVerifier : null,
                                    signedPackageVerifierSettings : null),
                                CancellationToken.None);
                        }

                    using (var packageReader = new PackageArchiveReader(File.OpenRead(satellitePackageInfo.FullName)))
                    {
                        var result = await PackageHelper.GetInstalledSatelliteFilesAsync(
                            packageReader,
                            packagePathResolver,
                            PackageSaveMode.Defaultv3,
                            CancellationToken.None);

                        var runtimePackageDirectory = result.Item1;
                        var satelliteFiles          = result.Item2;
                        var packageDirectoryName    = $"{packageIdentity.Id}.{packageIdentity.Version.ToNormalizedString()}";

                        Assert.Equal(Path.Combine(testDirectory.Path, packageDirectoryName), runtimePackageDirectory);
                        Assert.Empty(satelliteFiles);
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Asynchronously deletes a package.
        /// </summary>
        /// <param name="packageIdentity">A package identity.</param>
        /// <param name="nuGetProjectContext">A NuGet project context.</param>
        /// <param name="token">A cancellation token.</param>
        /// <returns>A task that represents the asynchronous operation.
        /// The task result (<see cref="Task{TResult}.Result" />) returns a <see cref="bool" />
        /// indication successfulness of the operation.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="packageIdentity" />
        /// is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="nuGetProjectContext" />
        /// is <c>null</c>.</exception>
        public async Task <bool> DeletePackage(PackageIdentity packageIdentity,
                                               INuGetProjectContext nuGetProjectContext,
                                               CancellationToken token)
        {
            if (packageIdentity == null)
            {
                throw new ArgumentNullException(nameof(packageIdentity));
            }

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

            var packageFilePath = GetInstalledPackageFilePath(packageIdentity);

            if (File.Exists(packageFilePath))
            {
                var packageDirectoryPath = Path.GetDirectoryName(packageFilePath);
                using (var packageReader = new PackageArchiveReader(packageFilePath))
                {
                    var installedSatelliteFilesPair = await PackageHelper.GetInstalledSatelliteFilesAsync(
                        packageReader,
                        PackagePathResolver,
                        GetPackageSaveMode(nuGetProjectContext),
                        token);

                    var runtimePackageDirectory = installedSatelliteFilesPair.Item1;
                    var installedSatelliteFiles = installedSatelliteFilesPair.Item2;
                    if (!string.IsNullOrEmpty(runtimePackageDirectory))
                    {
                        try
                        {
                            // Delete all the package files now
                            FileSystemUtility.DeleteFiles(installedSatelliteFiles, runtimePackageDirectory, nuGetProjectContext);
                        }
                        catch (Exception ex)
                        {
                            nuGetProjectContext.Log(MessageLevel.Warning, ex.Message);
                            // Catch all exception with delete so that the package file is always deleted
                        }
                    }

                    // Get all the package files before deleting the package file
                    var installedPackageFiles = await PackageHelper.GetInstalledPackageFilesAsync(
                        packageReader,
                        packageIdentity,
                        PackagePathResolver,
                        GetPackageSaveMode(nuGetProjectContext),
                        token);

                    try
                    {
                        // Delete all the package files now
                        FileSystemUtility.DeleteFiles(installedPackageFiles, packageDirectoryPath, nuGetProjectContext);
                    }
                    catch (Exception ex)
                    {
                        nuGetProjectContext.Log(MessageLevel.Warning, ex.Message);
                        // Catch all exception with delete so that the package file is always deleted
                    }
                }

                // Delete the package file
                FileSystemUtility.DeleteFile(packageFilePath, nuGetProjectContext);

                // Delete the package directory if any
                FileSystemUtility.DeleteDirectorySafe(packageDirectoryPath, recursive: true, nuGetProjectContext: nuGetProjectContext);

                // If this is the last package delete the package directory
                // If this is the last package delete the package directory
                if (!FileSystemUtility.GetFiles(Root, string.Empty, "*.*").Any() &&
                    !FileSystemUtility.GetDirectories(Root, string.Empty).Any())
                {
                    FileSystemUtility.DeleteDirectorySafe(Root, recursive: false, nuGetProjectContext: nuGetProjectContext);
                }
            }

            return(true);
        }