summaryrefslogtreecommitdiffhomepage
path: root/create
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2017-02-05 10:20:06 +0700
committerBjørn Erik Pedersen <[email protected]>2017-02-17 17:15:26 +0100
commit93ca7c9e958e34469a337e4efcc7c75774ec50fd (patch)
tree5dfa296cfe74fd5ef8f0d41ea4078704f453aa04 /create
parente34af6ee30f70f5780a281e2fd8f4ed9b487ee61 (diff)
downloadhugo-93ca7c9e958e34469a337e4efcc7c75774ec50fd.tar.gz
hugo-93ca7c9e958e34469a337e4efcc7c75774ec50fd.zip
all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables the use if `t.Parallel` in tests. Updates #2701 Fixes #3016
Diffstat (limited to 'create')
-rw-r--r--create/content.go34
-rw-r--r--create/content_test.go43
2 files changed, 45 insertions, 32 deletions
diff --git a/create/content.go b/create/content.go
index 6a03c8c9b..d38d3c7fb 100644
--- a/create/content.go
+++ b/create/content.go
@@ -29,7 +29,6 @@ import (
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/parser"
jww "github.com/spf13/jwalterweatherman"
- "github.com/spf13/viper"
)
// NewContent creates a new content file in the content directory based upon the
@@ -37,7 +36,7 @@ import (
func NewContent(s *hugolib.Site, kind, name string) (err error) {
jww.INFO.Println("attempting to create ", name, "of", kind)
- location := FindArchetype(s.Fs.Source, kind)
+ location := FindArchetype(s, kind)
var by []byte
@@ -67,23 +66,23 @@ func NewContent(s *hugolib.Site, kind, name string) (err error) {
return err
}
- if err = page.SetSourceMetaData(metadata, parser.FormatToLeadRune(viper.GetString("metaDataFormat"))); err != nil {
+ if err = page.SetSourceMetaData(metadata, parser.FormatToLeadRune(s.Cfg.GetString("metaDataFormat"))); err != nil {
return
}
page.SetSourceContent(psr.Content())
- if err = page.SafeSaveSourceAs(filepath.Join(viper.GetString("contentDir"), name)); err != nil {
+ if err = page.SafeSaveSourceAs(filepath.Join(s.Cfg.GetString("contentDir"), name)); err != nil {
return
}
- jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created")
+ jww.FEEDBACK.Println(s.PathSpec.AbsPathify(filepath.Join(s.Cfg.GetString("contentDir"), name)), "created")
- editor := viper.GetString("newContentEditor")
+ editor := s.Cfg.GetString("newContentEditor")
if editor != "" {
jww.FEEDBACK.Printf("Editing %s with %q ...\n", name, editor)
- cmd := exec.Command(editor, helpers.AbsPathify(path.Join(viper.GetString("contentDir"), name)))
+ cmd := exec.Command(editor, s.PathSpec.AbsPathify(path.Join(s.Cfg.GetString("contentDir"), name)))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -138,12 +137,7 @@ func createMetadata(archetype parser.Page, name string) (map[string]interface{},
metadata["title"] = helpers.MakeTitle(helpers.Filename(name))
}
- // TOD(bep) what is this?
- if x := parser.FormatSanitize(viper.GetString("metaDataFormat")); x == "json" || x == "yaml" || x == "toml" {
- metadata["date"] = date.Format(time.RFC3339)
- } else {
- metadata["date"] = date
- }
+ metadata["date"] = date.Format(time.RFC3339)
return metadata, nil
}
@@ -151,13 +145,13 @@ func createMetadata(archetype parser.Page, name string) (map[string]interface{},
// FindArchetype takes a given kind/archetype of content and returns an output
// path for that archetype. If no archetype is found, an empty string is
// returned.
-func FindArchetype(fs afero.Fs, kind string) (outpath string) {
- search := []string{helpers.AbsPathify(viper.GetString("archetypeDir"))}
+func FindArchetype(s *hugolib.Site, kind string) (outpath string) {
+ search := []string{s.PathSpec.AbsPathify(s.Cfg.GetString("archetypeDir"))}
- if viper.GetString("theme") != "" {
- themeDir := filepath.Join(helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), "/archetypes/")
- if _, err := fs.Stat(themeDir); os.IsNotExist(err) {
- jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", viper.GetString("theme"), themeDir)
+ if s.Cfg.GetString("theme") != "" {
+ themeDir := filepath.Join(s.PathSpec.AbsPathify(s.Cfg.GetString("themesDir")+"/"+s.Cfg.GetString("theme")), "/archetypes/")
+ if _, err := s.Fs.Source.Stat(themeDir); os.IsNotExist(err) {
+ jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", s.Cfg.GetString("theme"), themeDir)
} else {
search = append(search, themeDir)
}
@@ -177,7 +171,7 @@ func FindArchetype(fs afero.Fs, kind string) (outpath string) {
for _, p := range pathsToCheck {
curpath := filepath.Join(x, p)
jww.DEBUG.Println("checking", curpath, "for archetypes")
- if exists, _ := helpers.Exists(curpath, fs); exists {
+ if exists, _ := helpers.Exists(curpath, s.Fs.Source); exists {
jww.INFO.Println("curpath: " + curpath)
return curpath
}
diff --git a/create/content_test.go b/create/content_test.go
index fcfecff10..4ef230202 100644
--- a/create/content_test.go
+++ b/create/content_test.go
@@ -19,6 +19,8 @@ import (
"strings"
"testing"
+ "github.com/spf13/hugo/deps"
+
"github.com/spf13/hugo/hugolib"
"fmt"
@@ -33,7 +35,8 @@ import (
)
func TestNewContent(t *testing.T) {
- initViper()
+ v := viper.New()
+ initViper(v)
cases := []struct {
kind string
@@ -48,14 +51,17 @@ func TestNewContent(t *testing.T) {
}
for _, c := range cases {
- s, err := hugolib.NewEnglishSiteMem()
+ cfg, fs := newTestCfg()
+ h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs})
require.NoError(t, err)
- require.NoError(t, initFs(s.Fs))
+ require.NoError(t, initFs(fs))
+
+ s := h.Sites[0]
require.NoError(t, create.NewContent(s, c.kind, c.path))
fname := filepath.Join("content", filepath.FromSlash(c.path))
- content := readFileFromFs(t, s.Fs.Source, fname)
+ content := readFileFromFs(t, fs.Source, fname)
for i, v := range c.expected {
found := strings.Contains(content, v)
if !found {
@@ -65,14 +71,14 @@ func TestNewContent(t *testing.T) {
}
}
-func initViper() {
- viper.Reset()
- viper.Set("metaDataFormat", "toml")
- viper.Set("archetypeDir", "archetypes")
- viper.Set("contentDir", "content")
- viper.Set("themesDir", "themes")
- viper.Set("layoutDir", "layouts")
- viper.Set("theme", "sample")
+func initViper(v *viper.Viper) {
+ v.Set("metaDataFormat", "toml")
+ v.Set("archetypeDir", "archetypes")
+ v.Set("contentDir", "content")
+ v.Set("themesDir", "themes")
+ v.Set("layoutDir", "layouts")
+ v.Set("i18nDir", "i18n")
+ v.Set("theme", "sample")
}
func initFs(fs *hugofs.Fs) error {
@@ -143,3 +149,16 @@ func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string {
}
return string(b)
}
+
+func newTestCfg() (*viper.Viper, *hugofs.Fs) {
+
+ v := viper.New()
+ fs := hugofs.NewMem(v)
+
+ v.SetFs(fs.Source)
+
+ initViper(v)
+
+ return v, fs
+
+}