public void CollectCycles_WhenCycleInLocalPackages_ShouldOfferAllPossibleFixes()
        {
            var optimizer = StructureOptimizer.Create(new[]
            {
                new PackageMeta("", true, new PackageInfo
                {
                    Name         = "A",
                    Dependencies = new[] { new Dependency {
                                               Name = "B"
                                           } }
                }),
                new PackageMeta("", true, new PackageInfo
                {
                    Name         = "B",
                    Dependencies = new[] { new Dependency {
                                               Name = "C"
                                           } }
                }),
                new PackageMeta("", true, new PackageInfo
                {
                    Name         = "C",
                    Dependencies = new[] { new Dependency {
                                               Name = "A"
                                           } }
                })
            },
                                                      null,
                                                      "A");

            var actual = optimizer.CollectCycles().ToArray();

            Assert.That(actual, Has.Length.EqualTo(1));
            Assert.That(actual[0], Has.Length.EqualTo(3));
            Assert.That(actual[0], Has.Some.Matches <StructureOptimizer.CycleFixOffer>(
                            x => x.SourceMeta.PackageInfo.Name == "A" && x.DependencyToRemove == "B"));
            Assert.That(actual[0], Has.Some.Matches <StructureOptimizer.CycleFixOffer>(
                            x => x.SourceMeta.PackageInfo.Name == "B" && x.DependencyToRemove == "C"));
            Assert.That(actual[0], Has.Some.Matches <StructureOptimizer.CycleFixOffer>(
                            x => x.SourceMeta.PackageInfo.Name == "C" && x.DependencyToRemove == "A"));
        }
Example #2
0
        private static bool HandleCycles(PackageMeta[] localPackages, PackageRepository packageRepository, string target)
        {
            var structureOptimizer = StructureOptimizer.Create(localPackages, packageRepository, target);
            var optimizationOffer  = structureOptimizer.CollectCycles().FirstOrDefault();

            if (optimizationOffer == null)
            {
                return(false);
            }

            Console.WriteLine("Cycle found");
            foreach (var offer in optimizationOffer)
            {
                if (ConsoleAsk($"Remove dependency '{offer.DependencyToRemove}' from package '{offer.SourceMeta.PackageInfo.Name}'?"))
                {
                    var newPackageInfo = new PackageInfo
                    {
                        Name         = offer.SourceMeta.PackageInfo.Name,
                        Version      = offer.SourceMeta.PackageInfo.Version,
                        Dependencies = offer
                                       .SourceMeta
                                       .PackageInfo
                                       .Dependencies
                                       .Select(x => x.Name != offer.DependencyToRemove
                                ? new Dependency {
                            Name = x.Name, Version = x.Version
                        }
                                : null)
                                       .Where(x => x != null)
                                       .ToArray()
                    };

                    File.WriteAllText(offer.SourceMeta.Path, JsonConvert.SerializeObject(newPackageInfo, Formatting.Indented));
                    break;
                }
            }

            return(true);
        }