|
| 1 | +load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test") |
| 2 | + |
| 3 | +def _serialize_file(file): |
| 4 | + """Serializes a file into a struct that matches the `BazelFileInfo` type in the |
| 5 | + packager implementation. Useful for transmission of such information.""" |
| 6 | + |
| 7 | + return struct(path = file.path, shortPath = file.short_path) |
| 8 | + |
| 9 | +def _split_and_expand_command(ctx, command): |
| 10 | + """Splits a command into the binary and its arguments. Also Bazel locations are expanded.""" |
| 11 | + expanded_command = ctx.expand_location(command, targets = ctx.attr.data) |
| 12 | + return expanded_command.split(" ", 1) |
| 13 | + |
| 14 | +def _unwrap_label_keyed_mappings(dict, description): |
| 15 | + """Unwraps a label-keyed dictionary used for expressing mappings into a JSON-serializable |
| 16 | + dictionary that will match the `Record<string, BazelFileInfo>` type as in the test |
| 17 | + runner. Additionally, the list of referenced mapping files is returned so that these |
| 18 | + can be added to the runfiles of the tool relying on the serialized mappings. |
| 19 | +
|
| 20 | + This helper is used for serializing the `npm_packages` and `tool_mappings` |
| 21 | + dictionaries into JSON that can be passed to the test runner.""" |
| 22 | + |
| 23 | + serialized_mappings = {} |
| 24 | + referenced_files = [] |
| 25 | + |
| 26 | + for target in dict: |
| 27 | + name = dict[target] |
| 28 | + |
| 29 | + if not DefaultInfo in target: |
| 30 | + fail("Expected %s mapping for %s to have the `DefaultInfo` provider." % (description, target)) |
| 31 | + |
| 32 | + files = target[DefaultInfo].files.to_list() |
| 33 | + |
| 34 | + if len(files) != 1: |
| 35 | + fail("Expected %s target %s to only have a single file in `DefaultInfo`" % (description, target)) |
| 36 | + |
| 37 | + serialized_mappings[name] = _serialize_file(files[0]) |
| 38 | + referenced_files.append(files[0]) |
| 39 | + |
| 40 | + return serialized_mappings, referenced_files |
| 41 | + |
| 42 | +def _integration_test_config_impl(ctx): |
| 43 | + """Implementation of the `_integration_test_config` rule.""" |
| 44 | + |
| 45 | + npmPackageMappings, npmPackageFiles = \ |
| 46 | + _unwrap_label_keyed_mappings(ctx.attr.npm_packages, "NPM package") |
| 47 | + toolMappings, toolFiles = _unwrap_label_keyed_mappings(ctx.attr.tool_mappings, "Tool") |
| 48 | + |
| 49 | + config_file = ctx.actions.declare_file("%s.json" % ctx.attr.name) |
| 50 | + config = struct( |
| 51 | + testPackage = ctx.label.package, |
| 52 | + testFiles = [_serialize_file(f) for f in ctx.files.srcs], |
| 53 | + commands = [_split_and_expand_command(ctx, c) for c in ctx.attr.commands], |
| 54 | + npmPackageMappings = npmPackageMappings, |
| 55 | + toolMappings = toolMappings, |
| 56 | + ) |
| 57 | + |
| 58 | + ctx.actions.write( |
| 59 | + output = config_file, |
| 60 | + content = json.encode(config), |
| 61 | + ) |
| 62 | + |
| 63 | + runfiles = [config_file] + ctx.files.data + ctx.files.srcs + npmPackageFiles + toolFiles |
| 64 | + |
| 65 | + return [ |
| 66 | + DefaultInfo( |
| 67 | + files = depset([config_file]), |
| 68 | + runfiles = ctx.runfiles(files = runfiles), |
| 69 | + ), |
| 70 | + ] |
| 71 | + |
| 72 | +_integration_test_config = rule( |
| 73 | + implementation = _integration_test_config_impl, |
| 74 | + doc = """Rule which controls the integration test runner by writing a configuration file.""", |
| 75 | + attrs = { |
| 76 | + "srcs": attr.label_list( |
| 77 | + allow_files = True, |
| 78 | + mandatory = True, |
| 79 | + doc = "Files which need to be available when the integration test commands are invoked.", |
| 80 | + ), |
| 81 | + "data": attr.label_list( |
| 82 | + allow_files = True, |
| 83 | + doc = """ |
| 84 | + Files which will be available for runfile resolution. Useful when location |
| 85 | + expansion is used in a command.""", |
| 86 | + ), |
| 87 | + "commands": attr.string_list( |
| 88 | + mandatory = True, |
| 89 | + doc = """ |
| 90 | + List of commands to run as part of the integration test. The commands can rely on |
| 91 | + the global tools made available through the tool mappings. |
| 92 | +
|
| 93 | + Commands can also use Bazel make location expansion.""", |
| 94 | + ), |
| 95 | + "npm_packages": attr.label_keyed_string_dict( |
| 96 | + allow_files = True, |
| 97 | + doc = """ |
| 98 | + Dictionary of targets which map to NPM packages. This allows for NPM packages |
| 99 | + to be mapped to first-party built NPM artifacts.""", |
| 100 | + ), |
| 101 | + "tool_mappings": attr.label_keyed_string_dict( |
| 102 | + allow_files = True, |
| 103 | + doc = """ |
| 104 | + Dictionary of targets which map to global tools needed by the integration test. |
| 105 | + This allows for binaries like `node` to be made available to the integration test |
| 106 | + using the `PATH` environment variable.""", |
| 107 | + ), |
| 108 | + }, |
| 109 | +) |
| 110 | + |
| 111 | +def integration_test( |
| 112 | + name, |
| 113 | + srcs, |
| 114 | + commands, |
| 115 | + npm_packages = {}, |
| 116 | + tool_mappings = {}, |
| 117 | + data = [], |
| 118 | + tags = [], |
| 119 | + **kwargs): |
| 120 | + """Rule that allows for arbitrary commands to be executed within a temporary |
| 121 | + directory which will hold the specified test source files.""" |
| 122 | + |
| 123 | + config_target = "%s_config" % name |
| 124 | + |
| 125 | + _integration_test_config( |
| 126 | + name = config_target, |
| 127 | + srcs = srcs, |
| 128 | + data = data, |
| 129 | + commands = commands, |
| 130 | + npm_packages = npm_packages, |
| 131 | + tool_mappings = tool_mappings, |
| 132 | + tags = tags, |
| 133 | + ) |
| 134 | + |
| 135 | + nodejs_test( |
| 136 | + name = name, |
| 137 | + data = ["//bazel/integration/test_runner", ":" + config_target], |
| 138 | + templated_args = ["--bazel_patch_module_resolver", "$(rootpath :%s)" % config_target], |
| 139 | + entry_point = "//bazel/integration/test_runner:main.ts", |
| 140 | + tags = tags, |
| 141 | + **kwargs |
| 142 | + ) |
0 commit comments