fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
03a70796
0.7.0
1"""Unittest to verify workspace status stamping is applied to environment files""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" ) 4load ( "//rust:defs.bzl" , "rust_binary" , "rust_library" , "rust_test" ) 5load ( 6"//test/unit:common.bzl" , 7"assert_action_mnemonic" , 8"assert_argv_contains" , 9"assert_argv_contains_not" , 10) 11 12def _stamp_test_impl ( ctx , force_stamp ): 13env = analysistest . begin ( ctx ) 14target = analysistest . target_under_test ( env ) 15 16action = target . actions [ 0 ] 17assert_action_mnemonic ( env , action , "Rustc" ) 18 19if force_stamp : 20assert_argv_contains ( env , action , "--volatile-status-file" ) 21assert_argv_contains ( env , action , "bazel-out/volatile-status.txt" ) 22else : 23assert_argv_contains_not ( env , action , "--volatile-status-file" ) 24assert_argv_contains_not ( env , action , "bazel-out/volatile-status.txt" ) 25 26# Note that use of stable status invalidates targets any time it's updated. 27# This is undesirable behavior so it's intended to be excluded from the 28# Rustc action. In general this should be fine as other rules can be used 29# to produce template files for stamping in this action (ie. genrule). 30assert_argv_contains_not ( env , action , "bazel-out/stable-status.txt" ) 31 32return analysistest . end ( env ) 33 34def _force_stamp_test_impl ( ctx ): 35return _stamp_test_impl ( ctx , True ) 36 37def _skip_stamp_test_impl ( ctx ): 38return _stamp_test_impl ( ctx , False ) 39 40force_stamp_test = analysistest . make ( _force_stamp_test_impl ) 41skip_stamp_test = analysistest . make ( _skip_stamp_test_impl ) 42 43_STAMP_VALUES = ( 0 , 1 ) 44 45def _define_test_targets (): 46for stamp_value in _STAMP_VALUES : 47if stamp_value == 1 : 48name = "force_stamp" 49features = [ "force_stamp" ] 50else : 51name = "skip_stamp" 52features = [ "skip_stamp" ] 53 54rust_library ( 55name = name , 56srcs = [ "stamp.rs" ], 57edition = "2018" , 58rustc_env_files = [ ":stamp.env" ], 59stamp = stamp_value , 60crate_features = features , 61) 62 63rust_test ( 64name = "{}_unit_test" . format ( name ), 65crate = ":{}" . format ( name ), 66edition = "2018" , 67rustc_env_files = [ ":stamp.env" ], 68stamp = stamp_value , 69crate_features = features , 70) 71 72rust_binary ( 73name = "{}_bin" . format ( name ), 74srcs = [ "stamp_main.rs" ], 75edition = "2018" , 76deps = [ ":{}" . format ( name )], 77rustc_env_files = [ ":stamp.env" ], 78stamp = stamp_value , 79crate_features = features , 80) 81 82def stamp_test_suite ( name ): 83"""Entry-point macro called from the BUILD file. 84 85Args: 86name (str): Name of the macro. 87""" 88_define_test_targets () 89 90tests = [] 91 92for stamp_value in _STAMP_VALUES : 93if stamp_value == 1 : 94test_name = "force_stamp" 95stamp_test = force_stamp_test 96else : 97test_name = "skip_stamp" 98stamp_test = skip_stamp_test 99 100stamp_test ( 101name = "lib_{}_test" . format ( test_name ), 102target_under_test = Label ( "//test/unit/stamp:{}" . format ( test_name )), 103) 104 105stamp_test ( 106name = "test_{}_test" . format ( test_name ), 107target_under_test = Label ( "//test/unit/stamp:{}_unit_test" . format ( test_name )), 108) 109 110stamp_test ( 111name = "bin_{}_test" . format ( test_name ), 112target_under_test = Label ( "//test/unit/stamp:{}_bin" . format ( test_name )), 113) 114 115tests . extend ([ 116"lib_{}_test" . format ( test_name ), 117"test_{}_test" . format ( test_name ), 118"bin_{}_test" . format ( test_name ), 119]) 120 121native . test_suite ( 122name = name , 123tests = tests , 124)