Skip to content

Commit

Permalink
Add missing DataDir tests
Browse files Browse the repository at this point in the history
Also, now logs an ERROR on duplicate keys, instead of returning an error.
  • Loading branch information
bep committed Feb 8, 2015
1 parent 959938a commit 2f048fa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
4 changes: 2 additions & 2 deletions hugolib/site.go
Expand Up @@ -114,7 +114,7 @@ type SiteInfo struct {
BuildDrafts bool
canonifyUrls bool
paginationPageCount uint64
Data *map[string]interface{}
Data *map[string]interface{}
}

// SiteSocial is a place to put social details on a site level. These are the
Expand Down Expand Up @@ -295,7 +295,7 @@ func (s *Site) loadData(fs source.Input) (err error) {

for key, value := range current[r.BaseFileName()].(map[string]interface{}) {
if _, override := data[key]; override {
return errors.New("Data in " + r.Path() + " is overrided in subfolder.")
jww.ERROR.Printf("Data for key '%s' in path '%s' is overridden in subfolder", key, r.Path())
} else {
data[key] = value
}
Expand Down
65 changes: 57 additions & 8 deletions hugolib/site_test.go
Expand Up @@ -3,9 +3,9 @@ package hugolib
import (
"bytes"
"fmt"
"github.com/spf13/hugo/parser"
"html/template"
"io"
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -17,6 +17,7 @@ import (
"github.com/spf13/hugo/target"
"github.com/spf13/hugo/tpl"
"github.com/spf13/viper"
"reflect"
)

const (
Expand Down Expand Up @@ -747,17 +748,65 @@ func TestWeightedTaxonomies(t *testing.T) {
}
}

func TestDataDir(t *testing.T) {
func TestDataDirJson(t *testing.T) {
sources := []source.ByteSource{
{filepath.FromSlash("test" + string(os.PathSeparator) + "foo.yaml"), []byte("bar: foofoo")},
{filepath.FromSlash("test.yaml"), []byte("hello:\n- world: foo")},
{filepath.FromSlash("test/foo.json"), []byte(`{ "bar": "foofoo" }`)},
{filepath.FromSlash("test.json"), []byte(`{ "hello": [ { "world": "foo" } ] }`)},
}

expected, err := parser.HandleJsonMetaData([]byte(`{ "test": { "hello": [{ "world": "foo" }] , "foo": { "bar":"foofoo" } } }`))

if err != nil {
t.Fatalf("Error %s", err)
}

doTestDataDir(t, expected, sources)
}

func TestDataDirToml(t *testing.T) {
sources := []source.ByteSource{
{filepath.FromSlash("test/kung.toml"), []byte("[foo]\nbar = 1")},
}

expected, err := parser.HandleTomlMetaData([]byte("[test]\n[test.kung]\n[test.kung.foo]\nbar = 1"))

if err != nil {
t.Fatalf("Error %s", err)
}

doTestDataDir(t, expected, sources)
}

func TestDataDirYamlWithOverridenValue(t *testing.T) {
sources := []source.ByteSource{
{filepath.FromSlash("test/v1.yaml"), []byte("v2: 1")},
{filepath.FromSlash("test.yaml"), []byte("v1: 2")},
}

// YAML uses interface{} as keys - in this case we will get a mix of string and interface{} keys
expected := map[string]interface{}{"test": map[string]interface{}{"v1": 2}}

doTestDataDir(t, expected, sources)
}

func TestDataDirUnknownFormat(t *testing.T) {
sources := []source.ByteSource{
{filepath.FromSlash("test.roml"), []byte("boo")},
}
s := &Site{}
s.loadData(&source.InMemorySource{ByteSource: sources})
err := s.loadData(&source.InMemorySource{ByteSource: sources})
if err == nil {
t.Fatalf("Should return an error")
}
}

expected := "map[test:map[hello:[map[world:foo]] foo:map[bar:foofoo]]]"
if fmt.Sprint(s.Data) != expected {
t.Errorf("Expected structure '%s', got '%s'", expected, s.Data)
func doTestDataDir(t *testing.T, expected interface{}, sources []source.ByteSource) {
s := &Site{}
err := s.loadData(&source.InMemorySource{ByteSource: sources})
if err != nil {
t.Fatalf("Error loading data: %s", err)
}
if !reflect.DeepEqual(expected, s.Data) {
t.Errorf("Expected structure\n'%#v' got\n'%#v'", expected, s.Data)
}
}

0 comments on commit 2f048fa

Please sign in to comment.