config: Add tests for Map.AllowUnknown, rename named return values

This commit is contained in:
fox.cpp 2019-11-07 20:13:03 +03:00
parent 958a58673a
commit 7b08f624f0
No known key found for this signature in database
GPG key ID: E76D97CCEDE90B6C
5 changed files with 53 additions and 11 deletions

View file

@ -70,7 +70,7 @@ func (bl *DNSBL) Init(cfg *config.Map) error {
return check.FailAction{Reject: true}, nil
}, check.FailActionDirective, &bl.listedAction)
cfg.AllowUnknown()
unmatched, err := cfg.Process()
unknown, err := cfg.Process()
if err != nil {
return err
}
@ -82,7 +82,7 @@ func (bl *DNSBL) Init(cfg *config.Map) error {
bl.bls = append(bl.bls, cfg)
}
for _, node := range unmatched {
for _, node := range unknown {
if err := bl.readListCfg(node); err != nil {
return err
}

View file

@ -559,13 +559,13 @@ func (m *Map) Custom(name string, inheritGlobal, required bool, defaultVal func(
// Process maps variables from global configuration and block passed in NewMap.
//
// If Map instance was not created using NewMap - Process panics.
func (m *Map) Process() (unmatched []Node, err error) {
func (m *Map) Process() (unknown []Node, err error) {
return m.ProcessWith(m.Globals, m.Block)
}
// Process maps variables from global configuration and block passed in arguments.
func (m *Map) ProcessWith(globalCfg map[string]interface{}, block *Node) (unmatched []Node, err error) {
unmatched = make([]Node, 0, len(block.Children))
func (m *Map) ProcessWith(globalCfg map[string]interface{}, block *Node) (unknown []Node, err error) {
unknown = make([]Node, 0, len(block.Children))
matched := make(map[string]bool)
m.Values = make(map[string]interface{})
@ -581,7 +581,7 @@ func (m *Map) ProcessWith(globalCfg map[string]interface{}, block *Node) (unmatc
if !m.allowUnknown {
return nil, m.MatchErr("unexpected directive: %s", subnode.Name)
}
unmatched = append(unmatched, subnode)
unknown = append(unknown, subnode)
continue
}
@ -645,5 +645,5 @@ func (m *Map) ProcessWith(globalCfg map[string]interface{}, block *Node) (unmatc
}
}
return unmatched, nil
return unknown, nil
}

View file

@ -202,6 +202,48 @@ func TestMapProcess_Duplicate(t *testing.T) {
}
}
func TestMapProcess_Unexpected(t *testing.T) {
cfg := Node{
Children: []Node{
{
Name: "foo",
Args: []string{"baz"},
},
{
Name: "bar",
Args: []string{"baz"},
},
},
}
m := NewMap(nil, &cfg)
foo := ""
m.Custom("bar", false, true, nil, func(_ *Map, n *Node) (interface{}, error) {
return n.Args[0], nil
}, &foo)
_, err := m.Process()
if err == nil {
t.Errorf("Expected failure")
}
m.AllowUnknown()
unknown, err := m.Process()
if err != nil {
t.Errorf("Unexpected failure: %v", err)
}
if len(unknown) != 1 {
t.Fatalf("Wrong amount of unknown nodes: %v", len(unknown))
}
if unknown[0].Name != "foo" {
t.Fatalf("Wrong node in unknown: %v", unknown[0].Name)
}
}
func TestMapInt(t *testing.T) {
cfg := Node{
Children: []Node{

View file

@ -364,11 +364,11 @@ func (endp *Endpoint) setConfig(cfg *config.Map) error {
cfg.Bool("defer_sender_reject", false, true, &endp.deferServerReject)
cfg.Int("max_logged_rcpt_errors", false, false, 5, &endp.maxLoggedRcptErrors)
cfg.AllowUnknown()
unmatched, err := cfg.Process()
unknown, err := cfg.Process()
if err != nil {
return err
}
endp.pipeline, err = msgpipeline.New(cfg.Globals, unmatched)
endp.pipeline, err = msgpipeline.New(cfg.Globals, unknown)
if err != nil {
return err
}

View file

@ -34,14 +34,14 @@ func Start(cfg []config.Node) error {
globals.Custom("log", false, false, defaultLogOutput, logOutput, &log.DefaultLogger.Out)
globals.Bool("debug", false, log.DefaultLogger.Debug, &log.DefaultLogger.Debug)
globals.AllowUnknown()
unmatched, err := globals.Process()
unknown, err := globals.Process()
if err != nil {
return err
}
defer log.DefaultLogger.Out.Close()
insts, err := instancesFromConfig(globals.Values, unmatched)
insts, err := instancesFromConfig(globals.Values, unknown)
if err != nil {
return err
}