|
| 1 | +"""Tests documenting that cargo_build_script `data` files currently act as both |
| 2 | +compile_data and runtime data -- they appear in Rustc compile action inputs for |
| 3 | +both the direct library and transitive dependents. |
| 4 | +
|
| 5 | +See https://github.com/bazelbuild/rules_rust/issues/3609 for context.""" |
| 6 | + |
| 7 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") |
| 8 | +load("@bazel_skylib//rules:write_file.bzl", "write_file") |
| 9 | +load("//cargo:defs.bzl", "cargo_build_script") |
| 10 | +load("//rust:defs.bzl", "rust_library") |
| 11 | + |
| 12 | +def _cbs_data_in_rustc_inputs_impl(ctx): |
| 13 | + env = analysistest.begin(ctx) |
| 14 | + target = analysistest.target_under_test(env) |
| 15 | + |
| 16 | + rustc_action = None |
| 17 | + for action in target.actions: |
| 18 | + if action.mnemonic == "Rustc": |
| 19 | + rustc_action = action |
| 20 | + break |
| 21 | + |
| 22 | + asserts.false(env, rustc_action == None, "Expected a Rustc action") |
| 23 | + |
| 24 | + # cargo_build_script `data` currently flows into BuildInfo.compile_data |
| 25 | + # via script_data, so it appears in Rustc action inputs for both the |
| 26 | + # direct dependent and transitive dependents. This documents that |
| 27 | + # CBS `data` effectively acts as both compile_data and data today. |
| 28 | + data_inputs = [i for i in rustc_action.inputs.to_list() if "cbs_data_dep.txt" in i.path] |
| 29 | + asserts.true( |
| 30 | + env, |
| 31 | + len(data_inputs) > 0, |
| 32 | + "Expected CBS data file to appear in Rustc action inputs (CBS data currently acts as compile_data)", |
| 33 | + ) |
| 34 | + |
| 35 | + return analysistest.end(env) |
| 36 | + |
| 37 | +cbs_data_in_rustc_inputs_test = analysistest.make( |
| 38 | + _cbs_data_in_rustc_inputs_impl, |
| 39 | +) |
| 40 | + |
| 41 | +def _define_test_targets(): |
| 42 | + write_file( |
| 43 | + name = "cbs_data_dep_file", |
| 44 | + out = "cbs_data_dep.txt", |
| 45 | + content = ["data for build script", ""], |
| 46 | + newline = "unix", |
| 47 | + ) |
| 48 | + |
| 49 | + write_file( |
| 50 | + name = "build_rs_src", |
| 51 | + out = "build.rs", |
| 52 | + content = ["fn main() {}", ""], |
| 53 | + newline = "unix", |
| 54 | + ) |
| 55 | + |
| 56 | + cargo_build_script( |
| 57 | + name = "build_script", |
| 58 | + srcs = [":build.rs"], |
| 59 | + data = [":cbs_data_dep.txt"], |
| 60 | + edition = "2021", |
| 61 | + ) |
| 62 | + |
| 63 | + write_file( |
| 64 | + name = "lib_src", |
| 65 | + out = "lib.rs", |
| 66 | + content = ["pub fn hello() {}", ""], |
| 67 | + newline = "unix", |
| 68 | + ) |
| 69 | + |
| 70 | + rust_library( |
| 71 | + name = "lib", |
| 72 | + srcs = [":lib.rs"], |
| 73 | + deps = [":build_script"], |
| 74 | + edition = "2021", |
| 75 | + ) |
| 76 | + |
| 77 | + write_file( |
| 78 | + name = "bin_src", |
| 79 | + out = "bin.rs", |
| 80 | + content = ["extern crate lib;", ""], |
| 81 | + newline = "unix", |
| 82 | + ) |
| 83 | + |
| 84 | + rust_library( |
| 85 | + name = "bin", |
| 86 | + srcs = [":bin.rs"], |
| 87 | + deps = [":lib"], |
| 88 | + edition = "2021", |
| 89 | + ) |
| 90 | + |
| 91 | +def transitive_cbs_data_test_suite(name): |
| 92 | + """Entry-point macro called from the BUILD file. |
| 93 | +
|
| 94 | + Args: |
| 95 | + name (str): Name of the macro. |
| 96 | + """ |
| 97 | + _define_test_targets() |
| 98 | + |
| 99 | + cbs_data_in_rustc_inputs_test( |
| 100 | + name = "cbs_data_in_lib_compile_inputs_test", |
| 101 | + target_under_test = ":lib", |
| 102 | + ) |
| 103 | + |
| 104 | + cbs_data_in_rustc_inputs_test( |
| 105 | + name = "cbs_data_in_bin_compile_inputs_test", |
| 106 | + target_under_test = ":bin", |
| 107 | + ) |
| 108 | + |
| 109 | + native.test_suite( |
| 110 | + name = name, |
| 111 | + tests = [ |
| 112 | + ":cbs_data_in_lib_compile_inputs_test", |
| 113 | + ":cbs_data_in_bin_compile_inputs_test", |
| 114 | + ], |
| 115 | + ) |
0 commit comments