Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package rbcrun |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "runtime" |
| 22 | "testing" |
| 23 | |
| 24 | "go.starlark.net/resolve" |
| 25 | "go.starlark.net/starlark" |
| 26 | "go.starlark.net/starlarktest" |
| 27 | ) |
| 28 | |
| 29 | // In order to use "assert.star" from go/starlark.net/starlarktest in the tests, |
| 30 | // provide: |
| 31 | // * load function that handles "assert.star" |
| 32 | // * starlarktest.DataFile function that finds its location |
| 33 | |
| 34 | func init() { |
| 35 | starlarktestSetup() |
| 36 | } |
| 37 | |
| 38 | func starlarktestSetup() { |
| 39 | resolve.AllowLambda = true |
| 40 | starlarktest.DataFile = func(pkgdir, filename string) string { |
| 41 | // The caller expects this function to return the path to the |
| 42 | // data file. The implementation assumes that the source file |
| 43 | // containing the caller and the data file are in the same |
| 44 | // directory. It's ugly. Not sure what's the better way. |
| 45 | // TODO(asmundak): handle Bazel case |
| 46 | _, starlarktestSrcFile, _, _ := runtime.Caller(1) |
| 47 | if filepath.Base(starlarktestSrcFile) != "starlarktest.go" { |
| 48 | panic(fmt.Errorf("this function should be called from starlarktest.go, got %s", |
| 49 | starlarktestSrcFile)) |
| 50 | } |
| 51 | return filepath.Join(filepath.Dir(starlarktestSrcFile), filename) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Common setup for the tests: create thread, change to the test directory |
Cole Faust | a874f88 | 2023-05-05 11:46:51 -0700 | [diff] [blame] | 56 | func testSetup(t *testing.T) *starlark.Thread { |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 57 | thread := &starlark.Thread{ |
| 58 | Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 59 | if module == "assert.star" { |
| 60 | return starlarktest.LoadAssertModule() |
| 61 | } |
| 62 | return nil, fmt.Errorf("load not implemented") |
| 63 | }} |
| 64 | starlarktest.SetReporter(thread, t) |
| 65 | if err := os.Chdir(dataDir()); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | return thread |
| 69 | } |
| 70 | |
| 71 | func dataDir() string { |
| 72 | _, thisSrcFile, _, _ := runtime.Caller(0) |
| 73 | return filepath.Join(filepath.Dir(thisSrcFile), "testdata") |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func exerciseStarlarkTestFile(t *testing.T, starFile string) { |
| 77 | // In order to use "assert.star" from go/starlark.net/starlarktest in the tests, provide: |
| 78 | // * load function that handles "assert.star" |
| 79 | // * starlarktest.DataFile function that finds its location |
Cole Faust | a874f88 | 2023-05-05 11:46:51 -0700 | [diff] [blame] | 80 | if err := os.Chdir(dataDir()); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 83 | thread := &starlark.Thread{ |
| 84 | Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 85 | if module == "assert.star" { |
| 86 | return starlarktest.LoadAssertModule() |
| 87 | } |
| 88 | return nil, fmt.Errorf("load not implemented") |
| 89 | }} |
| 90 | starlarktest.SetReporter(thread, t) |
| 91 | _, thisSrcFile, _, _ := runtime.Caller(0) |
| 92 | filename := filepath.Join(filepath.Dir(thisSrcFile), starFile) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 93 | thread.SetLocal(executionModeKey, ExecutionModeRbc) |
| 94 | thread.SetLocal(shellKey, "/bin/sh") |
| 95 | if _, err := starlark.ExecFile(thread, filename, nil, rbcBuiltins); err != nil { |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 96 | if err, ok := err.(*starlark.EvalError); ok { |
| 97 | t.Fatal(err.Backtrace()) |
| 98 | } |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | } |
| 102 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 103 | func TestFileOps(t *testing.T) { |
| 104 | // TODO(asmundak): convert this to use exerciseStarlarkTestFile |
Cole Faust | a874f88 | 2023-05-05 11:46:51 -0700 | [diff] [blame] | 105 | thread := testSetup(t) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 106 | if _, err := starlark.ExecFile(thread, "file_ops.star", nil, rbcBuiltins); err != nil { |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 107 | if err, ok := err.(*starlark.EvalError); ok { |
| 108 | t.Fatal(err.Backtrace()) |
| 109 | } |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestLoad(t *testing.T) { |
| 115 | // TODO(asmundak): convert this to use exerciseStarlarkTestFile |
Cole Faust | a874f88 | 2023-05-05 11:46:51 -0700 | [diff] [blame] | 116 | thread := testSetup(t) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 117 | thread.Load = func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 118 | if module == "assert.star" { |
| 119 | return starlarktest.LoadAssertModule() |
| 120 | } else { |
| 121 | return loader(thread, module) |
| 122 | } |
| 123 | } |
| 124 | dir := dataDir() |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 125 | if err := os.Chdir(filepath.Dir(dir)); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame^] | 128 | thread.SetLocal(allowExternalEntrypointKey, false) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 129 | thread.SetLocal(callerDirKey, dir) |
Cole Faust | c63ce1a | 2023-05-09 14:56:36 -0700 | [diff] [blame] | 130 | thread.SetLocal(executionModeKey, ExecutionModeRbc) |
| 131 | if _, err := starlark.ExecFile(thread, "testdata/load.star", nil, rbcBuiltins); err != nil { |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 132 | if err, ok := err.(*starlark.EvalError); ok { |
| 133 | t.Fatal(err.Backtrace()) |
| 134 | } |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | } |
| 138 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 139 | func TestShell(t *testing.T) { |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 140 | exerciseStarlarkTestFile(t, "testdata/shell.star") |
| 141 | } |