コード例 #1
0
        public async Task TestBuildIntegratedNuGetProjectUninstallAllPackages()
        {
            // Arrange
            var packageIdentity  = new PackageIdentity("packageA", new NuGetVersion("1.0.0"));
            var packageIdentity2 = new PackageIdentity("packageB", new NuGetVersion("1.0.0"));

            using (var randomTestPackageSourcePath = TestDirectory.Create())
                using (var randomPackagesFolderPath = TestDirectory.Create())
                    using (var randomProjectFolderPath = TestDirectory.Create())
                    {
                        var randomConfig = Path.Combine(randomProjectFolderPath, "project.json");
                        var token        = CancellationToken.None;

                        CreateConfigJson(randomConfig);

                        var projectTargetFramework    = NuGetFramework.Parse("netcore50");
                        var testNuGetProjectContext   = new TestNuGetProjectContext();
                        var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(projectTargetFramework, testNuGetProjectContext, randomProjectFolderPath);
                        var projectFilePath           = Path.Combine(randomProjectFolderPath, $"{msBuildNuGetProjectSystem.ProjectName}.csproj");
                        var buildIntegratedProject    = new ProjectJsonNuGetProject(randomConfig, projectFilePath);

                        var packageFileInfo = TestPackagesGroupedByFolder.GetLegacyContentPackage(randomTestPackageSourcePath,
                                                                                                  packageIdentity.Id, packageIdentity.Version.ToNormalizedString());
                        var packageFileInfo2 = TestPackagesGroupedByFolder.GetLegacyContentPackage(randomTestPackageSourcePath,
                                                                                                   packageIdentity.Id, packageIdentity.Version.ToNormalizedString());
                        using (var packageStream = GetDownloadResourceResult(packageFileInfo))
                        {
                            await buildIntegratedProject.InstallPackageAsync(
                                packageIdentity.Id,
                                new VersionRange(packageIdentity.Version),
                                testNuGetProjectContext,
                                installationContext : null,
                                token : token);

                            await buildIntegratedProject.InstallPackageAsync(
                                packageIdentity2.Id,
                                new VersionRange(packageIdentity2.Version),
                                testNuGetProjectContext,
                                installationContext : null,
                                token : token);

                            // Act
                            await buildIntegratedProject.UninstallPackageAsync(packageIdentity2, new TestNuGetProjectContext(), CancellationToken.None);

                            await buildIntegratedProject.UninstallPackageAsync(packageIdentity, new TestNuGetProjectContext(), CancellationToken.None);
                        }

                        // Assert
                        // Check that the packages.config file exists after the installation
                        Assert.True(File.Exists(randomConfig));
                        // Check the number of packages and packages returned by project after the installation
                        var installedPackages = (await buildIntegratedProject.GetInstalledPackagesAsync(token)).ToList();
                        Assert.Equal(0, installedPackages.Count);
                    }
        }
コード例 #2
0
        public async Task DeletePackage_DeletesPackage()
        {
            // Arrange
            using (var randomTestSourcePath = TestDirectory.Create())
                using (var randomTestDestinationPath = TestDirectory.Create())
                {
                    var packageIdentity = new PackageIdentity("packageA", new NuGetVersion("1.0.0"));
                    var packageFileInfo = TestPackagesGroupedByFolder.GetLegacyContentPackage(
                        randomTestSourcePath,
                        packageIdentity.Id,
                        packageIdentity.Version.ToNormalizedString());
                    var folderNuGetProject      = new FolderNuGetProject(randomTestDestinationPath);
                    var packagePathResolver     = new PackagePathResolver(randomTestDestinationPath);
                    var packageInstallPath      = packagePathResolver.GetInstallPath(packageIdentity);
                    var nupkgFilePath           = Path.Combine(packageInstallPath, packagePathResolver.GetPackageFileName(packageIdentity));
                    var testNuGetProjectContext = new TestNuGetProjectContext();
                    var token = CancellationToken.None;
                    using (var packageStream = GetDownloadResourceResult(packageFileInfo))
                    {
                        // Act
                        await folderNuGetProject.InstallPackageAsync(packageIdentity, packageStream, testNuGetProjectContext, token);
                    }

                    var unNormalizedPackageIdentity = new PackageIdentity(packageIdentity.Id,
                                                                          new NuGetVersion(packageIdentity.Version + ".0"));

                    // Assert
                    Assert.True(File.Exists(nupkgFilePath));
                    Assert.True(File.Exists(Path.Combine(packageInstallPath, "Content/Scripts/test1.js")));
                    using (var packageStream = File.OpenRead(nupkgFilePath))
                    {
                        var zipArchive = new ZipArchive(packageStream);
                        Assert.Equal(6, zipArchive.Entries.Count);
                    }
                    Assert.True(folderNuGetProject.PackageExists(packageIdentity));
                    var packageDirectoryPath = folderNuGetProject.GetInstalledPath(unNormalizedPackageIdentity);
                    Assert.True(!string.IsNullOrEmpty(packageDirectoryPath));
                    Assert.True(Directory.Exists(packageDirectoryPath));

                    // Main Act
                    await folderNuGetProject.DeletePackage(packageIdentity, testNuGetProjectContext, CancellationToken.None);

                    // Assert
                    Assert.False(folderNuGetProject.PackageExists(unNormalizedPackageIdentity));
                    // Check that the package directories are deleted
                    Assert.False(Directory.Exists(packageDirectoryPath));
                }
        }