-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathargs_test.go
55 lines (45 loc) · 1.26 KB
/
args_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2020-Present VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package commands_test
import (
"fmt"
"testing"
"github.com/spf13/cobra"
"github.com/buildpacks-community/kpack-cli/pkg/commands"
)
func TestExactArgsWithUsage(t *testing.T) {
cmd := cobra.Command{
Args: commands.ExactArgsWithUsage(1),
}
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
_, err := fmt.Fprintln(cmd.OutOrStdout(), "some usage")
return err
})
expectedMsg := "accepts 1 arg(s), received 0\n\nsome usage\n"
err := cmd.ValidateArgs([]string{})
if err == nil || err.Error() != expectedMsg {
t.Errorf(`Did not return expected usage error from using wrong number of args.
Expected:
%v
Actual:
%v`, expectedMsg, err)
}
}
func TestOptionalArgsWithUsage(t *testing.T) {
cmd := cobra.Command{
Args: commands.OptionalArgsWithUsage(1),
}
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
_, err := fmt.Fprintln(cmd.OutOrStdout(), "some usage")
return err
})
expectedMsg := "accepts 0 or 1 arg(s), received 2\n\nsome usage\n"
err := cmd.ValidateArgs([]string{"some-arg-1", "some-arg-2"})
if err == nil || err.Error() != expectedMsg {
t.Errorf(`Did not return expected usage error from using wrong number of args.
Expected:
%v
Actual:
%v`, expectedMsg, err)
}
}