Use OS fs by default

Fixes #3032
This commit is contained in:
Bjørn Erik Pedersen 2017-02-15 10:00:34 +01:00
parent a988d3cf36
commit 691156c5ba
6 changed files with 69 additions and 28 deletions

View file

@ -48,7 +48,7 @@ func TestNewContent(t *testing.T) {
}
for _, c := range cases {
s, err := hugolib.NewEnglishSite()
s, err := hugolib.NewEnglishSiteMem()
require.NoError(t, err)
require.NoError(t, initFs(s.Fs))

4
deps/deps.go vendored
View file

@ -67,8 +67,8 @@ func New(cfg DepsCfg) *Deps {
}
if fs == nil {
// Default to the most used file systems.
fs = hugofs.NewMem()
// Default to the production file systems.
fs = hugofs.NewDefault()
}
d := &Deps{

View file

@ -148,6 +148,7 @@ func NewSite(cfg deps.DepsCfg) (*Site, error) {
return s, nil
}
// TODO(bep) globals clean below...
// NewSiteDefaultLang creates a new site in the default language.
// The site will have a template system loaded and ready to use.
// Note: This is mainly used in single site tests.
@ -155,14 +156,32 @@ func NewSiteDefaultLang(withTemplate ...func(templ tplapi.Template) error) (*Sit
return newSiteForLang(helpers.NewDefaultLanguage(), withTemplate...)
}
// NewSiteDefaultLang creates a new site in the default language.
// NewEnglishSite creates a new site in English language.
// The site will have a template system loaded and ready to use.
// Note: This is mainly used in single site tests.
func NewEnglishSite(withTemplate ...func(templ tplapi.Template) error) (*Site, error) {
return newSiteForLang(helpers.NewLanguage("en"), withTemplate...)
}
// NewSiteDefaultLang creates a new site in the default language.
// NewEnglishSite creates a new site in the English language with in-memory Fs.
// The site will have a template system loaded and ready to use.
// Note: This is mainly used in single site tests.
func NewEnglishSiteMem(withTemplate ...func(templ tplapi.Template) error) (*Site, error) {
withTemplates := func(templ tplapi.Template) error {
for _, wt := range withTemplate {
if err := wt(templ); err != nil {
return err
}
}
return nil
}
cfg := deps.DepsCfg{WithTemplate: withTemplates, Language: helpers.NewLanguage("en"), Fs: hugofs.NewMem()}
return newSiteForCfg(cfg)
}
// newSiteForLang creates a new site in the given language.
func newSiteForLang(lang *helpers.Language, withTemplate ...func(templ tplapi.Template) error) (*Site, error) {
withTemplates := func(templ tplapi.Template) error {
for _, wt := range withTemplate {
@ -173,6 +192,12 @@ func newSiteForLang(lang *helpers.Language, withTemplate ...func(templ tplapi.Te
return nil
}
cfg := deps.DepsCfg{WithTemplate: withTemplates, Language: lang}
return newSiteForCfg(cfg)
}
func newSiteForCfg(cfg deps.DepsCfg) (*Site, error) {
s, err := newSite(cfg)
if err != nil {

View file

@ -355,6 +355,13 @@ func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
}
func TestNewSiteDefaultLang(t *testing.T) {
s, err := NewSiteDefaultLang()
require.NoError(t, err)
require.Equal(t, hugofs.Os, s.Fs.Source)
require.Equal(t, hugofs.Os, s.Fs.Destination)
}
// Issue #1176
func TestSectionNaming(t *testing.T) {

View file

@ -50,13 +50,17 @@ import (
)
var (
logger = jww.NewNotepad(jww.LevelFatal, jww.LevelFatal, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
defaultDepsConfig = deps.DepsCfg{
logger = jww.NewNotepad(jww.LevelFatal, jww.LevelFatal, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
)
func newDefaultDepsCfg() deps.DepsCfg {
return deps.DepsCfg{
Language: helpers.NewLanguage("en"),
Fs: hugofs.NewMem(),
Logger: logger,
TemplateProvider: DefaultTemplateProvider,
}
)
}
type tstNoStringer struct {
}
@ -268,7 +272,7 @@ urlize: bat-man
tstInitTemplates()
config := defaultDepsConfig
config := newDefaultDepsCfg()
config.WithTemplate = func(templ tplapi.Template) error {
if _, err := templ.New("test").Parse(in); err != nil {
t.Fatal("Got error on parse", err)
@ -2798,7 +2802,9 @@ func TestPartialCached(t *testing.T) {
tmp = tc.tmpl
}
defaultDepsConfig.WithTemplate = func(templ tplapi.Template) error {
cfg := newDefaultDepsCfg()
cfg.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate("testroot", tmp)
if err != nil {
return err
@ -2811,7 +2817,7 @@ func TestPartialCached(t *testing.T) {
return nil
}
de := deps.New(defaultDepsConfig)
de := deps.New(cfg)
require.NoError(t, de.LoadTemplates())
buf := new(bytes.Buffer)
@ -2836,7 +2842,8 @@ func TestPartialCached(t *testing.T) {
}
func BenchmarkPartial(b *testing.B) {
defaultDepsConfig.WithTemplate = func(templ tplapi.Template) error {
cfg := newDefaultDepsCfg()
cfg.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate("testroot", `{{ partial "bench1" . }}`)
if err != nil {
return err
@ -2849,7 +2856,7 @@ func BenchmarkPartial(b *testing.B) {
return nil
}
de := deps.New(defaultDepsConfig)
de := deps.New(cfg)
require.NoError(b, de.LoadTemplates())
buf := new(bytes.Buffer)
@ -2866,7 +2873,8 @@ func BenchmarkPartial(b *testing.B) {
}
func BenchmarkPartialCached(b *testing.B) {
defaultDepsConfig.WithTemplate = func(templ tplapi.Template) error {
cfg := newDefaultDepsCfg()
cfg.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate("testroot", `{{ partialCached "bench1" . }}`)
if err != nil {
return err
@ -2879,7 +2887,7 @@ func BenchmarkPartialCached(b *testing.B) {
return nil
}
de := deps.New(defaultDepsConfig)
de := deps.New(cfg)
require.NoError(b, de.LoadTemplates())
buf := new(bytes.Buffer)
@ -2896,7 +2904,8 @@ func BenchmarkPartialCached(b *testing.B) {
}
func newTestFuncster() *templateFuncster {
d := deps.New(defaultDepsConfig)
cfg := newDefaultDepsCfg()
d := deps.New(cfg)
if err := d.LoadTemplates(); err != nil {
panic(err)
}
@ -2905,7 +2914,8 @@ func newTestFuncster() *templateFuncster {
}
func newTestTemplate(t *testing.T, name, template string) *template.Template {
defaultDepsConfig.WithTemplate = func(templ tplapi.Template) error {
cfg := newDefaultDepsCfg()
cfg.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplate(name, template)
if err != nil {
return err
@ -2913,7 +2923,7 @@ func newTestTemplate(t *testing.T, name, template string) *template.Template {
return nil
}
de := deps.New(defaultDepsConfig)
de := deps.New(cfg)
require.NoError(t, de.LoadTemplates())
return de.Tmpl.Lookup(name)

View file

@ -27,7 +27,6 @@ import (
"github.com/spf13/afero"
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
@ -80,7 +79,7 @@ html lang=en
d := "DATA"
config := defaultDepsConfig
config := newDefaultDepsCfg()
config.WithTemplate = func(templ tplapi.Template) error {
return templ.AddAceTemplate("mytemplate.ace", basePath, innerPath,
[]byte(this.baseContent), []byte(this.innerContent))
@ -149,7 +148,8 @@ func TestAddTemplateFileWithMaster(t *testing.T) {
masterTplName := "mt"
finalTplName := "tp"
defaultDepsConfig.WithTemplate = func(templ tplapi.Template) error {
cfg := newDefaultDepsCfg()
cfg.WithTemplate = func(templ tplapi.Template) error {
err := templ.AddTemplateFileWithMaster(finalTplName, overlayTplName, masterTplName)
@ -188,16 +188,14 @@ func TestAddTemplateFileWithMaster(t *testing.T) {
return nil
}
defaultDepsConfig.Fs = hugofs.NewMem()
if this.writeSkipper != 1 {
afero.WriteFile(defaultDepsConfig.Fs.Source, masterTplName, []byte(this.masterTplContent), 0644)
afero.WriteFile(cfg.Fs.Source, masterTplName, []byte(this.masterTplContent), 0644)
}
if this.writeSkipper != 2 {
afero.WriteFile(defaultDepsConfig.Fs.Source, overlayTplName, []byte(this.overlayTplContent), 0644)
afero.WriteFile(cfg.Fs.Source, overlayTplName, []byte(this.overlayTplContent), 0644)
}
deps.New(defaultDepsConfig)
deps.New(cfg)
}
@ -286,11 +284,12 @@ func TestTplGoFuzzReports(t *testing.T) {
H: "a,b,c,d,e,f",
}
defaultDepsConfig.WithTemplate = func(templ tplapi.Template) error {
cfg := newDefaultDepsCfg()
cfg.WithTemplate = func(templ tplapi.Template) error {
return templ.AddTemplate("fuzz", this.data)
}
de := deps.New(defaultDepsConfig)
de := deps.New(cfg)
require.NoError(t, de.LoadTemplates())
templ := de.Tmpl.(*GoHTMLTemplate)