| 1 |
package cliflagv3 |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/knadh/koanf/v2" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
"github.com/urfave/cli/v3" |
| 12 |
) |
| 13 |
|
| 14 |
// mockKoanf is a mock implementation of KoanfIntf for testing. |
| 15 |
type mockKoanf struct { |
| 16 |
keys map[string]bool |
| 17 |
} |
| 18 |
|
| 19 |
func (m *mockKoanf) Exists(key string) bool { |
| 20 |
return m.keys[key] |
| 21 |
} |
| 22 |
|
| 23 |
func TestCliFlag(t *testing.T) { |
| 24 |
cliApp := cli.Command{ |
| 25 |
Name: "testing", |
| 26 |
Action: func(ctx context.Context, cmd *cli.Command) error { |
| 27 |
p := Provider(cmd, ".") |
| 28 |
x, err := p.Read() |
| 29 |
require.NoError(t, err) |
| 30 |
require.NotEmpty(t, x) |
| 31 |
|
| 32 |
fmt.Printf("x: %v\n", x) |
| 33 |
|
| 34 |
k := koanf.New(".") |
| 35 |
err = k.Load(p, nil) |
| 36 |
|
| 37 |
fmt.Printf("k.All(): %v\n", k.All()) |
| 38 |
|
| 39 |
return nil |
| 40 |
}, |
| 41 |
Flags: []cli.Flag{ |
| 42 |
cli.HelpFlag, |
| 43 |
cli.VersionFlag, |
| 44 |
&cli.StringFlag{ |
| 45 |
Name: "test", |
| 46 |
Usage: "test flag", |
| 47 |
Value: "test", |
| 48 |
Aliases: []string{"t"}, |
| 49 |
Sources: cli.EnvVars("TEST_FLAG"), |
| 50 |
}, |
| 51 |
}, |
| 52 |
Commands: []*cli.Command{ |
| 53 |
{ |
| 54 |
Name: "x", |
| 55 |
Description: "yeah yeah testing", |
| 56 |
Action: func(ctx context.Context, cmd *cli.Command) error { |
| 57 |
p := ProviderWithConfig(cmd, ".", &Config{Defaults: []string{"other"}}) |
| 58 |
x, err := p.Read() |
| 59 |
require.NoError(t, err) |
| 60 |
require.NotEmpty(t, x) |
| 61 |
fmt.Printf("x: %s\n", x) |
| 62 |
|
| 63 |
k := koanf.New(".") |
| 64 |
err = k.Load(p, nil) |
| 65 |
|
| 66 |
fmt.Printf("k.All(): %v\n", k.All()) |
| 67 |
|
| 68 |
require.Equal(t, k.String("testing.x.lol"), "dsf") |
| 69 |
// "default" was not explicitly set and not in Defaults, |
| 70 |
// so it should not be emitted. |
| 71 |
require.Equal(t, k.String("testing.x.default"), "") |
| 72 |
require.Equal(t, k.String("testing.x.other"), "") |
| 73 |
return nil |
| 74 |
}, |
| 75 |
Flags: []cli.Flag{ |
| 76 |
cli.HelpFlag, |
| 77 |
cli.VersionFlag, |
| 78 |
&cli.StringFlag{ |
| 79 |
Name: "lol", |
| 80 |
Usage: "test flag", |
| 81 |
Value: "test", |
| 82 |
Required: true, |
| 83 |
Sources: cli.EnvVars("TEST_FLAG"), |
| 84 |
}, |
| 85 |
&cli.StringFlag{ |
| 86 |
Name: "default", |
| 87 |
Usage: "default test flag", |
| 88 |
Value: "test", |
| 89 |
}, |
| 90 |
&cli.StringFlag{ |
| 91 |
Name: "other", |
| 92 |
Usage: "other test flag", |
| 93 |
}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
} |
| 98 |
|
| 99 |
// The Action of the testing only runs if no subcommand is specified |
| 100 |
x := []string{"testing", "--test", "gf"} |
| 101 |
err := cliApp.Run(context.Background(), append(x, os.Environ()...)) |
| 102 |
require.NoError(t, err) |
| 103 |
|
| 104 |
// This runs the Action of the x |
| 105 |
x = []string{"testing", "x", "--lol", "dsf"} |
| 106 |
err = cliApp.Run(context.Background(), append(x, os.Environ()...)) |
| 107 |
require.NoError(t, err) |
| 108 |
} |
| 109 |
|
| 110 |
func TestCliFlagDefaults(t *testing.T) { |
| 111 |
t.Run("without KeyMap unset flags are skipped", func(t *testing.T) { |
| 112 |
cliApp := cli.Command{ |
| 113 |
Name: "app", |
| 114 |
Action: func(ctx context.Context, cmd *cli.Command) error { |
| 115 |
p := Provider(cmd, ".") |
| 116 |
mp, err := p.Read() |
| 117 |
require.NoError(t, err) |
| 118 |
|
| 119 |
// "name" was explicitly set, so it must appear. |
| 120 |
require.Contains(t, mp, "app") |
| 121 |
app := mp["app"].(map[string]any) |
| 122 |
require.Equal(t, "explicit", app["name"]) |
| 123 |
|
| 124 |
// "color" was NOT set and is not in Defaults, so it must be absent. |
| 125 |
require.NotContains(t, app, "color") |
| 126 |
return nil |
| 127 |
}, |
| 128 |
Flags: []cli.Flag{ |
| 129 |
&cli.StringFlag{Name: "name", Value: "default-name"}, |
| 130 |
&cli.StringFlag{Name: "color", Value: "red"}, |
| 131 |
}, |
| 132 |
} |
| 133 |
err := cliApp.Run(context.Background(), []string{"app", "--name", "explicit"}) |
| 134 |
require.NoError(t, err) |
| 135 |
}) |
| 136 |
|
| 137 |
t.Run("with KeyMap existing keys are not overwritten", func(t *testing.T) { |
| 138 |
cliApp := cli.Command{ |
| 139 |
Name: "app", |
| 140 |
Action: func(ctx context.Context, cmd *cli.Command) error { |
| 141 |
km := &mockKoanf{keys: map[string]bool{ |
| 142 |
"app.color": true, |
| 143 |
}} |
| 144 |
p := ProviderWithConfig(cmd, ".", &Config{KeyMap: km}) |
| 145 |
mp, err := p.Read() |
| 146 |
require.NoError(t, err) |
| 147 |
|
| 148 |
app := mp["app"].(map[string]any) |
| 149 |
require.Equal(t, "explicit", app["name"]) |
| 150 |
require.NotContains(t, app, "color") |
| 151 |
require.Equal(t, "large", app["size"]) |
| 152 |
return nil |
| 153 |
}, |
| 154 |
Flags: []cli.Flag{ |
| 155 |
&cli.StringFlag{Name: "name", Value: "default-name"}, |
| 156 |
&cli.StringFlag{Name: "color", Value: "red"}, |
| 157 |
&cli.StringFlag{Name: "size", Value: "large"}, |
| 158 |
}, |
| 159 |
} |
| 160 |
err := cliApp.Run(context.Background(), []string{"app", "--name", "explicit"}) |
| 161 |
require.NoError(t, err) |
| 162 |
}) |
| 163 |
} |