Skip to content

Commit

Permalink
Support passing in variables into tests (#91)
Browse files Browse the repository at this point in the history
* Support passing variables into tests

* Add test for test variables

* PR Feedback
  • Loading branch information
seena-stripe committed Aug 18, 2021
1 parent 5daccc0 commit ed7ed7f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
24 changes: 18 additions & 6 deletions internal/go/skycfg/skycfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ def test_helper1(t):
x = helper1()
t.assert(x == 12345)
def test_main(ctx):
msg = main(ctx)[0]
ctx.assert(len(msg.r_string) == 1)
ctx.assert(msg.r_string[0] == "var_value")
def main(ctx):
msg = test_proto.MessageV2()
msg.f_int64 = helper1()
msg.f_string = json.encode(helper2(ctx))
msg.r_string.append(ctx.vars["var_key"])
return [msg]
`,
Expand Down Expand Up @@ -275,6 +281,7 @@ func TestSkycfgEndToEnd(t *testing.T) {
FString: proto.String(
`{"key1":"value1","key2":"key3=value3","key4":{"key5":"value5","var_key":"var_value"}}`,
),
RString: []string{"var_value"},
},
},
},
Expand Down Expand Up @@ -380,11 +387,6 @@ func TestSkycfgTesting(t *testing.T) {
t.Error("Unexpected error loading test1.sky", err)
}

tests := config.Tests()
if len(tests) != 4 {
t.Error("Expected 4 tests but found", len(tests))
}

cases := map[string]testTestCase{
"test_helper1": testTestCase{
passes: true,
Expand All @@ -399,10 +401,20 @@ func TestSkycfgTesting(t *testing.T) {
"test_helper2_errors": testTestCase{
errors: true,
},
"test_main": testTestCase{
passes: true,
},
}

tests := config.Tests()
if len(tests) != len(cases) {
t.Error("Expected %d tests but found %d", len(cases), len(tests))
}

for _, test := range tests {
result, err := test.Run(ctx)
result, err := test.Run(ctx, skycfg.WithTestVars(starlark.StringDict{
"var_key": starlark.String("var_value"),
}))
testCase, ok := cases[test.Name()]
if !ok {
t.Error("Could not find test case for test", test.Name())
Expand Down
34 changes: 32 additions & 2 deletions skycfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,39 @@ func (t *Test) Name() string {
return t.callable.Name()
}

// An TestOption adjusts details of how a Skycfg config's test functions are
// executed.
type TestOption interface {
applyTest(*testOptions)
}

type testOptions struct {
vars *starlark.Dict
}

type fnTestOption func(*testOptions)

func (fn fnTestOption) applyTest(opts *testOptions) { fn(opts) }

// WithTestVars adds key:value pairs to the ctx.vars dict passed to tests
func WithTestVars(vars starlark.StringDict) TestOption {
return fnTestOption(func(opts *testOptions) {
for key, value := range vars {
opts.vars.SetKey(starlark.String(key), value)
}
})
}

// Run actually executes a test. It returns a TestResult if the test completes (even if it fails)
// The error return value will only be non-nil if the test execution itself errors.
func (t *Test) Run(ctx context.Context) (*TestResult, error) {
func (t *Test) Run(ctx context.Context, opts ...TestOption) (*TestResult, error) {
parsedOpts := &testOptions{
vars: &starlark.Dict{},
}
for _, opt := range opts {
opt.applyTest(parsedOpts)
}

thread := &starlark.Thread{
Print: skyPrint,
}
Expand All @@ -448,7 +478,7 @@ func (t *Test) Run(ctx context.Context) (*TestResult, error) {
testCtx := &impl.Module{
Name: "skycfg_test_ctx",
Attrs: starlark.StringDict(map[string]starlark.Value{
"vars": &starlark.Dict{},
"vars": parsedOpts.vars,
"assert": assertModule,
}),
}
Expand Down

0 comments on commit ed7ed7f

Please sign in to comment.