From 3547f9e368185121528494536ddf9fc35643aa2b Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Tue, 1 Sep 2020 13:58:23 -0400 Subject: [PATCH] format: Don't wrap space-prefixed diag details Diagnostic detail lines sometimes contain lines which include commands suggested for the user to execute. By convention, these start with leading whitespace to indicate that they are not prose. This commit changes the diagnostic formatter to wrap each line of the detail separately, and skips word wrapping for lines prefixed with space. This prevents ugly and confusing wrapping of long command lines. --- command/format/diagnostic.go | 12 +++++++++--- command/format/diagnostic_test.go | 32 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/command/format/diagnostic.go b/command/format/diagnostic.go index fa7584a4b..6ceb30894 100644 --- a/command/format/diagnostic.go +++ b/command/format/diagnostic.go @@ -175,11 +175,17 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color } if desc.Detail != "" { - detail := desc.Detail if width != 0 { - detail = wordwrap.WrapString(detail, uint(width)) + lines := strings.Split(desc.Detail, "\n") + for _, line := range lines { + if !strings.HasPrefix(line, " ") { + line = wordwrap.WrapString(line, uint(width)) + } + fmt.Fprintf(&buf, "%s\n", line) + } + } else { + fmt.Fprintf(&buf, "%s\n", desc.Detail) } - fmt.Fprintf(&buf, "%s\n", detail) } return buf.String() diff --git a/command/format/diagnostic_test.go b/command/format/diagnostic_test.go index 98b879d38..2062f633c 100644 --- a/command/format/diagnostic_test.go +++ b/command/format/diagnostic_test.go @@ -167,3 +167,35 @@ Error: Some error t.Fatalf("unexpected output: got:\n%s\nwant\n%s\n", output, expected) } } + +func TestDiagnostic_wrapDetailIncludingCommand(t *testing.T) { + var diags tfdiags.Diagnostics + + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Everything went wrong", + Detail: "This is a very long sentence about whatever went wrong which is supposed to wrap onto multiple lines. Thank-you very much for listening.\n\nTo fix this, run this very long command:\n terraform read-my-mind -please -thanks -but-do-not-wrap-this-line-because-it-is-prefixed-with-spaces\n\nHere is a coda which is also long enough to wrap and so it should eventually make it onto multiple lines. THE END", + }) + color := &colorstring.Colorize{ + Colors: colorstring.DefaultColors, + Reset: true, + Disable: true, + } + expected := ` +Error: Everything went wrong + +This is a very long sentence about whatever went wrong which is supposed to +wrap onto multiple lines. Thank-you very much for listening. + +To fix this, run this very long command: + terraform read-my-mind -please -thanks -but-do-not-wrap-this-line-because-it-is-prefixed-with-spaces + +Here is a coda which is also long enough to wrap and so it should eventually +make it onto multiple lines. THE END +` + output := Diagnostic(diags[0], nil, color, 76) + + if output != expected { + t.Fatalf("unexpected output: got:\n%s\nwant\n%s\n", output, expected) + } +}