|
| 1 | +"""Tests that rust_test targets receive codegen disambiguation flags. |
| 2 | +
|
| 3 | +rust_test targets pass --codegen=metadata and --codegen=extra-filename to rustc |
| 4 | +so that intermediate compilation artifacts (.o, .d files) get unique names. This |
| 5 | +prevents collisions with rust_binary or rust_library targets that share the same |
| 6 | +crate name, which would otherwise cause link failures on non-sandboxed builds |
| 7 | +(e.g. Windows or --spawn_strategy=standalone). See |
| 8 | +https://github.com/bazelbuild/rules_rust/pull/1434 for the original issue. |
| 9 | +
|
| 10 | +rustc flag documentation: |
| 11 | + https://doc.rust-lang.org/rustc/codegen-options/index.html#metadata |
| 12 | + https://doc.rust-lang.org/rustc/codegen-options/index.html#extra-filename |
| 13 | +""" |
| 14 | + |
| 15 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest") |
| 16 | +load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test") |
| 17 | +load("//test/unit:common.bzl", "assert_argv_contains_prefix") |
| 18 | + |
| 19 | +def _codegen_disambiguation_test_impl(ctx): |
| 20 | + env = analysistest.begin(ctx) |
| 21 | + tut = analysistest.target_under_test(env) |
| 22 | + assert_argv_contains_prefix(env, tut.actions[0], "--codegen=metadata=-") |
| 23 | + assert_argv_contains_prefix(env, tut.actions[0], "--codegen=extra-filename=-") |
| 24 | + return analysistest.end(env) |
| 25 | + |
| 26 | +codegen_disambiguation_test = analysistest.make( |
| 27 | + _codegen_disambiguation_test_impl, |
| 28 | +) |
| 29 | + |
| 30 | +def _codegen_disambiguation_targets(): |
| 31 | + rust_binary( |
| 32 | + name = "my_binary", |
| 33 | + srcs = ["foo.rs"], |
| 34 | + edition = "2018", |
| 35 | + ) |
| 36 | + |
| 37 | + rust_library( |
| 38 | + name = "my_library", |
| 39 | + srcs = ["foo.rs"], |
| 40 | + edition = "2018", |
| 41 | + ) |
| 42 | + |
| 43 | + rust_test( |
| 44 | + name = "my_test_with_srcs", |
| 45 | + srcs = ["foo.rs"], |
| 46 | + edition = "2018", |
| 47 | + ) |
| 48 | + |
| 49 | + codegen_disambiguation_test( |
| 50 | + name = "rust_test_srcs_codegen_disambiguation_test", |
| 51 | + target_under_test = ":my_test_with_srcs", |
| 52 | + ) |
| 53 | + |
| 54 | + rust_test( |
| 55 | + name = "my_test_with_crate_from_bin", |
| 56 | + crate = "my_binary", |
| 57 | + edition = "2018", |
| 58 | + ) |
| 59 | + |
| 60 | + codegen_disambiguation_test( |
| 61 | + name = "rust_test_crate_from_bin_codegen_disambiguation_test", |
| 62 | + target_under_test = ":my_test_with_crate_from_bin", |
| 63 | + ) |
| 64 | + |
| 65 | + rust_test( |
| 66 | + name = "my_test_with_crate_from_lib", |
| 67 | + crate = "my_library", |
| 68 | + edition = "2018", |
| 69 | + ) |
| 70 | + |
| 71 | + codegen_disambiguation_test( |
| 72 | + name = "rust_test_crate_from_lib_codegen_disambiguation_test", |
| 73 | + target_under_test = ":my_test_with_crate_from_lib", |
| 74 | + ) |
| 75 | + |
| 76 | +def codegen_disambiguation_test_suite(name): |
| 77 | + """Entry-point macro called from the BUILD file. |
| 78 | +
|
| 79 | + Args: |
| 80 | + name: Name of the macro. |
| 81 | + """ |
| 82 | + |
| 83 | + _codegen_disambiguation_targets() |
| 84 | + |
| 85 | + native.test_suite( |
| 86 | + name = name, |
| 87 | + tests = [ |
| 88 | + ":rust_test_srcs_codegen_disambiguation_test", |
| 89 | + ":rust_test_crate_from_bin_codegen_disambiguation_test", |
| 90 | + ":rust_test_crate_from_lib_codegen_disambiguation_test", |
| 91 | + ], |
| 92 | + ) |
0 commit comments