Skip to content

Commit

Permalink
fix: trim non-cycle stack elements in a cyclic dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat authored and gabotechs committed Feb 23, 2023
1 parent 3844b5a commit 4cdea72
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
18 changes: 18 additions & 0 deletions internal/dep_tree/.render_test/Weird cycle combination.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"tree": {
"0": {
"1": {
"2": null
}
}
},
"circularDependencies": [
[
"2",
"3",
"4",
"2"
]
],
"errors": {}
}
8 changes: 8 additions & 0 deletions internal/dep_tree/.render_test/Weird cycle combination.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0
└▷1
│4◁───┐
││ │
└┴▷2 │
│ │
└▷3┘
10 changes: 9 additions & 1 deletion internal/dep_tree/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ func (lc *LevelCalculator[T]) calculateLevel(
var level int
ctx, level = lc.calculateLevel(ctx, parent.Id, stack)
if level == cyclic {
cycleStack := []string{parent.Id}
for _, stackElement := range stack {
cycleStack = append(cycleStack, stackElement)
if stackElement == parent.Id {
break
}
}

lc.Cycles.Set(dep, DepCycle{
Cause: dep,
Stack: append([]string{parent.Id}, stack...),
Stack: cycleStack,
})
} else if level > maxLevel {
maxLevel = level
Expand Down
12 changes: 12 additions & 0 deletions internal/dep_tree/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ func TestNode_Level(t *testing.T) {
ExpectedLevels: []int{0, 3, 4, 1, 2},
ExpectedCycles: [][]string{{"1", "2", "3", "4", "1"}},
},
{
Name: "Cycle 6",
Children: map[int][]int{
0: {1},
1: {2},
2: {3},
3: {4},
4: {2},
},
ExpectedLevels: []int{0, 1, 2, 3, 1},
ExpectedCycles: [][]string{{"2", "3", "4", "2"}},
},
{
Name: "Avoid same level",
Children: map[int][]int{
Expand Down
10 changes: 10 additions & 0 deletions internal/dep_tree/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func TestRenderGraph(t *testing.T) {
{1},
},
},
{
Name: "Weird cycle combination",
Spec: [][]int{
0: {1},
1: {2},
2: {3},
3: {4},
4: {2},
},
},
{
Name: "Some nodes have errors",
Spec: [][]int{
Expand Down

0 comments on commit 4cdea72

Please sign in to comment.