blob: 10ce55ec2f54a63af6c7b410b416fbbea80f5db2 [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 {
Sasha Smundak24159db2020-10-26 15:43:21 -070057 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
71func dataDir() string {
72 _, thisSrcFile, _, _ := runtime.Caller(0)
73 return filepath.Join(filepath.Dir(thisSrcFile), "testdata")
Sasha Smundak24159db2020-10-26 15:43:21 -070074}
75
76func 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 Fausta874f882023-05-05 11:46:51 -070080 if err := os.Chdir(dataDir()); err != nil {
81 t.Fatal(err)
82 }
Sasha Smundak24159db2020-10-26 15:43:21 -070083 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 Faustc63ce1a2023-05-09 14:56:36 -070093 thread.SetLocal(executionModeKey, ExecutionModeRbc)
94 thread.SetLocal(shellKey, "/bin/sh")
95 if _, err := starlark.ExecFile(thread, filename, nil, rbcBuiltins); err != nil {
Sasha Smundak24159db2020-10-26 15:43:21 -070096 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)
Cole Faustc63ce1a2023-05-09 14:56:36 -0700106 if _, err := starlark.ExecFile(thread, "file_ops.star", nil, rbcBuiltins); err != nil {
Sasha Smundak24159db2020-10-26 15:43:21 -0700107 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()
Cole Faustc63ce1a2023-05-09 14:56:36 -0700125 if err := os.Chdir(filepath.Dir(dir)); err != nil {
126 t.Fatal(err)
127 }
Cole Faust386b3742023-06-06 16:55:58 -0700128 thread.SetLocal(allowExternalEntrypointKey, false)
Sasha Smundak24159db2020-10-26 15:43:21 -0700129 thread.SetLocal(callerDirKey, dir)
Cole Faustc63ce1a2023-05-09 14:56:36 -0700130 thread.SetLocal(executionModeKey, ExecutionModeRbc)
131 if _, err := starlark.ExecFile(thread, "testdata/load.star", nil, rbcBuiltins); err != nil {
Sasha Smundak24159db2020-10-26 15:43:21 -0700132 if err, ok := err.(*starlark.EvalError); ok {
133 t.Fatal(err.Backtrace())
134 }
135 t.Fatal(err)
136 }
137}
138
Sasha Smundak24159db2020-10-26 15:43:21 -0700139func TestShell(t *testing.T) {
Sasha Smundak24159db2020-10-26 15:43:21 -0700140 exerciseStarlarkTestFile(t, "testdata/shell.star")
141}