Skip to content

Commit

Permalink
config: check for duplicate inputs
Browse files Browse the repository at this point in the history
Given two identical paths "x/y" and "x/y", the go-bindata will generate
both assets.

This changes check for duplicate paths in validateInputs and repopulate
the Config.Input with new list.

Fix #37
  • Loading branch information
shuLhan committed Apr 28, 2020
1 parent c9156bb commit dbebd3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 15 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,28 @@ func NewConfig() *Config {
}

func (c *Config) validateInput() (err error) {
uniqPaths := make(map[string]struct{}, len(c.Input))
newInputs := make([]InputConfig, 0, len(c.Input))

for _, input := range c.Input {
input.Path = filepath.Clean(input.Path)
_, ok := uniqPaths[input.Path]
if ok {
continue
}
_, err = os.Lstat(input.Path)
if err != nil {
return fmt.Errorf("failed to stat input path '%s': %v",
input.Path, err)
}
uniqPaths[input.Path] = struct{}{}
newInputs = append(newInputs, input)
}
return
if len(newInputs) == 0 {
return ErrNoInput
}
c.Input = newInputs
return nil
}

//
Expand Down
24 changes: 22 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestValidateInput(t *testing.T) {
tests := []struct {
desc string
cfg *Config
exp []InputConfig
expErr string
}{{
desc: `With empty list`,
Expand All @@ -26,22 +27,39 @@ func TestValidateInput(t *testing.T) {
Path: "",
}},
},
expErr: `failed to stat input path '': lstat : no such file or directory`,
exp: []InputConfig{{
Path: ".",
}},
}, {
desc: `With directory not exist`,
cfg: &Config{
Input: []InputConfig{{
Path: "./notexist",
}},
},
expErr: `failed to stat input path './notexist': lstat ./notexist: no such file or directory`,
expErr: `failed to stat input path 'notexist': lstat notexist: no such file or directory`,
}, {
desc: `With file as input`,
cfg: &Config{
Input: []InputConfig{{
Path: "./README.md",
}},
},
exp: []InputConfig{{
Path: "README.md",
}},
}, {
desc: `With duplicate inputs`,
cfg: &Config{
Input: []InputConfig{{
Path: "./testdata/in/test.asset",
}, {
Path: "./testdata/in/test.asset",
}},
},
exp: []InputConfig{{
Path: "testdata/in/test.asset",
}},
}}

for _, test := range tests {
Expand All @@ -52,6 +70,8 @@ func TestValidateInput(t *testing.T) {
assert(t, test.expErr, err.Error(), true)
continue
}

assert(t, test.exp, test.cfg.Input, true)
}
}

Expand Down

0 comments on commit dbebd3f

Please sign in to comment.