public void TestUninstallResolverDependenciesDict()
        {
            IDictionary <PackageIdentity, HashSet <PackageIdentity> > dependenciesDict;
            var dependentsDict = UninstallResolver.GetPackageDependents(PackageDependencyInfo1,
                                                                        InstalledPackages1,
                                                                        out dependenciesDict);

            var keysList   = dependenciesDict.Keys.ToList();
            var valuesList = dependenciesDict.Values.ToList();

            Assert.Equal(2, dependenciesDict.Count);
            Assert.Equal(A1, keysList[0]);
            Assert.True(valuesList[0].Contains(B1));
            Assert.True(valuesList[0].Contains(C1));
            Assert.Equal(C1, keysList[1]);
            Assert.True(valuesList[1].Contains(D1));
            Assert.True(valuesList[1].Contains(E1));
        }
Exemplo n.º 2
0
        private async Task <ICollection <PackageIdentity> > GetPackagesCanBeUninstalledAsync(
            IEnumerable <SourcePackageDependencyInfo> markedForUninstall,
            IEnumerable <PackageIdentity> installedPackages)
        {
            IDictionary <PackageIdentity, HashSet <PackageIdentity> > dependenciesDictionary;
            var dependentsDictionary = UninstallResolver.GetPackageDependents(markedForUninstall, installedPackages, out dependenciesDictionary);

            //exclude packages which should not be removed, because of dependent packages
            HashSet <PackageIdentity> shouldBeExcludedSet = new HashSet <PackageIdentity>();

            foreach (var identity in dependenciesDictionary)
            {
                var markedOnUninstallDependency = identity.Key;

                HashSet <PackageIdentity> dependents;

                if (dependentsDictionary.TryGetValue(markedOnUninstallDependency, out dependents) && dependents is not null)
                {
                    var externalDependants = dependents.Where(x => !dependenciesDictionary.ContainsKey(x)).ToList();

                    if (externalDependants.Count > 0)
                    {
                        _nugetLogger.LogInformation($"{identity} package skipped, because one or more installed packages depends on it");
                    }

                    externalDependants.ForEach(d => shouldBeExcludedSet.Add(d));
                }
            }

            var markedForUninstallList = markedForUninstall.OfType <PackageIdentity>().ToList();

            foreach (var excludedDependency in shouldBeExcludedSet)
            {
                markedForUninstallList.Remove(excludedDependency);
            }

            return(markedForUninstallList);
        }