Merge pull request #25847 from zachwhaley/fix-tf-cli-args-no-color

Fix error when multiple -no-color arguments are used
This commit is contained in:
Alisdair McDiarmid 2020-08-24 10:13:59 -04:00 committed by GitHub
commit 35125717b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -440,14 +440,18 @@ func (m *Meta) process(args []string) []string {
// Set colorization // Set colorization
m.color = m.Color m.color = m.Color
for i, v := range args { i := 0 // output index
for _, v := range args {
if v == "-no-color" { if v == "-no-color" {
m.color = false m.color = false
m.Color = false m.Color = false
args = append(args[:i], args[i+1:]...) } else {
break // copy and increment index
args[i] = v
i++
} }
} }
args = args[:i]
// Set the UI // Set the UI
m.oldUi = m.Ui m.oldUi = m.Ui

View File

@ -56,6 +56,21 @@ func TestMetaColorize(t *testing.T) {
if !m.Colorize().Disable { if !m.Colorize().Disable {
t.Fatal("should be disabled") 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) { func TestMetaInputMode(t *testing.T) {