From 8b27fb617705a5f49c5337d3470a6e22f26e26d8 Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Sat, 8 Jun 2024 20:00:32 -0400 Subject: [PATCH] add context argument to fixture Stop/Start In order to integrate fixtures into debug printing, we needed to add context arguments to the `fixture.Start()` and `fixture.Stop()` methods. Signed-off-by: Jay Pipes --- context/context_test.go | 2 +- fixture/generic.go | 17 +++++++++-------- fixture/generic_test.go | 9 +++++---- fixture/json/json.go | 5 +++-- scenario/run.go | 4 ++-- types/fixture.go | 6 ++++-- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/context/context_test.go b/context/context_test.go index 0c92ee3..82fe57f 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -16,7 +16,7 @@ import ( "gopkg.in/yaml.v3" ) -func fooStart() {} +func fooStart(_ context.Context) {} type fooDefaults struct { Foo string `yaml:"foo"` diff --git a/fixture/generic.go b/fixture/generic.go index 40b1a3f..84c455b 100644 --- a/fixture/generic.go +++ b/fixture/generic.go @@ -5,6 +5,7 @@ package fixture import ( + "context" "strings" gdttypes "github.com/gdt-dev/gdt/types" @@ -12,22 +13,22 @@ import ( // genericFixture adapts functions and state dicts into the Fixture type type genericFixture struct { - starter func() - stopper func() + starter func(context.Context) + stopper func(context.Context) state map[string]interface{} } // Start sets up any resources the fixture uses -func (f *genericFixture) Start() { +func (f *genericFixture) Start(ctx context.Context) { if f.starter != nil { - f.starter() + f.starter(ctx) } } // Stop cleans up any resources the fixture uses -func (f *genericFixture) Stop() { +func (f *genericFixture) Stop(ctx context.Context) { if f.stopper != nil { - f.stopper() + f.stopper(ctx) } } @@ -54,14 +55,14 @@ func (f *genericFixture) State(key string) interface{} { type genericFixtureModifier func(s *genericFixture) // WithStarter allows a starter functor to be adapted into a fixture -func WithStarter(starter func()) genericFixtureModifier { +func WithStarter(starter func(context.Context)) genericFixtureModifier { return func(f *genericFixture) { f.starter = starter } } // WithStopper allows a stopper functor to be adapted into a fixture -func WithStopper(stopper func()) genericFixtureModifier { +func WithStopper(stopper func(context.Context)) genericFixtureModifier { return func(f *genericFixture) { f.stopper = stopper } diff --git a/fixture/generic_test.go b/fixture/generic_test.go index 5867476..48c73ac 100644 --- a/fixture/generic_test.go +++ b/fixture/generic_test.go @@ -5,6 +5,7 @@ package fixture_test import ( + "context" "testing" "github.com/gdt-dev/gdt/fixture" @@ -35,7 +36,7 @@ func TestStarter(t *testing.T) { started := false - starter := func() { + starter := func(_ context.Context) { started = true } @@ -45,7 +46,7 @@ func TestStarter(t *testing.T) { assert.False(started) - f.Start() + f.Start(context.TODO()) assert.True(started) } @@ -55,7 +56,7 @@ func TestStopper(t *testing.T) { stopped := false - stopper := func() { + stopper := func(_ context.Context) { stopped = true } @@ -65,7 +66,7 @@ func TestStopper(t *testing.T) { assert.False(stopped) - f.Stop() + f.Stop(context.TODO()) assert.True(stopped) } diff --git a/fixture/json/json.go b/fixture/json/json.go index 8829f5c..1a4629f 100644 --- a/fixture/json/json.go +++ b/fixture/json/json.go @@ -5,6 +5,7 @@ package json import ( + "context" "encoding/json" "io" "io/ioutil" @@ -18,9 +19,9 @@ type jsonFixture struct { data interface{} } -func (f *jsonFixture) Start() {} +func (f *jsonFixture) Start(_ context.Context) {} -func (f *jsonFixture) Stop() {} +func (f *jsonFixture) Stop(_ context.Context) {} // HasState returns true if the supplied JSONPath expression results in a found // value in the fixture's data diff --git a/scenario/run.go b/scenario/run.go index 7baa5d6..c31f796 100644 --- a/scenario/run.go +++ b/scenario/run.go @@ -32,8 +32,8 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error { if !found { return gdterrors.RequiredFixtureMissing(fname) } - fix.Start() - defer fix.Stop() + fix.Start(ctx) + defer fix.Stop(ctx) } } var rterr error diff --git a/types/fixture.go b/types/fixture.go index c4c0371..d421d78 100644 --- a/types/fixture.go +++ b/types/fixture.go @@ -4,12 +4,14 @@ package types +import "context" + // A Fixture allows state to be passed from setups type Fixture interface { // Start sets up the fixture - Start() + Start(context.Context) // Stop tears down the fixture, cleaning up any owned resources - Stop() + Stop(context.Context) // HasState returns true if the fixture contains some state with the given // key HasState(string) bool