From f80b50711dd465062a6590accab7cd7b5f34129f Mon Sep 17 00:00:00 2001 From: Ryosuke IWANAGA Date: Tue, 9 Dec 2014 14:50:45 +0900 Subject: [PATCH] Fix bug of knownFuncs usage when findFiles is called recursively --- convert.go | 9 ++++----- convert_test.go | 27 +++++++++++++++++++++++++++ testdata/dupname/foo/bar | 1 + testdata/dupname/foo_bar | 1 + 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 convert_test.go create mode 100644 testdata/dupname/foo/bar create mode 100644 testdata/dupname/foo_bar diff --git a/convert.go b/convert.go index 0d41ea0..7e6246d 100644 --- a/convert.go +++ b/convert.go @@ -27,9 +27,10 @@ func Translate(c *Config) error { return err } + var knownFuncs = make(map[string]int) // Locate all the assets. 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 { 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. // 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. -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 { dir, _ = filepath.Abs(dir) prefix, _ = filepath.Abs(prefix) @@ -129,8 +130,6 @@ func findFiles(dir, prefix string, recursive bool, toc *[]Asset, ignore []*regex sort.Sort(ByName(list)) } - knownFuncs := make(map[string]int) - for _, file := range list { var asset Asset 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 recursive { - findFiles(asset.Path, prefix, recursive, toc, ignore) + findFiles(asset.Path, prefix, recursive, toc, ignore, knownFuncs) } continue } diff --git a/convert_test.go b/convert_test.go new file mode 100644 index 0000000..4bfa9db --- /dev/null +++ b/convert_test.go @@ -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") + } +} diff --git a/testdata/dupname/foo/bar b/testdata/dupname/foo/bar new file mode 100644 index 0000000..ab2a2d9 --- /dev/null +++ b/testdata/dupname/foo/bar @@ -0,0 +1 @@ +// sample file diff --git a/testdata/dupname/foo_bar b/testdata/dupname/foo_bar new file mode 100644 index 0000000..ab2a2d9 --- /dev/null +++ b/testdata/dupname/foo_bar @@ -0,0 +1 @@ +// sample file