Skip to content

Commit 530f2b3

Browse files
Backport of Colorize diag details into v1.14 (#38148)
1 parent e168d93 commit 530f2b3

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

internal/command/format/diagnostic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func DiagnosticFromJSON(diag *viewsjson.Diagnostic, color *colorstring.Colorize,
8888
lines := strings.Split(diag.Detail, "\n")
8989
for _, line := range lines {
9090
if !strings.HasPrefix(line, " ") {
91-
line = wordwrap.WrapString(line, uint(paraWidth))
91+
line = wordwrap.WrapString(color.Color(line), uint(paraWidth))
9292
}
9393
fmt.Fprintf(&buf, "%s\n", line)
9494
}
@@ -162,7 +162,7 @@ func DiagnosticPlainFromJSON(diag *viewsjson.Diagnostic, width int) string {
162162
lines := strings.Split(diag.Detail, "\n")
163163
for _, line := range lines {
164164
if !strings.HasPrefix(line, " ") {
165-
line = wordwrap.WrapString(line, uint(width-1))
165+
line = wordwrap.WrapString(disabledColorize.Color(line), uint(width-1))
166166
}
167167
fmt.Fprintf(&buf, "%s\n", line)
168168
}

internal/command/init_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,69 @@ prompts.
15521552

15531553
}
15541554

1555+
func TestInit_cloudConfigColorTokensProcessed(t *testing.T) {
1556+
// This test verifies that when the error
1557+
// diagnostic detail contains color formatting tokens like [bold] and
1558+
// [reset], they are properly processed
1559+
// by the diagnostic formatter and do not appear as literal text in the
1560+
// output.
1561+
td := t.TempDir()
1562+
testCopyDir(t, testFixturePath("init-cloud-no-workspaces"), td)
1563+
t.Chdir(td)
1564+
1565+
ui := cli.NewMockUi()
1566+
view, done := testView(t)
1567+
c := &InitCommand{
1568+
Meta: Meta{
1569+
Ui: ui,
1570+
View: view,
1571+
},
1572+
}
1573+
1574+
args := []string{}
1575+
code := c.Run(args)
1576+
if code == 0 {
1577+
t.Fatalf("expected error, got success\n%s", done(t).Stdout())
1578+
}
1579+
1580+
gotStderr := done(t).Stderr()
1581+
1582+
expected := `
1583+
Error: failed to create backend alias to target "". The hostname is not in the correct format.
1584+
1585+
1586+
Error: Invalid workspaces configuration
1587+
1588+
on main.tf line 7, in terraform:
1589+
7: cloud {
1590+
1591+
Missing workspace mapping strategy. Either workspace "tags" or "name" is
1592+
required.
1593+
1594+
The 'workspaces' block configures how Terraform CLI maps its workspaces for
1595+
this single
1596+
configuration to workspaces within an HCP Terraform or Terraform Enterprise
1597+
organization. Two strategies are available:
1598+
1599+
tags - A set of tags used to select remote HCP Terraform or Terraform
1600+
Enterprise workspaces to be used for this single
1601+
configuration. New workspaces will automatically be tagged with these tag
1602+
values. Generally, this
1603+
is the primary and recommended strategy to use. This option conflicts with
1604+
"name".
1605+
1606+
name - The name of a single HCP Terraform or Terraform Enterprise workspace
1607+
to be used with this configuration.
1608+
When configured, only the specified workspace can be used. This option
1609+
conflicts with "tags"
1610+
and with the TF_WORKSPACE environment variable.
1611+
`
1612+
1613+
if diff := cmp.Diff(gotStderr, expected); diff != "" {
1614+
t.Errorf("unexpected output (-got +expected):\n%s", diff)
1615+
}
1616+
}
1617+
15551618
// make sure inputFalse stops execution on migrate
15561619
func TestInit_inputFalse(t *testing.T) {
15571620
td := t.TempDir()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This is a configuration with HCP Terraform mode activated but without
2+
# a workspaces block, which should trigger an "Invalid workspaces configuration"
3+
# error during initialization. This is used to test that the diagnostic
4+
# formatting correctly processes color tokens in the error detail message.
5+
6+
terraform {
7+
cloud {
8+
organization = "PLACEHOLDER"
9+
}
10+
}

0 commit comments

Comments
 (0)