summaryrefslogtreecommitdiffstats
path: root/hugolib/datafiles_test.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-02-05 10:20:06 +0700
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-02-17 17:15:26 +0100
commit93ca7c9e958e34469a337e4efcc7c75774ec50fd (patch)
tree5dfa296cfe74fd5ef8f0d41ea4078704f453aa04 /hugolib/datafiles_test.go
parente34af6ee30f70f5780a281e2fd8f4ed9b487ee61 (diff)
all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables the use if `t.Parallel` in tests. Updates #2701 Fixes #3016
Diffstat (limited to 'hugolib/datafiles_test.go')
-rw-r--r--hugolib/datafiles_test.go103
1 files changed, 69 insertions, 34 deletions
diff --git a/hugolib/datafiles_test.go b/hugolib/datafiles_test.go
index 0f848594a..3aac3201f 100644
--- a/hugolib/datafiles_test.go
+++ b/hugolib/datafiles_test.go
@@ -19,15 +19,24 @@ import (
"strings"
"testing"
+ "io/ioutil"
+ "log"
+ "os"
+
+ "github.com/spf13/hugo/deps"
+ jww "github.com/spf13/jwalterweatherman"
+
"github.com/spf13/hugo/parser"
"github.com/spf13/hugo/source"
"github.com/stretchr/testify/require"
)
func TestDataDirJSON(t *testing.T) {
+ t.Parallel()
+
sources := []source.ByteSource{
- {Name: filepath.FromSlash("test/foo.json"), Content: []byte(`{ "bar": "foofoo" }`)},
- {Name: filepath.FromSlash("test.json"), Content: []byte(`{ "hello": [ { "world": "foo" } ] }`)},
+ {Name: filepath.FromSlash("data/test/foo.json"), Content: []byte(`{ "bar": "foofoo" }`)},
+ {Name: filepath.FromSlash("data/test.json"), Content: []byte(`{ "hello": [ { "world": "foo" } ] }`)},
}
expected, err := parser.HandleJSONMetaData([]byte(`{ "test": { "hello": [{ "world": "foo" }] , "foo": { "bar":"foofoo" } } }`))
@@ -36,12 +45,14 @@ func TestDataDirJSON(t *testing.T) {
t.Fatalf("Error %s", err)
}
- doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}})
+ doTestDataDir(t, expected, sources)
}
func TestDataDirToml(t *testing.T) {
+ t.Parallel()
+
sources := []source.ByteSource{
- {Name: filepath.FromSlash("test/kung.toml"), Content: []byte("[foo]\nbar = 1")},
+ {Name: filepath.FromSlash("data/test/kung.toml"), Content: []byte("[foo]\nbar = 1")},
}
expected, err := parser.HandleTOMLMetaData([]byte("[test]\n[test.kung]\n[test.kung.foo]\nbar = 1"))
@@ -50,78 +61,102 @@ func TestDataDirToml(t *testing.T) {
t.Fatalf("Error %s", err)
}
- doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}})
+ doTestDataDir(t, expected, sources)
}
func TestDataDirYAMLWithOverridenValue(t *testing.T) {
+ t.Parallel()
+
sources := []source.ByteSource{
// filepath.Walk walks the files in lexical order, '/' comes before '.'. Simulate this:
- {Name: filepath.FromSlash("a.yaml"), Content: []byte("a: 1")},
- {Name: filepath.FromSlash("test/v1.yaml"), Content: []byte("v1-2: 2")},
- {Name: filepath.FromSlash("test/v2.yaml"), Content: []byte("v2:\n- 2\n- 3")},
- {Name: filepath.FromSlash("test.yaml"), Content: []byte("v1: 1")},
+ {Name: filepath.FromSlash("data/a.yaml"), Content: []byte("a: 1")},
+ {Name: filepath.FromSlash("data/test/v1.yaml"), Content: []byte("v1-2: 2")},
+ {Name: filepath.FromSlash("data/test/v2.yaml"), Content: []byte("v2:\n- 2\n- 3")},
+ {Name: filepath.FromSlash("data/test.yaml"), Content: []byte("v1: 1")},
}
expected := map[string]interface{}{"a": map[string]interface{}{"a": 1},
"test": map[string]interface{}{"v1": map[string]interface{}{"v1-2": 2}, "v2": map[string]interface{}{"v2": []interface{}{2, 3}}}}
- doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: sources}})
+ doTestDataDir(t, expected, sources)
}
// issue 892
func TestDataDirMultipleSources(t *testing.T) {
- s1 := []source.ByteSource{
- {Name: filepath.FromSlash("test/first.toml"), Content: []byte("bar = 1")},
- }
+ t.Parallel()
- s2 := []source.ByteSource{
- {Name: filepath.FromSlash("test/first.toml"), Content: []byte("bar = 2")},
- {Name: filepath.FromSlash("test/second.toml"), Content: []byte("tender = 2")},
+ sources := []source.ByteSource{
+ {Name: filepath.FromSlash("data/test/first.toml"), Content: []byte("bar = 1")},
+ {Name: filepath.FromSlash("themes/mytheme/data/test/first.toml"), Content: []byte("bar = 2")},
+ {Name: filepath.FromSlash("data/test/second.toml"), Content: []byte("tender = 2")},
}
expected, _ := parser.HandleTOMLMetaData([]byte("[test.first]\nbar = 1\n[test.second]\ntender=2"))
- doTestDataDir(t, expected, []source.Input{&source.InMemorySource{ByteSource: s1}, &source.InMemorySource{ByteSource: s2}})
+ doTestDataDir(t, expected, sources,
+ "theme", "mytheme")
}
func TestDataDirUnknownFormat(t *testing.T) {
+ t.Parallel()
+
sources := []source.ByteSource{
- {Name: filepath.FromSlash("test.roml"), Content: []byte("boo")},
+ {Name: filepath.FromSlash("data/test.roml"), Content: []byte("boo")},
}
- s, err := NewSiteDefaultLang()
- require.NoError(t, err)
- require.NoError(t, s.loadData([]source.Input{&source.InMemorySource{ByteSource: sources}}))
+ doTestDataDir(t, true, sources)
}
-func doTestDataDir(t *testing.T, expected interface{}, sources []source.Input) {
- s, err := NewSiteDefaultLang()
- require.NoError(t, err)
- require.NoError(t, s.loadData(sources))
+func doTestDataDir(t *testing.T, expected interface{}, sources []source.ByteSource, configKeyValues ...interface{}) {
+ var (
+ cfg, fs = newTestCfg()
+ )
+
+ for i := 0; i < len(configKeyValues); i += 2 {
+ cfg.Set(configKeyValues[i].(string), configKeyValues[i+1])
+ }
+
+ var (
+ logger = jww.NewNotepad(jww.LevelError, jww.LevelError, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
+ depsCfg = deps.DepsCfg{Fs: fs, Cfg: cfg, Logger: logger}
+ )
+
+ writeSource(t, fs, filepath.Join("content", "dummy.md"), "content")
+ writeSourcesToSource(t, "", fs, sources...)
+
+ expectBuildError := false
+
+ if ok, shouldFail := expected.(bool); ok && shouldFail {
+ expectBuildError = true
+ }
+
+ s := buildSingleSiteExpected(t, expectBuildError, depsCfg, BuildCfg{SkipRender: true})
- if !reflect.DeepEqual(expected, s.Data) {
+ if !expectBuildError && !reflect.DeepEqual(expected, s.Data) {
t.Errorf("Expected structure\n%#v got\n%#v", expected, s.Data)
}
}
func TestDataFromShortcode(t *testing.T) {
- testCommonResetState()
+ t.Parallel()
- cfg := newTestDepsConfig()
+ var (
+ cfg, fs = newTestCfg()
+ )
- writeSource(t, cfg.Fs, "data/hugo.toml", "slogan = \"Hugo Rocks!\"")
- writeSource(t, cfg.Fs, "layouts/_default/single.html", `
+ writeSource(t, fs, "data/hugo.toml", "slogan = \"Hugo Rocks!\"")
+ writeSource(t, fs, "layouts/_default/single.html", `
* Slogan from template: {{ .Site.Data.hugo.slogan }}
* {{ .Content }}`)
- writeSource(t, cfg.Fs, "layouts/shortcodes/d.html", `{{ .Page.Site.Data.hugo.slogan }}`)
- writeSource(t, cfg.Fs, "content/c.md", `---
+ writeSource(t, fs, "layouts/shortcodes/d.html", `{{ .Page.Site.Data.hugo.slogan }}`)
+ writeSource(t, fs, "content/c.md", `---
---
Slogan from shortcode: {{< d >}}
`)
- buildSingleSite(t, cfg, BuildCfg{})
+ buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
- content := readSource(t, cfg.Fs, "public/c/index.html")
+ content := readSource(t, fs, "public/c/index.html")
require.True(t, strings.Contains(content, "Slogan from template: Hugo Rocks!"), content)
require.True(t, strings.Contains(content, "Slogan from shortcode: Hugo Rocks!"), content)