fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
e404e516
patching
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_bin_with_transitive_proc_macro_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_lib_with_cc_lib_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_with_args_test_impl ( ctx ): 94env = analysistest . begin ( ctx ) 95tut = analysistest . target_under_test ( env ) 96 97_common_rustdoc_checks ( env , tut ) 98 99action = _get_rustdoc_action ( env , tut ) 100 101assert_argv_contains ( env , action , "--allow=rustdoc::broken_intra_doc_links" ) 102 103return analysistest . end ( env ) 104 105def _rustdoc_zip_output_test_impl ( ctx ): 106env = analysistest . begin ( ctx ) 107tut = analysistest . target_under_test ( env ) 108 109files = tut [ DefaultInfo ]. files . to_list () 110asserts . equals ( 111env , 112len ( files ), 1131 , 114"The target under this test should have 1 DefaultInfo file but has {}" . format ( 115len ( files ), 116), 117) 118 119zip_file = files [ 0 ] 120asserts . true ( 121env , 122zip_file . basename . endswith ( ".zip" ), 123"{} did not end with `.zip`" . format ( 124zip_file . path , 125), 126) 127 128return analysistest . end ( env ) 129 130def _rustdoc_with_json_error_format_test_impl ( ctx ): 131env = analysistest . begin ( ctx ) 132tut = analysistest . target_under_test ( env ) 133 134_common_rustdoc_checks ( env , tut ) 135 136action = _get_rustdoc_action ( env , tut ) 137 138assert_argv_contains ( env , action , "--error-format=json" ) 139 140return analysistest . end ( env ) 141 142rustdoc_for_lib_test = analysistest . make ( _rustdoc_for_lib_test_impl ) 143rustdoc_for_bin_test = analysistest . make ( _rustdoc_for_bin_test_impl ) 144rustdoc_for_bin_with_cc_lib_test = analysistest . make ( _rustdoc_for_bin_with_cc_lib_test_impl ) 145rustdoc_for_bin_with_transitive_cc_lib_test = analysistest . make ( _rustdoc_for_bin_with_transitive_cc_lib_test_impl ) 146rustdoc_for_proc_macro_test = analysistest . make ( _rustdoc_for_proc_macro_test_impl ) 147rustdoc_for_lib_with_proc_macro_test = analysistest . make ( _rustdoc_for_lib_with_proc_macro_test_impl ) 148rustdoc_for_bin_with_transitive_proc_macro_test = analysistest . make ( _rustdoc_for_bin_with_transitive_proc_macro_test_impl ) 149rustdoc_for_lib_with_cc_lib_test = analysistest . make ( _rustdoc_for_lib_with_cc_lib_test_impl ) 150rustdoc_with_args_test = analysistest . make ( _rustdoc_with_args_test_impl ) 151rustdoc_zip_output_test = analysistest . make ( _rustdoc_zip_output_test_impl ) 152rustdoc_with_json_error_format_test = analysistest . make ( _rustdoc_with_json_error_format_test_impl , config_settings = { 153str ( Label ( "//:error_format" )): "json" , 154}) 155 156def _target_maker ( rule_fn , name , rustdoc_deps = [], ** kwargs ): 157rule_fn ( 158name = name , 159edition = "2018" , 160** kwargs 161) 162 163rust_test ( 164name = "{}_test" . format ( name ), 165crate = ":{}" . format ( name ), 166edition = "2018" , 167) 168 169rust_doc ( 170name = "{}_doc" . format ( name ), 171crate = ":{}" . format ( name ), 172) 173 174rust_doc_test ( 175name = "{}_doctest" . format ( name ), 176crate = ":{}" . format ( name ), 177deps = rustdoc_deps , 178) 179 180def _define_targets (): 181rust_library ( 182name = "adder" , 183srcs = [ "adder.rs" ], 184edition = "2018" , 185) 186 187_target_maker ( 188rust_binary , 189name = "bin" , 190srcs = [ "rustdoc_bin.rs" ], 191) 192 193_target_maker ( 194rust_binary , 195name = "bin_with_cc" , 196srcs = [ "rustdoc_bin.rs" ], 197crate_features = [ "with_cc" ], 198deps = [ ":cc_lib" ], 199) 200 201_target_maker ( 202rust_binary , 203name = "bin_with_transitive_cc" , 204srcs = [ "rustdoc_bin.rs" ], 205crate_features = [ "with_cc" ], 206deps = [ ":transitive_cc_lib" ], 207) 208 209_target_maker ( 210rust_library , 211name = "lib" , 212srcs = [ "rustdoc_lib.rs" ], 213rustdoc_deps = [ ":adder" ], 214) 215 216_target_maker ( 217rust_library , 218name = "nodep_lib" , 219srcs = [ "rustdoc_nodep_lib.rs" ], 220) 221 222_target_maker ( 223rust_proc_macro , 224name = "rustdoc_proc_macro" , 225srcs = [ "rustdoc_proc_macro.rs" ], 226) 227 228_target_maker ( 229rust_library , 230name = "lib_with_proc_macro" , 231srcs = [ "rustdoc_lib.rs" ], 232rustdoc_deps = [ ":adder" ], 233proc_macro_deps = [ ":rustdoc_proc_macro" ], 234crate_features = [ "with_proc_macro" ], 235) 236 237_target_maker ( 238rust_library , 239name = "lib_nodep_with_proc_macro" , 240srcs = [ "rustdoc_nodep_lib.rs" ], 241proc_macro_deps = [ ":rustdoc_proc_macro" ], 242crate_features = [ "with_proc_macro" ], 243) 244 245_target_maker ( 246rust_binary , 247name = "bin_with_transitive_proc_macro" , 248srcs = [ "rustdoc_bin.rs" ], 249deps = [ ":lib_with_proc_macro" ], 250proc_macro_deps = [ ":rustdoc_proc_macro" ], 251crate_features = [ "with_proc_macro" ], 252) 253 254cc_library ( 255name = "cc_lib" , 256hdrs = [ "rustdoc.h" ], 257srcs = [ "rustdoc.cc" ], 258) 259 260cc_library ( 261name = "transitive_cc_lib" , 262hdrs = [ "rustdoc.h" ], 263srcs = [ "rustdoc.cc" ], 264deps = [ ":cc_lib" ], 265# This is not needed for :cc_lib, but it is needed in other 266# circumstances to link in system libraries. 267linkopts = [ "-lcc_lib" ], 268linkstatic = True , 269) 270 271_target_maker ( 272rust_library , 273name = "lib_with_cc" , 274srcs = [ "rustdoc_lib.rs" ], 275rustdoc_deps = [ ":adder" ], 276crate_features = [ "with_cc" ], 277deps = [ ":cc_lib" ], 278) 279 280_target_maker ( 281rust_library , 282name = "lib_nodep_with_cc" , 283srcs = [ "rustdoc_nodep_lib.rs" ], 284crate_features = [ "with_cc" ], 285deps = [ ":cc_lib" ], 286) 287 288cargo_build_script ( 289name = "lib_build_script" , 290srcs = [ "rustdoc_build.rs" ], 291edition = "2018" , 292) 293 294_target_maker ( 295rust_library , 296name = "lib_with_build_script" , 297srcs = [ "rustdoc_lib.rs" ], 298rustdoc_deps = [ ":adder" ], 299crate_features = [ "with_build_script" ], 300deps = [ ":lib_build_script" ], 301) 302 303_target_maker ( 304rust_library , 305name = "lib_nodep_with_build_script" , 306srcs = [ "rustdoc_nodep_lib.rs" ], 307crate_features = [ "with_build_script" ], 308deps = [ ":lib_build_script" ], 309) 310 311rust_library ( 312name = "lib_requires_args" , 313srcs = [ "rustdoc_requires_args.rs" ], 314edition = "2018" , 315) 316 317rust_doc ( 318name = "rustdoc_with_args" , 319crate = ":lib_requires_args" , 320rustdoc_flags = [ 321"--allow=rustdoc::broken_intra_doc_links" , 322], 323) 324 325rust_library ( 326name = "lib_dep_with_alias" , 327srcs = [ "rustdoc_test_dep_with_alias.rs" ], 328edition = "2018" , 329deps = [ ":adder" ], 330aliases = { 331":adder" : "aliased_adder" , 332}, 333) 334 335rust_doc_test ( 336name = "rustdoc_test_with_alias_test" , 337crate = ":lib_dep_with_alias" , 338) 339 340def rustdoc_test_suite ( name ): 341"""Entry-point macro called from the BUILD file. 342 343Args: 344name (str): Name of the macro. 345""" 346 347_define_targets () 348 349rustdoc_for_lib_test ( 350name = "rustdoc_for_lib_test" , 351target_under_test = ":lib_doc" , 352) 353 354rustdoc_for_bin_test ( 355name = "rustdoc_for_bin_test" , 356target_under_test = ":bin_doc" , 357) 358 359rustdoc_for_bin_with_cc_lib_test ( 360name = "rustdoc_for_bin_with_cc_lib_test" , 361target_under_test = ":bin_with_cc_doc" , 362) 363 364rustdoc_for_bin_with_transitive_cc_lib_test ( 365name = "rustdoc_for_bin_with_transitive_cc_lib_test" , 366target_under_test = ":bin_with_transitive_cc_doc" , 367) 368 369rustdoc_for_proc_macro_test ( 370name = "rustdoc_for_proc_macro_test" , 371target_under_test = ":rustdoc_proc_macro_doc" , 372) 373 374rustdoc_for_lib_with_proc_macro_test ( 375name = "rustdoc_for_lib_with_proc_macro_test" , 376target_under_test = ":lib_with_proc_macro_doc" , 377) 378 379rustdoc_for_bin_with_transitive_proc_macro_test ( 380name = "rustdoc_for_bin_with_transitive_proc_macro_test" , 381target_under_test = ":bin_with_transitive_proc_macro_doc" , 382) 383 384rustdoc_for_lib_with_cc_lib_test ( 385name = "rustdoc_for_lib_with_cc_lib_test" , 386target_under_test = ":lib_with_cc_doc" , 387) 388 389rustdoc_with_args_test ( 390name = "rustdoc_with_args_test" , 391target_under_test = ":rustdoc_with_args" , 392) 393 394rustdoc_with_json_error_format_test ( 395name = "rustdoc_with_json_error_format_test" , 396target_under_test = ":lib_doc" , 397) 398 399native . filegroup ( 400name = "lib_doc_zip" , 401srcs = [ ":lib_doc.zip" ], 402) 403 404rustdoc_zip_output_test ( 405name = "rustdoc_zip_output_test" , 406target_under_test = ":lib_doc_zip" , 407) 408 409native . test_suite ( 410name = name , 411tests = [ 412":rustdoc_for_lib_test" , 413":rustdoc_for_bin_test" , 414":rustdoc_for_bin_with_cc_lib_test" , 415":rustdoc_for_bin_with_transitive_cc_lib_test" , 416":rustdoc_for_proc_macro_test" , 417":rustdoc_for_lib_with_proc_macro_test" , 418":rustdoc_for_lib_with_cc_lib_test" , 419":rustdoc_with_args_test" , 420":rustdoc_with_json_error_format_test" , 421":rustdoc_zip_output_test" , 422], 423)