Skip to content

Commit e79f7cb

Browse files
committed
convert: fix missing assets due to symlinks pointing to the same source
in different inputs Symlinks implementation was added in 465fc29. However, that did not account for the fact that multiple symlinks can point to the same source. When that happens, we would still want assets to be generated accordingly. This commit fixes missing assets due to symlinks pointing to the same source in different inputs. Consider the following example: symlinkFile └── file1 -> ../symlinkSrc/file1 symlinkFileDup └── file1 -> ../symlinkSrc/file1 symlinkSrc └── file1 If both symlinkFile and symlinkFileDup are passed to go-bindata, we would still want both assets to be generated. The current implementation only generates the first asset encountered, which is symlinkFile/file1. That said, we still have a bug in which multiple symlinks in the same *input* pointing to the same source won't be generated properly. Consider the following case: ├── file1 -> ../symlinkSrc/file1 └── file2 -> ../symlinkSrc/file1 We would expect both file1 and file2 to be generated, but that is not the case. Fixing this requires a much more thorough approach as the current implementation does not support that easily. For now, we could generate those two files by passing both file1 and file2 as separate *inputs*. Signed-off-by: Jay Lim <[email protected]>
1 parent 60ffea5 commit e79f7cb

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func Translate(c *Config) error {
2626
}
2727

2828
var knownFuncs = make(map[string]int)
29-
var visitedPaths = make(map[string]bool)
3029
// Locate all the assets.
3130
for _, input := range c.Input {
31+
var visitedPaths = make(map[string]bool)
3232
if err := findFiles(input.Path, c.Prefix, input.Recursive, &toc, c.Ignore, knownFuncs, visitedPaths); err != nil {
3333
return err
3434
}

convert_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bindata
22

33
import (
4+
"os"
45
"regexp"
56
"strings"
67
"testing"
@@ -134,3 +135,37 @@ func TestNoPrefixExtensionMatch(t *testing.T) {
134135
t.Fatal("should have returned err retrieving nonexistent file, got nil")
135136
}
136137
}
138+
139+
func TestTranslate(t *testing.T) {
140+
t.Run("multiple symlinks pointing to the same file", func(t *testing.T) {
141+
// Build the config.
142+
c := NewConfig()
143+
c.Input = []InputConfig{
144+
{Path: "testdata/symlinkFile", Recursive: true},
145+
{Path: "testdata/symlinkFileDup", Recursive: true},
146+
}
147+
c.Output = "testdata/symlink_test.go" // Dummy value that isn't used
148+
149+
// Stub out implementation for diffAndWrite.
150+
var output string
151+
oldDiffAndWrite := diffAndWrite
152+
diffAndWrite = func(filename string, data []byte, mode os.FileMode) error {
153+
output = string(data)
154+
return nil
155+
}
156+
defer func() { diffAndWrite = oldDiffAndWrite }()
157+
158+
if err := Translate(c); err != nil {
159+
t.Fatal(err)
160+
}
161+
162+
if len(output) == 0 {
163+
t.Fatal("should have data in output file")
164+
}
165+
166+
if !strings.Contains(output, "testdata/symlinkFile/file1") ||
167+
!strings.Contains(output, "testdata/symlinkFileDup/file1") {
168+
t.Fatal("should have data for both symlinkFile and symlinkFileDup")
169+
}
170+
})
171+
}

filewriter.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"os"
88
)
99

10-
func diffAndWrite(filename string, data []byte, mode os.FileMode) error {
10+
// Note: diffAndWrite is doubled for testing purposes.
11+
var diffAndWrite = diffAndWriteImpl
12+
13+
func diffAndWriteImpl(filename string, data []byte, mode os.FileMode) error {
1114
// If the file has the same contents as data, try to avoid a write.
1215
f, err := os.Open(filename)
1316
if err != nil {

testdata/symlinkFileDup/file1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../symlinkSrc/file1

0 commit comments

Comments
 (0)