adding int alias and test

This commit is contained in:
James He 2022-08-25 16:48:26 -05:00 committed by Dan Buch
parent 50bbb4a53e
commit 5a5789af7a
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 23 additions and 6 deletions

View File

@ -186,12 +186,16 @@ func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
// ApplyInputSourceValue applies a int value to the flagSet if required
func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.IntFlag.Name) {
value, err := isc.Int(f.IntFlag.Name)
if err != nil {
return err
}
for _, name := range f.Names() {
_ = f.set.Set(name, strconv.FormatInt(int64(value), 10))
for _, name := range f.IntFlag.Names() {
if isc.isSet(name) {
value, err := isc.Int(name)
if err != nil {
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatInt(int64(value), 10))
}
}
}
}
return nil

View File

@ -332,6 +332,19 @@ func TestPathApplyInputSourceMethodEnvVarSet(t *testing.T) {
refute(t, "goodbye", c.String("test"))
}
func TestIntApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewIntFlag(&cli.IntFlag{Name: "test", Aliases: []string{"test_alias"}}),
FlagName: "test",
MapValue: 15,
}
c := runTest(t, tis)
expect(t, 15, c.Int("test_alias"))
c = runRacyTest(t, tis)
refute(t, 15, c.Int("test_alias"))
}
func TestIntApplyInputSourceMethodSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),