adding string alias and test

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

View File

@ -142,13 +142,15 @@ func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
// ApplyInputSourceValue applies a String value to the flagSet if required // ApplyInputSourceValue applies a String value to the flagSet if required
func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.StringFlag.Name) { if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.StringFlag.Name) {
value, err := isc.String(f.StringFlag.Name) for _, name := range f.StringFlag.Names(){
if err != nil { if isc.isSet(name) {
return err value, err := isc.String(name)
} if err != nil {
if value != "" { return err
for _, name := range f.Names() { }
_ = f.set.Set(name, value) for _, n := range f.Names() {
_ = f.set.Set(n, value)
}
} }
} }
} }

View File

@ -220,6 +220,20 @@ func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) {
refute(t, true, c.Bool("test")) refute(t, true, c.Bool("test"))
} }
func TestStringApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewStringFlag(&cli.StringFlag{Name: "test", Aliases: []string{"test_alias"}}),
FlagName: "test_alias",
MapValue: "hello",
ContextValueString: "goodbye",
}
c := runTest(t, tis)
expect(t, "goodbye", c.String("test_alias"))
c = runRacyTest(t, tis)
refute(t, "goodbye", c.String("test_alias"))
}
func TestStringApplyInputSourceMethodSet(t *testing.T) { func TestStringApplyInputSourceMethodSet(t *testing.T) {
tis := testApplyInputSource{ tis := testApplyInputSource{
Flag: NewStringFlag(&cli.StringFlag{Name: "test"}), Flag: NewStringFlag(&cli.StringFlag{Name: "test"}),