Skip to content

Commit

Permalink
Make LazyFileReader use the Afero source fs
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jul 30, 2016
1 parent 9489272 commit a0859dc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ func (s *Site) reReadFile(absFilePath string) (*source.File, error) {
jww.INFO.Println("rereading", absFilePath)
var file *source.File

reader, err := source.NewLazyFileReader(absFilePath)
reader, err := source.NewLazyFileReader(hugofs.Source(), absFilePath)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions source/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 9 additions & 8 deletions source/lazy_file_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down
28 changes: 17 additions & 11 deletions source/lazy_file_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit a0859dc

Please sign in to comment.