github.com/kruglovmax/helm@v3.0.0-beta.3+incompatible/cmd/helm/require/args_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  
    16  package require
    17  
    18  import (
    19  	"fmt"
    20  	"io/ioutil"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func TestArgs(t *testing.T) {
    28  	runTestCases(t, []testCase{{
    29  		validateFunc: NoArgs,
    30  	}, {
    31  		args:         []string{"one"},
    32  		validateFunc: NoArgs,
    33  		wantError:    `"root" accepts no arguments`,
    34  	}, {
    35  		args:         []string{"one"},
    36  		validateFunc: ExactArgs(1),
    37  	}, {
    38  		validateFunc: ExactArgs(1),
    39  		wantError:    `"root" requires 1 argument`,
    40  	}, {
    41  		validateFunc: ExactArgs(2),
    42  		wantError:    `"root" requires 2 arguments`,
    43  	}, {
    44  		args:         []string{"one"},
    45  		validateFunc: MaximumNArgs(1),
    46  	}, {
    47  		args:         []string{"one", "two"},
    48  		validateFunc: MaximumNArgs(1),
    49  		wantError:    `"root" accepts at most 1 argument`,
    50  	}, {
    51  		validateFunc: MinimumNArgs(1),
    52  		wantError:    `"root" requires at least 1 argument`,
    53  	}, {
    54  		args:         []string{"one", "two"},
    55  		validateFunc: MinimumNArgs(1),
    56  	}})
    57  }
    58  
    59  type testCase struct {
    60  	args         []string
    61  	validateFunc cobra.PositionalArgs
    62  	wantError    string
    63  }
    64  
    65  func runTestCases(t *testing.T, testCases []testCase) {
    66  	for i, tc := range testCases {
    67  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    68  			cmd := &cobra.Command{
    69  				Use:  "root",
    70  				Run:  func(*cobra.Command, []string) {},
    71  				Args: tc.validateFunc,
    72  			}
    73  			cmd.SetArgs(tc.args)
    74  			cmd.SetOutput(ioutil.Discard)
    75  
    76  			err := cmd.Execute()
    77  			if tc.wantError == "" {
    78  				if err != nil {
    79  					t.Fatalf("unexpected error, got '%v'", err)
    80  				}
    81  				return
    82  			}
    83  			if !strings.Contains(err.Error(), tc.wantError) {
    84  				t.Fatalf("unexpected error \n\nWANT:\n%q\n\nGOT:\n%q\n", tc.wantError, err)
    85  			}
    86  			if !strings.Contains(err.Error(), "Usage:") {
    87  				t.Fatalf("unexpected error: want Usage string\n\nGOT:\n%q\n", err)
    88  			}
    89  		})
    90  	}
    91  }