From e36d300efddc01230b0ff525ad550e5e49a7e85c Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 15 Jun 2020 13:23:12 -0400 Subject: [PATCH 1/2] quote dot node names providers can contain quotes, so we need to ensure proper quoting in the dot format. --- dag/marshal.go | 7 ++++++- dag/marshal_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/dag/marshal.go b/dag/marshal.go index ebb8a0a634..0ad45e8cb5 100644 --- a/dag/marshal.go +++ b/dag/marshal.go @@ -108,9 +108,14 @@ func newMarshalVertex(v Vertex) *marshalVertex { dn = nil } + // the name will be quoted again later, so we need to ensure it's properly + // escaped without quotes. + name := strconv.Quote(VertexName(v)) + name = name[1 : len(name)-1] + return &marshalVertex{ ID: marshalVertexID(v), - Name: VertexName(v), + Name: name, Attrs: make(map[string]string), graphNodeDotter: dn, } diff --git a/dag/marshal_test.go b/dag/marshal_test.go index a7e468dc1e..79f1c648ea 100644 --- a/dag/marshal_test.go +++ b/dag/marshal_test.go @@ -32,6 +32,21 @@ func TestGraphDot_basic(t *testing.T) { } } +func TestGraphDot_quoted(t *testing.T) { + var g Graph + quoted := `name["with-quotes"]` + other := `other` + g.Add(quoted) + g.Add(other) + g.Connect(BasicEdge(quoted, other)) + + actual := strings.TrimSpace(string(g.Dot(nil))) + expected := strings.TrimSpace(testGraphDotQuotedStr) + if actual != expected { + t.Fatalf("\ngot: %q\nwanted %q\n", actual, expected) + } +} + func TestGraphDot_attrs(t *testing.T) { var g Graph g.Add(&testGraphNodeDotter{ @@ -53,6 +68,14 @@ type testGraphNodeDotter struct{ Result *DotNode } func (n *testGraphNodeDotter) Name() string { return n.Result.Name } func (n *testGraphNodeDotter) DotNode(string, *DotOpts) *DotNode { return n.Result } +const testGraphDotQuotedStr = `digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] name[\"with-quotes\"]" -> "[root] other" + } +}` + const testGraphDotBasicStr = `digraph { compound = "true" newrank = "true" From b35a56cf6eeadb2d5b38b8602d8a39f82e266c31 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 15 Jun 2020 14:39:16 -0400 Subject: [PATCH 2/2] command tests looked for providers in the graphs The quotes in provider names needs to be escaped, so these tests were looking for the wrong output to begin with. --- command/graph_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/command/graph_test.go b/command/graph_test.go index 6600a7eaba..bb54d2d7a3 100644 --- a/command/graph_test.go +++ b/command/graph_test.go @@ -33,7 +33,7 @@ func TestGraph(t *testing.T) { } output := ui.OutputWriter.String() - if !strings.Contains(output, `provider["registry.terraform.io/hashicorp/test"]`) { + if !strings.Contains(output, `provider[\"registry.terraform.io/hashicorp/test\"]`) { t.Fatalf("doesn't look like digraph: %s", output) } } @@ -80,7 +80,7 @@ func TestGraph_noArgs(t *testing.T) { } output := ui.OutputWriter.String() - if !strings.Contains(output, `provider["registry.terraform.io/hashicorp/test"]`) { + if !strings.Contains(output, `provider[\"registry.terraform.io/hashicorp/test\"]`) { t.Fatalf("doesn't look like digraph: %s", output) } } @@ -161,7 +161,7 @@ func TestGraph_plan(t *testing.T) { } output := ui.OutputWriter.String() - if !strings.Contains(output, `provider["registry.terraform.io/hashicorp/test"]`) { + if !strings.Contains(output, `provider[\"registry.terraform.io/hashicorp/test\"]`) { t.Fatalf("doesn't look like digraph: %s", output) } }