github.com/neohugo/neohugo@v0.123.8/hugolib/content_factory_test.go (about)

     1  package hugolib
     2  
     3  import (
     4  	"bytes"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	qt "github.com/frankban/quicktest"
     9  	"github.com/neohugo/neohugo/hugofs"
    10  )
    11  
    12  func TestContentFactory(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	c := qt.New(t)
    16  
    17  	c.Run("Simple", func(c *qt.C) {
    18  		workingDir := "/my/work"
    19  		b := newTestSitesBuilder(c)
    20  		b.WithWorkingDir(workingDir).WithConfigFile("toml", `
    21  
    22  workingDir="/my/work"
    23  
    24  [module]
    25  [[module.mounts]]
    26  source = 'mcontent/en'
    27  target = 'content'
    28  lang  = 'en'
    29  [[module.mounts]]
    30  source = 'archetypes'
    31  target = 'archetypes'
    32  
    33  `)
    34  
    35  		b.WithSourceFile(filepath.Join("mcontent/en/bundle", "index.md"), "")
    36  
    37  		b.WithSourceFile(filepath.Join("archetypes", "post.md"), `---
    38  title: "{{ replace .Name "-" " " | title }}"
    39  date: {{ .Date }}
    40  draft: true
    41  ---
    42  
    43  Hello World.
    44  `)
    45  		b.CreateSites()
    46  		cf := NewContentFactory(b.H)
    47  		abs, err := cf.CreateContentPlaceHolder(filepath.FromSlash("mcontent/en/blog/mypage.md"), false)
    48  		b.Assert(err, qt.IsNil)
    49  		b.Assert(abs, qt.Equals, filepath.FromSlash("/my/work/mcontent/en/blog/mypage.md"))
    50  		b.Build(BuildCfg{SkipRender: true})
    51  
    52  		p := b.H.GetContentPage(abs)
    53  		b.Assert(p, qt.Not(qt.IsNil))
    54  
    55  		var buf bytes.Buffer
    56  		fi, err := b.H.BaseFs.Archetypes.Fs.Stat("post.md")
    57  		b.Assert(err, qt.IsNil)
    58  		b.Assert(cf.ApplyArchetypeFi(&buf, p, "", fi.(hugofs.FileMetaInfo)), qt.IsNil)
    59  
    60  		b.Assert(buf.String(), qt.Contains, `title: "Mypage"`)
    61  	})
    62  
    63  	// Issue #9129
    64  	c.Run("Content in both project and theme", func(c *qt.C) {
    65  		b := newTestSitesBuilder(c)
    66  		b.WithConfigFile("toml", `
    67  theme = 'ipsum'
    68  `)
    69  
    70  		themeDir := filepath.Join("themes", "ipsum")
    71  		b.WithSourceFile("content/posts/foo.txt", `Hello.`)
    72  		b.WithSourceFile(filepath.Join(themeDir, "content/posts/foo.txt"), `Hello.`)
    73  		b.CreateSites()
    74  		cf := NewContentFactory(b.H)
    75  		abs, err := cf.CreateContentPlaceHolder(filepath.FromSlash("posts/test.md"), false)
    76  		b.Assert(err, qt.IsNil)
    77  		b.Assert(abs, qt.Equals, filepath.FromSlash("content/posts/test.md"))
    78  	})
    79  }