diff options
author | Bjørn Erik Pedersen <[email protected]> | 2017-01-10 10:55:03 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2017-02-04 11:37:25 +0700 |
commit | c71e1b106e6011d148cac899f83c4685dee33a22 (patch) | |
tree | c5c7090f0c2398c7771e4908ebcc97aa7714ffd2 /hugofs | |
parent | 0ada40591216572b0e4c6a8ab986b0aa4fb13c13 (diff) | |
download | hugo-c71e1b106e6011d148cac899f83c4685dee33a22.tar.gz hugo-c71e1b106e6011d148cac899f83c4685dee33a22.zip |
all: Refactor to nonglobal file systems
Updates #2701
Fixes #2951
Diffstat (limited to 'hugofs')
-rw-r--r-- | hugofs/fs.go | 84 | ||||
-rw-r--r-- | hugofs/fs_test.go | 60 |
2 files changed, 53 insertions, 91 deletions
diff --git a/hugofs/fs.go b/hugofs/fs.go index 7f8abd337..3afa17956 100644 --- a/hugofs/fs.go +++ b/hugofs/fs.go @@ -19,76 +19,54 @@ import ( "github.com/spf13/viper" ) -var ( - sourceFs afero.Fs - destinationFs afero.Fs - osFs afero.Fs = &afero.OsFs{} - workingDirFs *afero.BasePathFs -) - -// Source returns Hugo's source file system. -func Source() afero.Fs { - return sourceFs -} +// Os points to an Os Afero file system. +var Os = &afero.OsFs{} -// SetSource sets Hugo's source file system -// and re-initializes dependent file systems. -func SetSource(fs afero.Fs) { - sourceFs = fs - initSourceDependencies() -} +type Fs struct { + // Source is Hugo's source file system. + Source afero.Fs -// Destination returns Hugo's destionation file system. -func Destination() afero.Fs { - return destinationFs -} - -// SetDestination sets Hugo's destionation file system -func SetDestination(fs afero.Fs) { - destinationFs = fs -} + // Destination is Hugo's destionation file system. + Destination afero.Fs -// Os returns an OS file system. -func Os() afero.Fs { - return osFs -} + // Os is an OS file system. + Os afero.Fs -// WorkingDir returns a read-only file system -// restricted to the project working dir. -func WorkingDir() *afero.BasePathFs { - return workingDirFs + // WorkingDir is a read-only file system + // restricted to the project working dir. + WorkingDir *afero.BasePathFs } -// InitDefaultFs initializes with the OS file system +// NewDefault creates a new Fs with the OS file system // as source and destination file systems. -func InitDefaultFs() { - InitFs(&afero.OsFs{}) +func NewDefault() *Fs { + fs := &afero.OsFs{} + return newFs(fs) } -// InitMemFs initializes with a MemMapFs as source and destination file systems. +// NewDefault creates a new Fs with the MemMapFs +// as source and destination file systems. // Useful for testing. -func InitMemFs() { - InitFs(&afero.MemMapFs{}) +func NewMem() *Fs { + fs := &afero.MemMapFs{} + return newFs(fs) } -// InitFs initializes with the given file system -// as source and destination file systems. -func InitFs(fs afero.Fs) { - sourceFs = fs - destinationFs = fs - - initSourceDependencies() +func newFs(base afero.Fs) *Fs { + return &Fs{ + Source: base, + Destination: base, + Os: &afero.OsFs{}, + WorkingDir: getWorkingDirFs(base), + } } -func initSourceDependencies() { +func getWorkingDirFs(base afero.Fs) *afero.BasePathFs { workingDir := viper.GetString("workingDir") if workingDir != "" { - workingDirFs = afero.NewBasePathFs(afero.NewReadOnlyFs(sourceFs), workingDir).(*afero.BasePathFs) + return afero.NewBasePathFs(afero.NewReadOnlyFs(base), workingDir).(*afero.BasePathFs) } -} - -func init() { - InitDefaultFs() + return nil } diff --git a/hugofs/fs_test.go b/hugofs/fs_test.go index 55007009f..5482e6d27 100644 --- a/hugofs/fs_test.go +++ b/hugofs/fs_test.go @@ -21,51 +21,35 @@ import ( "github.com/stretchr/testify/assert" ) -func TestInitDefault(t *testing.T) { +func TestNewDefault(t *testing.T) { viper.Reset() defer viper.Reset() - InitDefaultFs() + f := NewDefault() - assert.NotNil(t, Source()) - assert.IsType(t, new(afero.OsFs), Source()) - assert.NotNil(t, Destination()) - assert.IsType(t, new(afero.OsFs), Destination()) - assert.NotNil(t, Os()) - assert.IsType(t, new(afero.OsFs), Os()) - assert.Nil(t, WorkingDir()) + assert.NotNil(t, f.Source) + assert.IsType(t, new(afero.OsFs), f.Source) + assert.NotNil(t, f.Destination) + assert.IsType(t, new(afero.OsFs), f.Destination) + assert.NotNil(t, f.Os) + assert.IsType(t, new(afero.OsFs), f.Os) + assert.Nil(t, f.WorkingDir) + + assert.IsType(t, new(afero.OsFs), Os) } -func TestInitMemFs(t *testing.T) { +func TestNewMem(t *testing.T) { viper.Reset() defer viper.Reset() - InitMemFs() - - assert.NotNil(t, Source()) - assert.IsType(t, new(afero.MemMapFs), Source()) - assert.NotNil(t, Destination()) - assert.IsType(t, new(afero.MemMapFs), Destination()) - assert.IsType(t, new(afero.OsFs), Os()) - assert.Nil(t, WorkingDir()) -} - -func TestSetSource(t *testing.T) { - - InitMemFs() - - SetSource(new(afero.OsFs)) - assert.NotNil(t, Source()) - assert.IsType(t, new(afero.OsFs), Source()) -} - -func TestSetDestination(t *testing.T) { - - InitMemFs() + f := NewMem() - SetDestination(new(afero.OsFs)) - assert.NotNil(t, Destination()) - assert.IsType(t, new(afero.OsFs), Destination()) + assert.NotNil(t, f.Source) + assert.IsType(t, new(afero.MemMapFs), f.Source) + assert.NotNil(t, f.Destination) + assert.IsType(t, new(afero.MemMapFs), f.Destination) + assert.IsType(t, new(afero.OsFs), f.Os) + assert.Nil(t, f.WorkingDir) } func TestWorkingDir(t *testing.T) { @@ -74,8 +58,8 @@ func TestWorkingDir(t *testing.T) { viper.Set("workingDir", "/a/b/") - InitMemFs() + f := NewMem() - assert.NotNil(t, WorkingDir()) - assert.IsType(t, new(afero.BasePathFs), WorkingDir()) + assert.NotNil(t, f.WorkingDir) + assert.IsType(t, new(afero.BasePathFs), f.WorkingDir) } |