Skip to content

Commit

Permalink
Merge pull request #44 from jaypipes/runnable
Browse files Browse the repository at this point in the history
remove Scenario.evalPlugins
  • Loading branch information
jaypipes authored Jul 3, 2024
2 parents 06ca249 + 8fe4561 commit 8548d8c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 24 deletions.
2 changes: 2 additions & 0 deletions api/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
// output or behaviour. All gdt plugins have their own Spec structs that
// inherit from this base struct.
type Spec struct {
// Plugin is a pointer to the plugin that successfully parsed the test spec
Plugin Plugin `yaml:"-"`
// Defaults contains the parsed defaults for the Spec. These are injected
// by the scenario during parse.
Defaults *Defaults `yaml:"-"`
Expand Down
7 changes: 6 additions & 1 deletion internal/testutil/plugin/bar/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import (
"gopkg.in/yaml.v3"
)

var (
// this is just for testing purposes...
PluginRef = &Plugin{}
)

func init() {
plugin.Register(&Plugin{})
plugin.Register(PluginRef)
}

type Defaults struct {
Expand Down
7 changes: 6 additions & 1 deletion internal/testutil/plugin/foo/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ import (
"gopkg.in/yaml.v3"
)

var (
// this is just for testing purposes...
PluginRef = &Plugin{}
)

func init() {
plugin.Register(&Plugin{})
plugin.Register(PluginRef)
}

type InnerDefaults struct {
Expand Down
1 change: 1 addition & 0 deletions plugin/exec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestSimpleCommand(t *testing.T) {
expTests := []api.Evaluable{
&gdtexec.Spec{
Spec: api.Spec{
Plugin: gdtexec.PluginRef,
Index: 0,
Defaults: &api.Defaults{},
},
Expand Down
13 changes: 9 additions & 4 deletions plugin/exec/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import (
gdtplugin "github.com/gdt-dev/gdt/plugin"
)

var (
// this is just for testing purposes...
PluginRef = &plugin{}
)

func init() {
gdtplugin.Register(PluginRef)
}

var (
DefaultTimeout = "10s"
)
Expand All @@ -20,10 +29,6 @@ func OverrideDefaultTimeout(d string) {
DefaultTimeout = d
}

func init() {
gdtplugin.Register(Plugin())
}

const (
pluginName = "exec"
)
Expand Down
5 changes: 1 addition & 4 deletions scenario/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error {
s.Defaults = defaults
}
}
// We store a lookup to the parsing plugin for each parsed test spec
evalPlugins := map[int]api.Plugin{}
for i := 0; i < len(node.Content); i += 2 {
keyNode := node.Content[i]
if keyNode.Kind != yaml.ScalarNode {
Expand Down Expand Up @@ -116,10 +114,10 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error {
}
return err
}
base.Plugin = plugin
sp.SetBase(base)
s.Tests = append(s.Tests, sp)
parsed = true
evalPlugins[idx] = plugin
break
}
}
Expand Down Expand Up @@ -161,6 +159,5 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error {
}
}
}
s.evalPlugins = evalPlugins
return nil
}
8 changes: 8 additions & 0 deletions scenario/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func TestKnownSpec(t *testing.T) {
expTests := []api.Evaluable{
&foo.Spec{
Spec: api.Spec{
Plugin: foo.PluginRef,
Index: 0,
Name: "bar",
Defaults: expSpecDefaults,
Expand All @@ -244,6 +245,7 @@ func TestKnownSpec(t *testing.T) {
},
&foo.Spec{
Spec: api.Spec{
Plugin: foo.PluginRef,
Index: 1,
Description: "Bazzy Bizzy",
Defaults: expSpecDefaults,
Expand Down Expand Up @@ -272,13 +274,15 @@ func TestMultipleSpec(t *testing.T) {
expTests := []api.Evaluable{
&foo.Spec{
Spec: api.Spec{
Plugin: foo.PluginRef,
Index: 0,
Defaults: &api.Defaults{},
},
Foo: "bar",
},
&bar.Spec{
Spec: api.Spec{
Plugin: bar.PluginRef,
Index: 1,
Defaults: &api.Defaults{},
},
Expand Down Expand Up @@ -340,6 +344,7 @@ func TestEnvExpansion(t *testing.T) {
expTests := []api.Evaluable{
&foo.Spec{
Spec: api.Spec{
Plugin: foo.PluginRef,
Index: 0,
Name: "$NOT_EXPANDED",
Defaults: expSpecDefaults,
Expand All @@ -348,6 +353,7 @@ func TestEnvExpansion(t *testing.T) {
},
&foo.Spec{
Spec: api.Spec{
Plugin: foo.PluginRef,
Index: 1,
Description: "Bazzy Bizzy",
Defaults: expSpecDefaults,
Expand Down Expand Up @@ -406,6 +412,7 @@ func TestScenarioDefaults(t *testing.T) {
expTests := []api.Evaluable{
&foo.Spec{
Spec: api.Spec{
Plugin: foo.PluginRef,
Index: 0,
Defaults: expSpecDefaults,
Timeout: &api.Timeout{
Expand All @@ -416,6 +423,7 @@ func TestScenarioDefaults(t *testing.T) {
},
&foo.Spec{
Spec: api.Spec{
Plugin: foo.PluginRef,
Index: 1,
Defaults: expSpecDefaults,
},
Expand Down
35 changes: 21 additions & 14 deletions scenario/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {
defer fix.Stop(ctx)
}
}
var rterr error
var scDefaults *Defaults
scDefaultsAny, found := s.Defaults[DefaultsKey]
if found {
scDefaults = scDefaultsAny.(*Defaults)
}
// If the test author has specified any pre-flight checks in the `skip-if`
// collection, evaluate those first and if any failed, skip the scenario's
// tests.
Expand All @@ -63,6 +57,10 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {
return nil
}
}

var err error
scDefaults := s.getScenarioDefaults()

t.Run(s.Title(), func(t *testing.T) {
ctx = gdtcontext.PushTrace(ctx, s.Title())
defer func() {
Expand All @@ -86,7 +84,7 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {
specCtx = gdtcontext.PopTrace(specCtx)
}

plugin := s.evalPlugins[idx]
plugin := sb.Plugin

rt := getRetry(specCtx, scDefaults, plugin, spec)

Expand All @@ -109,15 +107,14 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {

select {
case <-specCtx.Done():
t.Fatalf("assertion failed: timeout exceeded (%s)", to.After)
popTracer()
specCancel()
t.Fatalf("assertion failed: timeout exceeded (%s)", to.After)
break
case runres := <-ch:
res = runres.r
rterr = runres.err
err = runres.err
}
if rterr != nil {
if err != nil {
popTracer()
specCancel()
break
Expand All @@ -134,14 +131,14 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error {
if res.HasData() {
ctx = gdtcontext.StorePriorRun(ctx, res.Data())
}
popTracer()
specCancel()
for _, fail := range res.Failures() {
t.Fatal(fail)
}
popTracer()
specCancel()
}
})
return rterr
return err
}

type runSpecRes struct {
Expand Down Expand Up @@ -375,3 +372,13 @@ func getRetry(
}
return nil
}

// getScenarioDefaults returns the Defaults parsed from the scenario's YAML
// file's `defaults` field, or nil if none were specified.
func (s *Scenario) getScenarioDefaults() *Defaults {
scDefaultsAny, found := s.Defaults[DefaultsKey]
if found {
return scDefaultsAny.(*Defaults)
}
return nil
}

0 comments on commit 8548d8c

Please sign in to comment.