aboutsummaryrefslogtreecommitdiffhomepage
path: root/source
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2017-01-10 10:55:03 +0100
committerBjørn Erik Pedersen <[email protected]>2017-02-04 11:37:25 +0700
commitc71e1b106e6011d148cac899f83c4685dee33a22 (patch)
treec5c7090f0c2398c7771e4908ebcc97aa7714ffd2 /source
parent0ada40591216572b0e4c6a8ab986b0aa4fb13c13 (diff)
downloadhugo-c71e1b106e6011d148cac899f83c4685dee33a22.tar.gz
hugo-c71e1b106e6011d148cac899f83c4685dee33a22.zip
all: Refactor to nonglobal file systems
Updates #2701 Fixes #2951
Diffstat (limited to 'source')
-rw-r--r--source/filesystem.go15
-rw-r--r--source/filesystem_test.go15
2 files changed, 21 insertions, 9 deletions
diff --git a/source/filesystem.go b/source/filesystem.go
index 7873b47f1..6089824a0 100644
--- a/source/filesystem.go
+++ b/source/filesystem.go
@@ -38,6 +38,12 @@ type Filesystem struct {
files []*File
Base string
AvoidPaths []string
+
+ fs *hugofs.Fs
+}
+
+func NewFilesystem(fs *hugofs.Fs, base string, avoidPaths ...string) *Filesystem {
+ return &Filesystem{fs: fs, Base: base, AvoidPaths: avoidPaths}
}
func (f *Filesystem) FilesByExts(exts ...string) []*File {
@@ -92,7 +98,7 @@ func (f *Filesystem) captureFiles() {
return err
}
if b {
- rd, err := NewLazyFileReader(hugofs.Source(), filePath)
+ rd, err := NewLazyFileReader(f.fs.Source, filePath)
if err != nil {
return err
}
@@ -101,7 +107,10 @@ func (f *Filesystem) captureFiles() {
return err
}
- err := helpers.SymbolicWalk(hugofs.Source(), f.Base, walker)
+ if f.fs == nil {
+ panic("Must have a fs")
+ }
+ err := helpers.SymbolicWalk(f.fs.Source, f.Base, walker)
if err != nil {
jww.ERROR.Println(err)
@@ -119,7 +128,7 @@ func (f *Filesystem) shouldRead(filePath string, fi os.FileInfo) (bool, error) {
jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filePath, err)
return false, nil
}
- linkfi, err := hugofs.Source().Stat(link)
+ linkfi, err := f.fs.Source.Stat(link)
if err != nil {
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
return false, nil
diff --git a/source/filesystem_test.go b/source/filesystem_test.go
index a1e111d2f..598a1b81d 100644
--- a/source/filesystem_test.go
+++ b/source/filesystem_test.go
@@ -19,10 +19,12 @@ import (
"runtime"
"strings"
"testing"
+
+ "github.com/spf13/hugo/hugofs"
)
func TestEmptySourceFilesystem(t *testing.T) {
- src := &Filesystem{Base: "Empty"}
+ src := NewFilesystem(hugofs.NewMem(), "Empty")
if len(src.Files()) != 0 {
t.Errorf("new filesystem should contain 0 files.")
}
@@ -37,13 +39,12 @@ type TestPath struct {
}
func TestAddFile(t *testing.T) {
+ fs := hugofs.NewMem()
tests := platformPaths
for _, test := range tests {
base := platformBase
- srcDefault := new(Filesystem)
- srcWithBase := &Filesystem{
- Base: base,
- }
+ srcDefault := NewFilesystem(fs, "")
+ srcWithBase := NewFilesystem(fs, base)
for _, src := range []*Filesystem{srcDefault, srcWithBase} {
@@ -99,8 +100,10 @@ func TestUnicodeNorm(t *testing.T) {
{NFC: "é", NFD: "\x65\xcc\x81"},
}
+ fs := hugofs.NewMem()
+
for _, path := range paths {
- src := new(Filesystem)
+ src := NewFilesystem(fs, "")
_ = src.add(path.NFD, strings.NewReader(""))
f := src.Files()[0]
if f.BaseFileName() != path.NFC {