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

MODIFY
graph/encoding/dot/encode_test.go
graph/encoding/dot/encode_test.go
package dot
import (
+ "bytes"
+ "os/exec"
"testing"
"gonum.org/v1/gonum/graph"
@@ -1474,6 +1476,7 @@ func TestEncode(t *testing.T) {
if string(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 {
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)
}
}

MODIFY
graph/encoding/graphql/decode_test.go
graph/encoding/graphql/decode_test.go
package graphql
import (
+ "bytes"
"errors"
"fmt"
+ "os/exec"
"sort"
"strconv"
"strings"
@@ -147,6 +149,7 @@ func TestDecode(t *testing.T) {
if 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
}
+
+// 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)
+ }
+}

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