Remove unneeded rbcrun features

- rblf_cli and rblf_env
- -c and -f

This is in preparation for making rbcrun able to function as a more
general purpose starlark interpreter.

Bug: 280685526
Test: go test, ./out/rbc ./build/make/tests/run.rbc, ./build/bazel/ci/rbc_dashboard.py --quick aosp_arm64
Change-Id: Ifff9ce7b4369422f39c5003bb85a168c78bde7cf
This commit is contained in:
Cole Faust
2023-05-05 11:46:51 -07:00
parent 19f8eb1423
commit a874f88cee
9 changed files with 39 additions and 152 deletions

View File

@@ -223,16 +223,6 @@ func makeStringList(items []string) *starlark.List {
return starlark.NewList(elems)
}
// propsetFromEnv constructs a propset from the array of KEY=value strings
func structFromEnv(env []string) *starlarkstruct.Struct {
sd := make(map[string]starlark.Value, len(env))
for _, x := range env {
kv := strings.SplitN(x, "=", 2)
sd[kv[0]] = starlark.String(kv[1])
}
return starlarkstruct.FromStringDict(starlarkstruct.Default, sd)
}
func log(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
sep := " "
if err := starlark.UnpackArgs("print", nil, kwargs, "sep?", &sep); err != nil {
@@ -255,12 +245,10 @@ func log(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwa
return starlark.None, nil
}
func setup(env []string) {
func setup() {
// Create the symbols that aid makefile conversion. See README.md
builtins = starlark.StringDict{
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
"rblf_cli": structFromEnv(env),
"rblf_env": structFromEnv(os.Environ()),
// To convert find-copy-subdir and product-copy-files-by pattern
"rblf_find_files": starlark.NewBuiltin("rblf_find_files", find),
// To convert makefile's $(shell cmd)
@@ -285,11 +273,8 @@ func setup(env []string) {
// and the name that appears in error messages;
// * src is an optional source of bytes to use instead of filename
// (it can be a string, or a byte array, or an io.Reader instance)
// * commandVars is an array of "VAR=value" items. They are accessible from
// the starlark script as members of the `rblf_cli` propset.
func Run(filename string, src interface{}, commandVars []string) error {
setup(commandVars)
func Run(filename string, src interface{}) error {
setup()
mainThread := &starlark.Thread{
Name: "main",
Print: func(_ *starlark.Thread, msg string) { fmt.Println(msg) },

View File

@@ -53,8 +53,8 @@ func starlarktestSetup() {
}
// Common setup for the tests: create thread, change to the test directory
func testSetup(t *testing.T, env []string) *starlark.Thread {
setup(env)
func testSetup(t *testing.T) *starlark.Thread {
setup()
thread := &starlark.Thread{
Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
if module == "assert.star" {
@@ -72,14 +72,16 @@ func testSetup(t *testing.T, env []string) *starlark.Thread {
func dataDir() string {
_, thisSrcFile, _, _ := runtime.Caller(0)
return filepath.Join(filepath.Dir(thisSrcFile), "testdata")
}
func exerciseStarlarkTestFile(t *testing.T, starFile string) {
// In order to use "assert.star" from go/starlark.net/starlarktest in the tests, provide:
// * load function that handles "assert.star"
// * starlarktest.DataFile function that finds its location
setup(nil)
setup()
if err := os.Chdir(dataDir()); err != nil {
t.Fatal(err)
}
thread := &starlark.Thread{
Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
if module == "assert.star" {
@@ -98,26 +100,9 @@ func exerciseStarlarkTestFile(t *testing.T, starFile string) {
}
}
func TestCliAndEnv(t *testing.T) {
// TODO(asmundak): convert this to use exerciseStarlarkTestFile
if err := os.Setenv("TEST_ENVIRONMENT_FOO", "test_environment_foo"); err != nil {
t.Fatal(err)
}
thread := testSetup(t, []string{"CLI_FOO=foo"})
if _, err := starlark.ExecFile(thread, "cli_and_env.star", nil, builtins); err != nil {
if err, ok := err.(*starlark.EvalError); ok {
t.Fatal(err.Backtrace())
}
t.Fatal(err)
}
}
func TestFileOps(t *testing.T) {
// TODO(asmundak): convert this to use exerciseStarlarkTestFile
if err := os.Setenv("TEST_DATA_DIR", dataDir()); err != nil {
t.Fatal(err)
}
thread := testSetup(t, nil)
thread := testSetup(t)
if _, err := starlark.ExecFile(thread, "file_ops.star", nil, builtins); err != nil {
if err, ok := err.(*starlark.EvalError); ok {
t.Fatal(err.Backtrace())
@@ -128,7 +113,7 @@ func TestFileOps(t *testing.T) {
func TestLoad(t *testing.T) {
// TODO(asmundak): convert this to use exerciseStarlarkTestFile
thread := testSetup(t, nil)
thread := testSetup(t)
thread.Load = func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
if module == "assert.star" {
return starlarktest.LoadAssertModule()
@@ -148,8 +133,5 @@ func TestLoad(t *testing.T) {
}
func TestShell(t *testing.T) {
if err := os.Setenv("TEST_DATA_DIR", dataDir()); err != nil {
t.Fatal(err)
}
exerciseStarlarkTestFile(t, "testdata/shell.star")
}

View File

@@ -20,44 +20,25 @@ import (
"go.starlark.net/starlark"
"os"
"rbcrun"
"strings"
)
var (
execprog = flag.String("c", "", "execute program `prog`")
rootdir = flag.String("d", ".", "the value of // for load paths")
file = flag.String("f", "", "file to execute")
perfFile = flag.String("perf", "", "save performance data")
)
func main() {
flag.Parse()
filename := *file
var src interface{}
var env []string
filename := ""
rc := 0
for _, arg := range flag.Args() {
if strings.Contains(arg, "=") {
env = append(env, arg)
} else if filename == "" {
if filename == "" {
filename = arg
} else {
quit("only one file can be executed\n")
}
}
if *execprog != "" {
if filename != "" {
quit("either -c or file name should be present\n")
}
filename = "<cmdline>"
src = *execprog
}
if filename == "" {
if len(env) > 0 {
fmt.Fprintln(os.Stderr,
"no file to run -- if your file's name contains '=', use -f to specify it")
}
flag.Usage()
os.Exit(1)
}
@@ -75,7 +56,8 @@ func main() {
}
}
rbcrun.LoadPathRoot = *rootdir
err := rbcrun.Run(filename, src, env)
err := rbcrun.Run(filename, nil)
rc := 0
if *perfFile != "" {
if err2 := starlark.StopProfile(); err2 != nil {
fmt.Fprintln(os.Stderr, err2)

View File

@@ -1,11 +0,0 @@
# Tests rblf_env access
load("assert.star", "assert")
def test():
assert.eq(rblf_env.TEST_ENVIRONMENT_FOO, "test_environment_foo")
assert.fails(lambda: rblf_env.FOO_BAR_BAZ, ".*struct has no .FOO_BAR_BAZ attribute$")
assert.eq(rblf_cli.CLI_FOO, "foo")
test()

View File

@@ -1,22 +1,21 @@
# Tests file ops builtins
load("assert.star", "assert")
def test():
myname = "file_ops.star"
files = rblf_wildcard("*.star")
assert.true(myname in files, "expected %s in %s" % (myname, files))
files = rblf_wildcard("*.star", rblf_env.TEST_DATA_DIR)
files = rblf_wildcard("*.star")
assert.true(myname in files, "expected %s in %s" % (myname, files))
files = rblf_wildcard("*.xxx")
assert.true(len(files) == 0, "expansion should be empty but contains %s" % files)
mydir = "testdata"
myrelname = "%s/%s" % (mydir, myname)
files = rblf_find_files(rblf_env.TEST_DATA_DIR + "/../", "*")
files = rblf_find_files("../", "*")
assert.true(mydir in files and myrelname in files, "expected %s and %s in %s" % (mydir, myrelname, files))
files = rblf_find_files(rblf_env.TEST_DATA_DIR + "/../", "*", only_files=1)
files = rblf_find_files("../", "*", only_files=1)
assert.true(mydir not in files, "did not expect %s in %s" % (mydir, files))
assert.true(myrelname in files, "expected %s in %s" % (myrelname, files))
files = rblf_find_files(rblf_env.TEST_DATA_DIR + "/../", "*.star")
files = rblf_find_files("../", "*.star")
assert.true(myrelname in files, "expected %s in %s" % (myrelname, files))
test()

View File

@@ -1,5 +1,5 @@
# Tests "queue" data type
load("assert.star", "assert")
assert.eq("load.star shell.star", rblf_shell("cd %s && ls -1 shell.star load.star 2>&1" % rblf_env.TEST_DATA_DIR))
assert.eq("shell.star", rblf_shell("cd %s && echo shell.sta*" % rblf_env.TEST_DATA_DIR))
assert.eq("load.star shell.star", rblf_shell("ls -1 shell.star load.star 2>&1"))
assert.eq("shell.star", rblf_shell("echo shell.sta*"))