diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-11-29 20:18:09 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-11-29 20:18:09 +0100 |
commit | 96018ab98c1153c9d882656abf99685f2fb7e0c4 (patch) | |
tree | 99347fbdc20f9f2eaf0fb2e954fcaa6a74e83417 /create/content_test.go | |
parent | 65d4d96e7f24a27f69acec07ea56d5ab3ddc63c3 (diff) |
create: Fix archetype title and date handling
Fixes #2750
Diffstat (limited to 'create/content_test.go')
-rw-r--r-- | create/content_test.go | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/create/content_test.go b/create/content_test.go index 50acc89b4..3d2453b7b 100644 --- a/create/content_test.go +++ b/create/content_test.go @@ -16,10 +16,14 @@ package create_test import ( "os" "path/filepath" + "strings" "testing" + "fmt" + "github.com/spf13/afero" "github.com/spf13/hugo/create" + "github.com/spf13/hugo/helpers" "github.com/spf13/hugo/hugofs" "github.com/spf13/viper" ) @@ -33,35 +37,32 @@ func TestNewContent(t *testing.T) { } cases := []struct { - kind string - path string - resultStrings []string + kind string + path string + expected []string }{ - {"post", "post/sample-1.md", []string{`title = "sample 1"`, `test = "test1"`}}, + {"post", "post/sample-1.md", []string{`title = "Post Arch title"`, `test = "test1"`, "date = \"2015-01-12T19:20:04-07:00\""}}, {"stump", "stump/sample-2.md", []string{`title = "sample 2"`}}, // no archetype file {"", "sample-3.md", []string{`title = "sample 3"`}}, // no archetype {"product", "product/sample-4.md", []string{`title = "sample 4"`}}, // empty archetype front matter } for i, c := range cases { + if i > 0 { + break + } err = create.NewContent(hugofs.Source(), c.kind, c.path) if err != nil { t.Errorf("[%d] NewContent: %s", i, err) } - fname := filepath.Join(os.TempDir(), "content", filepath.FromSlash(c.path)) - _, err = hugofs.Source().Stat(fname) - if err != nil { - t.Errorf("[%d] Stat: %s", i, err) - } + fname := filepath.Join("content", filepath.FromSlash(c.path)) + content := readFileFromFs(t, hugofs.Source(), fname) - for _, v := range c.resultStrings { - found, err := afero.FileContainsBytes(hugofs.Source(), fname, []byte(v)) - if err != nil { - t.Errorf("[%d] FileContainsBytes: %s", i, err) - } + for i, v := range c.expected { + found := strings.Contains(content, v) if !found { - t.Errorf("content missing from output: %q", v) + t.Errorf("[%d] %q missing from output:\n%q", i, v, content) } } } @@ -70,14 +71,14 @@ func TestNewContent(t *testing.T) { func initViper() { viper.Reset() viper.Set("metaDataFormat", "toml") - viper.Set("archetypeDir", filepath.Join(os.TempDir(), "archetypes")) - viper.Set("contentDir", filepath.Join(os.TempDir(), "content")) - viper.Set("themesDir", filepath.Join(os.TempDir(), "themes")) + viper.Set("archetypeDir", "archetypes") + viper.Set("contentDir", "content") + viper.Set("themesDir", "themes") viper.Set("theme", "sample") } func initFs() error { - hugofs.SetSource(new(afero.MemMapFs)) + hugofs.InitMemFs() perm := os.FileMode(0755) var err error @@ -88,7 +89,6 @@ func initFs() error { filepath.Join("themes", "sample", "archetypes"), } for _, dir := range dirs { - dir = filepath.Join(os.TempDir(), dir) err = hugofs.Source().Mkdir(dir, perm) if err != nil { return err @@ -101,11 +101,11 @@ func initFs() error { content string }{ { - path: filepath.Join(os.TempDir(), "archetypes", "post.md"), - content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"post arch\"\ntest = \"test1\"\n+++\n", + path: filepath.Join("archetypes", "post.md"), + content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"Post Arch title\"\ntest = \"test1\"\n+++\n", }, { - path: filepath.Join(os.TempDir(), "archetypes", "product.md"), + path: filepath.Join("archetypes", "product.md"), content: "+++\n+++\n", }, } { @@ -123,3 +123,22 @@ func initFs() error { return nil } + +// TODO(bep) extract common testing package with this and some others +func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string { + filename = filepath.FromSlash(filename) + b, err := afero.ReadFile(fs, filename) + if err != nil { + // Print some debug info + root := strings.Split(filename, helpers.FilePathSeparator)[0] + afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error { + if info != nil && !info.IsDir() { + fmt.Println(" ", path) + } + + return nil + }) + t.Fatalf("Failed to read file: %s", err) + } + return string(b) +} |