2021-06-09 04:58:18 -04:00
|
|
|
// Copyright 2021 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/para"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultConfigProvider(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
c.Run("Set and get", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
var k string
|
2022-03-17 17:03:27 -04:00
|
|
|
var v any
|
2021-06-09 04:58:18 -04:00
|
|
|
|
|
|
|
k, v = "foo", "bar"
|
|
|
|
cfg.Set(k, v)
|
|
|
|
c.Assert(cfg.Get(k), qt.Equals, v)
|
|
|
|
c.Assert(cfg.Get(strings.ToUpper(k)), qt.Equals, v)
|
|
|
|
c.Assert(cfg.GetString(k), qt.Equals, v)
|
|
|
|
|
|
|
|
k, v = "foo", 42
|
|
|
|
cfg.Set(k, v)
|
|
|
|
c.Assert(cfg.Get(k), qt.Equals, v)
|
|
|
|
c.Assert(cfg.GetInt(k), qt.Equals, v)
|
|
|
|
|
|
|
|
c.Assert(cfg.Get(""), qt.DeepEquals, maps.Params{
|
|
|
|
"foo": 42,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Set and get map", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("foo", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"bar": "baz",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get("foo"), qt.DeepEquals, maps.Params{
|
|
|
|
"bar": "baz",
|
|
|
|
})
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
c.Assert(cfg.GetStringMap("foo"), qt.DeepEquals, map[string]any{"bar": string("baz")})
|
2021-06-09 04:58:18 -04:00
|
|
|
c.Assert(cfg.GetStringMapString("foo"), qt.DeepEquals, map[string]string{"bar": string("baz")})
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Set and get nested", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("a", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"B": "bv",
|
|
|
|
})
|
|
|
|
cfg.Set("a.c", "cv")
|
|
|
|
|
|
|
|
c.Assert(cfg.Get("a"), qt.DeepEquals, maps.Params{
|
|
|
|
"b": "bv",
|
|
|
|
"c": "cv",
|
|
|
|
})
|
|
|
|
c.Assert(cfg.Get("a.c"), qt.Equals, "cv")
|
|
|
|
|
|
|
|
cfg.Set("b.a", "av")
|
|
|
|
c.Assert(cfg.Get("b"), qt.DeepEquals, maps.Params{
|
|
|
|
"a": "av",
|
|
|
|
})
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("b", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"b": "bv",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get("b"), qt.DeepEquals, maps.Params{
|
|
|
|
"a": "av",
|
|
|
|
"b": "bv",
|
|
|
|
})
|
|
|
|
|
|
|
|
cfg = New()
|
|
|
|
|
|
|
|
cfg.Set("a", "av")
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"a": "av2",
|
|
|
|
"b": "bv2",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get(""), qt.DeepEquals, maps.Params{
|
|
|
|
"a": "av2",
|
|
|
|
"b": "bv2",
|
|
|
|
})
|
|
|
|
|
|
|
|
cfg = New()
|
|
|
|
|
|
|
|
cfg.Set("a", "av")
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"b": "bv2",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get(""), qt.DeepEquals, maps.Params{
|
|
|
|
"a": "av",
|
|
|
|
"b": "bv2",
|
|
|
|
})
|
|
|
|
|
|
|
|
cfg = New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("", map[string]any{
|
|
|
|
"foo": map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"a": "av",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("", map[string]any{
|
|
|
|
"foo": map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"b": "bv2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get("foo"), qt.DeepEquals, maps.Params{
|
|
|
|
"a": "av",
|
|
|
|
"b": "bv2",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Merge default strategy", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("a", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"B": "bv",
|
|
|
|
})
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Merge("a", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"B": "bv2",
|
|
|
|
"c": "cv2",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get("a"), qt.DeepEquals, maps.Params{
|
|
|
|
"b": "bv",
|
|
|
|
"c": "cv2",
|
|
|
|
})
|
|
|
|
|
|
|
|
cfg = New()
|
|
|
|
|
|
|
|
cfg.Set("a", "av")
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Merge("", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"a": "av2",
|
|
|
|
"b": "bv2",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get(""), qt.DeepEquals, maps.Params{
|
|
|
|
"a": "av",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Merge shallow", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("a", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"_merge": "shallow",
|
|
|
|
"B": "bv",
|
2022-03-17 17:03:27 -04:00
|
|
|
"c": map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"b": "bv",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Merge("a", map[string]any{
|
|
|
|
"c": map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"d": "dv2",
|
|
|
|
},
|
|
|
|
"e": "ev2",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get("a"), qt.DeepEquals, maps.Params{
|
|
|
|
"e": "ev2",
|
|
|
|
"_merge": maps.ParamsMergeStrategyShallow,
|
|
|
|
"b": "bv",
|
|
|
|
"c": maps.Params{
|
|
|
|
"b": "bv",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-22 03:53:37 -04:00
|
|
|
// Issue #8679
|
|
|
|
c.Run("Merge typed maps", func(c *qt.C) {
|
2022-03-17 17:03:27 -04:00
|
|
|
for _, left := range []any{
|
2021-06-22 03:53:37 -04:00
|
|
|
map[string]string{
|
|
|
|
"c": "cv1",
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
map[string]any{
|
2021-06-22 03:53:37 -04:00
|
|
|
"c": "cv1",
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{
|
2021-06-22 03:53:37 -04:00
|
|
|
"c": "cv1",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
cfg := New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("", map[string]any{
|
2021-06-22 03:53:37 -04:00
|
|
|
"b": left,
|
|
|
|
})
|
|
|
|
|
|
|
|
cfg.Merge("", maps.Params{
|
|
|
|
"b": maps.Params{
|
|
|
|
"c": "cv2",
|
|
|
|
"d": "dv2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get(""), qt.DeepEquals, maps.Params{
|
|
|
|
"b": maps.Params{
|
|
|
|
"c": "cv1",
|
|
|
|
"d": "dv2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
for _, left := range []any{
|
2021-06-22 03:53:37 -04:00
|
|
|
map[string]string{
|
|
|
|
"b": "bv1",
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
map[string]any{
|
2021-06-22 03:53:37 -04:00
|
|
|
"b": "bv1",
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{
|
2021-06-22 03:53:37 -04:00
|
|
|
"b": "bv1",
|
|
|
|
},
|
|
|
|
} {
|
2022-03-17 17:03:27 -04:00
|
|
|
for _, right := range []any{
|
2021-06-22 03:53:37 -04:00
|
|
|
map[string]string{
|
|
|
|
"b": "bv2",
|
|
|
|
"c": "cv2",
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
map[string]any{
|
2021-06-22 03:53:37 -04:00
|
|
|
"b": "bv2",
|
|
|
|
"c": "cv2",
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
map[any]any{
|
2021-06-22 03:53:37 -04:00
|
|
|
"b": "bv2",
|
|
|
|
"c": "cv2",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
cfg := New()
|
|
|
|
|
|
|
|
cfg.Set("a", left)
|
|
|
|
|
|
|
|
cfg.Merge("a", right)
|
|
|
|
|
|
|
|
c.Assert(cfg.Get(""), qt.DeepEquals, maps.Params{
|
|
|
|
"a": maps.Params{
|
|
|
|
"b": "bv1",
|
|
|
|
"c": "cv2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-27 07:24:49 -04:00
|
|
|
// Issue #8701
|
|
|
|
c.Run("Prevent _merge only maps", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("", map[string]any{
|
2021-06-27 07:24:49 -04:00
|
|
|
"B": "bv",
|
|
|
|
})
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Merge("", map[string]any{
|
|
|
|
"c": map[string]any{
|
2021-06-27 07:24:49 -04:00
|
|
|
"_merge": "shallow",
|
|
|
|
"d": "dv2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.Get(""), qt.DeepEquals, maps.Params{
|
|
|
|
"b": "bv",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
c.Run("IsSet", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
cfg.Set("a", map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"B": "bv",
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Assert(cfg.IsSet("A"), qt.IsTrue)
|
|
|
|
c.Assert(cfg.IsSet("a.b"), qt.IsTrue)
|
|
|
|
c.Assert(cfg.IsSet("z"), qt.IsFalse)
|
|
|
|
})
|
|
|
|
|
|
|
|
c.Run("Para", func(c *qt.C) {
|
|
|
|
cfg := New()
|
|
|
|
p := para.New(4)
|
|
|
|
r, _ := p.Start(context.Background())
|
|
|
|
|
|
|
|
setAndGet := func(k string, v int) error {
|
|
|
|
vs := strconv.Itoa(v)
|
|
|
|
cfg.Set(k, v)
|
|
|
|
err := errors.New("get failed")
|
|
|
|
if cfg.Get(k) != v {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cfg.GetInt(k) != v {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cfg.GetString(k) != vs {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !cfg.IsSet(k) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
i := i
|
|
|
|
r.Run(func() error {
|
|
|
|
const v = 42
|
|
|
|
k := fmt.Sprintf("k%d", i)
|
|
|
|
if err := setAndGet(k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := maps.Params{
|
|
|
|
"new": 42,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Merge("", m)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Assert(r.Wait(), qt.IsNil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDefaultConfigProvider(b *testing.B) {
|
|
|
|
type cfger interface {
|
2022-03-17 17:03:27 -04:00
|
|
|
Get(key string) any
|
|
|
|
Set(key string, value any)
|
2021-06-09 04:58:18 -04:00
|
|
|
IsSet(key string) bool
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
newMap := func() map[string]any {
|
|
|
|
return map[string]any{
|
|
|
|
"a": map[string]any{
|
|
|
|
"b": map[string]any{
|
2021-06-09 04:58:18 -04:00
|
|
|
"c": 32,
|
|
|
|
"d": 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"b": 62,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runMethods := func(b *testing.B, cfg cfger) {
|
|
|
|
m := newMap()
|
|
|
|
cfg.Set("mymap", m)
|
|
|
|
cfg.Set("num", 32)
|
|
|
|
if !(cfg.IsSet("mymap") && cfg.IsSet("mymap.a") && cfg.IsSet("mymap.a.b") && cfg.IsSet("mymap.a.b.c")) {
|
|
|
|
b.Fatal("IsSet failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Get("num") != 32 {
|
|
|
|
b.Fatal("Get failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Get("mymap.a.b.c") != 32 {
|
|
|
|
b.Fatal("Get failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Run("Custom", func(b *testing.B) {
|
|
|
|
cfg := New()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
runMethods(b, cfg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|