Merge pull request #153 from alexflint/test-empty-map
Fix case where an empty environment variable is parsed in a slice or map
This commit is contained in:
commit
679be43af3
18
parse.go
18
parse.go
|
@ -441,13 +441,17 @@ func (p *Parser) captureEnvVars(specs []*spec, wasPresent map[*spec]bool) error
|
||||||
if spec.cardinality == multiple {
|
if spec.cardinality == multiple {
|
||||||
// expect a CSV string in an environment
|
// expect a CSV string in an environment
|
||||||
// variable in the case of multiple values
|
// variable in the case of multiple values
|
||||||
values, err := csv.NewReader(strings.NewReader(value)).Read()
|
var values []string
|
||||||
if err != nil {
|
var err error
|
||||||
return fmt.Errorf(
|
if len(strings.TrimSpace(value)) > 0 {
|
||||||
"error reading a CSV string from environment variable %s with multiple values: %v",
|
values, err = csv.NewReader(strings.NewReader(value)).Read()
|
||||||
spec.env,
|
if err != nil {
|
||||||
err,
|
return fmt.Errorf(
|
||||||
)
|
"error reading a CSV string from environment variable %s with multiple values: %v",
|
||||||
|
spec.env,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err = setSliceOrMap(p.val(spec.dest), values, !spec.separate); err != nil {
|
if err = setSliceOrMap(p.val(spec.dest), values, !spec.separate); err != nil {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
|
|
|
@ -721,6 +721,15 @@ func TestEnvironmentVariableSliceArgumentString(t *testing.T) {
|
||||||
assert.Equal(t, []string{"bar", "baz, qux"}, args.Foo)
|
assert.Equal(t, []string{"bar", "baz, qux"}, args.Foo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEnvironmentVariableSliceEmpty(t *testing.T) {
|
||||||
|
var args struct {
|
||||||
|
Foo []string `arg:"env"`
|
||||||
|
}
|
||||||
|
_, err := parseWithEnv("", []string{`FOO=`}, &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Len(t, args.Foo, 0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnvironmentVariableSliceArgumentInteger(t *testing.T) {
|
func TestEnvironmentVariableSliceArgumentInteger(t *testing.T) {
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo []int `arg:"env"`
|
Foo []int `arg:"env"`
|
||||||
|
@ -775,6 +784,15 @@ func TestEnvironmentVariableMap(t *testing.T) {
|
||||||
assert.Equal(t, "ninetynine", args.Foo[99])
|
assert.Equal(t, "ninetynine", args.Foo[99])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEnvironmentVariableEmptyMap(t *testing.T) {
|
||||||
|
var args struct {
|
||||||
|
Foo map[int]string `arg:"env"`
|
||||||
|
}
|
||||||
|
_, err := parseWithEnv("", []string{`FOO=`}, &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Len(t, args.Foo, 0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnvironmentVariableIgnored(t *testing.T) {
|
func TestEnvironmentVariableIgnored(t *testing.T) {
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo string `arg:"env"`
|
Foo string `arg:"env"`
|
||||||
|
|
Loading…
Reference in New Issue