fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
2bad96d9
cleanup
1"""Analysis test for for rust_bindgen_library rule.""" 2 3load ( "@rules_cc//cc:defs.bzl" , "cc_library" ) 4load ( "@rules_rust//rust:defs.bzl" , "rust_binary" ) 5load ( "@rules_rust_bindgen//:defs.bzl" , "rust_bindgen_library" ) 6load ( "@rules_testing//lib:analysis_test.bzl" , "analysis_test" , "test_suite" ) 7 8def _test_cc_linkopt_impl ( env , target ): 9# Assert 10env . expect . that_action ( target . actions [ 0 ]) \ 11. contains_at_least_args ([ "--codegen=link-arg=-shared" ]) 12 13def _test_cc_linkopt ( name ): 14# Arrange 15cc_library ( 16name = name + "_cc" , 17srcs = [ "simple.cc" ], 18hdrs = [ "simple.h" ], 19linkopts = [ "-shared" ], 20tags = [ "manual" ], 21) 22rust_bindgen_library ( 23name = name + "_rust_bindgen" , 24cc_lib = name + "_cc" , 25header = "simple.h" , 26tags = [ "manual" ], 27edition = "2021" , 28) 29rust_binary ( 30name = name + "_rust_binary" , 31srcs = [ "main.rs" ], 32deps = [ name + "_rust_bindgen" ], 33tags = [ "manual" ], 34edition = "2021" , 35) 36 37# Act 38# TODO: Use targets attr to also verify `rust_bindgen_library` not having 39# the linkopt after https://github.com/bazelbuild/rules_testing/issues/67 40# is released 41analysis_test ( 42name = name , 43target = name + "_rust_binary" , 44impl = _test_cc_linkopt_impl , 45) 46 47def _test_cc_lib_object_merging_impl ( env , target ): 48env . expect . that_int ( len ( target . actions )). is_greater_than ( 2 ) 49env . expect . that_action ( target . actions [ 0 ]). mnemonic (). contains ( "RustBindgen" ) 50env . expect . that_action ( target . actions [ 1 ]). mnemonic (). contains ( "FileWrite" ) 51env . expect . that_action ( target . actions [ 1 ]). content (). contains ( "-lstatic=test_cc_lib_object_merging_cc" ) 52env . expect . that_action ( target . actions [ 2 ]). mnemonic (). contains ( "FileWrite" ) 53env . expect . that_action ( target . actions [ 2 ]). content (). contains ( "-Lnative=" ) 54 55def _test_cc_lib_object_merging_disabled_impl ( env , target ): 56# no FileWrite actions writing arg files registered 57env . expect . that_int ( len ( target . actions )). is_greater_than ( 0 ) 58env . expect . that_action ( target . actions [ 0 ]). mnemonic (). contains ( "RustBindgen" ) 59 60def _test_cc_lib_object_merging ( name ): 61cc_library ( 62name = name + "_cc" , 63hdrs = [ "simple.h" ], 64srcs = [ "simple.cc" ], 65) 66 67rust_bindgen_library ( 68name = name + "_rust_bindgen" , 69cc_lib = name + "_cc" , 70header = "simple.h" , 71tags = [ "manual" ], 72edition = "2021" , 73) 74 75analysis_test ( 76name = name , 77target = name + "_rust_bindgen__bindgen" , 78impl = _test_cc_lib_object_merging_impl , 79) 80 81def _test_cc_lib_object_merging_disabled ( name ): 82cc_library ( 83name = name + "_cc" , 84hdrs = [ "simple.h" ], 85srcs = [ "simple.cc" ], 86) 87 88rust_bindgen_library ( 89name = name + "_rust_bindgen" , 90cc_lib = name + "_cc" , 91header = "simple.h" , 92tags = [ "manual" ], 93merge_cc_lib_objects_into_rlib = False , 94edition = "2021" , 95) 96 97analysis_test ( 98name = name , 99target = name + "_rust_bindgen__bindgen" , 100impl = _test_cc_lib_object_merging_disabled_impl , 101) 102 103def bindgen_test_suite ( name ): 104test_suite ( 105name = name , 106tests = [ 107_test_cc_linkopt , 108_test_cc_lib_object_merging , 109_test_cc_lib_object_merging_disabled , 110], 111)