1def generated_file_staleness_test(name, outs, generated_pattern): 2 """Tests that checked-in file(s) match the contents of generated file(s). 3 4 The resulting test will verify that all output files exist and have the 5 correct contents. If the test fails, it can be invoked with --fix to 6 bring the checked-in files up to date. 7 8 Args: 9 name: Name of the rule. 10 outs: the checked-in files that are copied from generated files. 11 generated_pattern: the pattern for transforming each "out" file into a 12 generated file. For example, if generated_pattern="generated/%s" then 13 a file foo.txt will look for generated file generated/foo.txt. 14 """ 15 16 script_name = name + ".py" 17 script_src = ":staleness_test.py" 18 19 # Filter out non-existing rules so Blaze doesn't error out before we even 20 # run the test. 21 existing_outs = native.glob(include = outs) 22 23 # The file list contains a few extra bits of information at the end. 24 # These get unpacked by the Config class in staleness_test_lib.py. 25 file_list = outs + [generated_pattern, native.package_name() or ".", name] 26 27 native.genrule( 28 name = name + "_makescript", 29 outs = [script_name], 30 srcs = [script_src], 31 testonly = 1, 32 cmd = "cat $(location " + script_src + ") > $@; " + 33 "sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n ".join(file_list) + "|' $@", 34 ) 35 36 native.py_test( 37 name = name, 38 srcs = [script_name], 39 data = existing_outs + [generated_pattern % file for file in outs], 40 deps = [ 41 ":staleness_test_lib", 42 ], 43 ) 44