|
| 1 | +"""Test that $(location) in rustc_env expands compile_data targets in rust_test.""" |
| 2 | + |
| 3 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest") |
| 4 | +load("@bazel_skylib//rules:write_file.bzl", "write_file") |
| 5 | +load("//rust:defs.bzl", "rust_library", "rust_test") |
| 6 | +load("//test/unit:common.bzl", "assert_env_value") |
| 7 | + |
| 8 | +def _find_action(tut, mnemonic): |
| 9 | + for action in tut.actions: |
| 10 | + if action.mnemonic == mnemonic: |
| 11 | + return action |
| 12 | + return None |
| 13 | + |
| 14 | +# --------------------------------------------------------------------------- |
| 15 | +# Test: standalone rust_test with generated compile_data in rustc_env |
| 16 | +# --------------------------------------------------------------------------- |
| 17 | + |
| 18 | +def _standalone_test_impl(ctx): |
| 19 | + env = analysistest.begin(ctx) |
| 20 | + tut = analysistest.target_under_test(env) |
| 21 | + action = _find_action(tut, "Rustc") |
| 22 | + if not action: |
| 23 | + fail("No Rustc action found") |
| 24 | + expected = "${pwd}/" + ctx.bin_dir.path + "/test/unit/compile_data_env/generated.txt" |
| 25 | + assert_env_value(env, action, "GENERATED_PATH", expected) |
| 26 | + return analysistest.end(env) |
| 27 | + |
| 28 | +standalone_compile_data_env_test = analysistest.make(_standalone_test_impl) |
| 29 | + |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | +# Test: rust_test wrapping a crate with source compile_data in rustc_env |
| 32 | +# --------------------------------------------------------------------------- |
| 33 | + |
| 34 | +def _crate_wrap_test_impl(ctx): |
| 35 | + env = analysistest.begin(ctx) |
| 36 | + tut = analysistest.target_under_test(env) |
| 37 | + action = _find_action(tut, "Rustc") |
| 38 | + if not action: |
| 39 | + fail("No Rustc action found") |
| 40 | + assert_env_value(env, action, "DATA_PATH", "test/unit/compile_data_env/data.txt") |
| 41 | + return analysistest.end(env) |
| 42 | + |
| 43 | +crate_wrap_compile_data_env_test = analysistest.make(_crate_wrap_test_impl) |
| 44 | + |
| 45 | +# --------------------------------------------------------------------------- |
| 46 | +# Subjects and test suite |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | + |
| 49 | +def _test_subjects(): |
| 50 | + write_file( |
| 51 | + name = "gen_file", |
| 52 | + out = "generated.txt", |
| 53 | + content = ["hello"], |
| 54 | + newline = "unix", |
| 55 | + ) |
| 56 | + |
| 57 | + # Standalone test: compile_data with a generated file |
| 58 | + rust_test( |
| 59 | + name = "standalone_test", |
| 60 | + srcs = ["test.rs"], |
| 61 | + compile_data = [":gen_file"], |
| 62 | + edition = "2021", |
| 63 | + rustc_env = { |
| 64 | + "GENERATED_PATH": "$(execpath :gen_file)", |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
| 68 | + # Crate-wrap test: compile_data with a source file (not generated, |
| 69 | + # to avoid triggering transform_sources which has a separate bug |
| 70 | + # with crate= + generated compile_data) |
| 71 | + rust_library( |
| 72 | + name = "mylib", |
| 73 | + srcs = ["lib.rs"], |
| 74 | + edition = "2021", |
| 75 | + ) |
| 76 | + |
| 77 | + rust_test( |
| 78 | + name = "crate_wrap_test", |
| 79 | + crate = ":mylib", |
| 80 | + compile_data = ["data.txt"], |
| 81 | + edition = "2021", |
| 82 | + rustc_env = { |
| 83 | + "DATA_PATH": "$(rootpath data.txt)", |
| 84 | + }, |
| 85 | + ) |
| 86 | + |
| 87 | +def compile_data_env_test_suite(name): |
| 88 | + """Entry-point macro called from the BUILD file. |
| 89 | +
|
| 90 | + Args: |
| 91 | + name: Name of the macro. |
| 92 | + """ |
| 93 | + _test_subjects() |
| 94 | + |
| 95 | + standalone_compile_data_env_test( |
| 96 | + name = "standalone_compile_data_env_test", |
| 97 | + target_under_test = ":standalone_test", |
| 98 | + ) |
| 99 | + |
| 100 | + crate_wrap_compile_data_env_test( |
| 101 | + name = "crate_wrap_compile_data_env_test", |
| 102 | + target_under_test = ":crate_wrap_test", |
| 103 | + ) |
| 104 | + |
| 105 | + native.test_suite( |
| 106 | + name = name, |
| 107 | + tests = [ |
| 108 | + ":standalone_compile_data_env_test", |
| 109 | + ":crate_wrap_compile_data_env_test", |
| 110 | + ], |
| 111 | + ) |
0 commit comments