1"""Tests for toolchain channel transitions"""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest")
4load("@bazel_skylib//rules:write_file.bzl", "write_file")
5load("//rust:defs.bzl", "rust_binary")
6load(
7    "//test/unit:common.bzl",
8    "assert_action_mnemonic",
9    "assert_argv_contains",
10    "assert_argv_contains_not",
11)
12
13def _channel_transition_test_impl(ctx, is_nightly):
14    env = analysistest.begin(ctx)
15    target = analysistest.target_under_test(env)
16
17    action = target.actions[0]
18    assert_action_mnemonic(env, action, "Rustc")
19
20    if is_nightly:
21        assert_argv_contains(env, action, "-Zunstable-options")
22    else:
23        assert_argv_contains_not(env, action, "-Zunstable-options")
24
25    return analysistest.end(env)
26
27def _nightly_transition_test_impl(ctx):
28    return _channel_transition_test_impl(ctx, True)
29
30nightly_transition_test = analysistest.make(
31    _nightly_transition_test_impl,
32    doc = "Test that targets can be forced to use a nightly toolchain",
33    config_settings = {
34        str(Label("//rust/toolchain/channel:channel")): "nightly",
35    },
36)
37
38def _stable_transition_test_impl(ctx):
39    return _channel_transition_test_impl(ctx, False)
40
41stable_transition_test = analysistest.make(
42    _stable_transition_test_impl,
43    doc = "Test that targets can be forced to use a stable toolchain",
44    config_settings = {
45        str(Label("//rust/toolchain/channel:channel")): "stable",
46    },
47)
48
49def channel_transitions_test_suite(name):
50    """Entry-point macro called from the BUILD file.
51
52    Args:
53        name (str): Name of the macro.
54    """
55
56    write_file(
57        name = "main_rs",
58        out = "main.rs",
59        content = [
60            "fn main() {}",
61            "",
62        ],
63    )
64
65    rust_binary(
66        name = "bin",
67        srcs = ["main.rs"],
68        edition = "2018",
69        rustc_flags = select({
70            "@rules_rust//rust/toolchain/channel:nightly": ["-Zunstable-options"],
71            "//conditions:default": [],
72        }),
73    )
74
75    nightly_transition_test(
76        name = "nightly_transition_test",
77        target_under_test = ":bin",
78    )
79
80    stable_transition_test(
81        name = "stable_transition_test",
82        target_under_test = ":bin",
83    )
84
85    native.test_suite(
86        name = name,
87        tests = [
88            ":nightly_transition_test",
89            ":stable_transition_test",
90        ],
91    )
92