diff --git a/bundle/file.go b/bundle/file.go index ab1d1c72a2..183ab7532a 100644 --- a/bundle/file.go +++ b/bundle/file.go @@ -76,6 +76,20 @@ type dirLoader struct { // NewDirectoryLoader returns a basic DirectoryLoader implementation // that will load files from a given root directory path. func NewDirectoryLoader(root string) DirectoryLoader { + + if len(root) > 1 { + // Normalize relative directories, ex "./src/bundle" -> "src/bundle" + // We don't need an absolute path, but this makes the joined/trimmed + // paths more uniform. + if root[0] == '.' && root[1] == filepath.Separator { + if len(root) == 2 { + root = root[:1] // "./" -> "." + } else { + root = root[2:] // remove leading "./" + } + } + } + d := dirLoader{ root: root, } diff --git a/bundle/file_test.go b/bundle/file_test.go index 761ce8fd82..3b4f8b22ee 100644 --- a/bundle/file_test.go +++ b/bundle/file_test.go @@ -106,3 +106,67 @@ func testLoader(t *testing.T, loader DirectoryLoader, expectedFiles map[string]s t.Fatalf("Expected to read %d files, read %d", len(expectedFiles), fileCount) } } + +func TestNewDirectoryLoaderNormalizedRoot(t *testing.T) { + cases := []struct { + note string + root string + expected string + }{ + { + note: "abs", + root: "/a/b/c", + expected: "/a/b/c", + }, + { + note: "trailing slash", + root: "/a/b/c/", + expected: "/a/b/c/", + }, + { + note: "empty", + root: "", + expected: "", + }, + { + note: "single abs", + root: "/", + expected: "/", + }, + { + note: "single relative", + root: "foo", + expected: "foo", + }, + { + note: "single relative dot", + root: ".", + expected: ".", + }, + { + note: "single relative dot slash", + root: "./", + expected: ".", + }, + { + note: "relative leading dot slash", + root: "./a/b/c", + expected: "a/b/c", + }, + { + note: "relative", + root: "a/b/c", + expected: "a/b/c", + }, + } + + for _, tc := range cases { + t.Run(tc.note, func(t *testing.T) { + l := NewDirectoryLoader(tc.root) + actual := l.(*dirLoader).root + if actual != tc.expected { + t.Fatalf("Expected root %s got %s", tc.expected, actual) + } + }) + } +}