Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions internal/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,14 @@ func TestParse(t *testing.T) {
t.Fatalf("reading solution file %s: %v", solution, err)
}
if runtime.GOOS == "windows" {
sbuf = bytes.Replace(sbuf, []byte("testdata/"), []byte("testdata\\"), -1)
sbuf = bytes.Replace(sbuf, []byte("/path/to/"), []byte("\\path\\to\\"), -1)
if flags[0] == "dot" {
// The .dot test has the paths inside strings, so \ must be escaped.
sbuf = bytes.Replace(sbuf, []byte("testdata/"), []byte(`testdata\\`), -1)
sbuf = bytes.Replace(sbuf, []byte("/path/to/"), []byte(`\\path\\to\\`), -1)
} else {
sbuf = bytes.Replace(sbuf, []byte("testdata/"), []byte(`testdata\`), -1)
sbuf = bytes.Replace(sbuf, []byte("/path/to/"), []byte(`\path\to\`), -1)
}
}

if flags[0] == "svg" {
Expand Down
24 changes: 15 additions & 9 deletions internal/graph/dotgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (b *builder) addLegend() {
}
title := labels[0]
fmt.Fprintf(b, `subgraph cluster_L { "%s" [shape=box fontsize=16`, title)
fmt.Fprintf(b, ` label="%s\l"`, strings.Join(escapeForDot(labels), `\l`))
fmt.Fprintf(b, ` label="%s\l"`, strings.Join(escapeAllForDot(labels), `\l`))
if b.config.LegendURL != "" {
fmt.Fprintf(b, ` URL="%s" target="_blank"`, b.config.LegendURL)
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (b *builder) addNode(node *Node, nodeID int, maxFlat float64) {

// Create DOT attribute for node.
attr := fmt.Sprintf(`label="%s" id="node%d" fontsize=%d shape=%s tooltip="%s (%s)" color="%s" fillcolor="%s"`,
label, nodeID, fontSize, shape, node.Info.PrintableName(), cumValue,
label, nodeID, fontSize, shape, escapeForDot(node.Info.PrintableName()), cumValue,
dotColor(float64(node.CumValue())/float64(abs64(b.config.Total)), false),
dotColor(float64(node.CumValue())/float64(abs64(b.config.Total)), true))

Expand Down Expand Up @@ -305,7 +305,8 @@ func (b *builder) addEdge(edge *Edge, from, to int, hasNodelets bool) {
arrow = "..."
}
tooltip := fmt.Sprintf(`"%s %s %s (%s)"`,
edge.Src.Info.PrintableName(), arrow, edge.Dest.Info.PrintableName(), w)
escapeForDot(edge.Src.Info.PrintableName()), arrow,
escapeForDot(edge.Dest.Info.PrintableName()), w)
attr = fmt.Sprintf(`%s tooltip=%s labeltooltip=%s`, attr, tooltip, tooltip)

if edge.Residual {
Expand Down Expand Up @@ -382,7 +383,7 @@ func dotColor(score float64, isBackground bool) string {

func multilinePrintableName(info *NodeInfo) string {
infoCopy := *info
infoCopy.Name = ShortenFunctionName(infoCopy.Name)
infoCopy.Name = escapeForDot(ShortenFunctionName(infoCopy.Name))
infoCopy.Name = strings.Replace(infoCopy.Name, "::", `\n`, -1)
infoCopy.Name = strings.Replace(infoCopy.Name, ".", `\n`, -1)
if infoCopy.File != "" {
Expand Down Expand Up @@ -473,13 +474,18 @@ func min64(a, b int64) int64 {
return b
}

// escapeForDot escapes double quotes and backslashes, and replaces Graphviz's
// "center" character (\n) with a left-justified character.
// See https://graphviz.org/doc/info/attrs.html#k:escString for more info.
func escapeForDot(in []string) []string {
// escapeAllForDot applies escapeForDot to all strings in the given slice.
func escapeAllForDot(in []string) []string {
var out = make([]string, len(in))
for i := range in {
out[i] = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(in[i], `\`, `\\`), `"`, `\"`), "\n", `\l`)
out[i] = escapeForDot(in[i])
}
return out
}

// escapeForDot escapes double quotes and backslashes, and replaces Graphviz's
// "center" character (\n) with a left-justified character.
// See https://graphviz.org/doc/info/attrs.html#k:escString for more info.
func escapeForDot(str string) string {
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(str, `\`, `\\`), `"`, `\"`), "\n", `\l`)
}
16 changes: 14 additions & 2 deletions internal/graph/dotgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ func TestComposeWithStandardGraphAndURL(t *testing.T) {
compareGraphs(t, buf.Bytes(), "compose6.dot")
}

func TestComposeWithNamesThatNeedEscaping(t *testing.T) {
g := baseGraph()
a, c := baseAttrsAndConfig()
g.Nodes[0].Info = NodeInfo{Name: `var"src"`}
g.Nodes[1].Info = NodeInfo{Name: `var"#dest#"`}

var buf bytes.Buffer
ComposeDot(&buf, g, a, c)

compareGraphs(t, buf.Bytes(), "compose7.dot")
}

func baseGraph() *Graph {
src := &Node{
Info: NodeInfo{Name: "src"},
Expand Down Expand Up @@ -359,8 +371,8 @@ func TestEscapeForDot(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
if got := escapeForDot(tc.input); !reflect.DeepEqual(got, tc.want) {
t.Errorf("escapeForDot(%s) = %s, want %s", tc.input, got, tc.want)
if got := escapeAllForDot(tc.input); !reflect.DeepEqual(got, tc.want) {
t.Errorf("escapeAllForDot(%s) = %s, want %s", tc.input, got, tc.want)
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions internal/graph/testdata/compose7.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
digraph "testtitle" {
node [style=filled fillcolor="#f8f8f8"]
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\llabel3: \"foo\"\l" tooltip="testtitle"] }
N1 [label="var\"src\"\n10 (10.00%)\nof 25 (25.00%)" id="node1" fontsize=22 shape=box tooltip="var\"src\" (25)" color="#b23c00" fillcolor="#edddd5"]
N2 [label="var\"#dest#\"\n15 (15.00%)\nof 25 (25.00%)" id="node2" fontsize=24 shape=box tooltip="var\"#dest#\" (25)" color="#b23c00" fillcolor="#edddd5"]
N1 -> N2 [label=" 10" weight=11 color="#b28559" tooltip="var\"src\" -> var\"#dest#\" (10)" labeltooltip="var\"src\" -> var\"#dest#\" (10)"]
}
7 changes: 6 additions & 1 deletion internal/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ func TestSource(t *testing.T) {
t.Fatalf("%s: %v", tc.want, err)
}
if runtime.GOOS == "windows" {
gold = bytes.Replace(gold, []byte("testdata/"), []byte("testdata\\"), -1)
if tc.rpt.options.OutputFormat == Dot {
// The .dot test has the paths inside strings, so \ must be escaped.
gold = bytes.Replace(gold, []byte("testdata/"), []byte(`testdata\\`), -1)
} else {
gold = bytes.Replace(gold, []byte("testdata/"), []byte(`testdata\`), -1)
}
}
if string(b.String()) != string(gold) {
d, err := proftest.Diff(gold, b.Bytes())
Expand Down