fork/rules_rust

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

David TolnayOutside of examples, fill in all `edition` attrs (#1257)03a70796

0.7.0
3.7 KiB124 linesraw
1"""Unittest to verify workspace status stamping is applied to environment files"""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest")
4load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
5load(
6    "//test/unit:common.bzl",
7    "assert_action_mnemonic",
8    "assert_argv_contains",
9    "assert_argv_contains_not",
10)
11
12def _stamp_test_impl(ctx, force_stamp):
13    env = analysistest.begin(ctx)
14    target = analysistest.target_under_test(env)
15
16    action = target.actions[0]
17    assert_action_mnemonic(env, action, "Rustc")
18
19    if force_stamp:
20        assert_argv_contains(env, action, "--volatile-status-file")
21        assert_argv_contains(env, action, "bazel-out/volatile-status.txt")
22    else:
23        assert_argv_contains_not(env, action, "--volatile-status-file")
24        assert_argv_contains_not(env, action, "bazel-out/volatile-status.txt")
25
26    # Note that use of stable status invalidates targets any time it's updated.
27    # This is undesirable behavior so it's intended to be excluded from the
28    # Rustc action. In general this should be fine as other rules can be used
29    # to produce template files for stamping in this action (ie. genrule).
30    assert_argv_contains_not(env, action, "bazel-out/stable-status.txt")
31
32    return analysistest.end(env)
33
34def _force_stamp_test_impl(ctx):
35    return _stamp_test_impl(ctx, True)
36
37def _skip_stamp_test_impl(ctx):
38    return _stamp_test_impl(ctx, False)
39
40force_stamp_test = analysistest.make(_force_stamp_test_impl)
41skip_stamp_test = analysistest.make(_skip_stamp_test_impl)
42
43_STAMP_VALUES = (0, 1)
44
45def _define_test_targets():
46    for stamp_value in _STAMP_VALUES:
47        if stamp_value == 1:
48            name = "force_stamp"
49            features = ["force_stamp"]
50        else:
51            name = "skip_stamp"
52            features = ["skip_stamp"]
53
54        rust_library(
55            name = name,
56            srcs = ["stamp.rs"],
57            edition = "2018",
58            rustc_env_files = [":stamp.env"],
59            stamp = stamp_value,
60            crate_features = features,
61        )
62
63        rust_test(
64            name = "{}_unit_test".format(name),
65            crate = ":{}".format(name),
66            edition = "2018",
67            rustc_env_files = [":stamp.env"],
68            stamp = stamp_value,
69            crate_features = features,
70        )
71
72        rust_binary(
73            name = "{}_bin".format(name),
74            srcs = ["stamp_main.rs"],
75            edition = "2018",
76            deps = [":{}".format(name)],
77            rustc_env_files = [":stamp.env"],
78            stamp = stamp_value,
79            crate_features = features,
80        )
81
82def stamp_test_suite(name):
83    """Entry-point macro called from the BUILD file.
84
85    Args:
86        name (str): Name of the macro.
87    """
88    _define_test_targets()
89
90    tests = []
91
92    for stamp_value in _STAMP_VALUES:
93        if stamp_value == 1:
94            test_name = "force_stamp"
95            stamp_test = force_stamp_test
96        else:
97            test_name = "skip_stamp"
98            stamp_test = skip_stamp_test
99
100        stamp_test(
101            name = "lib_{}_test".format(test_name),
102            target_under_test = Label("//test/unit/stamp:{}".format(test_name)),
103        )
104
105        stamp_test(
106            name = "test_{}_test".format(test_name),
107            target_under_test = Label("//test/unit/stamp:{}_unit_test".format(test_name)),
108        )
109
110        stamp_test(
111            name = "bin_{}_test".format(test_name),
112            target_under_test = Label("//test/unit/stamp:{}_bin".format(test_name)),
113        )
114
115        tests.extend([
116            "lib_{}_test".format(test_name),
117            "test_{}_test".format(test_name),
118            "bin_{}_test".format(test_name),
119        ])
120
121    native.test_suite(
122        name = name,
123        tests = tests,
124    )