istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/cmd/root_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"bytes"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  func checkHelpForFlag(t *testing.T, gotHelpText, flag string, wantExists bool) {
    26  	t.Helper()
    27  
    28  	if strings.Contains(gotHelpText, flag) != wantExists {
    29  		if wantExists {
    30  			t.Errorf("%q flag was expected but not found in help text", flag)
    31  		} else {
    32  			t.Errorf("%q flag was found in help text but not expected", flag)
    33  		}
    34  	}
    35  }
    36  
    37  func TestHideInheritedFlags(t *testing.T) {
    38  	const (
    39  		parentFlag0 = "parent-flag0"
    40  		parentFlag1 = "parent-flag1"
    41  		parentFlag2 = "parent-flag2"
    42  		childFlag2  = "child-flag2"
    43  	)
    44  	parent := &cobra.Command{Use: "parent"}
    45  	_ = parent.PersistentFlags().String(parentFlag0, "", parentFlag0)
    46  	_ = parent.PersistentFlags().String(parentFlag1, "", parentFlag1)
    47  	_ = parent.PersistentFlags().String(parentFlag2, "", parentFlag2)
    48  	var out bytes.Buffer
    49  	parent.SetOut(&out)
    50  	parent.SetErr(&out)
    51  
    52  	child := &cobra.Command{
    53  		Use: "child",
    54  		Run: func(c *cobra.Command, args []string) {},
    55  	}
    56  	_ = parent.PersistentFlags().String(childFlag2, "", childFlag2)
    57  	parent.AddCommand(child)
    58  
    59  	// verify both parent flags and the child flag are visible by default
    60  	parent.SetArgs([]string{"child", "--help"})
    61  	if err := parent.Execute(); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	got := out.String()
    65  	out.Reset()
    66  	checkHelpForFlag(t, got, parentFlag0, true)
    67  	checkHelpForFlag(t, got, parentFlag1, true)
    68  	checkHelpForFlag(t, got, parentFlag2, true)
    69  	checkHelpForFlag(t, got, childFlag2, true)
    70  
    71  	// verify the hidden parent flag is not visible in help text
    72  	hideInheritedFlags(child, parentFlag1, parentFlag2)
    73  	if err := parent.Execute(); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	got = out.String()
    77  	out.Reset()
    78  	checkHelpForFlag(t, got, parentFlag0, true)
    79  	checkHelpForFlag(t, got, parentFlag1, false)
    80  	checkHelpForFlag(t, got, parentFlag1, false)
    81  	checkHelpForFlag(t, got, childFlag2, true)
    82  }