2020-12-14 16:07:27 +00:00
|
|
|
package flags
|
2020-12-11 03:26:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-12-11 03:26:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFlagHelper_Pointers_Set(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-12-11 03:26:10 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
B BoolValue
|
|
|
|
b bool = true
|
|
|
|
|
|
|
|
D DurationValue
|
|
|
|
d time.Duration = 2 * time.Minute
|
|
|
|
|
|
|
|
U UintValue
|
|
|
|
u uint = 99
|
|
|
|
)
|
|
|
|
flagSet := flag.NewFlagSet("test", flag.PanicOnError)
|
|
|
|
flagSet.Var(&B, "b", "bool")
|
|
|
|
flagSet.Var(&D, "d", "duration")
|
|
|
|
flagSet.Var(&U, "u", "uint")
|
|
|
|
|
|
|
|
args := []string{"-b", "false", "-d", "1m", "-u", "42"}
|
|
|
|
err := flagSet.Parse(args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "false", B.String())
|
|
|
|
B.Merge(&b)
|
|
|
|
require.Equal(t, false, b)
|
|
|
|
|
|
|
|
require.Equal(t, "1m0s", D.String())
|
|
|
|
D.Merge(&d)
|
|
|
|
require.Equal(t, 1*time.Minute, d)
|
|
|
|
|
|
|
|
require.Equal(t, "42", U.String())
|
|
|
|
U.Merge(&u)
|
|
|
|
require.Equal(t, uint(42), u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlagHelper_Pointers_Ignored(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-12-11 03:26:10 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
B BoolValue
|
|
|
|
b bool = true
|
|
|
|
|
|
|
|
D DurationValue
|
|
|
|
d time.Duration = 2 * time.Minute
|
|
|
|
|
|
|
|
U UintValue
|
|
|
|
u uint = 99
|
|
|
|
)
|
|
|
|
flagSet := flag.NewFlagSet("test", flag.PanicOnError)
|
|
|
|
flagSet.Var(&B, "b", "bool")
|
|
|
|
flagSet.Var(&D, "d", "duration")
|
|
|
|
flagSet.Var(&U, "u", "uint")
|
|
|
|
|
|
|
|
var args []string
|
|
|
|
err := flagSet.Parse(args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "false", B.String())
|
|
|
|
B.Merge(&b)
|
|
|
|
require.Equal(t, true, b)
|
|
|
|
|
|
|
|
require.Equal(t, "0s", D.String())
|
|
|
|
D.Merge(&d)
|
|
|
|
require.Equal(t, 2*time.Minute, d)
|
|
|
|
|
|
|
|
require.Equal(t, "0", U.String())
|
|
|
|
U.Merge(&u)
|
|
|
|
require.Equal(t, uint(99), u)
|
|
|
|
}
|