diff options
Diffstat (limited to 'create/content_test.go')
-rw-r--r-- | create/content_test.go | 89 |
1 files changed, 51 insertions, 38 deletions
diff --git a/create/content_test.go b/create/content_test.go index e321900bc..6a3852d29 100644 --- a/create/content_test.go +++ b/create/content_test.go @@ -35,7 +35,6 @@ import ( ) func TestNewContent(t *testing.T) { - assert := require.New(t) cases := []struct { kind string @@ -50,12 +49,12 @@ func TestNewContent(t *testing.T) { {"product", "product/sample-4.md", []string{`title = "SAMPLE-4"`}}, // empty archetype front matter {"lang", "post/lang-1.md", []string{`Site Lang: en|Name: Lang 1|i18n: Hugo Rocks!`}}, {"lang", "post/lang-2.en.md", []string{`Site Lang: en|Name: Lang 2|i18n: Hugo Rocks!`}}, - {"lang", "post/lang-3.nn.md", []string{`Site Lang: nn|Name: Lang 3|i18n: Hugo Rokkar!`}}, + {"lang", "content/post/lang-3.nn.md", []string{`Site Lang: nn|Name: Lang 3|i18n: Hugo Rokkar!`}}, {"lang", "content_nn/post/lang-4.md", []string{`Site Lang: nn|Name: Lang 4|i18n: Hugo Rokkar!`}}, {"lang", "content_nn/post/lang-5.en.md", []string{`Site Lang: en|Name: Lang 5|i18n: Hugo Rocks!`}}, {"lang", "post/my-bundle/index.md", []string{`Site Lang: en|Name: My Bundle|i18n: Hugo Rocks!`}}, {"lang", "post/my-bundle/index.en.md", []string{`Site Lang: en|Name: My Bundle|i18n: Hugo Rocks!`}}, - {"lang", "post/my-bundle/index.nn.md", []string{`Site Lang: nn|Name: My Bundle|i18n: Hugo Rokkar!`}}, + {"lang", "content/post/my-bundle/index.nn.md", []string{`Site Lang: nn|Name: My Bundle|i18n: Hugo Rokkar!`}}, {"shortcodes", "shortcodes/go.md", []string{ `title = "GO"`, "{{< myshortcode >}}", @@ -64,37 +63,43 @@ func TestNewContent(t *testing.T) { } for i, c := range cases { - cfg, fs := newTestCfg(assert) - assert.NoError(initFs(fs)) - h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs}) - assert.NoError(err) - - assert.NoError(create.NewContent(h, c.kind, c.path)) - - fname := filepath.FromSlash(c.path) - if !strings.HasPrefix(fname, "content") { - fname = filepath.Join("content", fname) - } - content := readFileFromFs(t, fs.Source, fname) - for _, v := range c.expected { - found := strings.Contains(content, v) - if !found { - t.Fatalf("[%d] %q missing from output:\n%q", i, v, content) + c := c + t.Run(fmt.Sprintf("%s-%d", c.kind, i), func(t *testing.T) { + t.Parallel() + assert := require.New(t) + mm := afero.NewMemMapFs() + assert.NoError(initFs(mm)) + cfg, fs := newTestCfg(assert, mm) + h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs}) + assert.NoError(err) + + assert.NoError(create.NewContent(h, c.kind, c.path)) + + fname := filepath.FromSlash(c.path) + if !strings.HasPrefix(fname, "content") { + fname = filepath.Join("content", fname) } - } + content := readFileFromFs(t, fs.Source, fname) + for _, v := range c.expected { + found := strings.Contains(content, v) + if !found { + t.Fatalf("[%d] %q missing from output:\n%q", i, v, content) + } + } + }) + } } func TestNewContentFromDir(t *testing.T) { + mm := afero.NewMemMapFs() assert := require.New(t) - cfg, fs := newTestCfg(assert) - assert.NoError(initFs(fs)) archetypeDir := filepath.Join("archetypes", "my-bundle") - assert.NoError(fs.Source.Mkdir(archetypeDir, 0755)) + assert.NoError(mm.MkdirAll(archetypeDir, 0755)) archetypeThemeDir := filepath.Join("themes", "mytheme", "archetypes", "my-theme-bundle") - assert.NoError(fs.Source.Mkdir(archetypeThemeDir, 0755)) + assert.NoError(mm.MkdirAll(archetypeThemeDir, 0755)) contentFile := ` File: %s @@ -103,15 +108,18 @@ Name: {{ replace .Name "-" " " | title }} i18n: {{ T "hugo" }} ` - assert.NoError(afero.WriteFile(fs.Source, filepath.Join(archetypeDir, "index.md"), []byte(fmt.Sprintf(contentFile, "index.md")), 0755)) - assert.NoError(afero.WriteFile(fs.Source, filepath.Join(archetypeDir, "index.nn.md"), []byte(fmt.Sprintf(contentFile, "index.nn.md")), 0755)) + assert.NoError(afero.WriteFile(mm, filepath.Join(archetypeDir, "index.md"), []byte(fmt.Sprintf(contentFile, "index.md")), 0755)) + assert.NoError(afero.WriteFile(mm, filepath.Join(archetypeDir, "index.nn.md"), []byte(fmt.Sprintf(contentFile, "index.nn.md")), 0755)) - assert.NoError(afero.WriteFile(fs.Source, filepath.Join(archetypeDir, "pages", "bio.md"), []byte(fmt.Sprintf(contentFile, "bio.md")), 0755)) - 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(mm, filepath.Join(archetypeDir, "pages", "bio.md"), []byte(fmt.Sprintf(contentFile, "bio.md")), 0755)) + assert.NoError(afero.WriteFile(mm, filepath.Join(archetypeDir, "resources", "hugo1.json"), []byte(`hugo1: {{ printf "no template handling in here" }}`), 0755)) + assert.NoError(afero.WriteFile(mm, 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)) + assert.NoError(afero.WriteFile(mm, filepath.Join(archetypeThemeDir, "index.md"), []byte(fmt.Sprintf(contentFile, "index.md")), 0755)) + assert.NoError(afero.WriteFile(mm, filepath.Join(archetypeThemeDir, "resources", "hugo1.json"), []byte(`hugo1: {{ printf "no template handling in here" }}`), 0755)) + + assert.NoError(initFs(mm)) + cfg, fs := newTestCfg(assert, mm) h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs}) assert.NoError(err) @@ -135,7 +143,7 @@ i18n: {{ T "hugo" }} } -func initFs(fs *hugofs.Fs) error { +func initFs(fs afero.Fs) error { perm := os.FileMode(0755) var err error @@ -146,8 +154,8 @@ func initFs(fs *hugofs.Fs) error { filepath.Join("themes", "sample", "archetypes"), } for _, dir := range dirs { - err = fs.Source.Mkdir(dir, perm) - if err != nil { + err = fs.Mkdir(dir, perm) + if err != nil && !os.IsExist(err) { return err } } @@ -198,7 +206,7 @@ Some text. `, }, } { - f, err := fs.Source.Create(v.path) + f, err := fs.Create(v.path) if err != nil { return err } @@ -221,6 +229,7 @@ func assertContains(assert *require.Assertions, v interface{}, matches ...string // TODO(bep) extract common testing package with this and some others func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string { + t.Helper() filename = filepath.FromSlash(filename) b, err := afero.ReadFile(fs, filename) if err != nil { @@ -238,12 +247,11 @@ func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string { return string(b) } -func newTestCfg(assert *require.Assertions) (*viper.Viper, *hugofs.Fs) { +func newTestCfg(assert *require.Assertions, mm afero.Fs) (*viper.Viper, *hugofs.Fs) { cfg := ` theme = "mytheme" - [languages] [languages.en] weight = 1 @@ -254,8 +262,13 @@ languageName = "Nynorsk" contentDir = "content_nn" ` + if mm == nil { + mm = afero.NewMemMapFs() + } - mm := afero.NewMemMapFs() + mm.MkdirAll(filepath.FromSlash("content_nn"), 0777) + + mm.MkdirAll(filepath.FromSlash("themes/mytheme"), 0777) assert.NoError(afero.WriteFile(mm, filepath.Join("i18n", "en.toml"), []byte(`[hugo] other = "Hugo Rocks!"`), 0755)) |