Skip to content

Commit

Permalink
fir orias
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Feb 22, 2024
1 parent 0012407 commit 87aa89b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions internal/testloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"testing"

"go.starlark.net/starlark"
Expand All @@ -18,8 +19,20 @@ type ModuleLoadFunc func() (starlark.StringDict, error)
// ThreadLoadFunc is a function that loads a Starlark module by name, usually used by the Starlark thread.
type ThreadLoadFunc func(thread *starlark.Thread, module string) (starlark.StringDict, error)

var initTestOnce sync.Once

// NewAssertLoader creates a Starlark thread loader that loads a module by name or asserts.star for testing.
func NewAssertLoader(moduleName string, loader ModuleLoadFunc) ThreadLoadFunc {
initTestOnce.Do(func() {
starlarktest.DataFile = func(pkgdir, filename string) string {
_, currFileName, _, ok := runtime.Caller(1)
if !ok {
return ""
}
return filepath.Join(filepath.Dir(currFileName), filename)
}
})
// for assert loader
return func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
switch module {
case moduleName:
Expand Down Expand Up @@ -51,13 +64,6 @@ func NewAssertLoader(moduleName string, loader ModuleLoadFunc) ThreadLoadFunc {
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
}, nil
case "assert.star":
starlarktest.DataFile = func(pkgdir, filename string) string {
_, currFileName, _, ok := runtime.Caller(1)
if !ok {
return ""
}
return filepath.Join(filepath.Dir(currFileName), filename)
}
return starlarktest.LoadAssertModule()
}

Expand Down

0 comments on commit 87aa89b

Please sign in to comment.