• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Unit tests for rust toolchains."""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load("@bazel_skylib//rules:write_file.bzl", "write_file")
5load("@rules_rust_toolchain_test_target_json//:defs.bzl", "TARGET_JSON")
6load("//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain")
7load("//rust/platform:triple.bzl", "triple")
8
9def _toolchain_specifies_target_triple_test_impl(ctx):
10    env = analysistest.begin(ctx)
11    toolchain_info = analysistest.target_under_test(env)[platform_common.ToolchainInfo]
12
13    asserts.equals(env, None, toolchain_info.target_json)
14    asserts.equals(env, "toolchain-test-triple", toolchain_info.target_flag_value)
15    asserts.equals(env, triple("toolchain-test-triple"), toolchain_info.target_triple)
16    asserts.equals(env, "toolchain", toolchain_info.target_arch)
17
18    return analysistest.end(env)
19
20def _toolchain_specifies_target_json_test_impl(ctx):
21    env = analysistest.begin(ctx)
22    target = analysistest.target_under_test(env)
23    toolchain_info = target[platform_common.ToolchainInfo]
24
25    asserts.equals(env, None, toolchain_info.target_triple)
26    asserts.equals(env, "x86_64", toolchain_info.target_arch)
27
28    # The specific name here is not as vaulable as identifying that `target_json` is a json file
29    expected_basename = "{}.target.json".format(target.label.name)
30    asserts.equals(env, expected_basename, toolchain_info.target_json.basename)
31
32    # The value is expected to be to a generated file in bazel-out.
33    asserts.true(env, toolchain_info.target_flag_value.startswith("bazel-out/"))
34    asserts.true(env, toolchain_info.target_flag_value.endswith("/bin/{}/{}".format(ctx.label.package, expected_basename)))
35
36    return analysistest.end(env)
37
38def _toolchain_location_expands_linkflags_impl(ctx):
39    env = analysistest.begin(ctx)
40    toolchain_info = analysistest.target_under_test(env)[platform_common.ToolchainInfo]
41
42    asserts.equals(
43        env,
44        "test:test/unit/toolchain/config.txt",
45        toolchain_info.stdlib_linkflags.linking_context.linker_inputs.to_list()[0].user_link_flags[0],
46    )
47
48    return analysistest.end(env)
49
50toolchain_specifies_target_triple_test = analysistest.make(_toolchain_specifies_target_triple_test_impl)
51toolchain_specifies_target_json_test = analysistest.make(_toolchain_specifies_target_json_test_impl)
52toolchain_location_expands_linkflags_test = analysistest.make(_toolchain_location_expands_linkflags_impl)
53
54def _define_test_targets():
55    native.filegroup(
56        name = "stdlib_srcs",
57        srcs = ["config.txt"],
58    )
59    rust_stdlib_filegroup(
60        name = "std_libs",
61        srcs = [":stdlib_srcs"],
62    )
63
64    native.filegroup(
65        name = "target_json",
66        srcs = ["toolchain-test-triple.json"],
67    )
68
69    write_file(
70        name = "mock_rustc",
71        out = "mock_rustc.exe",
72        content = [],
73        is_executable = True,
74    )
75
76    write_file(
77        name = "mock_rustdoc",
78        out = "mock_rustdoc.exe",
79        content = [],
80        is_executable = True,
81    )
82
83    rust_toolchain(
84        name = "rust_triple_toolchain",
85        binary_ext = "",
86        dylib_ext = ".so",
87        exec_triple = "x86_64-unknown-none",
88        rust_doc = ":mock_rustdoc",
89        rust_std = ":std_libs",
90        rustc = ":mock_rustc",
91        staticlib_ext = ".a",
92        stdlib_linkflags = [],
93        target_triple = "toolchain-test-triple",
94    )
95
96    encoded_target_json = json.encode(TARGET_JSON)
97
98    rust_toolchain(
99        name = "rust_json_toolchain",
100        binary_ext = "",
101        dylib_ext = ".so",
102        exec_triple = "x86_64-unknown-none",
103        rust_doc = ":mock_rustdoc",
104        rust_std = ":std_libs",
105        rustc = ":mock_rustc",
106        staticlib_ext = ".a",
107        stdlib_linkflags = [],
108        target_json = encoded_target_json,
109    )
110
111    rust_toolchain(
112        name = "rust_inline_json_toolchain",
113        binary_ext = "",
114        dylib_ext = ".so",
115        exec_triple = "x86_64-unknown-none",
116        rust_doc = ":mock_rustdoc",
117        rust_std = ":std_libs",
118        rustc = ":mock_rustc",
119        staticlib_ext = ".a",
120        stdlib_linkflags = [],
121        target_json = json.encode(
122            {
123                "arch": "x86_64",
124                "env": "gnu",
125                "llvm-target": "x86_64-unknown-linux-gnu",
126                "target-family": ["unix"],
127                "target-pointer-width": "64",
128            },
129        ),
130    )
131
132    rust_toolchain(
133        name = "rust_location_expand_toolchain",
134        binary_ext = "",
135        dylib_ext = ".so",
136        exec_triple = "x86_64-unknown-none",
137        rust_doc = ":mock_rustdoc",
138        rust_std = ":std_libs",
139        rustc = ":mock_rustc",
140        staticlib_ext = ".a",
141        stdlib_linkflags = ["test:$(location :stdlib_srcs)"],
142        target_json = encoded_target_json,
143    )
144
145def toolchain_test_suite(name):
146    """Entry-point macro called from the BUILD file.
147
148    Args:
149        name (str): The name of the test suite.
150    """
151    _define_test_targets()
152
153    toolchain_specifies_target_triple_test(
154        name = "toolchain_specifies_target_triple_test",
155        target_under_test = ":rust_triple_toolchain",
156    )
157    toolchain_specifies_target_json_test(
158        name = "toolchain_specifies_target_json_test",
159        target_under_test = ":rust_json_toolchain",
160    )
161    toolchain_specifies_target_json_test(
162        name = "toolchain_specifies_inline_target_json_test",
163        target_under_test = ":rust_inline_json_toolchain",
164    )
165    toolchain_location_expands_linkflags_test(
166        name = "toolchain_location_expands_linkflags_test",
167        target_under_test = ":rust_location_expand_toolchain",
168    )
169
170    native.test_suite(
171        name = name,
172        tests = [
173            ":toolchain_specifies_target_triple_test",
174            ":toolchain_specifies_target_json_test",
175            ":toolchain_specifies_inline_target_json_test",
176            ":toolchain_location_expands_linkflags_test",
177        ],
178    )
179