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