Skip to content

Commit

Permalink
fix: spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Jan 31, 2024
1 parent f6feb15 commit 2fac6ee
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
16 changes: 8 additions & 8 deletions internal/dep_tree/dep_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type DepTree[T any] struct {
// callbacks
onStartLoading func()
onNodeStartLoad func(*graph.Node[T])
onNodeFinishLoad func([]*graph.Node[T])
onNodeFinishLoad func(*graph.Node[T], []*graph.Node[T])
onFinishLoad func()
// cache
longestPathCache map[string]int
Expand All @@ -51,7 +51,7 @@ func NewDepTree[T any](parser NodeParser[T], ids []string) *DepTree[T] {
Cycles: orderedmap.NewOrderedMap[[2]string, graph.Cycle](),
onStartLoading: func() {},
onNodeStartLoad: func(_ *graph.Node[T]) {},
onNodeFinishLoad: func(_ []*graph.Node[T]) {},
onNodeFinishLoad: func(_ *graph.Node[T], _ []*graph.Node[T]) {},
onFinishLoad: func() {},
longestPathCache: map[string]int{},
}
Expand All @@ -70,20 +70,20 @@ func (dt *DepTree[T]) WithStdErrLoader() *DepTree[T] {
progressbar.OptionSetRenderBlankState(true),
)
diff := make(map[string]bool)
total := 0
done := 0
dt.onStartLoading = func() {
bar.Reset()
}
dt.onNodeStartLoad = func(n *graph.Node[T]) {
total += 1
_ = bar.Set(total)
bar.Describe(fmt.Sprintf("(%d/%d) Loading %s...", total, len(diff), dt.NodeParser.Display(n)))
done += 1
_ = bar.Set(done)
bar.Describe(fmt.Sprintf("(%d/%d) Loading %s...", done, len(diff), dt.NodeParser.Display(n)))
}
dt.onNodeFinishLoad = func(ns []*graph.Node[T]) {
dt.onNodeFinishLoad = func(n *graph.Node[T], ns []*graph.Node[T]) {
for _, n := range ns {
diff[n.Id] = true
}
bar.ChangeMax(len(diff))
bar.Describe(fmt.Sprintf("(%d/%d) Loading %s...", done, len(diff), dt.NodeParser.Display(n)))
}
dt.onFinishLoad = func() {
bar.Describe("Finished loading")
Expand Down
2 changes: 1 addition & 1 deletion internal/dep_tree/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (dt *DepTree[T]) LoadGraph() error {
node.AddErrors(err)
continue
}
dt.onNodeFinishLoad(deps)
dt.onNodeFinishLoad(node, deps)

for _, dep := range deps {
// No own child.
Expand Down
41 changes: 41 additions & 0 deletions internal/dep_tree/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"
"testing"

"github.com/gabotechs/dep-tree/internal/graph"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -44,6 +45,46 @@ func TestLoadDeps_ErrorHandle(t *testing.T) {
a.ErrorContains(node2.Errors[0], "no negative children")
}

func TestLoadDeps_Callbacks(t *testing.T) {
a := require.New(t)

testGraph := &TestParser{
Spec: [][]int{
0: {1},
1: {2},
2: {0, 3},
3: {4},
4: {3},
5: {},
},
}

dt := NewDepTree[[]int](testGraph, []string{"5", "0"})
startLoad := 0
startNode := 0
finishLoad := 0
finishNode := 0
dt.onStartLoading = func() {
startLoad++
}
dt.onFinishLoad = func() {
finishLoad++
}
dt.onNodeStartLoad = func(_ *graph.Node[[]int]) {
startNode++
}
dt.onNodeFinishLoad = func(_ *graph.Node[[]int], _ []*graph.Node[[]int]) {
finishNode++
}
err := dt.LoadGraph()
a.NoError(err)

a.Equal(1, startLoad)
a.Equal(6, startNode)
a.Equal(1, finishLoad)
a.Equal(6, finishNode)
}

func TestLoadDeps_loadGraph(t *testing.T) {
tests := []struct {
Name string
Expand Down

0 comments on commit 2fac6ee

Please sign in to comment.