all: Refactor to nonglobal file systems

Updates #2701
Fixes #2951
This commit is contained in:
Bjørn Erik Pedersen
2017-01-10 10:55:03 +01:00
parent 0ada405912
commit c71e1b106e
71 changed files with 2219 additions and 1731 deletions

View File

@@ -34,15 +34,15 @@ import (
// NewContent creates a new content file in the content directory based upon the
// given kind, which is used to lookup an archetype.
func NewContent(fs afero.Fs, kind, name string) (err error) {
func NewContent(s *hugolib.Site, kind, name string) (err error) {
jww.INFO.Println("attempting to create ", name, "of", kind)
location := FindArchetype(fs, kind)
location := FindArchetype(s.Fs.Source, kind)
var by []byte
if location != "" {
by, err = afero.ReadFile(fs, location)
by, err = afero.ReadFile(s.Fs.Source, location)
if err != nil {
jww.ERROR.Println(err)
}
@@ -62,9 +62,7 @@ func NewContent(fs afero.Fs, kind, name string) (err error) {
return err
}
site := hugolib.NewSiteDefaultLang()
page, err := site.NewPage(name)
page, err := s.NewPage(name)
if err != nil {
return err
}

View File

@@ -19,23 +19,22 @@ import (
"strings"
"testing"
"github.com/spf13/hugo/hugolib"
"fmt"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/afero"
"github.com/spf13/hugo/create"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestNewContent(t *testing.T) {
initViper()
err := initFs()
if err != nil {
t.Fatalf("initialization error: %s", err)
}
cases := []struct {
kind string
path string
@@ -48,15 +47,15 @@ func TestNewContent(t *testing.T) {
{"product", "product/sample-4.md", []string{`title = "sample 4"`}}, // empty archetype front matter
}
for i, c := range cases {
err = create.NewContent(hugofs.Source(), c.kind, c.path)
if err != nil {
t.Errorf("[%d] NewContent: %s", i, err)
}
for _, c := range cases {
s, err := hugolib.NewEnglishSite()
require.NoError(t, err)
require.NoError(t, initFs(s.Fs))
require.NoError(t, create.NewContent(s, c.kind, c.path))
fname := filepath.Join("content", filepath.FromSlash(c.path))
content := readFileFromFs(t, hugofs.Source(), fname)
content := readFileFromFs(t, s.Fs.Source, fname)
for i, v := range c.expected {
found := strings.Contains(content, v)
if !found {
@@ -72,11 +71,11 @@ func initViper() {
viper.Set("archetypeDir", "archetypes")
viper.Set("contentDir", "content")
viper.Set("themesDir", "themes")
viper.Set("layoutDir", "layouts")
viper.Set("theme", "sample")
}
func initFs() error {
hugofs.InitMemFs()
func initFs(fs *hugofs.Fs) error {
perm := os.FileMode(0755)
var err error
@@ -87,7 +86,7 @@ func initFs() error {
filepath.Join("themes", "sample", "archetypes"),
}
for _, dir := range dirs {
err = hugofs.Source().Mkdir(dir, perm)
err = fs.Source.Mkdir(dir, perm)
if err != nil {
return err
}
@@ -111,7 +110,7 @@ func initFs() error {
content: "+++\ndate =\"\"\ntitle = \"Empty Date Arch title\"\ntest = \"test1\"\n+++\n",
},
} {
f, err := hugofs.Source().Create(v.path)
f, err := fs.Source.Create(v.path)
if err != nil {
return err
}