blob: e109c02c5446e9013b425a2af5858d24957c21a7 [file] [log] [blame]
Sasha Smundak24159db2020-10-26 15:43:21 -07001// 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
15package rbcrun
16
17import (
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
34func init() {
35 starlarktestSetup()
36}
37
38func 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 Fausta874f882023-05-05 11:46:51 -070056func testSetup(t *testing.T) *starlark.Thread {
57 setup()
Sasha Smundak24159db2020-10-26 15:43:21 -070058 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
72func dataDir() string {
73 _, thisSrcFile, _, _ := runtime.Caller(0)
74 return filepath.Join(filepath.Dir(thisSrcFile), "testdata")
Sasha Smundak24159db2020-10-26 15:43:21 -070075}
76
77func 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 Fausta874f882023-05-05 11:46:51 -070081 setup()
82 if err := os.Chdir(dataDir()); err != nil {
83 t.Fatal(err)
84 }
Sasha Smundak24159db2020-10-26 15:43:21 -070085 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 Smundak24159db2020-10-26 15:43:21 -0700103func TestFileOps(t *testing.T) {
104 // TODO(asmundak): convert this to use exerciseStarlarkTestFile
Cole Fausta874f882023-05-05 11:46:51 -0700105 thread := testSetup(t)
Sasha Smundak24159db2020-10-26 15:43:21 -0700106 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
114func TestLoad(t *testing.T) {
115 // TODO(asmundak): convert this to use exerciseStarlarkTestFile
Cole Fausta874f882023-05-05 11:46:51 -0700116 thread := testSetup(t)
Sasha Smundak24159db2020-10-26 15:43:21 -0700117 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 Smundak24159db2020-10-26 15:43:21 -0700135func TestShell(t *testing.T) {
Sasha Smundak24159db2020-10-26 15:43:21 -0700136 exerciseStarlarkTestFile(t, "testdata/shell.star")
137}