source: Make sure .File.Dir() ends with a slash

Updates #4190
This commit is contained in:
Bjørn Erik Pedersen
2017-12-28 11:32:02 +01:00
parent 3cdf19e9b7
commit 1b0780dbeb
5 changed files with 35 additions and 15 deletions

View File

@@ -162,9 +162,12 @@ func (fi *FileInfo) init() {
}
func (sp *SourceSpec) NewFileInfo(baseDir, filename string, fi os.FileInfo) *FileInfo {
dir, name := filepath.Split(filename)
dir = strings.TrimSuffix(dir, helpers.FilePathSeparator)
dir, name := filepath.Split(filename)
if !strings.HasSuffix(dir, helpers.FilePathSeparator) {
dir = dir + helpers.FilePathSeparator
}
baseDir = strings.TrimSuffix(baseDir, helpers.FilePathSeparator)
relDir := ""

View File

@@ -14,9 +14,31 @@
package source
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestFileInfo(t *testing.T) {
assert := require.New(t)
s := newTestSourceSpec()
for _, this := range []struct {
base string
filename string
assert func(f *FileInfo)
}{
{"/a/", filepath.FromSlash("/a/b/page.md"), func(f *FileInfo) {
assert.Equal(filepath.FromSlash("/a/b/page.md"), f.Filename())
assert.Equal(filepath.FromSlash("b/"), f.Dir())
assert.Equal(filepath.FromSlash("b/page.md"), f.Path())
}},
} {
f := s.NewFileInfo(this.base, this.filename, nil)
this.assert(f)
}
}