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 { |
| 57 | setup() |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 58 | thread := &starlark.Thread{ |
| 59 | Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 60 | if module == "assert.star" { |
| 61 | return starlarktest.LoadAssertModule() |
| 62 | } |
| 63 | return nil, fmt.Errorf("load not implemented") |
| 64 | }} |
| 65 | starlarktest.SetReporter(thread, t) |
| 66 | if err := os.Chdir(dataDir()); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | return thread |
| 70 | } |
| 71 | |
| 72 | func dataDir() string { |
| 73 | _, thisSrcFile, _, _ := runtime.Caller(0) |
| 74 | return filepath.Join(filepath.Dir(thisSrcFile), "testdata") |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | func exerciseStarlarkTestFile(t *testing.T, starFile string) { |
| 78 | // In order to use "assert.star" from go/starlark.net/starlarktest in the tests, provide: |
| 79 | // * load function that handles "assert.star" |
| 80 | // * starlarktest.DataFile function that finds its location |
Cole Faust | a874f88 | 2023-05-05 11:46:51 -0700 | [diff] [blame^] | 81 | setup() |
| 82 | if err := os.Chdir(dataDir()); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 85 | thread := &starlark.Thread{ |
| 86 | Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) { |
| 87 | if module == "assert.star" { |
| 88 | return starlarktest.LoadAssertModule() |
| 89 | } |
| 90 | return nil, fmt.Errorf("load not implemented") |
| 91 | }} |
| 92 | starlarktest.SetReporter(thread, t) |
| 93 | _, thisSrcFile, _, _ := runtime.Caller(0) |
| 94 | filename := filepath.Join(filepath.Dir(thisSrcFile), starFile) |
| 95 | if _, err := starlark.ExecFile(thread, filename, nil, builtins); err != nil { |
| 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) |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 106 | if _, err := starlark.ExecFile(thread, "file_ops.star", nil, builtins); err != nil { |
| 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() |
| 125 | thread.SetLocal(callerDirKey, dir) |
| 126 | LoadPathRoot = filepath.Dir(dir) |
| 127 | if _, err := starlark.ExecFile(thread, "load.star", nil, builtins); err != nil { |
| 128 | if err, ok := err.(*starlark.EvalError); ok { |
| 129 | t.Fatal(err.Backtrace()) |
| 130 | } |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | } |
| 134 | |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 135 | func TestShell(t *testing.T) { |
Sasha Smundak | 24159db | 2020-10-26 15:43:21 -0700 | [diff] [blame] | 136 | exerciseStarlarkTestFile(t, "testdata/shell.star") |
| 137 | } |