2018-07-03 18:18:51 +00:00
|
|
|
// Package mtest implements functionality useful for testing.
|
|
|
|
package mtest
|
2019-02-03 01:58:18 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2019-02-03 01:58:18 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
2019-06-15 23:51:04 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
2019-02-03 01:58:18 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
|
|
|
)
|
|
|
|
|
2019-06-15 23:51:04 +00:00
|
|
|
type envCmpKey int
|
2019-02-03 01:58:18 +00:00
|
|
|
|
2019-06-15 23:51:04 +00:00
|
|
|
// Component creates and returns a root Component suitable for testing.
|
|
|
|
func Component() *mcmp.Component {
|
|
|
|
cmp := new(mcmp.Component)
|
2019-02-05 20:18:17 +00:00
|
|
|
logger := mlog.NewLogger()
|
|
|
|
logger.SetMaxLevel(mlog.DebugLevel)
|
2019-06-15 23:51:04 +00:00
|
|
|
mlog.SetLogger(cmp, logger)
|
|
|
|
|
|
|
|
mrun.InitHook(cmp, func(context.Context) error {
|
2019-06-15 23:52:55 +00:00
|
|
|
envVals := mcmp.SeriesValues(cmp, envCmpKey(0))
|
2019-06-15 23:51:04 +00:00
|
|
|
env := make([]string, 0, len(envVals))
|
|
|
|
for _, val := range envVals {
|
|
|
|
tup := val.([2]string)
|
|
|
|
env = append(env, tup[0]+"="+tup[1])
|
|
|
|
}
|
|
|
|
return mcfg.Populate(cmp, &mcfg.SourceEnv{Env: env})
|
|
|
|
})
|
2019-02-03 01:58:18 +00:00
|
|
|
|
2019-06-15 23:51:04 +00:00
|
|
|
return cmp
|
2019-02-03 01:58:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 23:51:04 +00:00
|
|
|
// Env sets the given environment variable on the given Component, such that it
|
|
|
|
// will be used as if it was a real environment variable when the Run function
|
|
|
|
// from this package is called.
|
2019-02-03 01:58:18 +00:00
|
|
|
//
|
2019-06-15 23:51:04 +00:00
|
|
|
// This function will panic if not called on the root Component.
|
|
|
|
func Env(cmp *mcmp.Component, key, val string) {
|
|
|
|
if len(cmp.Path()) != 0 {
|
|
|
|
panic("Env should only be called on the root Component")
|
|
|
|
}
|
|
|
|
mcmp.AddSeriesValue(cmp, envCmpKey(0), [2]string{key, val})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run performs the following using the given Component:
|
2019-02-03 01:58:18 +00:00
|
|
|
//
|
2019-06-15 23:51:04 +00:00
|
|
|
// - Calls mrun.Init, which calls mcfg.Populate using any variables set by Env.
|
2019-02-03 01:58:18 +00:00
|
|
|
//
|
|
|
|
// - Calls the passed in body callback.
|
|
|
|
//
|
2019-06-15 23:51:04 +00:00
|
|
|
// - Calls mrun.Shutdown
|
2019-02-03 01:58:18 +00:00
|
|
|
//
|
2019-06-15 23:51:04 +00:00
|
|
|
// The intention is that Run is used within a test on a Component created via
|
|
|
|
// this package's Component function, after any setup functions have been called
|
|
|
|
// (e.g. mnet.AddListener).
|
|
|
|
func Run(cmp *mcmp.Component, t *testing.T, body func()) {
|
|
|
|
if err := mrun.Init(context.Background(), cmp); err != nil {
|
2019-02-03 01:58:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body()
|
|
|
|
|
2019-06-15 23:51:04 +00:00
|
|
|
if err := mrun.Shutdown(context.Background(), cmp); err != nil {
|
2019-02-03 01:58:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|