diff options
author | Bjørn Erik Pedersen <[email protected]> | 2016-07-30 15:14:41 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2016-07-30 15:21:02 +0200 |
commit | a0859dc67284988a397e265f7e83ea07357e9eb8 (patch) | |
tree | c55c13a55b474281788ea35433ddc121d26dde5e /source | |
parent | 94892726816ce0c0af05e10c4706149f4b4baf1c (diff) | |
download | hugo-a0859dc67284988a397e265f7e83ea07357e9eb8.tar.gz hugo-a0859dc67284988a397e265f7e83ea07357e9eb8.zip |
Make LazyFileReader use the Afero source fs
Fixes #2317
Diffstat (limited to 'source')
-rw-r--r-- | source/filesystem.go | 5 | ||||
-rw-r--r-- | source/lazy_file_reader.go | 17 | ||||
-rw-r--r-- | source/lazy_file_reader_test.go | 28 |
3 files changed, 29 insertions, 21 deletions
diff --git a/source/filesystem.go b/source/filesystem.go index 0ce5808d1..9d723685e 100644 --- a/source/filesystem.go +++ b/source/filesystem.go @@ -14,13 +14,14 @@ package source import ( - "github.com/spf13/hugo/hugofs" "io" "os" "path/filepath" "regexp" "strings" + "github.com/spf13/hugo/hugofs" + "github.com/spf13/viper" "github.com/spf13/hugo/helpers" @@ -84,7 +85,7 @@ func (f *Filesystem) captureFiles() { return err } if b { - rd, err := NewLazyFileReader(filePath) + rd, err := NewLazyFileReader(hugofs.Source(), filePath) if err != nil { return err } diff --git a/source/lazy_file_reader.go b/source/lazy_file_reader.go index 6e75055ad..8ebe8df21 100644 --- a/source/lazy_file_reader.go +++ b/source/lazy_file_reader.go @@ -19,14 +19,15 @@ import ( "errors" "fmt" "io" - "io/ioutil" - "os" + + "github.com/spf13/afero" ) // LazyFileReader is an io.Reader implementation to postpone reading the file // contents until it is really needed. It keeps filename and file contents once // it is read. type LazyFileReader struct { + fs afero.Fs filename string contents *bytes.Reader pos int64 @@ -35,13 +36,13 @@ type LazyFileReader struct { // NewLazyFileReader creates and initializes a new LazyFileReader of filename. // It checks whether the file can be opened. If it fails, it returns nil and an // error. -func NewLazyFileReader(filename string) (*LazyFileReader, error) { - f, err := os.Open(filename) +func NewLazyFileReader(fs afero.Fs, filename string) (*LazyFileReader, error) { + f, err := fs.Open(filename) if err != nil { return nil, err } defer f.Close() - return &LazyFileReader{filename: filename, contents: nil, pos: 0}, nil + return &LazyFileReader{fs: fs, filename: filename, contents: nil, pos: 0}, nil } // Filename returns a file name which LazyFileReader keeps @@ -55,7 +56,7 @@ func (l *LazyFileReader) Filename() string { // the file. func (l *LazyFileReader) Read(p []byte) (n int, err error) { if l.contents == nil { - b, err := ioutil.ReadFile(l.filename) + b, err := afero.ReadFile(l.fs, l.filename) if err != nil { return 0, fmt.Errorf("failed to read content from %s: %s", l.filename, err.Error()) } @@ -80,7 +81,7 @@ func (l *LazyFileReader) Seek(offset int64, whence int) (pos int64, err error) { case 1: pos = l.pos + offset case 2: - fi, err := os.Stat(l.filename) + fi, err := l.fs.Stat(l.filename) if err != nil { return 0, fmt.Errorf("failed to get %q info: %s", l.filename, err.Error()) } @@ -115,7 +116,7 @@ func (l *LazyFileReader) WriteTo(w io.Writer) (n int64, err error) { l.pos += n return n, err } - f, err := os.Open(l.filename) + f, err := l.fs.Open(l.filename) if err != nil { return 0, fmt.Errorf("failed to open %s to read content: %s", l.filename, err.Error()) } diff --git a/source/lazy_file_reader_test.go b/source/lazy_file_reader_test.go index 8da6d0fc2..5522c9f1b 100644 --- a/source/lazy_file_reader_test.go +++ b/source/lazy_file_reader_test.go @@ -19,25 +19,29 @@ import ( "io/ioutil" "os" "testing" + + "github.com/spf13/afero" ) func TestNewLazyFileReader(t *testing.T) { + fs := afero.NewOsFs() filename := "itdoesnotexistfile" - _, err := NewLazyFileReader(filename) + _, err := NewLazyFileReader(fs, filename) if err == nil { t.Errorf("NewLazyFileReader %s: error expected but no error is returned", filename) } filename = "lazy_file_reader_test.go" - _, err = NewLazyFileReader(filename) + _, err = NewLazyFileReader(fs, filename) if err != nil { t.Errorf("NewLazyFileReader %s: %v", filename, err) } } func TestFilename(t *testing.T) { + fs := afero.NewOsFs() filename := "lazy_file_reader_test.go" - rd, err := NewLazyFileReader(filename) + rd, err := NewLazyFileReader(fs, filename) if err != nil { t.Fatalf("NewLazyFileReader %s: %v", filename, err) } @@ -47,18 +51,19 @@ func TestFilename(t *testing.T) { } func TestRead(t *testing.T) { + fs := afero.NewOsFs() filename := "lazy_file_reader_test.go" - fi, err := os.Stat(filename) + fi, err := fs.Stat(filename) if err != nil { t.Fatalf("os.Stat: %v", err) } - b, err := ioutil.ReadFile(filename) + b, err := afero.ReadFile(fs, filename) if err != nil { t.Fatalf("ioutil.ReadFile: %v", err) } - rd, err := NewLazyFileReader(filename) + rd, err := NewLazyFileReader(fs, filename) if err != nil { t.Fatalf("NewLazyFileReader %s: %v", filename, err) } @@ -92,9 +97,9 @@ func TestSeek(t *testing.T) { moveto int64 expected []byte } - + fs := afero.NewOsFs() filename := "lazy_file_reader_test.go" - b, err := ioutil.ReadFile(filename) + b, err := afero.ReadFile(fs, filename) if err != nil { t.Fatalf("ioutil.ReadFile: %v", err) } @@ -108,7 +113,7 @@ func TestSeek(t *testing.T) { {seek: 3, expected: nil}, {seek: os.SEEK_SET, offset: -1, expected: nil}, } { - rd, err := NewLazyFileReader(filename) + rd, err := NewLazyFileReader(fs, filename) if err != nil { t.Errorf("[%d] NewLazyFileReader %s: %v", i, filename, err) continue @@ -140,7 +145,7 @@ func TestSeek(t *testing.T) { } // cache case - rd, err := NewLazyFileReader(filename) + rd, err := NewLazyFileReader(fs, filename) if err != nil { t.Fatalf("NewLazyFileReader %s: %v", filename, err) } @@ -185,6 +190,7 @@ func TestSeek(t *testing.T) { } func TestWriteTo(t *testing.T) { + fs := afero.NewOsFs() filename := "lazy_file_reader_test.go" fi, err := os.Stat(filename) if err != nil { @@ -196,7 +202,7 @@ func TestWriteTo(t *testing.T) { t.Fatalf("ioutil.ReadFile: %v", err) } - rd, err := NewLazyFileReader(filename) + rd, err := NewLazyFileReader(fs, filename) if err != nil { t.Fatalf("NewLazyFileReader %s: %v", filename, err) } |