Skip to content

Commit 3dfc9d9

Browse files
committed
add test
1 parent 04315f5 commit 3dfc9d9

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

internal/command/format/diagnostic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
@@ -1629,6 +1629,69 @@ prompts.
16291629
})
16301630
}
16311631

1632+
func TestInit_cloudConfigColorTokensProcessed(t *testing.T) {
1633+
// This test verifies that when the error
1634+
// diagnostic detail contains color formatting tokens like [bold] and
1635+
// [reset], they are properly processed
1636+
// by the diagnostic formatter and do not appear as literal text in the
1637+
// output.
1638+
td := t.TempDir()
1639+
testCopyDir(t, testFixturePath("init-cloud-no-workspaces"), td)
1640+
t.Chdir(td)
1641+
1642+
ui := cli.NewMockUi()
1643+
view, done := testView(t)
1644+
c := &InitCommand{
1645+
Meta: Meta{
1646+
Ui: ui,
1647+
View: view,
1648+
},
1649+
}
1650+
1651+
args := []string{}
1652+
code := c.Run(args)
1653+
if code == 0 {
1654+
t.Fatalf("expected error, got success\n%s", done(t).Stdout())
1655+
}
1656+
1657+
gotStderr := done(t).Stderr()
1658+
1659+
expected := `
1660+
Error: failed to create backend alias to target "". The hostname is not in the correct format.
1661+
1662+
1663+
Error: Invalid workspaces configuration
1664+
1665+
on main.tf line 7, in terraform:
1666+
7: cloud {
1667+
1668+
Missing workspace mapping strategy. Either workspace "tags" or "name" is
1669+
required.
1670+
1671+
The 'workspaces' block configures how Terraform CLI maps its workspaces for
1672+
this single
1673+
configuration to workspaces within an HCP Terraform or Terraform Enterprise
1674+
organization. Two strategies are available:
1675+
1676+
tags - A set of tags used to select remote HCP Terraform or Terraform
1677+
Enterprise workspaces to be used for this single
1678+
configuration. New workspaces will automatically be tagged with these tag
1679+
values. Generally, this
1680+
is the primary and recommended strategy to use. This option conflicts with
1681+
"name".
1682+
1683+
name - The name of a single HCP Terraform or Terraform Enterprise workspace
1684+
to be used with this configuration.
1685+
When configured, only the specified workspace can be used. This option
1686+
conflicts with "tags"
1687+
and with the TF_WORKSPACE environment variable.
1688+
`
1689+
1690+
if diff := cmp.Diff(gotStderr, expected); diff != "" {
1691+
t.Errorf("unexpected output (-got +expected):\n%s", diff)
1692+
}
1693+
}
1694+
16321695
// make sure inputFalse stops execution on migrate
16331696
func TestInit_inputFalse(t *testing.T) {
16341697
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)