Repository navigation
Expand file tree
/
Copy pathcommand_test.go
More file actions
52 lines (44 loc) · 1.62 KB
/
Copy pathcommand_test.go
File metadata and controls
52 lines (44 loc) · 1.62 KB
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
package cli
import (
"context"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type nestedFlagBlock struct {
Address string `desc:"listen address" mapstructure:"address"`
Port int `desc:"listen port" mapstructure:"port"`
}
type inheritFlagsConfig struct {
Visible string `desc:"a visible flag" mapstructure:"visible"`
Hidden nestedFlagBlock `flag:",hidden" mapstructure:"hidden"`
Deprecated nestedFlagBlock `flag:",deprecated" mapstructure:"deprecated"`
}
// TestNewCommand_NestedFlagsInheritParentTags pins the behaviour consumers
// rely on to keep whole config blocks out of --help: a hidden or deprecated
// tag on a struct field applies to every flag generated from its fields.
func TestNewCommand_NestedFlagsInheritParentTags(t *testing.T) {
t.Parallel()
cmd := NewCommand(&cobra.Command{Use: "test"}, func(context.Context, *inheritFlagsConfig) error { return nil }, &inheritFlagsConfig{})
tests := []struct {
flag string
wantHidden bool
wantDeprecated bool
}{
{flag: "visible"},
{flag: "hidden-address", wantHidden: true},
{flag: "hidden-port", wantHidden: true},
{flag: "deprecated-address", wantDeprecated: true},
{flag: "deprecated-port", wantDeprecated: true},
}
for _, tt := range tests {
t.Run(tt.flag, func(t *testing.T) {
t.Parallel()
f := cmd.Flags().Lookup(tt.flag)
require.NotNil(t, f, "flag --%s was not registered", tt.flag)
assert.Equal(t, tt.wantHidden, f.Hidden, "hidden")
assert.Equal(t, tt.wantDeprecated, f.Deprecated != "", "deprecated")
})
}
}