fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
03a70796
0.8.1
1"""Unittest to verify properties of rustdoc rules""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" ) 4load ( "@rules_cc//cc:defs.bzl" , "cc_library" ) 5load ( "//rust:defs.bzl" , "rust_binary" , "rust_doc" , "rust_doc_test" , "rust_library" , "rust_proc_macro" , "rust_test" ) 6load ( 7"//test/unit:common.bzl" , 8"assert_action_mnemonic" , 9"assert_argv_contains_prefix_not" , 10) 11 12def _common_rustdoc_checks ( env , tut ): 13actions = tut . actions 14action = actions [ 0 ] 15assert_action_mnemonic ( env , action , "Rustdoc" ) 16 17# These flags, while required for `Rustc` actions, should be omitted for 18# `Rustdoc` actions 19assert_argv_contains_prefix_not ( env , action , "--remap-path-prefix" ) 20assert_argv_contains_prefix_not ( env , action , "--emit" ) 21 22def _rustdoc_for_lib_test_impl ( ctx ): 23env = analysistest . begin ( ctx ) 24tut = analysistest . target_under_test ( env ) 25 26_common_rustdoc_checks ( env , tut ) 27 28return analysistest . end ( env ) 29 30def _rustdoc_for_bin_test_impl ( ctx ): 31env = analysistest . begin ( ctx ) 32tut = analysistest . target_under_test ( env ) 33 34_common_rustdoc_checks ( env , tut ) 35 36return analysistest . end ( env ) 37 38def _rustdoc_for_proc_macro_test_impl ( ctx ): 39env = analysistest . begin ( ctx ) 40tut = analysistest . target_under_test ( env ) 41 42_common_rustdoc_checks ( env , tut ) 43 44return analysistest . end ( env ) 45 46def _rustdoc_for_lib_with_proc_macro_test_impl ( ctx ): 47env = analysistest . begin ( ctx ) 48tut = analysistest . target_under_test ( env ) 49 50_common_rustdoc_checks ( env , tut ) 51 52return analysistest . end ( env ) 53 54def _rustdoc_for_bin_with_transitive_proc_macro_test_impl ( ctx ): 55env = analysistest . begin ( ctx ) 56tut = analysistest . target_under_test ( env ) 57 58_common_rustdoc_checks ( env , tut ) 59 60return analysistest . end ( env ) 61 62def _rustdoc_for_lib_with_cc_lib_test_impl ( ctx ): 63env = analysistest . begin ( ctx ) 64tut = analysistest . target_under_test ( env ) 65 66_common_rustdoc_checks ( env , tut ) 67 68return analysistest . end ( env ) 69 70rustdoc_for_lib_test = analysistest . make ( _rustdoc_for_lib_test_impl ) 71rustdoc_for_bin_test = analysistest . make ( _rustdoc_for_bin_test_impl ) 72rustdoc_for_proc_macro_test = analysistest . make ( _rustdoc_for_proc_macro_test_impl ) 73rustdoc_for_lib_with_proc_macro_test = analysistest . make ( _rustdoc_for_lib_with_proc_macro_test_impl ) 74rustdoc_for_bin_with_transitive_proc_macro_test = analysistest . make ( _rustdoc_for_bin_with_transitive_proc_macro_test_impl ) 75rustdoc_for_lib_with_cc_lib_test = analysistest . make ( _rustdoc_for_lib_with_cc_lib_test_impl ) 76 77def _target_maker ( rule_fn , name , rustdoc_deps = [], ** kwargs ): 78rule_fn ( 79name = name , 80edition = "2018" , 81** kwargs 82) 83 84rust_test ( 85name = "{}_test" . format ( name ), 86crate = ":{}" . format ( name ), 87edition = "2018" , 88) 89 90rust_doc ( 91name = "{}_doc" . format ( name ), 92crate = ":{}" . format ( name ), 93) 94 95rust_doc_test ( 96name = "{}_doctest" . format ( name ), 97crate = ":{}" . format ( name ), 98deps = rustdoc_deps , 99) 100 101def _define_targets (): 102rust_library ( 103name = "adder" , 104srcs = [ "adder.rs" ], 105edition = "2018" , 106) 107 108_target_maker ( 109rust_binary , 110name = "bin" , 111srcs = [ "rustdoc_bin.rs" ], 112) 113 114_target_maker ( 115rust_library , 116name = "lib" , 117srcs = [ "rustdoc_lib.rs" ], 118rustdoc_deps = [ ":adder" ], 119) 120 121_target_maker ( 122rust_proc_macro , 123name = "rustdoc_proc_macro" , 124srcs = [ "rustdoc_proc_macro.rs" ], 125) 126 127_target_maker ( 128rust_library , 129name = "lib_with_proc_macro" , 130srcs = [ "rustdoc_lib.rs" ], 131rustdoc_deps = [ ":adder" ], 132proc_macro_deps = [ ":rustdoc_proc_macro" ], 133crate_features = [ "with_proc_macro" ], 134) 135 136_target_maker ( 137rust_binary , 138name = "bin_with_transitive_proc_macro" , 139srcs = [ "rustdoc_bin.rs" ], 140deps = [ ":lib_with_proc_macro" ], 141proc_macro_deps = [ ":rustdoc_proc_macro" ], 142crate_features = [ "with_proc_macro" ], 143) 144 145cc_library ( 146name = "cc_lib" , 147hdrs = [ "rustdoc.h" ], 148srcs = [ "rustdoc.cc" ], 149) 150 151_target_maker ( 152rust_library , 153name = "lib_with_cc" , 154srcs = [ "rustdoc_lib.rs" ], 155rustdoc_deps = [ ":adder" ], 156crate_features = [ "with_cc" ], 157deps = [ ":cc_lib" ], 158) 159 160def rustdoc_test_suite ( name ): 161"""Entry-point macro called from the BUILD file. 162 163Args: 164name (str): Name of the macro. 165""" 166 167_define_targets () 168 169rustdoc_for_lib_test ( 170name = "rustdoc_for_lib_test" , 171target_under_test = ":lib_doc" , 172) 173 174rustdoc_for_bin_test ( 175name = "rustdoc_for_bin_test" , 176target_under_test = ":bin_doc" , 177) 178 179rustdoc_for_proc_macro_test ( 180name = "rustdoc_for_proc_macro_test" , 181target_under_test = ":rustdoc_proc_macro_doc" , 182) 183 184rustdoc_for_lib_with_proc_macro_test ( 185name = "rustdoc_for_lib_with_proc_macro_test" , 186target_under_test = ":lib_with_proc_macro_doc" , 187) 188 189rustdoc_for_bin_with_transitive_proc_macro_test ( 190name = "rustdoc_for_bin_with_transitive_proc_macro_test" , 191target_under_test = ":bin_with_transitive_proc_macro_doc" , 192) 193 194rustdoc_for_lib_with_cc_lib_test ( 195name = "rustdoc_for_lib_with_cc_lib_test" , 196target_under_test = ":lib_with_cc_doc" , 197) 198 199native . test_suite ( 200name = name , 201tests = [ 202":rustdoc_for_lib_test" , 203":rustdoc_for_bin_test" , 204":rustdoc_for_proc_macro_test" , 205":rustdoc_for_lib_with_proc_macro_test" , 206":rustdoc_for_lib_with_cc_lib_test" , 207], 208)