diff --git a/main.go b/main.go index 2d26bb508..0b7f9c1d4 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,17 @@ const ( envTmpLogPath = "TF_TEMP_LOG_PATH" ) +// ui wraps the primary output cli.Ui, and redirects Warn calls to Output +// calls. This ensures that warnings are sent to stdout, and are properly +// serialized within the stdout stream. +type ui struct { + cli.Ui +} + +func (u *ui) Warn(msg string) { + u.Ui.Output(msg) +} + func main() { os.Exit(realMain()) } @@ -82,11 +93,11 @@ func realMain() int { } func init() { - Ui = &cli.BasicUi{ + Ui = &ui{&cli.BasicUi{ Writer: os.Stdout, ErrorWriter: os.Stderr, Reader: os.Stdin, - } + }} } func wrappedMain() int { diff --git a/main_test.go b/main_test.go index 406195e8b..9b9594f10 100644 --- a/main_test.go +++ b/main_test.go @@ -264,3 +264,20 @@ func (c *testCommandCLI) Run(args []string) int { func (c *testCommandCLI) Synopsis() string { return "" } func (c *testCommandCLI) Help() string { return "" } + +func TestWarnOutput(t *testing.T) { + mock := cli.NewMockUi() + wrapped := &ui{mock} + wrapped.Warn("WARNING") + + stderr := mock.ErrorWriter.String() + stdout := mock.OutputWriter.String() + + if stderr != "" { + t.Fatalf("unexpected stderr: %q", stderr) + } + + if stdout != "WARNING\n" { + t.Fatalf("unexpected stdout: %q\n", stdout) + } +}