Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Test cases for graphviz
Browse files Browse the repository at this point in the history
Removed strings.HasPrefix() usage
  • Loading branch information
Rhymond committed Apr 1, 2017
1 parent 70d8108 commit 1a64b30
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 25 deletions.
46 changes: 21 additions & 25 deletions cmd/dep/graphviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ func (g graphviz) New() *graphviz {
}

func (g graphviz) output() bytes.Buffer {
g.b.WriteString("digraph { node [shape=box]; ")
g.b.WriteString("digraph {\n\tnode [shape=box];")

for _, gvp := range g.ps {
g.h[gvp.project] = gvp.hash()

// Create node string
g.b.WriteString(fmt.Sprintf("%d [label=\"%s\"];", gvp.hash(), gvp.label()))
g.b.WriteString(fmt.Sprintf("\n\t%d [label=\"%s\"];", gvp.hash(), gvp.label()))
}

// Store relations to avoid duplication
Expand All @@ -48,11 +46,11 @@ func (g graphviz) output() bytes.Buffer {
for _, dp := range g.ps {
for _, bsc := range dp.children {
for pr, hsh := range g.h {
if strings.HasPrefix(bsc, pr) && isPathPrefixOrEqual(pr, bsc) {
r := fmt.Sprintf("%d -> %d", g.h[dp.project], hsh)
if isPathPrefix(bsc, pr) {
r := fmt.Sprintf("\n\t%d -> %d", g.h[dp.project], hsh)

if _, ex := rels[r]; !ex {
g.b.WriteString(r + "; ")
g.b.WriteString(r + ";")
rels[r] = true
}

Expand All @@ -61,18 +59,18 @@ func (g graphviz) output() bytes.Buffer {
}
}

g.b.WriteString("}")

g.b.WriteString("\n}")
return g.b
}

func (g *graphviz) createNode(p, v string, c []string) {
func (g *graphviz) createNode(project, version string, children []string) {
pr := &gvnode{
project: p,
version: v,
children: c,
project: project,
version: version,
children: children,
}

g.h[pr.project] = pr.hash()
g.ps = append(g.ps, pr)
}

Expand All @@ -89,26 +87,24 @@ func (dp gvnode) label() string {
label = append(label, dp.version)
}

return strings.Join(label, "\n")
return strings.Join(label, "\\n")
}

// Ensure that the literal string prefix is a path tree match and
// guard against possibilities like this:
// isPathPrefix ensures that the literal string prefix is a path tree match and
// guards against possibilities like this:
//
// github.com/sdboyer/foo
// github.com/sdboyer/foobar/baz
//
// Verify that either the input is the same length as the match (in which
// case we know they're equal), or that the next character is a "/". (Import
// paths are defined to always use "/", not the OS-specific path separator.)
func isPathPrefixOrEqual(pre, path string) bool {
prflen, pathlen := len(pre), len(path)
if pathlen == prflen+1 {
// this can never be the case
// Verify that prefix is path match and either the input is the same length as
// the match (in which case we know they're equal), or that the next character
// is a "/". (Import paths are defined to always use "/", not the OS-specific
// path separator.)
func isPathPrefix(path, pre string) bool {
pathlen, prflen := len(path), len(pre)
if pathlen < prflen || path[0:prflen] != pre || pathlen == prflen+1 {
return false
}

// we assume something else (a trie) has done equality check up to the point
// of the prefix, so we just check len
return prflen == pathlen || strings.Index(path[prflen:], "/") == 0
}
75 changes: 75 additions & 0 deletions cmd/dep/graphviz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"testing"

"github.com/golang/dep/test"
)

func TestEmptyProject(t *testing.T) {
g := new(graphviz).New()
h := test.NewHelper(t)
defer h.Cleanup()

b := g.output()
expected := h.GetTestFileString("graphviz/empty.dot")

if b.String() != expected {
t.Fatalf("expected '%v', got '%v'", expected, b.String())
}
}

func TestSimpleProject(t *testing.T) {
g := new(graphviz).New()
h := test.NewHelper(t)
defer h.Cleanup()

g.createNode("project", "", []string{"foo", "bar"})
g.createNode("foo", "master", []string{"bar"})
g.createNode("bar", "dev", []string{})

b := g.output()
expected := h.GetTestFileString("graphviz/case1.dot")
if b.String() != expected {
t.Fatalf("expected '%v', got '%v'", expected, b.String())
}
}

func TestNoLinks(t *testing.T) {
g := new(graphviz).New()
h := test.NewHelper(t)
defer h.Cleanup()

g.createNode("project", "", []string{})

b := g.output()
expected := h.GetTestFileString("graphviz/case2.dot")
if b.String() != expected {
t.Fatalf("expected '%v', got '%v'", expected, b.String())
}
}

func TestIsPathPrefix(t *testing.T) {
tcs := []struct {
path string
pre string
expected bool
}{
{"github.com/sdboyer/foo/bar", "github.com/sdboyer/foo", true},
{"github.com/sdboyer/foobar", "github.com/sdboyer/foo", false},
{"github.com/sdboyer/bar/foo", "github.com/sdboyer/foo", false},
{"golang.org/sdboyer/bar/foo", "github.com/sdboyer/foo", false},
{"golang.org/sdboyer/FOO", "github.com/sdboyer/foo", false},
}

for _, tc := range tcs {
r := isPathPrefix(tc.path, tc.pre)
if tc.expected != r {
t.Fatalf("expected '%v', got '%v'", tc.expected, r)
}
}
}
9 changes: 9 additions & 0 deletions cmd/dep/testdata/graphviz/case1.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
digraph {
node [shape=box];
4106060478 [label="project"];
2851307223 [label="foo\nmaster"];
1991736602 [label="bar\ndev"];
4106060478 -> 2851307223;
4106060478 -> 1991736602;
2851307223 -> 1991736602;
}
4 changes: 4 additions & 0 deletions cmd/dep/testdata/graphviz/case2.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
digraph {
node [shape=box];
4106060478 [label="project"];
}
3 changes: 3 additions & 0 deletions cmd/dep/testdata/graphviz/empty.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
digraph {
node [shape=box];
}

0 comments on commit 1a64b30

Please sign in to comment.