Skip to content

Commit

Permalink
Merge pull request #135 from brandhuf/improve_cycle_error_message_order
Browse files Browse the repository at this point in the history
small improvement to the errror message of detected cycles
  • Loading branch information
fgather authored Oct 27, 2021
2 parents cfad5a2 + d1e1820 commit 31cca57
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions ArchUnitNET/Fluent/Slices/SlicesShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent.Freeze;
using StronglyConnectedComponents;
Expand Down Expand Up @@ -49,28 +50,39 @@ private static IEnumerable<EvaluationResult> EvaluateBeFreeOfCycles(IEnumerable<
{
foreach (var cycle in cycles)
{
var description = "Cycle found:";
var depsBetweenSlices = new List<List<string>>();
foreach (var slice in cycle.Contents)
{
var dependencies = slice.Dependencies.ToList();
foreach (var otherSlice in cycle.Contents.Except(new[] {slice}))
foreach (var otherSlice in cycle.Contents.Except(new[] { slice }))
{
var depsToSlice = dependencies.Where(dependency =>
otherSlice.Types.Contains(dependency.Target))
.Distinct(new TypeDependencyComparer()).ToList();
if (depsToSlice.Any())
{
description += "\n" + slice.Description + " -> " + otherSlice.Description;
description = depsToSlice.Aggregate(description,
(current, dependency) =>
current + ("\n\t" + dependency.Origin + " -> " + dependency.Target));
var depsFromThisSliceToOtherSlice = new List<string>
{ slice.Description + " -> " + otherSlice.Description };
depsFromThisSliceToOtherSlice.AddRange(depsToSlice.Select(dependency =>
"\t" + dependency.Origin + " -> " + dependency.Target));
depsBetweenSlices.Add(depsFromThisSliceToOtherSlice);
}
}
}

description += "\n";
var description = new StringBuilder();
description.AppendLine("Cycle found:");
var orderedDeps = depsBetweenSlices.OrderBy(l => l.Count);
foreach (var lines in orderedDeps)
{
foreach (var line in lines)
{
description.AppendLine(line);
}
}

var cycleIdentifier = new EnumerableIdentifier(cycle.Contents.Select(slice => slice.Identifier));
yield return new EvaluationResult(cycle, cycleIdentifier, false, description, archRule,
yield return new EvaluationResult(cycle, cycleIdentifier, false, description.ToString(), archRule,
architecture);
}
}
Expand Down Expand Up @@ -98,7 +110,7 @@ private static IEnumerable<EvaluationResult> EvaluateNotDependOnEachOther(IEnume

foreach (var slice in slicesList)
{
var sliceDependencies = FindDependencies(slice, slicesList).Except(new[] {slice}).ToList();
var sliceDependencies = FindDependencies(slice, slicesList).Except(new[] { slice }).ToList();
var passed = !sliceDependencies.Any();
var description = slice.Description + " does not depend on another slice.";
if (!passed)
Expand Down

0 comments on commit 31cca57

Please sign in to comment.