fix: slices/maps of existingfile would fail to work
This commit is contained in:
@@ -540,7 +540,7 @@ func sliceDecoder(r *Registry) MapperFunc {
|
|||||||
var childScanner *Scanner
|
var childScanner *Scanner
|
||||||
if ctx.Value.Flag != nil {
|
if ctx.Value.Flag != nil {
|
||||||
t := ctx.Scan.Pop()
|
t := ctx.Scan.Pop()
|
||||||
// If decoding a flag, we need an value.
|
// If decoding a flag, we need a value.
|
||||||
if t.IsEOL() {
|
if t.IsEOL() {
|
||||||
return fmt.Errorf("missing value, expecting \"<arg>%c...\"", sep)
|
return fmt.Errorf("missing value, expecting \"<arg>%c...\"", sep)
|
||||||
}
|
}
|
||||||
@@ -641,7 +641,7 @@ func existingFileMapper(r *Registry) MapperFunc {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ctx.Value.Active || ctx.Value.Set {
|
if !ctx.Value.Active || (ctx.Value.Set && ctx.Value.Target.Type() == target.Type()) {
|
||||||
// early return to avoid checking extra files that may not exist;
|
// early return to avoid checking extra files that may not exist;
|
||||||
// this hack only works because the value provided on the cli is
|
// this hack only works because the value provided on the cli is
|
||||||
// checked before the default value is checked (if default is set).
|
// checked before the default value is checked (if default is set).
|
||||||
@@ -677,7 +677,7 @@ func existingDirMapper(r *Registry) MapperFunc {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ctx.Value.Active || ctx.Value.Set {
|
if !ctx.Value.Active || (ctx.Value.Set && ctx.Value.Target.Type() == target.Type()) {
|
||||||
// early return to avoid checking extra dirs that may not exist;
|
// early return to avoid checking extra dirs that may not exist;
|
||||||
// this hack only works because the value provided on the cli is
|
// this hack only works because the value provided on the cli is
|
||||||
// checked before the default value is checked (if default is set).
|
// checked before the default value is checked (if default is set).
|
||||||
|
|||||||
@@ -553,6 +553,20 @@ func TestExistingFileMapper(t *testing.T) {
|
|||||||
assert.Contains(t, err.Error(), "exists but is a directory")
|
assert.Contains(t, err.Error(), "exists but is a directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExistingFileMapperSlice(t *testing.T) {
|
||||||
|
type CLI struct {
|
||||||
|
Files []string `type:"existingfile"`
|
||||||
|
}
|
||||||
|
var cli CLI
|
||||||
|
p := mustNew(t, &cli)
|
||||||
|
_, err := p.Parse([]string{"--files", "testdata/file.txt", "--files", "testdata/file.txt"})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotZero(t, cli.Files)
|
||||||
|
pwd, err := os.Getwd()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, []string{filepath.Join(pwd, "testdata", "file.txt"), filepath.Join(pwd, "testdata", "file.txt")}, cli.Files)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExistingFileMapperDefaultMissing(t *testing.T) {
|
func TestExistingFileMapperDefaultMissing(t *testing.T) {
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
File string `type:"existingfile" default:"testdata/missing.txt"`
|
File string `type:"existingfile" default:"testdata/missing.txt"`
|
||||||
|
|||||||
Reference in New Issue
Block a user