Skip to content

Commit

Permalink
create: Improve archetype directory discovery and tests
Browse files Browse the repository at this point in the history
Updates #9146
  • Loading branch information
bep committed Nov 12, 2021
1 parent 057d02d commit 5f3f608
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion create/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ func (b *contentBuilder) setArcheTypeFilenameToUse(ext string) {
if b.kind != "" {
pathsToCheck = append(pathsToCheck, b.kind+ext)
}
pathsToCheck = append(pathsToCheck, "default"+ext, "default")

pathsToCheck = append(pathsToCheck, "default"+ext)

for _, p := range pathsToCheck {
fi, err := b.archeTypeFs.Stat(p)
Expand Down
17 changes: 16 additions & 1 deletion create/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ func TestNewContentFromDirSiteFunction(t *testing.T) {
c := qt.New(t)

archetypeDir := filepath.Join("archetypes", "my-bundle")
defaultArchetypeDir := filepath.Join("archetypes", "default")
c.Assert(mm.MkdirAll(archetypeDir, 0o755), qt.IsNil)
c.Assert(mm.MkdirAll(defaultArchetypeDir, 0o755), qt.IsNil)

contentFile := `
File: %s
Expand All @@ -176,6 +178,7 @@ site RegularPages: {{ len site.RegularPages }}
`

c.Assert(afero.WriteFile(mm, filepath.Join(archetypeDir, "index.md"), []byte(fmt.Sprintf(contentFile, "index.md")), 0o755), qt.IsNil)
c.Assert(afero.WriteFile(mm, filepath.Join(defaultArchetypeDir, "index.md"), []byte("default archetype index.md"), 0o755), qt.IsNil)

c.Assert(initFs(mm), qt.IsNil)
cfg, fs := newTestCfg(c, mm)
Expand All @@ -185,8 +188,20 @@ site RegularPages: {{ len site.RegularPages }}
c.Assert(len(h.Sites), qt.Equals, 2)

c.Assert(create.NewContent(h, "my-bundle", "post/my-post"), qt.IsNil)

cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-post/index.md")), `site RegularPages: 10`)

// Default bundle archetype
c.Assert(create.NewContent(h, "", "post/my-post2"), qt.IsNil)
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-post2/index.md")), `default archetype index.md`)

// Regular file with bundle kind.
c.Assert(create.NewContent(h, "my-bundle", "post/foo.md"), qt.IsNil)
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/foo.md")), `draft: true`)

// Regular files should fall back to the default archetype (we have no regular file archetype).
c.Assert(create.NewContent(h, "my-bundle", "mypage.md"), qt.IsNil)
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "mypage.md")), `draft: true`)

}

func TestNewContentFromDirNoSite(t *testing.T) {
Expand Down

0 comments on commit 5f3f608

Please sign in to comment.