mirror of
https://github.com/mcuadros/ascode
synced 2024-11-23 01:11:59 +01:00
a339458eab
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"testing"
|
|
|
|
"go.starlark.net/resolve"
|
|
"go.starlark.net/starlark"
|
|
"go.starlark.net/starlarktest"
|
|
)
|
|
|
|
func init() {
|
|
// The tests make extensive use of these not-yet-standard features.
|
|
resolve.AllowLambda = true
|
|
resolve.AllowNestedDef = true
|
|
resolve.AllowFloat = true
|
|
resolve.AllowSet = true
|
|
}
|
|
|
|
func TestProvider(t *testing.T) {
|
|
test(t, "testdata/provider.star")
|
|
}
|
|
|
|
func TestNestedBlock(t *testing.T) {
|
|
test(t, "testdata/nested.star")
|
|
}
|
|
|
|
func TestResource(t *testing.T) {
|
|
test(t, "testdata/resource.star")
|
|
}
|
|
|
|
func TestHCL(t *testing.T) {
|
|
test(t, "testdata/hcl.star")
|
|
}
|
|
|
|
func test(t *testing.T, filename string) {
|
|
log.SetOutput(ioutil.Discard)
|
|
thread := &starlark.Thread{Load: load}
|
|
starlarktest.SetReporter(thread, t)
|
|
|
|
provider := starlark.NewBuiltin("provider", func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
|
name := args.Index(0).(starlark.String)
|
|
version := args.Index(1).(starlark.String)
|
|
return MakeProvider(&PluginManager{".providers"}, string(name), string(version))
|
|
})
|
|
|
|
predeclared := starlark.StringDict{
|
|
"provider": provider,
|
|
"hcl": BuiltinHCL(),
|
|
}
|
|
|
|
_, err := starlark.ExecFile(thread, filename, nil, predeclared)
|
|
if err != nil {
|
|
if err, ok := err.(*starlark.EvalError); ok {
|
|
t.Fatal(err.Backtrace())
|
|
}
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// load implements the 'load' operation as used in the evaluator tests.
|
|
func load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
|
|
if module == "assert.star" {
|
|
return starlarktest.LoadAssertModule()
|
|
}
|
|
|
|
return nil, fmt.Errorf("load not implemented")
|
|
}
|