|
| 1 | +"""Unittest to verify re-exported symbols propagate to downstream crates""" |
| 2 | + |
| 3 | +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") |
| 4 | +load("//test/unit:common.bzl", "assert_argv_contains_prefix", "assert_argv_contains_prefix_suffix") |
| 5 | + |
| 6 | +def _exports_test_impl(ctx, dependencies, externs): |
| 7 | + env = analysistest.begin(ctx) |
| 8 | + target = analysistest.target_under_test(env) |
| 9 | + |
| 10 | + action = target.actions[0] |
| 11 | + asserts.equals(env, action.mnemonic, "Rustc") |
| 12 | + |
| 13 | + # Transitive symbols that get re-exported are expected to be located by a `-Ldependency` flag. |
| 14 | + # The assert below ensures that each dependnecy flag is passed to the Rustc action. For details see |
| 15 | + # https://doc.rust-lang.org/rustc/command-line-arguments.html#-l-add-a-directory-to-the-library-search-path |
| 16 | + for dep in dependencies: |
| 17 | + assert_argv_contains_prefix_suffix( |
| 18 | + env = env, |
| 19 | + action = action, |
| 20 | + prefix = "-Ldependency", |
| 21 | + suffix = dep, |
| 22 | + ) |
| 23 | + |
| 24 | + for dep in externs: |
| 25 | + assert_argv_contains_prefix( |
| 26 | + env = env, |
| 27 | + action = action, |
| 28 | + prefix = "--extern={}=".format(dep), |
| 29 | + ) |
| 30 | + |
| 31 | + return analysistest.end(env) |
| 32 | + |
| 33 | +def _lib_exports_test_impl(ctx): |
| 34 | + # This test is only expected to be used with |
| 35 | + # `//test/unit/exports/lib_c` |
| 36 | + return _exports_test_impl( |
| 37 | + ctx = ctx, |
| 38 | + dependencies = ["lib_a", "lib_b"], |
| 39 | + externs = ["lib_b"], |
| 40 | + ) |
| 41 | + |
| 42 | +def _test_exports_test_impl(ctx): |
| 43 | + # This test is only expected to be used with |
| 44 | + # `//test/unit/exports/lib_c:lib_c_test` |
| 45 | + return _exports_test_impl( |
| 46 | + ctx = ctx, |
| 47 | + dependencies = ["lib_a", "lib_b"], |
| 48 | + externs = ["lib_b"], |
| 49 | + ) |
| 50 | + |
| 51 | +lib_exports_test = analysistest.make(_lib_exports_test_impl) |
| 52 | +test_exports_test = analysistest.make(_test_exports_test_impl) |
| 53 | + |
| 54 | +def exports_test_suite(name): |
| 55 | + """Entry-point macro called from the BUILD file. |
| 56 | +
|
| 57 | + Args: |
| 58 | + name (str): Name of the macro. |
| 59 | + """ |
| 60 | + |
| 61 | + lib_exports_test( |
| 62 | + name = "lib_exports_test", |
| 63 | + target_under_test = "//test/unit/exports/lib_c", |
| 64 | + ) |
| 65 | + |
| 66 | + test_exports_test( |
| 67 | + name = "test_exports_test", |
| 68 | + target_under_test = "//test/unit/exports/lib_c:lib_c_test", |
| 69 | + ) |
| 70 | + |
| 71 | + native.test_suite( |
| 72 | + name = name, |
| 73 | + tests = [ |
| 74 | + ":lib_exports_test", |
| 75 | + ":test_exports_test", |
| 76 | + ], |
| 77 | + ) |
0 commit comments