-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathshell_rules.bzl
206 lines (172 loc) · 7.6 KB
/
shell_rules.bzl
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
load("@prelude//decls:test_common.bzl", "test_common")
load(":common.bzl", "buck", "prelude_rule")
load(":re_test_common.bzl", "re_test_common")
sh_binary = prelude_rule(
name = "sh_binary",
docs = """
An `sh_binary()` is used to execute a shell script.
""",
examples = """
This sh\\_binary() just cats a sample data file back at the user.
```
# $REPO/BUCK
sh_binary(
name = "script",
main = "script.sh",
resources = [
"data.dat",
],
)
```
```
# Sample data file with data we need at runtime
$ echo "I'm a datafile" > data.dat
# Create a simple script that prints out the resource
$ cat > script.sh
#!/bin/sh
cat $BUCK_DEFAULT_RUNTIME_RESOURCES/data.dat
# Make sure the script is executable
$ chmod u+x script.sh
# Run the script, and see that it prints out the resource we provided
$ buck run //:script
Jobs completed: 4. Time elapsed: 0.2s.
BUILD SUCCEEDED
I'm a datafile
```
""",
further = None,
attrs = (
# @unsorted-dict-items
{
"main": attrs.source(doc = """
Either the path to the script (relative to the build file), or a `build target`.
This file must be executable in order to be run.
"""),
"resources": attrs.list(attrs.source(allow_directory = True), default = [], doc = """
A list of files or build rules that this rule requires in order to run. These could be things such as
random data files.
When the script runs, the `$BUCK_DEFAULT_RUNTIME_RESOURCES`
environment variable specifies the directory that contains these resources.
This directory's location is determined entirely by Buck; the script should
not assume the directory's location.
The resources are also made available in a tree structure that mirrors
their locations in the source and `buck-out` trees. The
environment variable `$BUCK_PROJECT_ROOT` specifies a directory
that contains all the resources, laid out in their locations relative to
the original buck project root.
"""),
"append_script_extension": attrs.bool(default = True, doc = """
By default, sh_binary ensures that the script has an appropriate extension (e.g. `.sh` or `.bat`),
appending one itself if necessary. Setting this to False prevents that behavior and makes the caller
responsible for ensuring an existing appropriate extension.
"""),
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"deps": attrs.list(attrs.dep(), default = []),
"labels": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"_target_os_type": buck.target_os_type_arg(),
}
),
)
sh_test = prelude_rule(
name = "sh_test",
docs = """
A `sh_test()` is a test rule that can pass results to the test runner by invoking a shell script.
**NOTE:** This rule is not currently supported on Windows.
""",
examples = """
This sh\\_test() fails if a string does not match a value.
```
# $REPO/BUCK
sh_test(
name = "script_pass",
test = "script.sh",
args = ["--pass"],
)
sh_test(
name = "script_fail",
test = "script.sh",
args = ["--fail"],
)
```
```
# Create a simple script that prints out the resource
$ cat > script.sh
#!/bin/sh
for arg in $@; do
if [ "$arg" == "--pass" ]; then
echo "Passed"
exit 0;
fi
done
echo "Failed"
exit 1
# Make sure the script is executable
$ chmod u+x script.sh
# Run the script, and see that one test passes, one fails
$ buck test //:script_pass //:script_fail
FAILURE script.sh sh_test
Building: finished in 0.0 sec (100%) 2/2 jobs, 0 updated
Total time: 0.0 sec
Testing: finished in 0.0 sec (1 PASS/1 FAIL)
RESULTS FOR //:script_fail //:script_pass
FAIL <100ms 0 Passed 0 Skipped 1 Failed //:script_fail
FAILURE script.sh sh_test
====STANDARD OUT====
Failed
PASS <100ms 1 Passed 0 Skipped 0 Failed //:script_pass
TESTS FAILED: 1 FAILURE
Failed target: //:script_fail
FAIL //:script_fail
```
""",
further = None,
attrs = (
# @unsorted-dict-items
buck.inject_test_env_arg() |
{
"test": attrs.option(attrs.one_of(attrs.dep(), attrs.source()), default = None, doc = """
Either the path to the script (relative to the build file), or a `build target`.
This file must be executable in order to be run.
"""),
"args": attrs.list(attrs.arg(), default = [], doc = """
The list of arguments to invoke this script with. These are literal values, and no shell interpolation is done.
These can contain `string parameter macros`
, for example, to give the location of a generated binary to the test script.
"""),
"env": attrs.dict(key = attrs.string(), value = attrs.arg(), sorted = False, default = {}, doc = """
Environment variable overrides that should be used when running the script. The key is the variable name, and the value is its value.
The values can contain `string parameter macros`
such as the location of a generated binary to be used by the test script.
"""),
"type": attrs.option(attrs.string(), default = None, doc = """
If provided, this will be sent to any configured `.buckconfig`
"""),
"contacts": attrs.list(attrs.string(), default = []),
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
"deps": attrs.list(attrs.dep(), default = []),
"labels": attrs.list(attrs.string(), default = []),
"licenses": attrs.list(attrs.source(), default = []),
"list_args": attrs.option(attrs.list(attrs.string()), default = None),
"list_env": attrs.option(attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False), default = None),
"resources": attrs.list(attrs.source(), default = []),
"run_args": attrs.list(attrs.string(), default = []),
"run_env": attrs.dict(key = attrs.string(), value = attrs.string(), sorted = False, default = {}),
"run_test_separately": attrs.bool(default = False),
"test_rule_timeout_ms": attrs.option(attrs.int(), default = None),
} | test_common.attributes() |
re_test_common.test_args() |
test_common.attributes()
),
)
shell_rules = struct(
sh_binary = sh_binary,
sh_test = sh_test,
)