Skip to content

Commit

Permalink
Fix archetype handling of directories in theme
Browse files Browse the repository at this point in the history
Fixes #5318
  • Loading branch information
bep committed Oct 26, 2018
1 parent 6b78b38 commit 7857863
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
10 changes: 6 additions & 4 deletions create/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewContent(
siteUsed := false

if archetypeFilename != "" {

var err error
siteUsed, err = usesSiteVar(archetypeFs, archetypeFilename)
if err != nil {
Expand Down Expand Up @@ -130,7 +131,7 @@ func newContentFromDir(
// Just copy the file to destination.
in, err := sourceFs.Open(filename)
if err != nil {
return err
return errors.Wrap(err, "failed to open non-content file")
}

targetFilename := filepath.Join(targetPath, strings.TrimPrefix(filename, archetypeDir))
Expand Down Expand Up @@ -158,11 +159,11 @@ func newContentFromDir(

content, err := executeArcheTypeAsTemplate(s, name, archetypeDir, targetFilename, filename)
if err != nil {
return err
return errors.Wrap(err, "failed to execute archetype template")
}

if err := helpers.SafeWriteToDisk(targetFilename, bytes.NewReader(content), targetFs); err != nil {
return err
return errors.Wrap(err, "failed to save results")
}
}

Expand All @@ -189,6 +190,7 @@ func mapArcheTypeDir(
var m archetypeMap

walkFn := func(filename string, fi os.FileInfo, err error) error {

if err != nil {
return err
}
Expand Down Expand Up @@ -216,7 +218,7 @@ func mapArcheTypeDir(
}

if err := helpers.SymbolicWalk(fs, archetypeDir, walkFn); err != nil {
return m, err
return m, errors.Wrapf(err, "failed to walk archetype dir %q", archetypeDir)
}

return m, nil
Expand Down
12 changes: 12 additions & 0 deletions create/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func TestNewContentFromDir(t *testing.T) {
archetypeDir := filepath.Join("archetypes", "my-bundle")
assert.NoError(fs.Source.Mkdir(archetypeDir, 0755))

archetypeThemeDir := filepath.Join("themes", "mytheme", "archetypes", "my-theme-bundle")
assert.NoError(fs.Source.Mkdir(archetypeThemeDir, 0755))

contentFile := `
File: %s
Site Lang: {{ .Site.Language.Lang }}
Expand All @@ -107,6 +110,9 @@ i18n: {{ T "hugo" }}
assert.NoError(afero.WriteFile(fs.Source, filepath.Join(archetypeDir, "resources", "hugo1.json"), []byte(`hugo1: {{ printf "no template handling in here" }}`), 0755))
assert.NoError(afero.WriteFile(fs.Source, filepath.Join(archetypeDir, "resources", "hugo2.xml"), []byte(`hugo2: {{ printf "no template handling in here" }}`), 0755))

assert.NoError(afero.WriteFile(fs.Source, filepath.Join(archetypeThemeDir, "index.md"), []byte(fmt.Sprintf(contentFile, "index.md")), 0755))
assert.NoError(afero.WriteFile(fs.Source, filepath.Join(archetypeThemeDir, "resources", "hugo1.json"), []byte(`hugo1: {{ printf "no template handling in here" }}`), 0755))

h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs})
assert.NoError(err)
assert.Equal(2, len(h.Sites))
Expand All @@ -123,6 +129,10 @@ i18n: {{ T "hugo" }}

assertContains(assert, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-post/pages/bio.md")), `File: bio.md`, `Site Lang: en`, `Name: My Post`)

assert.NoError(create.NewContent(h, "my-theme-bundle", "post/my-theme-post"))
assertContains(assert, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-theme-post/index.md")), `File: index.md`, `Site Lang: en`, `Name: My Theme Post`, `i18n: Hugo Rocks!`)
assertContains(assert, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-theme-post/resources/hugo1.json")), `hugo1: {{ printf "no template handling in here" }}`)

}

func initFs(fs *hugofs.Fs) error {
Expand Down Expand Up @@ -231,6 +241,8 @@ func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string {
func newTestCfg(assert *require.Assertions) (*viper.Viper, *hugofs.Fs) {

cfg := `
theme = "mytheme"
[languages]
[languages.en]
Expand Down
3 changes: 3 additions & 0 deletions hugolib/filesystems/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ func (b *sourceFilesystemsBuilder) createFs(
}

if b.hasTheme {
if !strings.HasPrefix(themeFolder, filePathSeparator) {
themeFolder = filePathSeparator + themeFolder
}
themeFolderFs := newRealBase(afero.NewBasePathFs(b.themeFs, themeFolder))
if fs == nil {
fs = themeFolderFs
Expand Down
30 changes: 22 additions & 8 deletions hugolib/filesystems/basefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ func TestNewBaseFs(t *testing.T) {

// Write some data to the themes
for _, theme := range themes {
for _, dir := range []string{"i18n", "data"} {
for _, dir := range []string{"i18n", "data", "archetypes", "layouts"} {
base := filepath.Join(workingDir, "themes", theme, dir)
filename := filepath.Join(base, fmt.Sprintf("theme-file-%s.txt", theme))
fs.Source.Mkdir(base, 0755)
afero.WriteFile(fs.Source, filepath.Join(base, fmt.Sprintf("theme-file-%s-%s.txt", theme, dir)), []byte(fmt.Sprintf("content:%s:%s", theme, dir)), 0755)
afero.WriteFile(fs.Source, filename, []byte(fmt.Sprintf("content:%s:%s", theme, dir)), 0755)
}
// Write some files to the root of the theme
base := filepath.Join(workingDir, "themes", theme)
afero.WriteFile(fs.Source, filepath.Join(base, fmt.Sprintf("theme-root-%s.txt", theme)), []byte(fmt.Sprintf("content:%s", theme)), 0755)
afero.WriteFile(fs.Source, filepath.Join(base, "file-root.txt"), []byte(fmt.Sprintf("content:%s", theme)), 0755)
afero.WriteFile(fs.Source, filepath.Join(base, "file-theme-root.txt"), []byte(fmt.Sprintf("content:%s", theme)), 0755)
}

afero.WriteFile(fs.Source, filepath.Join(workingDir, "file-root.txt"), []byte("content-project"), 0755)
Expand Down Expand Up @@ -102,13 +103,13 @@ theme = ["atheme"]

checkFileCount(bfs.Content.Fs, "", assert, 3)
checkFileCount(bfs.I18n.Fs, "", assert, 6) // 4 + 2 themes
checkFileCount(bfs.Layouts.Fs, "", assert, 5)
checkFileCount(bfs.Layouts.Fs, "", assert, 7)
checkFileCount(bfs.Static[""].Fs, "", assert, 6)
checkFileCount(bfs.Data.Fs, "", assert, 9) // 7 + 2 themes
checkFileCount(bfs.Archetypes.Fs, "", assert, 8)
checkFileCount(bfs.Data.Fs, "", assert, 9) // 7 + 2 themes
checkFileCount(bfs.Archetypes.Fs, "", assert, 10) // 8 + 2 themes
checkFileCount(bfs.Assets.Fs, "", assert, 9)
checkFileCount(bfs.Resources.Fs, "", assert, 10)
checkFileCount(bfs.Work.Fs, "", assert, 69)
checkFileCount(bfs.Work.Fs, "", assert, 78)

assert.Equal([]string{filepath.FromSlash("/my/work/mydata"), filepath.FromSlash("/my/work/themes/btheme/data"), filepath.FromSlash("/my/work/themes/atheme/data")}, bfs.Data.Dirnames)

Expand All @@ -127,6 +128,18 @@ theme = ["atheme"]
checkFileContent(bfs.Work.Fs, "file-root.txt", assert, "content-project")
checkFileContent(bfs.Work.Fs, "theme-root-atheme.txt", assert, "content:atheme")

// https://github.com/gohugoio/hugo/issues/5318
// Check both project and theme.
for _, fs := range []afero.Fs{bfs.Archetypes.Fs, bfs.Layouts.Fs} {
for _, filename := range []string{"/file1.txt", "/theme-file-atheme.txt"} {
filename = filepath.FromSlash(filename)
f, err := fs.Open(filename)
assert.NoError(err)
name := f.Name()
f.Close()
assert.Equal(filename, name)
}
}
}

func createConfig() *viper.Viper {
Expand Down Expand Up @@ -344,6 +357,7 @@ func setConfigAndWriteSomeFilesTo(fs afero.Fs, v *viper.Viper, key, val string,
v.Set(key, val)
fs.Mkdir(val, 0755)
for i := 0; i < num; i++ {
afero.WriteFile(fs, filepath.Join(workingDir, val, fmt.Sprintf("file%d.txt", i+1)), []byte(fmt.Sprintf("content:%s:%d", key, i+1)), 0755)
filename := filepath.Join(workingDir, val, fmt.Sprintf("file%d.txt", i+1))
afero.WriteFile(fs, filename, []byte(fmt.Sprintf("content:%s:%d", key, i+1)), 0755)
}
}

0 comments on commit 7857863

Please sign in to comment.