Fix bug of knownFuncs usage when findFiles is called recursively

This commit is contained in:
Ryosuke IWANAGA 2014-12-09 14:50:45 +09:00
parent f7b87a0a9a
commit f80b50711d
4 changed files with 33 additions and 5 deletions

View File

@ -27,9 +27,10 @@ func Translate(c *Config) error {
return err return err
} }
var knownFuncs = make(map[string]int)
// Locate all the assets. // Locate all the assets.
for _, input := range c.Input { for _, input := range c.Input {
err = findFiles(input.Path, c.Prefix, input.Recursive, &toc, c.Ignore) err = findFiles(input.Path, c.Prefix, input.Recursive, &toc, c.Ignore, knownFuncs)
if err != nil { if err != nil {
return err return err
} }
@ -95,7 +96,7 @@ func (v ByName) Less(i, j int) bool { return v[i].Name() < v[j].Name() }
// findFiles recursively finds all the file paths in the given directory tree. // findFiles recursively finds all the file paths in the given directory tree.
// They are added to the given map as keys. Values will be safe function names // They are added to the given map as keys. Values will be safe function names
// for each file, which will be used when generating the output code. // for each file, which will be used when generating the output code.
func findFiles(dir, prefix string, recursive bool, toc *[]Asset, ignore []*regexp.Regexp) error { func findFiles(dir, prefix string, recursive bool, toc *[]Asset, ignore []*regexp.Regexp, knownFuncs map[string]int) error {
if len(prefix) > 0 { if len(prefix) > 0 {
dir, _ = filepath.Abs(dir) dir, _ = filepath.Abs(dir)
prefix, _ = filepath.Abs(prefix) prefix, _ = filepath.Abs(prefix)
@ -129,8 +130,6 @@ func findFiles(dir, prefix string, recursive bool, toc *[]Asset, ignore []*regex
sort.Sort(ByName(list)) sort.Sort(ByName(list))
} }
knownFuncs := make(map[string]int)
for _, file := range list { for _, file := range list {
var asset Asset var asset Asset
asset.Path = filepath.Join(dir, file.Name()) asset.Path = filepath.Join(dir, file.Name())
@ -149,7 +148,7 @@ func findFiles(dir, prefix string, recursive bool, toc *[]Asset, ignore []*regex
if file.IsDir() { if file.IsDir() {
if recursive { if recursive {
findFiles(asset.Path, prefix, recursive, toc, ignore) findFiles(asset.Path, prefix, recursive, toc, ignore, knownFuncs)
} }
continue continue
} }

27
convert_test.go Normal file
View File

@ -0,0 +1,27 @@
package bindata
import (
"regexp"
"testing"
)
func TestSafeFunctionName(t *testing.T) {
var knownFuncs = make(map[string]int)
name1 := safeFunctionName("foo/bar", knownFuncs)
name2 := safeFunctionName("foo_bar", knownFuncs)
if name1 == name2 {
t.Errorf("name collision")
}
}
func TestFindFiles(t *testing.T) {
var toc []Asset
var knownFuncs = make(map[string]int)
err := findFiles("testdata/dupname", "testdata/dupname", true, &toc, []*regexp.Regexp{}, knownFuncs)
if err != nil {
t.Errorf("expected to be no error: %+v", err)
}
if toc[0].Func == toc[1].Func {
t.Errorf("name collision")
}
}

1
testdata/dupname/foo/bar vendored Normal file
View File

@ -0,0 +1 @@
// sample file

1
testdata/dupname/foo_bar vendored Normal file
View File

@ -0,0 +1 @@
// sample file