private async Task <(InstalledPackageResultStatus, IReadOnlyCollection <NuGetInstalledPackage>)> GetInstalledPackagesAsync(BuildIntegratedNuGetProject project, CancellationToken cancellationToken)
        {
            NuGetInstalledPackage ToNuGetInstalledPackage(PackageReference packageReference, FallbackPackagePathResolver pathResolver)
            {
                var id = packageReference.PackageIdentity.Id;

                var    versionRange   = packageReference.AllowedVersions;
                string requestedRange =
                    packageReference.AllowedVersions.OriginalString // most packages
                    ?? packageReference.AllowedVersions.ToShortString();
                string version          = versionRange.MinVersion.OriginalVersion ?? versionRange.MinVersion.ToNormalizedString();
                var    installPath      = pathResolver.GetPackageDirectory(id, version);
                bool   directDependency = true;

                return(NuGetContractsFactory.CreateNuGetInstalledPackage(id, requestedRange, version, installPath, directDependency));
            }

            InstalledPackageResultStatus status;
            IReadOnlyCollection <NuGetInstalledPackage> installedPackages;

            var cacheContext = new DependencyGraphCacheContext();

            var(packageSpecs, messages) = await project.GetPackageSpecsAndAdditionalMessagesAsync(cacheContext);

            if (messages?.Any(m => m.Level == LogLevel.Error) == true)
            {
                // Although we know that the project will fail to restore, we may still know about some direct dependencies, so let's return the packages that we know about.
                status = InstalledPackageResultStatus.ProjectInvalid;
            }
            else
            {
                status = InstalledPackageResultStatus.Successful;
            }

            var packageSpec  = packageSpecs.Single(s => s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.PackageReference || s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.ProjectJson);
            var packagesPath = VSRestoreSettingsUtilities.GetPackagesPath(_settings, packageSpec);
            FallbackPackagePathResolver pathResolver = new FallbackPackagePathResolver(packagesPath, VSRestoreSettingsUtilities.GetFallbackFolders(_settings, packageSpec));

            var packageReferences = await project.GetInstalledPackagesAsync(cancellationToken);

            installedPackages = packageReferences.Select(p => ToNuGetInstalledPackage(p, pathResolver))
                                .ToList();

            return(status, installedPackages);
        }
        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 = TestFileSystemUtility.CreateRandomTestFolder())
                using (var randomPackagesFolderPath = TestFileSystemUtility.CreateRandomTestFolder())
                    using (var randomProjectFolderPath = TestFileSystemUtility.CreateRandomTestFolder())
                    {
                        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 BuildIntegratedNuGetProject(randomConfig, projectFilePath, msBuildNuGetProjectSystem);

                        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, packageStream, testNuGetProjectContext, token);

                            await buildIntegratedProject.InstallPackageAsync(packageIdentity2, packageStream, testNuGetProjectContext, 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);
                    }
        }
        private async Task <(InstalledPackageResultStatus, IReadOnlyCollection <NuGetInstalledPackage>)> GetInstalledPackagesAsync(BuildIntegratedNuGetProject project, CancellationToken cancellationToken)
        {
            NuGetInstalledPackage ToNuGetInstalledPackage(PackageReference packageReference, FallbackPackagePathResolver pathResolver, bool directDependency)
            {
                var id = packageReference.PackageIdentity.Id;

                string requestedRange = null;

                if (directDependency)
                {
                    requestedRange =
                        packageReference.AllowedVersions?.OriginalString      // When Version is specified
                        ?? packageReference.AllowedVersions?.ToShortString(); // Probably only when Version is not specified in msbuild
                }

                string version =
                    packageReference.PackageIdentity.Version?.ToNormalizedString()
                    ?? string.Empty;

                var installPath =
                    version != null
                    ? pathResolver.GetPackageDirectory(id, version)
                    : null;

                return(NuGetContractsFactory.CreateNuGetInstalledPackage(id, requestedRange, version, installPath, directDependency));
            }

            InstalledPackageResultStatus status;
            List <NuGetInstalledPackage> installedPackages;

            (InstalledPackageResultStatus, IReadOnlyCollection <NuGetInstalledPackage>) ErrorResult(InstalledPackageResultStatus status)
            {
                return(status, null);
            }

            var cacheContext = new DependencyGraphCacheContext();
            IReadOnlyList <ProjectModel.PackageSpec>       packageSpecs;
            IReadOnlyList <ProjectModel.IAssetsLogMessage> messages;

            try
            {
                (packageSpecs, messages) = await project.GetPackageSpecsAndAdditionalMessagesAsync(cacheContext);
            }
            catch (ProjectNotNominatedException)
            {
                return(ErrorResult(InstalledPackageResultStatus.ProjectNotReady));
            }
            catch (InvalidDataException)
            {
                return(ErrorResult(InstalledPackageResultStatus.ProjectInvalid));
            }

            if (messages?.Any(m => m.Level == LogLevel.Error) == true)
            {
                // Although we know that the project will fail to restore, we may still know about some direct dependencies, so let's return the packages that we know about.
                status = InstalledPackageResultStatus.ProjectInvalid;
            }
            else
            {
                status = InstalledPackageResultStatus.Successful;
            }

            var packageSpec  = packageSpecs.Single(s => s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.PackageReference || s.RestoreMetadata.ProjectStyle == ProjectModel.ProjectStyle.ProjectJson);
            var packagesPath = VSRestoreSettingsUtilities.GetPackagesPath(_settings, packageSpec);
            FallbackPackagePathResolver pathResolver = new FallbackPackagePathResolver(packagesPath, VSRestoreSettingsUtilities.GetFallbackFolders(_settings, packageSpec));

            IReadOnlyCollection <PackageReference> directPackages;
            IReadOnlyCollection <PackageReference> transitivePackages;

            if (project is IPackageReferenceProject packageReferenceProject)
            {
                var installed = await packageReferenceProject.GetInstalledAndTransitivePackagesAsync(cancellationToken);

                directPackages     = installed.InstalledPackages;
                transitivePackages = installed.TransitivePackages;
            }
            else
            {
                directPackages     = (await project.GetInstalledPackagesAsync(cancellationToken)).ToList();
                transitivePackages = Array.Empty <PackageReference>();
            }

            installedPackages = new List <NuGetInstalledPackage>(directPackages.Count + (transitivePackages?.Count ?? 0));

            installedPackages.AddRange(directPackages.Select(p => ToNuGetInstalledPackage(p, pathResolver, directDependency: true)));
            if (transitivePackages != null)
            {
                installedPackages.AddRange(transitivePackages.Select(p => ToNuGetInstalledPackage(p, pathResolver, directDependency: false)));
            }

            return(status, installedPackages);
        }