mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parent
d6c8cd7718
commit
9bba9a3a98
2 changed files with 82 additions and 82 deletions
|
@ -46,7 +46,9 @@ func InterfaceToConfig(in interface{}, format metadecoders.Format, w io.Writer)
|
||||||
return err
|
return err
|
||||||
|
|
||||||
case metadecoders.TOML:
|
case metadecoders.TOML:
|
||||||
return toml.NewEncoder(w).Encode(in)
|
enc := toml.NewEncoder(w)
|
||||||
|
enc.SetIndentTables(true)
|
||||||
|
return enc.Encode(in)
|
||||||
case metadecoders.JSON:
|
case metadecoders.JSON:
|
||||||
b, err := json.MarshalIndent(in, "", " ")
|
b, err := json.MarshalIndent(in, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -30,7 +30,9 @@ func TestRemarshal(t *testing.T) {
|
||||||
ns := New(newDeps(v))
|
ns := New(newDeps(v))
|
||||||
c := qt.New(t)
|
c := qt.New(t)
|
||||||
|
|
||||||
tomlExample := `title = 'Test Metadata'
|
c.Run("Roundtrip variants", func(c *qt.C) {
|
||||||
|
|
||||||
|
tomlExample := `title = 'Test Metadata'
|
||||||
|
|
||||||
[[resources]]
|
[[resources]]
|
||||||
src = '**image-4.png'
|
src = '**image-4.png'
|
||||||
|
@ -46,7 +48,7 @@ func TestRemarshal(t *testing.T) {
|
||||||
byline = 'bep'
|
byline = 'bep'
|
||||||
`
|
`
|
||||||
|
|
||||||
yamlExample := `resources:
|
yamlExample := `resources:
|
||||||
- params:
|
- params:
|
||||||
byline: picasso
|
byline: picasso
|
||||||
src: '**image-4.png'
|
src: '**image-4.png'
|
||||||
|
@ -59,7 +61,7 @@ func TestRemarshal(t *testing.T) {
|
||||||
title: Test Metadata
|
title: Test Metadata
|
||||||
`
|
`
|
||||||
|
|
||||||
jsonExample := `{
|
jsonExample := `{
|
||||||
"resources": [
|
"resources": [
|
||||||
{
|
{
|
||||||
"params": {
|
"params": {
|
||||||
|
@ -81,44 +83,37 @@ title: Test Metadata
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
variants := []struct {
|
variants := []struct {
|
||||||
format string
|
format string
|
||||||
data string
|
data string
|
||||||
}{
|
}{
|
||||||
{"yaml", yamlExample},
|
{"yaml", yamlExample},
|
||||||
{"json", jsonExample},
|
{"json", jsonExample},
|
||||||
{"toml", tomlExample},
|
{"toml", tomlExample},
|
||||||
{"TOML", tomlExample},
|
{"TOML", tomlExample},
|
||||||
{"Toml", tomlExample},
|
{"Toml", tomlExample},
|
||||||
{" TOML ", tomlExample},
|
{" TOML ", tomlExample},
|
||||||
}
|
|
||||||
|
|
||||||
for _, v1 := range variants {
|
|
||||||
for _, v2 := range variants {
|
|
||||||
// Both from and to may be the same here, but that is fine.
|
|
||||||
fromTo := qt.Commentf("%s => %s", v2.format, v1.format)
|
|
||||||
|
|
||||||
converted, err := ns.Remarshal(v1.format, v2.data)
|
|
||||||
c.Assert(err, qt.IsNil, fromTo)
|
|
||||||
diff := htesting.DiffStrings(v1.data, converted)
|
|
||||||
if len(diff) > 0 {
|
|
||||||
t.Errorf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v", fromTo, v1.data, converted, diff)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemarshalComments(t *testing.T) {
|
for _, v1 := range variants {
|
||||||
t.Parallel()
|
for _, v2 := range variants {
|
||||||
|
// Both from and to may be the same here, but that is fine.
|
||||||
|
fromTo := qt.Commentf("%s => %s", v2.format, v1.format)
|
||||||
|
|
||||||
v := config.New()
|
converted, err := ns.Remarshal(v1.format, v2.data)
|
||||||
v.Set("contentDir", "content")
|
c.Assert(err, qt.IsNil, fromTo)
|
||||||
ns := New(newDeps(v))
|
diff := htesting.DiffStrings(v1.data, converted)
|
||||||
|
if len(diff) > 0 {
|
||||||
|
t.Errorf("[%s] Expected \n%v\ngot\n%v\ndiff:\n%v", fromTo, v1.data, converted, diff)
|
||||||
|
}
|
||||||
|
|
||||||
c := qt.New(t)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
input := `
|
})
|
||||||
|
|
||||||
|
c.Run("Comments", func(c *qt.C) {
|
||||||
|
input := `
|
||||||
Hugo = "Rules"
|
Hugo = "Rules"
|
||||||
|
|
||||||
# It really does!
|
# It really does!
|
||||||
|
@ -129,56 +124,59 @@ a = "b"
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
expected := `Hugo = 'Rules'
|
expected := `Hugo = 'Rules'
|
||||||
[m]
|
[m]
|
||||||
a = 'b'
|
a = 'b'
|
||||||
`
|
`
|
||||||
|
|
||||||
for _, format := range []string{"json", "yaml", "toml"} {
|
for _, format := range []string{"json", "yaml", "toml"} {
|
||||||
fromTo := qt.Commentf("%s => %s", "toml", format)
|
fromTo := qt.Commentf("%s => %s", "toml", format)
|
||||||
|
|
||||||
converted := input
|
converted := input
|
||||||
var err error
|
var err error
|
||||||
// Do a round-trip conversion
|
// Do a round-trip conversion
|
||||||
for _, toFormat := range []string{format, "toml"} {
|
for _, toFormat := range []string{format, "toml"} {
|
||||||
converted, err = ns.Remarshal(toFormat, converted)
|
converted, err = ns.Remarshal(toFormat, converted)
|
||||||
c.Assert(err, qt.IsNil, fromTo)
|
c.Assert(err, qt.IsNil, fromTo)
|
||||||
|
}
|
||||||
|
|
||||||
|
diff := htesting.DiffStrings(expected, converted)
|
||||||
|
if len(diff) > 0 {
|
||||||
|
t.Fatalf("[%s] Expected \n%v\ngot\n>>%v\ndiff:\n%v\n", fromTo, expected, converted, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Issue 8850
|
||||||
|
c.Run("TOML Indent", func(c *qt.C) {
|
||||||
|
input := `
|
||||||
|
|
||||||
|
[params]
|
||||||
|
[params.variables]
|
||||||
|
a = "b"
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
|
converted, err := ns.Remarshal("toml", input)
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
c.Assert(converted, qt.Equals, "[params]\n [params.variables]\n a = 'b'\n\n\n")
|
||||||
|
})
|
||||||
|
|
||||||
|
c.Run("Map input", func(c *qt.C) {
|
||||||
|
input := map[string]interface{}{
|
||||||
|
"hello": "world",
|
||||||
}
|
}
|
||||||
|
|
||||||
diff := htesting.DiffStrings(expected, converted)
|
output, err := ns.Remarshal("toml", input)
|
||||||
if len(diff) > 0 {
|
c.Assert(err, qt.IsNil)
|
||||||
t.Fatalf("[%s] Expected \n%v\ngot\n>>%v\ndiff:\n%v\n", fromTo, expected, converted, diff)
|
c.Assert(output, qt.Equals, "hello = 'world'\n")
|
||||||
}
|
})
|
||||||
}
|
|
||||||
}
|
c.Run("Error", func(c *qt.C) {
|
||||||
|
_, err := ns.Remarshal("asdf", "asdf")
|
||||||
func TestTestRemarshalError(t *testing.T) {
|
c.Assert(err, qt.Not(qt.IsNil))
|
||||||
t.Parallel()
|
|
||||||
c := qt.New(t)
|
_, err = ns.Remarshal("json", "asdf")
|
||||||
|
c.Assert(err, qt.Not(qt.IsNil))
|
||||||
v := config.New()
|
})
|
||||||
v.Set("contentDir", "content")
|
|
||||||
ns := New(newDeps(v))
|
|
||||||
|
|
||||||
_, err := ns.Remarshal("asdf", "asdf")
|
|
||||||
c.Assert(err, qt.Not(qt.IsNil))
|
|
||||||
|
|
||||||
_, err = ns.Remarshal("json", "asdf")
|
|
||||||
c.Assert(err, qt.Not(qt.IsNil))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTestRemarshalMapInput(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
c := qt.New(t)
|
|
||||||
v := config.New()
|
|
||||||
v.Set("contentDir", "content")
|
|
||||||
ns := New(newDeps(v))
|
|
||||||
|
|
||||||
input := map[string]interface{}{
|
|
||||||
"hello": "world",
|
|
||||||
}
|
|
||||||
|
|
||||||
output, err := ns.Remarshal("toml", input)
|
|
||||||
c.Assert(err, qt.IsNil)
|
|
||||||
c.Assert(output, qt.Equals, "hello = 'world'\n")
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue