aboutsummaryrefslogtreecommitdiff
path: root/tools/rbcrun/host_test.go
blob: 3be5ee67aae15362bfd0bd957bac2e65b65937bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rbcrun

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"testing"

	"go.starlark.net/resolve"
	"go.starlark.net/starlark"
	"go.starlark.net/starlarktest"
)

// In order to use "assert.star" from go/starlark.net/starlarktest in the tests,
// provide:
//  * load function that handles "assert.star"
//  * starlarktest.DataFile function that finds its location

func init() {
	starlarktestSetup()
}

func starlarktestSetup() {
	resolve.AllowLambda = true
	starlarktest.DataFile = func(pkgdir, filename string) string {
		// The caller expects this function to return the path to the
		// data file. The implementation assumes that the source file
		// containing the caller and the data file are in the same
		// directory. It's ugly. Not sure what's the better way.
		// TODO(asmundak): handle Bazel case
		_, starlarktestSrcFile, _, _ := runtime.Caller(1)
		if filepath.Base(starlarktestSrcFile) != "starlarktest.go" {
			panic(fmt.Errorf("this function should be called from starlarktest.go, got %s",
				starlarktestSrcFile))
		}
		return filepath.Join(filepath.Dir(starlarktestSrcFile), filename)
	}
}

// Common setup for the tests: create thread, change to the test directory
func testSetup(t *testing.T, env []string) *starlark.Thread {
	setup(env)
	thread := &starlark.Thread{
		Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
			if module == "assert.star" {
				return starlarktest.LoadAssertModule()
			}
			return nil, fmt.Errorf("load not implemented")
		}}
	starlarktest.SetReporter(thread, t)
	if err := os.Chdir(dataDir()); err != nil {
		t.Fatal(err)
	}
	return thread
}

func dataDir() string {
	_, thisSrcFile, _, _ := runtime.Caller(0)
	return filepath.Join(filepath.Dir(thisSrcFile), "testdata")

}

func exerciseStarlarkTestFile(t *testing.T, starFile string) {
	// In order to use "assert.star" from go/starlark.net/starlarktest in the tests, provide:
	//  * load function that handles "assert.star"
	//  * starlarktest.DataFile function that finds its location
	setup(nil)
	thread := &starlark.Thread{
		Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
			if module == "assert.star" {
				return starlarktest.LoadAssertModule()
			}
			return nil, fmt.Errorf("load not implemented")
		}}
	starlarktest.SetReporter(thread, t)
	_, thisSrcFile, _, _ := runtime.Caller(0)
	filename := filepath.Join(filepath.Dir(thisSrcFile), starFile)
	if _, err := starlark.ExecFile(thread, filename, nil, builtins); err != nil {
		if err, ok := err.(*starlark.EvalError); ok {
			t.Fatal(err.Backtrace())
		}
		t.Fatal(err)
	}
}

func TestCliAndEnv(t *testing.T) {
	// TODO(asmundak): convert this to use exerciseStarlarkTestFile
	if err := os.Setenv("TEST_ENVIRONMENT_FOO", "test_environment_foo"); err != nil {
		t.Fatal(err)
	}
	thread := testSetup(t, []string{"CLI_FOO=foo"})
	if _, err := starlark.ExecFile(thread, "cli_and_env.star", nil, builtins); err != nil {
		if err, ok := err.(*starlark.EvalError); ok {
			t.Fatal(err.Backtrace())
		}
		t.Fatal(err)
	}
}

func TestFileOps(t *testing.T) {
	// TODO(asmundak): convert this to use exerciseStarlarkTestFile
	if err := os.Setenv("TEST_DATA_DIR", dataDir()); err != nil {
		t.Fatal(err)
	}
	thread := testSetup(t, nil)
	if _, err := starlark.ExecFile(thread, "file_ops.star", nil, builtins); err != nil {
		if err, ok := err.(*starlark.EvalError); ok {
			t.Fatal(err.Backtrace())
		}
		t.Fatal(err)
	}
}

func TestLoad(t *testing.T) {
	// TODO(asmundak): convert this to use exerciseStarlarkTestFile
	thread := testSetup(t, nil)
	thread.Load = func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
		if module == "assert.star" {
			return starlarktest.LoadAssertModule()
		} else {
			return loader(thread, module)
		}
	}
	dir := dataDir()
	thread.SetLocal(callerDirKey, dir)
	LoadPathRoot = filepath.Dir(dir)
	if _, err := starlark.ExecFile(thread, "load.star", nil, builtins); err != nil {
		if err, ok := err.(*starlark.EvalError); ok {
			t.Fatal(err.Backtrace())
		}
		t.Fatal(err)
	}
}

func TestRegex(t *testing.T) {
	exerciseStarlarkTestFile(t, "testdata/regex.star")
}

func TestShell(t *testing.T) {
	if err := os.Setenv("TEST_DATA_DIR", dataDir()); err != nil {
		t.Fatal(err)
	}
	exerciseStarlarkTestFile(t, "testdata/shell.star")
}