From af8d5a69d8c2c6e43987804f9a866c2d62ab0326 Mon Sep 17 00:00:00 2001 From: Zachary Whaley Date: Thu, 13 Aug 2020 13:10:09 -0500 Subject: [PATCH] Fix error when TF_CLI_ARGS adds -no-color argument Fixes #25845 --- command/meta.go | 10 +++++++--- command/meta_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/command/meta.go b/command/meta.go index a4b00935fb..e2e99323a1 100644 --- a/command/meta.go +++ b/command/meta.go @@ -435,14 +435,18 @@ func (m *Meta) process(args []string) []string { // Set colorization m.color = m.Color - for i, v := range args { + i := 0 // output index + for _, v := range args { if v == "-no-color" { m.color = false m.Color = false - args = append(args[:i], args[i+1:]...) - break + } else { + // copy and increment index + args[i] = v + i++ } } + args = args[:i] // Set the UI m.oldUi = m.Ui diff --git a/command/meta_test.go b/command/meta_test.go index 5ebae9a8e9..595cedfe1c 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -55,6 +55,21 @@ func TestMetaColorize(t *testing.T) { if !m.Colorize().Disable { t.Fatal("should be disabled") } + + // Test disable #2 + // Verify multiple -no-color options are removed from args slice. + // E.g. an additional -no-color arg could be added by TF_CLI_ARGS. + m = new(Meta) + m.Color = true + args = []string{"foo", "-no-color", "bar", "-no-color"} + args2 = []string{"foo", "bar"} + args = m.process(args) + if !reflect.DeepEqual(args, args2) { + t.Fatalf("bad: %#v", args) + } + if !m.Colorize().Disable { + t.Fatal("should be disabled") + } } func TestMetaInputMode(t *testing.T) {