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
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
56func testSetup(t *testing.T, env []string) *starlark.Thread {
57	setup(env)
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
72func dataDir() string {
73	_, thisSrcFile, _, _ := runtime.Caller(0)
74	return filepath.Join(filepath.Dir(thisSrcFile), "testdata")
75
76}
77
78func exerciseStarlarkTestFile(t *testing.T, starFile string) {
79	// In order to use "assert.star" from go/starlark.net/starlarktest in the tests, provide:
80	//  * load function that handles "assert.star"
81	//  * starlarktest.DataFile function that finds its location
82	setup(nil)
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)
93	if _, err := starlark.ExecFile(thread, filename, nil, builtins); err != nil {
94		if err, ok := err.(*starlark.EvalError); ok {
95			t.Fatal(err.Backtrace())
96		}
97		t.Fatal(err)
98	}
99}
100
101func TestCliAndEnv(t *testing.T) {
102	// TODO(asmundak): convert this to use exerciseStarlarkTestFile
103	if err := os.Setenv("TEST_ENVIRONMENT_FOO", "test_environment_foo"); err != nil {
104		t.Fatal(err)
105	}
106	thread := testSetup(t, []string{"CLI_FOO=foo"})
107	if _, err := starlark.ExecFile(thread, "cli_and_env.star", nil, builtins); err != nil {
108		if err, ok := err.(*starlark.EvalError); ok {
109			t.Fatal(err.Backtrace())
110		}
111		t.Fatal(err)
112	}
113}
114
115func TestFileOps(t *testing.T) {
116	// TODO(asmundak): convert this to use exerciseStarlarkTestFile
117	if err := os.Setenv("TEST_DATA_DIR", dataDir()); err != nil {
118		t.Fatal(err)
119	}
120	thread := testSetup(t, nil)
121	if _, err := starlark.ExecFile(thread, "file_ops.star", nil, builtins); err != nil {
122		if err, ok := err.(*starlark.EvalError); ok {
123			t.Fatal(err.Backtrace())
124		}
125		t.Fatal(err)
126	}
127}
128
129func TestLoad(t *testing.T) {
130	// TODO(asmundak): convert this to use exerciseStarlarkTestFile
131	thread := testSetup(t, nil)
132	thread.Load = func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
133		if module == "assert.star" {
134			return starlarktest.LoadAssertModule()
135		} else {
136			return loader(thread, module)
137		}
138	}
139	dir := dataDir()
140	thread.SetLocal(callerDirKey, dir)
141	LoadPathRoot = filepath.Dir(dir)
142	if _, err := starlark.ExecFile(thread, "load.star", nil, builtins); err != nil {
143		if err, ok := err.(*starlark.EvalError); ok {
144			t.Fatal(err.Backtrace())
145		}
146		t.Fatal(err)
147	}
148}
149
150func TestRegex(t *testing.T) {
151	exerciseStarlarkTestFile(t, "testdata/regex.star")
152}
153
154func TestShell(t *testing.T) {
155	if err := os.Setenv("TEST_DATA_DIR", dataDir()); err != nil {
156		t.Fatal(err)
157	}
158	exerciseStarlarkTestFile(t, "testdata/shell.star")
159}
160