fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
c3d71edd
cleanup
1"""Unittest to verify properties of rustdoc rules""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" ) 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" , 11"assert_argv_contains_prefix_not" , 12) 13 14def _get_rustdoc_action ( env , tut ): 15actions = tut . actions 16action = actions [ 0 ] 17assert_action_mnemonic ( env , action , "Rustdoc" ) 18 19return action 20 21def _common_rustdoc_checks ( env , tut ): 22action = _get_rustdoc_action ( env , tut ) 23 24# These flags, while required for `Rustc` actions, should be omitted for 25# `Rustdoc` actions 26assert_argv_contains_prefix_not ( env , action , "--remap-path-prefix" ) 27assert_argv_contains_prefix_not ( env , action , "--emit" ) 28 29def _rustdoc_for_lib_test_impl ( ctx ): 30env = analysistest . begin ( ctx ) 31tut = analysistest . target_under_test ( env ) 32 33_common_rustdoc_checks ( env , tut ) 34 35return analysistest . end ( env ) 36 37def _rustdoc_for_bin_test_impl ( ctx ): 38env = analysistest . begin ( ctx ) 39tut = analysistest . target_under_test ( env ) 40 41_common_rustdoc_checks ( env , tut ) 42 43return analysistest . end ( env ) 44 45def _rustdoc_for_bin_with_cc_lib_test_impl ( ctx ): 46env = analysistest . begin ( ctx ) 47tut = analysistest . target_under_test ( env ) 48 49_common_rustdoc_checks ( env , tut ) 50 51return analysistest . end ( env ) 52 53def _rustdoc_for_bin_with_transitive_cc_lib_test_impl ( ctx ): 54env = analysistest . begin ( ctx ) 55tut = analysistest . target_under_test ( env ) 56 57_common_rustdoc_checks ( env , tut ) 58 59return analysistest . end ( env ) 60 61def _rustdoc_for_proc_macro_test_impl ( ctx ): 62env = analysistest . begin ( ctx ) 63tut = analysistest . target_under_test ( env ) 64 65_common_rustdoc_checks ( env , tut ) 66 67return analysistest . end ( env ) 68 69def _rustdoc_for_lib_with_proc_macro_test_impl ( ctx ): 70env = analysistest . begin ( ctx ) 71tut = analysistest . target_under_test ( env ) 72 73_common_rustdoc_checks ( env , tut ) 74 75return analysistest . end ( env ) 76 77def _rustdoc_for_lib_with_proc_macro_in_docs_test_impl ( ctx ): 78env = analysistest . begin ( ctx ) 79tut = analysistest . target_under_test ( env ) 80 81_common_rustdoc_checks ( env , tut ) 82 83return analysistest . end ( env ) 84 85def _rustdoc_for_bin_with_transitive_proc_macro_test_impl ( ctx ): 86env = analysistest . begin ( ctx ) 87tut = analysistest . target_under_test ( env ) 88 89_common_rustdoc_checks ( env , tut ) 90 91return analysistest . end ( env ) 92 93def _rustdoc_for_lib_with_cc_lib_test_impl ( ctx ): 94env = analysistest . begin ( ctx ) 95tut = analysistest . target_under_test ( env ) 96 97_common_rustdoc_checks ( env , tut ) 98 99return analysistest . end ( env ) 100 101def _rustdoc_with_args_test_impl ( ctx ): 102env = analysistest . begin ( ctx ) 103tut = analysistest . target_under_test ( env ) 104 105_common_rustdoc_checks ( env , tut ) 106 107action = _get_rustdoc_action ( env , tut ) 108 109assert_argv_contains ( env , action , "--allow=rustdoc::broken_intra_doc_links" ) 110 111return analysistest . end ( env ) 112 113def _rustdoc_zip_output_test_impl ( ctx ): 114env = analysistest . begin ( ctx ) 115tut = analysistest . target_under_test ( env ) 116 117files = tut [ DefaultInfo ]. files . to_list () 118asserts . equals ( 119env , 120len ( files ), 1211 , 122"The target under this test should have 1 DefaultInfo file but has {}" . format ( 123len ( files ), 124), 125) 126 127zip_file = files [ 0 ] 128asserts . true ( 129env , 130zip_file . basename . endswith ( ".zip" ), 131"{} did not end with `.zip`" . format ( 132zip_file . path , 133), 134) 135 136return analysistest . end ( env ) 137 138def _rustdoc_with_json_error_format_test_impl ( ctx ): 139env = analysistest . begin ( ctx ) 140tut = analysistest . target_under_test ( env ) 141 142_common_rustdoc_checks ( env , tut ) 143 144action = _get_rustdoc_action ( env , tut ) 145 146assert_argv_contains ( env , action , "--error-format=json" ) 147 148return analysistest . end ( env ) 149 150rustdoc_for_lib_test = analysistest . make ( _rustdoc_for_lib_test_impl ) 151rustdoc_for_bin_test = analysistest . make ( _rustdoc_for_bin_test_impl ) 152rustdoc_for_bin_with_cc_lib_test = analysistest . make ( _rustdoc_for_bin_with_cc_lib_test_impl ) 153rustdoc_for_bin_with_transitive_cc_lib_test = analysistest . make ( _rustdoc_for_bin_with_transitive_cc_lib_test_impl ) 154rustdoc_for_proc_macro_test = analysistest . make ( _rustdoc_for_proc_macro_test_impl ) 155rustdoc_for_lib_with_proc_macro_test = analysistest . make ( _rustdoc_for_lib_with_proc_macro_test_impl ) 156rustdoc_for_lib_with_proc_macro_in_docs_test = analysistest . make ( _rustdoc_for_lib_with_proc_macro_in_docs_test_impl ) 157rustdoc_for_bin_with_transitive_proc_macro_test = analysistest . make ( _rustdoc_for_bin_with_transitive_proc_macro_test_impl ) 158rustdoc_for_lib_with_cc_lib_test = analysistest . make ( _rustdoc_for_lib_with_cc_lib_test_impl ) 159rustdoc_with_args_test = analysistest . make ( _rustdoc_with_args_test_impl ) 160rustdoc_zip_output_test = analysistest . make ( _rustdoc_zip_output_test_impl ) 161rustdoc_with_json_error_format_test = analysistest . make ( _rustdoc_with_json_error_format_test_impl , config_settings = { 162str ( Label ( "//rust/settings:error_format" )): "json" , 163}) 164 165def _target_maker ( rule_fn , name , rustdoc_deps = [], rustdoc_proc_macro_deps = [], ** kwargs ): 166rule_fn ( 167name = name , 168edition = "2018" , 169** kwargs 170) 171 172rust_test ( 173name = "{}_test" . format ( name ), 174crate = ":{}" . format ( name ), 175edition = "2018" , 176) 177 178rust_doc ( 179name = "{}_doc" . format ( name ), 180crate = ":{}" . format ( name ), 181) 182 183rust_doc_test ( 184name = "{}_doctest" . format ( name ), 185crate = ":{}" . format ( name ), 186deps = rustdoc_deps , 187proc_macro_deps = rustdoc_proc_macro_deps , 188) 189 190def _define_targets (): 191rust_library ( 192name = "adder" , 193srcs = [ "adder.rs" ], 194edition = "2018" , 195) 196 197_target_maker ( 198rust_binary , 199name = "bin" , 200srcs = [ "rustdoc_bin.rs" ], 201) 202 203_target_maker ( 204rust_binary , 205name = "bin_with_cc" , 206srcs = [ "rustdoc_bin.rs" ], 207crate_features = [ "with_cc" ], 208deps = [ ":cc_lib" ], 209) 210 211_target_maker ( 212rust_binary , 213name = "bin_with_transitive_cc" , 214srcs = [ "rustdoc_bin.rs" ], 215crate_features = [ "with_cc" ], 216deps = [ ":transitive_cc_lib" ], 217) 218 219_target_maker ( 220rust_library , 221name = "lib" , 222srcs = [ "rustdoc_lib.rs" ], 223rustdoc_deps = [ ":adder" ], 224) 225 226_target_maker ( 227rust_library , 228name = "nodep_lib" , 229srcs = [ "rustdoc_nodep_lib.rs" ], 230) 231 232_target_maker ( 233rust_proc_macro , 234name = "rustdoc_proc_macro" , 235srcs = [ "rustdoc_proc_macro.rs" ], 236) 237 238_target_maker ( 239rust_library , 240name = "lib_with_proc_macro" , 241srcs = [ "rustdoc_lib.rs" ], 242rustdoc_deps = [ ":adder" ], 243proc_macro_deps = [ ":rustdoc_proc_macro" ], 244crate_features = [ "with_proc_macro" ], 245) 246 247_target_maker ( 248rust_library , 249name = "lib_with_proc_macro_in_docs" , 250srcs = [ "procmacro_in_rustdoc.rs" ], 251proc_macro_deps = [ ":rustdoc_proc_macro" ], 252) 253 254_target_maker ( 255rust_library , 256name = "lib_with_proc_macro_only_in_docs" , 257srcs = [ "procmacro_in_rustdoc.rs" ], 258rustdoc_proc_macro_deps = [ ":rustdoc_proc_macro" ], 259) 260 261_target_maker ( 262rust_library , 263name = "lib_nodep_with_proc_macro" , 264srcs = [ "rustdoc_nodep_lib.rs" ], 265proc_macro_deps = [ ":rustdoc_proc_macro" ], 266crate_features = [ "with_proc_macro" ], 267) 268 269_target_maker ( 270rust_binary , 271name = "bin_with_transitive_proc_macro" , 272srcs = [ "rustdoc_bin.rs" ], 273deps = [ ":lib_with_proc_macro" ], 274proc_macro_deps = [ ":rustdoc_proc_macro" ], 275crate_features = [ "with_proc_macro" ], 276) 277 278cc_library ( 279name = "cc_lib" , 280hdrs = [ "rustdoc.h" ], 281srcs = [ "rustdoc.cc" ], 282) 283 284cc_library ( 285name = "transitive_cc_lib" , 286hdrs = [ "rustdoc.h" ], 287srcs = [ "rustdoc.cc" ], 288deps = [ ":cc_lib" ], 289# This is not needed for :cc_lib, but it is needed in other 290# circumstances to link in system libraries. 291linkopts = [ "-lcc_lib" ], 292linkstatic = True , 293) 294 295_target_maker ( 296rust_library , 297name = "lib_with_cc" , 298srcs = [ "rustdoc_lib.rs" ], 299rustdoc_deps = [ ":adder" ], 300crate_features = [ "with_cc" ], 301deps = [ ":cc_lib" ], 302) 303 304_target_maker ( 305rust_library , 306name = "lib_nodep_with_cc" , 307srcs = [ "rustdoc_nodep_lib.rs" ], 308crate_features = [ "with_cc" ], 309deps = [ ":cc_lib" ], 310) 311 312cargo_build_script ( 313name = "lib_build_script" , 314srcs = [ "rustdoc_build.rs" ], 315edition = "2018" , 316) 317 318_target_maker ( 319rust_library , 320name = "lib_with_build_script" , 321srcs = [ "rustdoc_lib.rs" ], 322rustdoc_deps = [ ":adder" ], 323crate_features = [ "with_build_script" ], 324deps = [ ":lib_build_script" ], 325) 326 327_target_maker ( 328rust_library , 329name = "lib_nodep_with_build_script" , 330srcs = [ "rustdoc_nodep_lib.rs" ], 331crate_features = [ "with_build_script" ], 332deps = [ ":lib_build_script" ], 333) 334 335rust_library ( 336name = "lib_requires_args" , 337srcs = [ "rustdoc_requires_args.rs" ], 338edition = "2018" , 339) 340 341rust_doc ( 342name = "rustdoc_with_args" , 343crate = ":lib_requires_args" , 344rustdoc_flags = [ 345"--allow=rustdoc::broken_intra_doc_links" , 346], 347) 348 349rust_library ( 350name = "lib_dep_with_alias" , 351srcs = [ "rustdoc_test_dep_with_alias.rs" ], 352edition = "2018" , 353deps = [ ":adder" ], 354aliases = { 355":adder" : "aliased_adder" , 356}, 357) 358 359rust_doc_test ( 360name = "rustdoc_test_with_alias_test" , 361crate = ":lib_dep_with_alias" , 362) 363 364def rustdoc_test_suite ( name ): 365"""Entry-point macro called from the BUILD file. 366 367Args: 368name (str): Name of the macro. 369""" 370 371_define_targets () 372 373rustdoc_for_lib_test ( 374name = "rustdoc_for_lib_test" , 375target_under_test = ":lib_doc" , 376) 377 378rustdoc_for_bin_test ( 379name = "rustdoc_for_bin_test" , 380target_under_test = ":bin_doc" , 381) 382 383rustdoc_for_bin_with_cc_lib_test ( 384name = "rustdoc_for_bin_with_cc_lib_test" , 385target_under_test = ":bin_with_cc_doc" , 386) 387 388rustdoc_for_bin_with_transitive_cc_lib_test ( 389name = "rustdoc_for_bin_with_transitive_cc_lib_test" , 390target_under_test = ":bin_with_transitive_cc_doc" , 391) 392 393rustdoc_for_proc_macro_test ( 394name = "rustdoc_for_proc_macro_test" , 395target_under_test = ":rustdoc_proc_macro_doc" , 396) 397 398rustdoc_for_lib_with_proc_macro_in_docs_test ( 399name = "rustdoc_for_lib_with_proc_macro_in_docs_test" , 400target_under_test = ":lib_with_proc_macro_in_docs_doc" , 401) 402 403rustdoc_for_lib_with_proc_macro_test ( 404name = "rustdoc_for_lib_with_proc_macro_test" , 405target_under_test = ":lib_with_proc_macro_doc" , 406) 407 408rustdoc_for_bin_with_transitive_proc_macro_test ( 409name = "rustdoc_for_bin_with_transitive_proc_macro_test" , 410target_under_test = ":bin_with_transitive_proc_macro_doc" , 411) 412 413rustdoc_for_lib_with_cc_lib_test ( 414name = "rustdoc_for_lib_with_cc_lib_test" , 415target_under_test = ":lib_with_cc_doc" , 416) 417 418rustdoc_with_args_test ( 419name = "rustdoc_with_args_test" , 420target_under_test = ":rustdoc_with_args" , 421) 422 423rustdoc_with_json_error_format_test ( 424name = "rustdoc_with_json_error_format_test" , 425target_under_test = ":lib_doc" , 426) 427 428native . filegroup ( 429name = "lib_doc_zip" , 430srcs = [ ":lib_doc.zip" ], 431) 432 433rustdoc_zip_output_test ( 434name = "rustdoc_zip_output_test" , 435target_under_test = ":lib_doc_zip" , 436) 437 438native . test_suite ( 439name = name , 440tests = [ 441":rustdoc_for_lib_test" , 442":rustdoc_for_bin_test" , 443":rustdoc_for_bin_with_cc_lib_test" , 444":rustdoc_for_bin_with_transitive_cc_lib_test" , 445":rustdoc_for_proc_macro_test" , 446":rustdoc_for_lib_with_proc_macro_in_docs_test" , 447":rustdoc_for_lib_with_proc_macro_test" , 448":rustdoc_for_lib_with_cc_lib_test" , 449":rustdoc_with_args_test" , 450":rustdoc_with_json_error_format_test" , 451":rustdoc_zip_output_test" , 452], 453)