Skip to content

Commit eac81e5

Browse files
committed
fix: use toast formatting for terraform test results
1 parent 976f5e8 commit eac81e5

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

cmd/terraform/test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/cloudposse/atmos/pkg/flags"
1919
h "github.com/cloudposse/atmos/pkg/hooks"
2020
"github.com/cloudposse/atmos/pkg/ui"
21+
"github.com/cloudposse/atmos/pkg/ui/theme"
2122
)
2223

2324
// testParser handles flag parsing for the test command.
@@ -172,25 +173,33 @@ func formatTerraformTestStatusLine(line string) string {
172173
if len(body) != len(line) {
173174
ending = terraformTestOutputNewline
174175
}
175-
if !strings.Contains(ansi.Strip(body), "...") {
176+
plain := ansi.Strip(body)
177+
if !strings.Contains(plain, "...") {
176178
return line
177179
}
178-
match := terraformTestStatusSuffixRE.FindStringSubmatchIndex(body)
180+
match := terraformTestStatusSuffixRE.FindStringSubmatchIndex(plain)
179181
if match == nil {
180182
return line
181183
}
182-
status := body[match[2]:match[3]]
183-
prefix := body[:match[0]]
184+
status := plain[match[2]:match[3]]
185+
text := strings.TrimLeft(plain, " \t")
184186
switch status {
185187
case "pass":
186-
return prefix + ui.FormatSuccess(status) + ending
188+
return formatTerraformTestToast(theme.IconCheckmark, text, ending, line)
187189
case "fail", "error":
188-
return prefix + ui.FormatError(status) + ending
190+
return formatTerraformTestToast(theme.IconXMark, text, ending, line)
189191
default:
190192
return line
191193
}
192194
}
193195

196+
func formatTerraformTestToast(icon, text, ending, fallback string) string {
197+
if ui.Format == nil {
198+
return fallback
199+
}
200+
return strings.TrimSuffix(ui.Format.Toast(icon, text), terraformTestOutputNewline) + ending
201+
}
202+
194203
// appendJSONFlag adds `-json` to the terraform test pass-through flags unless it
195204
// is already present, so CI runs emit the machine-readable event stream. Both
196205
// Terraform and OpenTofu support `test -json`, so this is tool-agnostic and does

cmd/terraform/test_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,42 @@ package terraform
22

33
import (
44
"bytes"
5+
stdio "io"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910

1011
"github.com/cloudposse/atmos/pkg/ansi"
12+
iolib "github.com/cloudposse/atmos/pkg/io"
13+
"github.com/cloudposse/atmos/pkg/ui"
1114
)
1215

16+
type terraformTestStreams struct {
17+
stdin stdio.Reader
18+
stdout stdio.Writer
19+
stderr stdio.Writer
20+
}
21+
22+
func (s *terraformTestStreams) Input() stdio.Reader { return s.stdin }
23+
func (s *terraformTestStreams) Output() stdio.Writer { return s.stdout }
24+
func (s *terraformTestStreams) Error() stdio.Writer { return s.stderr }
25+
func (s *terraformTestStreams) RawOutput() stdio.Writer { return s.stdout }
26+
func (s *terraformTestStreams) RawError() stdio.Writer { return s.stderr }
27+
28+
func initTerraformTestUI(t *testing.T) {
29+
t.Helper()
30+
streams := &terraformTestStreams{
31+
stdin: &bytes.Buffer{},
32+
stdout: &bytes.Buffer{},
33+
stderr: &bytes.Buffer{},
34+
}
35+
ioCtx, err := iolib.NewContext(iolib.WithStreams(streams))
36+
require.NoError(t, err)
37+
ui.InitFormatter(ioCtx)
38+
t.Cleanup(ui.Reset)
39+
}
40+
1341
func TestAppendJSONFlag(t *testing.T) {
1442
t.Run("appends when missing", func(t *testing.T) {
1543
got := appendJSONFlag([]string{"-run=smoke"})
@@ -44,22 +72,22 @@ func TestFormatTerraformTestStatusLine(t *testing.T) {
4472
{
4573
name: "success",
4674
line: " run \"ok\"... pass\n",
47-
want: " run \"ok\"... pass\n",
75+
want: "run \"ok\"... pass\n",
4876
},
4977
{
5078
name: "failure",
5179
line: " run \"broken\"... fail\n",
52-
want: " run \"broken\"... fail\n",
80+
want: "run \"broken\"... fail\n",
5381
},
5482
{
5583
name: "error",
5684
line: "tests/app.tftest.hcl... error\n",
57-
want: "tests/app.tftest.hcl... error\n",
85+
want: "tests/app.tftest.hcl... error\n",
5886
},
5987
{
6088
name: "strips terraform status color",
6189
line: "tests/app.tftest.hcl... \x1b[32mpass\x1b[0m\n",
62-
want: "tests/app.tftest.hcl... pass\n",
90+
want: "tests/app.tftest.hcl... pass\n",
6391
},
6492
{
6593
name: "in progress unchanged",
@@ -75,12 +103,14 @@ func TestFormatTerraformTestStatusLine(t *testing.T) {
75103

76104
for _, tt := range tests {
77105
t.Run(tt.name, func(t *testing.T) {
106+
initTerraformTestUI(t)
78107
assert.Equal(t, tt.want, ansi.Strip(formatTerraformTestStatusLine(tt.line)))
79108
})
80109
}
81110
}
82111

83112
func TestTerraformTestStatusWriterBuffersPartialLines(t *testing.T) {
113+
initTerraformTestUI(t)
84114
var out bytes.Buffer
85115
w := newTerraformTestStatusWriter(&out)
86116

@@ -92,5 +122,5 @@ func TestTerraformTestStatusWriterBuffersPartialLines(t *testing.T) {
92122
n, err = w.Write([]byte("ss\n run \"broken\"... fail\n"))
93123
require.NoError(t, err)
94124
assert.Equal(t, len("ss\n run \"broken\"... fail\n"), n)
95-
assert.Equal(t, " run \"ok\"... pass\n run \"broken\"... fail\n", ansi.Strip(out.String()))
125+
assert.Equal(t, "run \"ok\"... pass\nrun \"broken\"... fail\n", ansi.Strip(out.String()))
96126
}

0 commit comments

Comments
 (0)