graph/encoding/dot: ensure that generated DOT is syntactically valid (#795)

This commit is contained in:
Dan Kortschak
2019-01-14 22:42:24 +10:30
committed by Robin Eklind
parent bb6edb12aa
commit 03f4920c3e
3 changed files with 48 additions and 0 deletions

View File

@@ -34,6 +34,11 @@ before_install:
# Required for dot parser checks. # Required for dot parser checks.
- ./.travis/install-gocc.sh 0e2cfc030005b281b2e5a2de04fa7fe1d5063722 - ./.travis/install-gocc.sh 0e2cfc030005b281b2e5a2de04fa7fe1d5063722
addons:
apt:
packages:
- graphviz
go_import_path: gonum.org/v1/gonum go_import_path: gonum.org/v1/gonum
# Get deps, build, test, and ensure the code is gofmt'ed. # Get deps, build, test, and ensure the code is gofmt'ed.

View File

@@ -5,6 +5,8 @@
package dot package dot
import ( import (
"bytes"
"os/exec"
"testing" "testing"
"gonum.org/v1/gonum/graph" "gonum.org/v1/gonum/graph"
@@ -1474,6 +1476,7 @@ func TestEncode(t *testing.T) {
if string(got) != test.want { if string(got) != test.want {
t.Errorf("unexpected DOT result for test %d:\ngot: %s\nwant:%s", i, got, test.want) t.Errorf("unexpected DOT result for test %d:\ngot: %s\nwant:%s", i, got, test.want)
} }
checkDOT(t, got)
} }
} }
@@ -1729,5 +1732,24 @@ func TestEncodeMulti(t *testing.T) {
if string(got) != test.want { if string(got) != test.want {
t.Errorf("unexpected DOT result for test %d:\ngot: %s\nwant:%s", i, got, test.want) t.Errorf("unexpected DOT result for test %d:\ngot: %s\nwant:%s", i, got, test.want)
} }
checkDOT(t, got)
}
}
// checkDOT hands b to the dot executable if it exists and fails t if dot
// returns an error.
func checkDOT(t *testing.T, b []byte) {
dot, err := exec.LookPath("dot")
if err != nil {
t.Logf("skipping DOT syntax check: %v", err)
return
}
cmd := exec.Command(dot)
cmd.Stdin = bytes.NewReader(b)
stderr := &bytes.Buffer{}
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
t.Errorf("invalid DOT syntax: %v\n%s\ninput:\n%s", err, stderr.String(), b)
} }
} }

View File

@@ -5,8 +5,10 @@
package graphql package graphql
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"os/exec"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -147,6 +149,7 @@ func TestDecode(t *testing.T) {
if gotDOT != test.wantDOT { if gotDOT != test.wantDOT {
t.Errorf("unexpected DOT encoding for %q:\ngot:\n%s\nwant:\n%s", test.name, gotDOT, test.wantDOT) t.Errorf("unexpected DOT encoding for %q:\ngot:\n%s\nwant:\n%s", test.name, gotDOT, test.wantDOT)
} }
checkDOT(t, b)
} }
} }
@@ -219,3 +222,21 @@ func (a attributes) Attributes() []encoding.Attribute {
} }
return attr return attr
} }
// checkDOT hands b to the dot executable if it exists and fails t if dot
// returns an error.
func checkDOT(t *testing.T, b []byte) {
dot, err := exec.LookPath("dot")
if err != nil {
t.Logf("skipping DOT syntax check: %v", err)
return
}
cmd := exec.Command(dot)
cmd.Stdin = bytes.NewReader(b)
stderr := &bytes.Buffer{}
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
t.Errorf("invalid DOT syntax: %v\n%s\ninput:\n%s", err, stderr.String(), b)
}
}