1"""Test definitions for rustfmt test rules""" 2 3load( 4 "@rules_rust//rust:defs.bzl", 5 "rust_binary", 6 "rust_library", 7 "rust_shared_library", 8 "rust_static_library", 9 "rustfmt_test", 10) 11 12_VARIANTS = { 13 "rust_binary": rust_binary, 14 "rust_library": rust_library, 15 "rust_shared_library": rust_shared_library, 16 "rust_static_library": rust_static_library, 17} 18 19def rustfmt_integration_test_suite(name, **kwargs): 20 """Generate a test suite for rustfmt integration tests. 21 22 Targets generated are expected to be executed using a target 23 named: `{name}.test_runner` 24 25 Args: 26 name (str): The name of the test suite 27 **kwargs (dict): Additional keyword arguments for the underlying test_suite. 28 """ 29 30 tests = [] 31 for variant, rust_rule in _VARIANTS.items(): 32 # 33 # Test edition 2018 34 # 35 rust_rule( 36 name = "{}_formatted_2018".format(variant), 37 srcs = ["srcs/2018/formatted.rs"], 38 edition = "2018", 39 ) 40 41 rustfmt_test( 42 name = "{}_formatted_2018_test".format(variant), 43 targets = [":{}_formatted_2018".format(variant)], 44 ) 45 46 rust_rule( 47 name = "{}_unformatted_2018".format(variant), 48 srcs = ["srcs/2018/unformatted.rs"], 49 edition = "2018", 50 tags = ["norustfmt"], 51 ) 52 53 rustfmt_test( 54 name = "{}_unformatted_2018_test".format(variant), 55 tags = ["manual"], 56 targets = [":{}_unformatted_2018".format(variant)], 57 ) 58 59 # 60 # Test edition 2015 61 # 62 rust_rule( 63 name = "{}_formatted_2015".format(variant), 64 srcs = ["srcs/2015/formatted.rs"], 65 edition = "2015", 66 ) 67 68 rustfmt_test( 69 name = "{}_formatted_2015_test".format(variant), 70 targets = [":{}_formatted_2015".format(variant)], 71 ) 72 73 rust_rule( 74 name = "{}_unformatted_2015".format(variant), 75 srcs = ["srcs/2015/unformatted.rs"], 76 edition = "2015", 77 tags = ["norustfmt"], 78 ) 79 80 rustfmt_test( 81 name = "{}_unformatted_2015_test".format(variant), 82 tags = ["manual"], 83 targets = [":{}_unformatted_2015".format(variant)], 84 ) 85 86 # 87 # Test targets with generated sources 88 # 89 rust_rule( 90 name = "{}_generated".format(variant), 91 srcs = [ 92 "srcs/generated/lib.rs", 93 "srcs/generated/generated.rs", 94 ], 95 crate_root = "srcs/generated/lib.rs", 96 edition = "2021", 97 ) 98 99 rustfmt_test( 100 name = "{}_generated_test".format(variant), 101 targets = [":{}_generated".format(variant)], 102 ) 103 104 tests.extend([ 105 "{}_formatted_2015_test".format(variant), 106 "{}_formatted_2018_test".format(variant), 107 "{}_unformatted_2015_test".format(variant), 108 "{}_unformatted_2018_test".format(variant), 109 "{}_generated_test".format(variant), 110 ]) 111 112 native.test_suite( 113 name = name, 114 tests = tests, 115 **kwargs 116 ) 117 118 native.sh_binary( 119 name = "{}.test_runner".format(name), 120 srcs = ["rustfmt_failure_test.sh"], 121 testonly = True, 122 ) 123