diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-01-10 10:55:03 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-02-04 11:37:25 +0700 |
commit | c71e1b106e6011d148cac899f83c4685dee33a22 (patch) | |
tree | c5c7090f0c2398c7771e4908ebcc97aa7714ffd2 /create | |
parent | 0ada40591216572b0e4c6a8ab986b0aa4fb13c13 (diff) |
all: Refactor to nonglobal file systems
Updates #2701
Fixes #2951
Diffstat (limited to 'create')
-rw-r--r-- | create/content.go | 10 | ||||
-rw-r--r-- | create/content_test.go | 33 |
2 files changed, 20 insertions, 23 deletions
diff --git a/create/content.go b/create/content.go index 195080d88..6a03c8c9b 100644 --- a/create/content.go +++ b/create/content.go @@ -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 } diff --git a/create/content_test.go b/create/content_test.go index cdee13fb8..df29527fe 100644 --- a/create/content_test.go +++ b/create/content_test.go @@ -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)) - fname := filepath.Join("content", filepath.FromSlash(c.path)) - content := readFileFromFs(t, hugofs.Source(), fname) + 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) 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 } |