Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error return to Fixture.Start() #29

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(_ context.Context) {}
func fooStart(_ context.Context) error { return nil }

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

// genericFixture adapts functions and state dicts into the Fixture type
type genericFixture struct {
starter func(context.Context)
starter func(context.Context) error
stopper func(context.Context)
state map[string]interface{}
}

// Start sets up any resources the fixture uses
func (f *genericFixture) Start(ctx context.Context) {
func (f *genericFixture) Start(ctx context.Context) error {
if f.starter != nil {
f.starter(ctx)
return f.starter(ctx)
}
return nil
}

// Stop cleans up any resources the fixture uses
Expand Down Expand Up @@ -55,7 +56,7 @@ 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(context.Context)) genericFixtureModifier {
func WithStarter(starter func(context.Context) error) genericFixtureModifier {
return func(f *genericFixture) {
f.starter = starter
}
Expand Down
3 changes: 2 additions & 1 deletion fixture/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func TestStarter(t *testing.T) {

started := false

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

f := fixture.New(
Expand Down
2 changes: 1 addition & 1 deletion fixture/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type jsonFixture struct {
data interface{}
}

func (f *jsonFixture) Start(_ context.Context) {}
func (f *jsonFixture) Start(_ context.Context) error { return nil }

func (f *jsonFixture) Stop(_ context.Context) {}

Expand Down
4 changes: 3 additions & 1 deletion scenario/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {
if !found {
return gdterrors.RequiredFixtureMissing(fname)
}
fix.Start(ctx)
if err := fix.Start(ctx); err != nil {
return err
}
defer fix.Stop(ctx)
}
}
Expand Down
20 changes: 20 additions & 0 deletions scenario/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ func TestMissingFixtures(t *testing.T) {
assert.ErrorIs(err, gdterrors.RuntimeError)
}

func TestFixtureStartError(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

fp := filepath.Join("testdata", "fixture-start-error.yaml")
f, err := os.Open(fp)
require.Nil(err)

s, err := scenario.FromReader(f, scenario.WithPath(fp))
require.Nil(err)
require.NotNil(s)

ctx := gdtcontext.New()
ctx = gdtcontext.RegisterFixture(ctx, "start-error", errStarterFixture)

err = s.Run(ctx, t)
assert.NotNil(err)
assert.ErrorContains(err, "error starting fixture!")
}

func TestDebugFlushing(t *testing.T) {
require := require.New(t)

Expand Down
22 changes: 22 additions & 0 deletions scenario/stub_fixtures_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.

package scenario_test

import (
"context"
"fmt"

"github.com/gdt-dev/gdt/fixture"
)

var (
errStarter = func(_ context.Context) error {
return fmt.Errorf("error starting fixture!")
}

errStarterFixture = fixture.New(
fixture.WithStarter(errStarter),
)
)
7 changes: 7 additions & 0 deletions scenario/testdata/fixture-start-error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: fixture-start-error
description: a scenario with a fixture that errors in start
fixtures:
- start-error
tests:
- foo: bar

2 changes: 1 addition & 1 deletion types/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "context"
// A Fixture allows state to be passed from setups
type Fixture interface {
// Start sets up the fixture
Start(context.Context)
Start(context.Context) error
// Stop tears down the fixture, cleaning up any owned resources
Stop(context.Context)
// HasState returns true if the fixture contains some state with the given
Expand Down
Loading