Skip to content

Commit

Permalink
Adding tests and a few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Branislav Milojkovic authored and Branislav Milojkovic committed Jul 27, 2024
1 parent fc25fb8 commit 0818b7c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
17 changes: 11 additions & 6 deletions gnovm/cmd/gno/depgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func execDepGraph(cfg *depGraphCfg, args []string, io commands.IO) error {
allPkgs gnomod.PkgList
)

// first we walk through all the folders and collect all packages
for _, arg := range args {
pkgs, err := gnomod.ListPkgs(arg)
if err != nil {
Expand All @@ -68,20 +69,22 @@ func execDepGraph(cfg *depGraphCfg, args []string, io commands.IO) error {
allPkgs = append(allPkgs, pkgs...)
}

//make one big graph (eg. for the entire examples/ dir)
// make one big graph (eg. for the entire examples/ dir)
if !multipleGraphs {
nodeData := "" //nodes
graphData := "" //edges

//subgraph for .../p/...
// we need subgraphs to make columns in the layout and for colors

// subgraph for .../p/...
nodeData += "subgraph {\nrank=same\n"
for _, pkg := range allPkgs {
if strings.Contains(pkg.Name, "gno.land/p") {
nodeData += "\"" + pkg.Name + "\" [color=\"blue\"]\n"
}
}

//subgraph for .../r/...
// subgraph for .../r/...
nodeData += "}\nsubgraph {\nrank=same\n"
for _, pkg := range allPkgs {
if strings.Contains(pkg.Name, "gno.land/r") {
Expand All @@ -91,7 +94,6 @@ func execDepGraph(cfg *depGraphCfg, args []string, io commands.IO) error {
nodeData += "}"

for _, pkg := range allPkgs {

err := buildGraphData(pkg, allPkgs, make(map[string]bool), make(map[string]bool), &graphData)

if err != nil {
Expand All @@ -106,14 +108,16 @@ func execDepGraph(cfg *depGraphCfg, args []string, io commands.IO) error {
graphFileData := fmt.Sprintf("Digraph G {\nrankdir=\"LR\"\nranksep=20\n%s\n%s\n}", nodeData, graphData)
file.Write([]byte(graphFileData))
file.Close()
} else { //useful for testing - makes a separate graph for each found package
} else { // useful for testing - makes a separate graph for each found package
if !osm.DirExists(output) {
err := os.MkdirAll(output, os.ModePerm)
if err != nil {
return fmt.Errorf("couldn't make output dir: %w", err)

Check warning on line 115 in gnovm/cmd/gno/depgraph.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/depgraph.go#L108-L115

Added lines #L108 - L115 were not covered by tests
}
}

// decided not to use colors or layouts here since it's a simple graph

for _, pkg := range allPkgs {
pkgPath, err := filepath.Abs(pkg.Dir)
if err != nil {
Expand Down Expand Up @@ -156,6 +160,7 @@ func execDepGraph(cfg *depGraphCfg, args []string, io commands.IO) error {
return nil

Check warning on line 160 in gnovm/cmd/gno/depgraph.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/depgraph.go#L160

Added line #L160 was not covered by tests
}

// walk through all requires recursively and note dependencies
func buildGraphData(pkg gnomod.Pkg, allPkgs []gnomod.Pkg, visited map[string]bool, onStack map[string]bool, graphData *string) error {
if onStack[pkg.Name] {
return fmt.Errorf("cycle detected: %s", pkg.Name)

Check warning on line 166 in gnovm/cmd/gno/depgraph.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/depgraph.go#L166

Added line #L166 was not covered by tests
Expand All @@ -178,7 +183,7 @@ func buildGraphData(pkg gnomod.Pkg, allPkgs []gnomod.Pkg, visited map[string]boo
return err

Check warning on line 183 in gnovm/cmd/gno/depgraph.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/depgraph.go#L183

Added line #L183 was not covered by tests
}
found = true
//this check is wildly inefficient. should change graph data to map, then convert to string at end
// this check is wildly inefficient - change this to not work with strings directly
if !strings.Contains(*graphData, "\""+pkg.Name+"\" -> \""+req+"\"\n") {
*graphData += "\"" + pkg.Name + "\" -> \"" + req + "\"\n"
}
Expand Down
25 changes: 25 additions & 0 deletions gnovm/cmd/gno/depgraph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "testing"

func TestGnoDepGraph(t *testing.T) {
tc := []testMainCase{
{
// lacking input
args: []string{"depgraph"},
errShouldBe: "flag: help requested",
},
{
// input is not a package
args: []string{"depgraph", "supercalifragilisticexpialidocious"},
errShouldContain: "error in parsing gno.mod",
},
{
// given input where some requires are missing

args: []string{"depgraph", "../../../examples/gno.land/p/"},
errShouldContain: "error in building graph",
},
}
testMainCaseRun(t, tc)
}

0 comments on commit 0818b7c

Please sign in to comment.