Skip to content

Commit

Permalink
graph/encoding/dot: ensure that generated DOT is syntactically valid
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Jan 14, 2019
1 parent 24f0d08 commit e7f0778
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions graph/encoding/dot/encode_test.go
Expand Up @@ -5,6 +5,8 @@
package dot

import (
"bytes"
"os/exec"
"testing"

"gonum.org/v1/gonum/graph"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}
21 changes: 21 additions & 0 deletions graph/encoding/graphql/decode_test.go
Expand Up @@ -5,8 +5,10 @@
package graphql

import (
"bytes"
"errors"
"fmt"
"os/exec"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

0 comments on commit e7f0778

Please sign in to comment.