Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/azd/pkg/input/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ func (c *AskerConsole) IsUnformatted() bool {
func (c *AskerConsole) Message(ctx context.Context, message string) {
// Disable output when formatting is enabled
if c.formatter != nil && c.formatter.Kind() == output.JsonFormat {
// Empty messages are visual separators (blank lines) in text mode.
// In JSON mode they have no semantic value, so skip them.
if message == "" {
return
}

// we call json.Marshal directly, because the formatter marshalls using indentation, and we would prefer
// these objects be written on a single line.
var obj any = output.EventForMessage(message)
Expand Down
28 changes: 28 additions & 0 deletions cli/azd/pkg/input/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,34 @@ func TestAskerConsole_Message_InvalidQuery_FallsBack(t *testing.T) {
"invalid query should fall back to full envelope")
}

func TestAskerConsole_Message_EmptySkippedInJson(t *testing.T) {
buf := &strings.Builder{}
formatter := &output.JsonFormatter{}

c := NewConsole(
true,
false,
false,
Writers{Output: writerAdapter{buf}},
ConsoleHandles{
Stderr: os.Stderr,
Stdin: os.Stdin,
Stdout: writerAdapter{buf},
},
formatter,
nil,
)

// An empty message should produce no JSON output (it's just a visual separator in text mode)
c.Message(context.Background(), "")
require.Empty(t, buf.String(), "empty message should not emit any JSON output")

// A non-empty message should still produce JSON output
c.Message(context.Background(), "hello")
require.NotEmpty(t, buf.String(), "non-empty message should emit JSON output")
require.Contains(t, buf.String(), `"consoleMessage"`)
}

// writerAdapter wraps *strings.Builder to satisfy io.Writer for test purposes.
type writerAdapter struct {
*strings.Builder
Expand Down