Skip to content

Commit

Permalink
Merge pull request #333 from tennashi/fix_dep_tree
Browse files Browse the repository at this point in the history
Fix dep tree
  • Loading branch information
tyru authored Jan 31, 2022
2 parents 6e4b673 + 8d0e3bc commit 146b1fc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 8 deletions.
18 changes: 10 additions & 8 deletions plugconf/plugconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ type reposDepTree struct {

type reposDepNode struct {
repos *lockjson.Repos
dependTo []reposDepNode
dependedBy []reposDepNode
dependTo []*reposDepNode
dependedBy []*reposDepNode
}

// ParseMultiPlugconf parses plugconfs of given reposList.
Expand Down Expand Up @@ -858,11 +858,11 @@ func makeNodes(reposPath pathutil.ReposPath, reposMap map[pathutil.ReposPath]*lo
visited[reposPath] = node
for i := range depsMap[reposPath] {
dep := makeNodes(depsMap[reposPath][i], reposMap, depsMap, rdepsMap, visited)
node.dependTo = append(node.dependTo, *dep)
node.dependTo = append(node.dependTo, dep)
}
for i := range rdepsMap[reposPath] {
rdep := makeNodes(rdepsMap[reposPath][i], reposMap, depsMap, rdepsMap, visited)
node.dependedBy = append(node.dependedBy, *rdep)
node.dependedBy = append(node.dependedBy, rdep)
}
return node
}
Expand All @@ -874,17 +874,19 @@ func visitNode(node *reposDepNode, callback func(*reposDepNode), visited map[pat
visited[node.repos.Path] = true
callback(node)
for i := range node.dependTo {
visitNode(&node.dependTo[i], callback, visited)
visitNode(node.dependTo[i], callback, visited)
}
for i := range node.dependedBy {
visitNode(&node.dependedBy[i], callback, visited)
visitNode(node.dependedBy[i], callback, visited)
}
}

func makeRank(rank map[pathutil.ReposPath]int, node *reposDepNode, value int) {
rank[node.repos.Path] = value
if r, ok := rank[node.repos.Path]; !ok || r < value {
rank[node.repos.Path] = value
}
for i := range node.dependedBy {
makeRank(rank, &node.dependedBy[i], value+1)
makeRank(rank, node.dependedBy[i], value+1)
}
}

Expand Down
94 changes: 94 additions & 0 deletions plugconf/plugconf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package plugconf

import (
"reflect"
"testing"

"github.com/vim-volt/volt/lockjson"
"github.com/vim-volt/volt/pathutil"
)

func TestSortByDepends(t *testing.T) {
type input struct {
reposList []lockjson.Repos
plugconfMap map[pathutil.ReposPath]*ParsedInfo
}

cases := []struct {
input input
want []lockjson.Repos
}{
{
input: input{
reposList: []lockjson.Repos{
{Path: pathutil.DecodeReposPath("test/test-1")},
{Path: pathutil.DecodeReposPath("test/test-2")},
{Path: pathutil.DecodeReposPath("test/test-3")},
},
plugconfMap: map[pathutil.ReposPath]*ParsedInfo{
pathutil.DecodeReposPath("test/test-1"): {
depends: []pathutil.ReposPath{
pathutil.DecodeReposPath("test/test-2"),
},
},
pathutil.DecodeReposPath("test/test-2"): {
depends: []pathutil.ReposPath{
pathutil.DecodeReposPath("test/test-3"),
},
},
pathutil.DecodeReposPath("test/test-3"): {},
},
},
want: []lockjson.Repos{
{Path: pathutil.DecodeReposPath("test/test-3")},
{Path: pathutil.DecodeReposPath("test/test-2")},
{Path: pathutil.DecodeReposPath("test/test-1")},
},
},
{
input: input{
reposList: []lockjson.Repos{
{Path: pathutil.DecodeReposPath("Shougo/ddc-matcher_head")},
{Path: pathutil.DecodeReposPath("Shougo/ddc.vim")},
{Path: pathutil.DecodeReposPath("shun/ddc-vim-lsp")},
{Path: pathutil.DecodeReposPath("vim-denops/denops.vim")},
},
plugconfMap: map[pathutil.ReposPath]*ParsedInfo{
pathutil.DecodeReposPath("vim-denops/denops.vim"): {},
pathutil.DecodeReposPath("Shougo/ddc.vim"): {
depends: []pathutil.ReposPath{
pathutil.DecodeReposPath("vim-denops/denops.vim"),
},
},
pathutil.DecodeReposPath("Shougo/ddc-matcher_head"): {
depends: []pathutil.ReposPath{
pathutil.DecodeReposPath("vim-denops/denops.vim"),
pathutil.DecodeReposPath("Shougo/ddc.vim"),
},
},
pathutil.DecodeReposPath("shun/ddc-vim-lsp"): {
depends: []pathutil.ReposPath{
pathutil.DecodeReposPath("vim-denops/denops.vim"),
pathutil.DecodeReposPath("Shougo/ddc.vim"),
pathutil.DecodeReposPath("Shougo/ddc-matcher_head"),
},
},
},
},
want: []lockjson.Repos{
{Path: pathutil.DecodeReposPath("vim-denops/denops.vim")},
{Path: pathutil.DecodeReposPath("Shougo/ddc.vim")},
{Path: pathutil.DecodeReposPath("Shougo/ddc-matcher_head")},
{Path: pathutil.DecodeReposPath("shun/ddc-vim-lsp")},
},
},
}

for _, tt := range cases {
sortByDepends(tt.input.reposList, tt.input.plugconfMap)

if !reflect.DeepEqual(tt.input.reposList, tt.want) {
t.Fatalf("want: %v, but got: %v", tt.want, tt.input.reposList)
}
}
}

0 comments on commit 146b1fc

Please sign in to comment.