Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape labels when generating graph #557

Merged
merged 1 commit into from
Sep 5, 2020
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
13 changes: 12 additions & 1 deletion 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(labels, `\l`))
fmt.Fprintf(b, ` label="%s\l"`, strings.Join(escapeForDot(labels), `\l`))
if b.config.LegendURL != "" {
fmt.Fprintf(b, ` URL="%s" target="_blank"`, b.config.LegendURL)
}
Expand Down Expand Up @@ -472,3 +472,14 @@ 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 {
var out = make([]string, len(in))
for i := range in {
out[i] = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(in[i], `\`, `\\`), `"`, `\"`), "\n", `\l`)
}
return out
}
42 changes: 41 additions & 1 deletion internal/graph/dotgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func baseAttrsAndConfig() (*DotAttributes, *DotConfig) {
}
c := &DotConfig{
Title: "testtitle",
Labels: []string{"label1", "label2"},
Labels: []string{"label1", "label2", `label3: "foo"`},
Total: 100,
FormatValue: func(v int64) string {
return strconv.FormatInt(v, 10)
Expand Down Expand Up @@ -326,6 +326,46 @@ func TestTagCollapse(t *testing.T) {
}
}

func TestEscapeForDot(t *testing.T) {
for _, tc := range []struct {
desc string
input []string
want []string
}{
{
desc: "with multiple doubles quotes",
input: []string{`label: "foo" and "bar"`},
want: []string{`label: \"foo\" and \"bar\"`},
},
{
desc: "with graphviz center line character",
input: []string{"label: foo \n bar"},
want: []string{`label: foo \l bar`},
},
{
desc: "with two backslashes",
input: []string{`label: \\`},
want: []string{`label: \\\\`},
},
{
desc: "with two double quotes together",
input: []string{`label: ""`},
want: []string{`label: \"\"`},
},
{
desc: "with multiple labels",
input: []string{`label1: "foo"`, `label2: "bar"`},
want: []string{`label1: \"foo\"`, `label2: \"bar\"`},
},
} {
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)
}
})
}
}

func tagString(t []*Tag) string {
var ret []string
for _, s := range t {
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/testdata/compose1.dot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
digraph "testtitle" {
node [style=filled fillcolor="#f8f8f8"]
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\l" tooltip="testtitle"] }
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\llabel3: \"foo\"\l" tooltip="testtitle"] }
N1 [label="src\n10 (10.00%)\nof 25 (25.00%)" id="node1" fontsize=22 shape=box tooltip="src (25)" color="#b23c00" fillcolor="#edddd5"]
N2 [label="dest\n15 (15.00%)\nof 25 (25.00%)" id="node2" fontsize=24 shape=box tooltip="dest (25)" color="#b23c00" fillcolor="#edddd5"]
N1 -> N2 [label=" 10" weight=11 color="#b28559" tooltip="src -> dest (10)" labeltooltip="src -> dest (10)"]
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/testdata/compose2.dot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
digraph "testtitle" {
node [style=filled fillcolor="#f8f8f8"]
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\l" tooltip="testtitle"] }
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\llabel3: \"foo\"\l" tooltip="testtitle"] }
N1 [label="SRC10 (10.00%)\nof 25 (25.00%)" id="node1" fontsize=24 shape=folder tooltip="src (25)" color="#b23c00" fillcolor="#edddd5" style="bold,filled" peripheries=2 URL="www.google.com" target="_blank"]
N2 [label="dest\n0 of 25 (25.00%)" id="node2" fontsize=8 shape=box tooltip="dest (25)" color="#b23c00" fillcolor="#edddd5"]
N1 -> N2 [label=" 10" weight=11 color="#b28559" tooltip="src -> dest (10)" labeltooltip="src -> dest (10)"]
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/testdata/compose3.dot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
digraph "testtitle" {
node [style=filled fillcolor="#f8f8f8"]
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\l" tooltip="testtitle"] }
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\llabel3: \"foo\"\l" tooltip="testtitle"] }
N1 [label="src\n10 (10.00%)\nof 25 (25.00%)" id="node1" fontsize=22 shape=box tooltip="src (25)" color="#b23c00" fillcolor="#edddd5"]
N1_0 [label = "tag1" id="N1_0" fontsize=8 shape=box3d tooltip="10"]
N1 -> N1_0 [label=" 10" weight=100 tooltip="10" labeltooltip="10"]
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/testdata/compose4.dot
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
digraph "testtitle" {
node [style=filled fillcolor="#f8f8f8"]
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\l" tooltip="testtitle"] }
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\llabel3: \"foo\"\l" tooltip="testtitle"] }
}
2 changes: 1 addition & 1 deletion internal/graph/testdata/compose5.dot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
digraph "testtitle" {
node [style=filled fillcolor="#f8f8f8"]
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\l" tooltip="testtitle"] }
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\llabel3: \"foo\"\l" tooltip="testtitle"] }
N1 [label="src\n10 (10.00%)\nof 25 (25.00%)" id="node1" fontsize=22 shape=box tooltip="src (25)" color="#b23c00" fillcolor="#edddd5"]
N1_0 [label = "tag1" id="N1_0" fontsize=8 shape=box3d tooltip="10"]
N1 -> N1_0 [label=" 10" weight=100 tooltip="10" labeltooltip="10"]
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/testdata/compose6.dot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
digraph "testtitle" {
node [style=filled fillcolor="#f8f8f8"]
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\l" URL="http://example.com" target="_blank" tooltip="testtitle"] }
subgraph cluster_L { "label1" [shape=box fontsize=16 label="label1\llabel2\llabel3: \"foo\"\l" URL="http://example.com" target="_blank" tooltip="testtitle"] }
N1 [label="src\n10 (10.00%)\nof 25 (25.00%)" id="node1" fontsize=22 shape=box tooltip="src (25)" color="#b23c00" fillcolor="#edddd5"]
N2 [label="dest\n15 (15.00%)\nof 25 (25.00%)" id="node2" fontsize=24 shape=box tooltip="dest (25)" color="#b23c00" fillcolor="#edddd5"]
N1 -> N2 [label=" 10" weight=11 color="#b28559" tooltip="src -> dest (10)" labeltooltip="src -> dest (10)"]
Expand Down
2 changes: 1 addition & 1 deletion internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ func reportLabels(rpt *Report, g *graph.Graph, origCount, droppedNodes, droppedE
// Help new users understand the graph.
// A new line is intentionally added here to better show this message.
if fullHeaders {
label = append(label, "\\lSee https://git.io/JfYMW for how to read the graph")
label = append(label, "\nSee https://git.io/JfYMW for how to read the graph")
}

return label
Expand Down