Skip to content

Commit

Permalink
Merge pull request #23 from jaypipes/fixture-debug
Browse files Browse the repository at this point in the history
add context argument to fixture Stop/Start
  • Loading branch information
jaypipes authored Jun 9, 2024
2 parents d9a4f14 + 8b27fb6 commit 8a8a812
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"gopkg.in/yaml.v3"
)

func fooStart() {}
func fooStart(_ context.Context) {}

type fooDefaults struct {
Foo string `yaml:"foo"`
Expand Down
17 changes: 9 additions & 8 deletions fixture/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
package fixture

import (
"context"
"strings"

gdttypes "github.com/gdt-dev/gdt/types"
)

// 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)
}
}

Expand All @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions fixture/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package fixture_test

import (
"context"
"testing"

"github.com/gdt-dev/gdt/fixture"
Expand Down Expand Up @@ -35,7 +36,7 @@ func TestStarter(t *testing.T) {

started := false

starter := func() {
starter := func(_ context.Context) {
started = true
}

Expand All @@ -45,7 +46,7 @@ func TestStarter(t *testing.T) {

assert.False(started)

f.Start()
f.Start(context.TODO())

assert.True(started)
}
Expand All @@ -55,7 +56,7 @@ func TestStopper(t *testing.T) {

stopped := false

stopper := func() {
stopper := func(_ context.Context) {
stopped = true
}

Expand All @@ -65,7 +66,7 @@ func TestStopper(t *testing.T) {

assert.False(stopped)

f.Stop()
f.Stop(context.TODO())

assert.True(stopped)
}
5 changes: 3 additions & 2 deletions fixture/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package json

import (
"context"
"encoding/json"
"io"
"io/ioutil"
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scenario/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions types/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8a8a812

Please sign in to comment.