fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreAdded bzlmod support to core rules_rust (#3034)69ddb09d

main
4.2 KiB118 linesraw
1# Copyright 2020 The Bazel Authors. All rights reserved.
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
15"""Process wrapper test."""
16
17def _process_wrapper_tester_impl(ctx):
18    args = ctx.actions.args()
19    outputs = []
20    combined = ctx.attr.test_config == "combined"
21
22    if combined or ctx.attr.test_config == "stdout":
23        stdout_output = ctx.actions.declare_file(ctx.label.name + ".stdout")
24        outputs.append(stdout_output)
25        args.add("--stdout-file", stdout_output)
26
27    if combined or ctx.attr.test_config == "stderr":
28        stderr_output = ctx.actions.declare_file(ctx.label.name + ".stderr")
29        outputs.append(stderr_output)
30        args.add("--stderr-file", stderr_output)
31
32    if combined or (ctx.attr.test_config != "stdout" and ctx.attr.test_config != "stderr"):
33        touch_output = ctx.actions.declare_file(ctx.label.name + ".touch")
34        outputs.append(touch_output)
35        args.add("--touch-file", touch_output)
36        if ctx.attr.test_config == "copy-output":
37            copy_output = ctx.actions.declare_file(ctx.label.name + ".touch.copy")
38            outputs.append(copy_output)
39            args.add_all("--copy-output", [touch_output, copy_output])
40
41    if combined or ctx.attr.test_config == "env-files":
42        args.add_all(ctx.files.env_files, before_each = "--env-file")
43
44    if combined or ctx.attr.test_config == "arg-files":
45        args.add_all(ctx.files.arg_files, before_each = "--arg-file")
46
47    if combined or ctx.attr.test_config == "subst-pwd":
48        args.add("--subst", "pwd=${pwd}")
49        args.add("--subst", "key=value")
50
51    args.add("--")
52
53    args.add(ctx.executable._process_wrapper_tester)
54    args.add(ctx.attr.test_config)
55    args.add("--current-dir", "${pwd}")
56    args.add("--test-subst", "subst key to ${key}")
57
58    extra_args = ctx.actions.args()
59    if combined or ctx.attr.test_config == "subst-pwd":
60        extra_args.set_param_file_format("multiline")
61        extra_args.use_param_file("@%s", use_always = True)
62        extra_args.add("${pwd}")
63
64    env = {"CURRENT_DIR": "${pwd}/test_path"}
65
66    ctx.actions.run(
67        mnemonic = "RustcProcessWrapperTester",
68        executable = ctx.executable._process_wrapper,
69        inputs = ctx.files.env_files + ctx.files.arg_files,
70        outputs = outputs,
71        arguments = [args, extra_args],
72        env = env,
73        tools = [ctx.executable._process_wrapper_tester],
74    )
75
76    return [DefaultInfo(files = depset(outputs))]
77
78process_wrapper_tester = rule(
79    implementation = _process_wrapper_tester_impl,
80    doc = "This rule unit tests the different process_wrapper functionality.",
81    attrs = {
82        "arg_files": attr.label_list(
83            doc = "Files containing newline delimited arguments.",
84        ),
85        "env_files": attr.label_list(
86            doc = "Files containing newline delimited environment key/value pairs.",
87        ),
88        "test_config": attr.string(
89            doc = "The desired test configuration.",
90            mandatory = True,
91            values = [
92                "arg-files",
93                "basic",
94                "combined",
95                "copy-output",
96                "env-files",
97                "stderr",
98                "stdout",
99                "subst-pwd",
100            ],
101        ),
102        "use_param_file": attr.bool(
103            doc = "Whether or not to use a params file with the process wrapper.",
104            default = False,
105        ),
106        "_process_wrapper": attr.label(
107            default = Label("//util/process_wrapper"),
108            executable = True,
109            allow_single_file = True,
110            cfg = "exec",
111        ),
112        "_process_wrapper_tester": attr.label(
113            default = Label("//test/process_wrapper:process_wrapper_tester"),
114            executable = True,
115            cfg = "exec",
116        ),
117    },
118)