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

@@ -5,8 +5,10 @@
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)
}
}