mtest: refactor to use Component instead of Context
This commit is contained in:
parent
31ed0fe625
commit
af836c1cab
@ -6,60 +6,64 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
||||||
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
||||||
)
|
)
|
||||||
|
|
||||||
type envCtxKey int
|
type envCmpKey int
|
||||||
|
|
||||||
// Context creates and returns a root Context suitable for testing.
|
// Component creates and returns a root Component suitable for testing.
|
||||||
func Context() context.Context {
|
func Component() *mcmp.Component {
|
||||||
ctx := context.Background()
|
cmp := new(mcmp.Component)
|
||||||
logger := mlog.NewLogger()
|
logger := mlog.NewLogger()
|
||||||
logger.SetMaxLevel(mlog.DebugLevel)
|
logger.SetMaxLevel(mlog.DebugLevel)
|
||||||
return mlog.WithLogger(ctx, logger)
|
mlog.SetLogger(cmp, logger)
|
||||||
|
|
||||||
|
mrun.InitHook(cmp, func(context.Context) error {
|
||||||
|
envVals := mcmp.GetSeriesValues(cmp, envCmpKey(0))
|
||||||
|
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})
|
||||||
|
})
|
||||||
|
|
||||||
|
return cmp
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithEnv sets the given environment variable on the given Context, such that
|
// Env sets the given environment variable on the given Component, such that it
|
||||||
// it will be used as if it was a real environment variable when the Run
|
// will be used as if it was a real environment variable when the Run function
|
||||||
// function from this package is called.
|
// from this package is called.
|
||||||
func WithEnv(ctx context.Context, key, val string) context.Context {
|
//
|
||||||
prevEnv, _ := ctx.Value(envCtxKey(0)).([][2]string)
|
// This function will panic if not called on the root Component.
|
||||||
env := make([][2]string, len(prevEnv), len(prevEnv)+1)
|
func Env(cmp *mcmp.Component, key, val string) {
|
||||||
copy(env, prevEnv)
|
if len(cmp.Path()) != 0 {
|
||||||
env = append(env, [2]string{key, val})
|
panic("Env should only be called on the root Component")
|
||||||
return context.WithValue(ctx, envCtxKey(0), env)
|
}
|
||||||
|
mcmp.AddSeriesValue(cmp, envCmpKey(0), [2]string{key, val})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run performs the following using the given Context:
|
// Run performs the following using the given Component:
|
||||||
//
|
//
|
||||||
// - Calls mcfg.Populate using any variables set by WithEnv.
|
// - Calls mrun.Init, which calls mcfg.Populate using any variables set by Env.
|
||||||
//
|
|
||||||
// - Calls mrun.Start
|
|
||||||
//
|
//
|
||||||
// - Calls the passed in body callback.
|
// - Calls the passed in body callback.
|
||||||
//
|
//
|
||||||
// - Calls mrun.Stop
|
// - Calls mrun.Shutdown
|
||||||
//
|
//
|
||||||
// The intention is that Run is used within a test on a Context created via
|
// The intention is that Run is used within a test on a Component created via
|
||||||
// NewCtx, after any setup functions have been called (e.g. mnet.WithListener).
|
// this package's Component function, after any setup functions have been called
|
||||||
func Run(ctx context.Context, t *testing.T, body func()) {
|
// (e.g. mnet.AddListener).
|
||||||
envTups, _ := ctx.Value(envCtxKey(0)).([][2]string)
|
func Run(cmp *mcmp.Component, t *testing.T, body func()) {
|
||||||
env := make([]string, 0, len(envTups))
|
if err := mrun.Init(context.Background(), cmp); err != nil {
|
||||||
for _, tup := range envTups {
|
|
||||||
env = append(env, tup[0]+"="+tup[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, err := mcfg.Populate(ctx, &mcfg.SourceEnv{Env: env})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
} else if err := mrun.Start(ctx); err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
body()
|
body()
|
||||||
|
|
||||||
if err := mrun.Stop(ctx); err != nil {
|
if err := mrun.Shutdown(context.Background(), cmp); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestRun(t *T) {
|
func TestRun(t *T) {
|
||||||
ctx := Context()
|
cmp := Component()
|
||||||
ctx, arg := mcfg.WithRequiredString(ctx, "arg", "Required by this test")
|
Env(cmp, "ARG", "foo")
|
||||||
ctx = WithEnv(ctx, "ARG", "foo")
|
|
||||||
Run(ctx, t, func() {
|
arg := mcfg.String(cmp, "arg", mcfg.ParamRequired())
|
||||||
|
Run(cmp, t, func() {
|
||||||
if *arg != "foo" {
|
if *arg != "foo" {
|
||||||
t.Fatalf(`arg not set to "foo", is set to %q`, *arg)
|
t.Fatalf(`arg not set to "foo", is set to %q`, *arg)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user