|
| 1 | +_js_filetype = FileType([".js"]) |
| 2 | +_modules_filetype = FileType(["node_modules"]) |
| 3 | + |
| 4 | +BASH_TEMPLATE = """ |
| 5 | +#!/usr/bin/env bash |
| 6 | +set -e |
| 7 | +
|
| 8 | +# Resolve to 'this' node instance if other scripts |
| 9 | +# have '/usr/bin/env node' shebangs |
| 10 | +export PATH={node_bin_path}:$PATH |
| 11 | +
|
| 12 | +# Used by NPM |
| 13 | +export NODE_PATH={node_paths} |
| 14 | +
|
| 15 | +# Run it |
| 16 | +"{node}" "{mocha}" {mocha_args} "{script_path}" $@ |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def _get_abs_sourcepath(file): |
| 21 | + filename = str(file) |
| 22 | + #print("filename: %s" % filename) |
| 23 | + parts = filename.partition("[source]]") |
| 24 | + prefix = parts[0][len("Artifact:["):] |
| 25 | + suffix = parts[2] |
| 26 | + d = "/".join([prefix, suffix]) |
| 27 | + #print("abs filename: %s" % d) |
| 28 | + return d |
| 29 | + |
| 30 | + |
| 31 | +def _get_node_modules_dir_from_binfile(file): |
| 32 | + bin = str(file) |
| 33 | + parts = bin.partition("[source]]") |
| 34 | + prefix = parts[0][len("Artifact:["):] |
| 35 | + suffix_parts = parts[2].split("/") |
| 36 | + #print("prefix: %s, suffix_parts: %s" % (prefix, suffix_parts)) |
| 37 | + return "/".join([prefix] + suffix_parts[0:2] + ["node_modules"]) |
| 38 | + |
| 39 | + |
| 40 | +def _get_node_modules_dir_from_package_json(file): |
| 41 | + filename = str(file) |
| 42 | + parts = filename.split("]") |
| 43 | + prefix = parts[0][len("Artifact:[["):] |
| 44 | + middle = parts[1] |
| 45 | + suffix = parts[2].split("/") |
| 46 | + d = "/".join([prefix, middle] + suffix[0:-3] + ["node_modules"]) |
| 47 | + return d |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +def mocha_test_impl(ctx): |
| 52 | + inputs = [] |
| 53 | + srcs = [] |
| 54 | + script = ctx.file.main |
| 55 | + node = ctx.file._node |
| 56 | + mocha = ctx.file.mocha |
| 57 | + node_paths = [] |
| 58 | + node_paths.append(_get_node_modules_dir_from_binfile(mocha)) |
| 59 | + |
| 60 | + mocha_path = _get_abs_sourcepath(mocha) |
| 61 | + |
| 62 | + for file in ctx.files.modules: |
| 63 | + #print("file: %s" % file) |
| 64 | + if not file.basename.endswith("node_modules"): |
| 65 | + fail("npm_dependency should be a path to a node_modules/ directory.") |
| 66 | + node_paths += [_get_node_modules_dir_from_binfile(file)] |
| 67 | + |
| 68 | + for dep in ctx.attr.deps: |
| 69 | + lib = dep.node_library |
| 70 | + srcs += lib.transitive_srcs |
| 71 | + inputs += [lib.package_json, lib.npm_package_json] |
| 72 | + node_paths += [_get_node_modules_dir_from_package_json(lib.package_json)] |
| 73 | + for file in lib.transitive_node_modules: |
| 74 | + node_paths += [file.path] |
| 75 | + inputs.append(file) |
| 76 | + |
| 77 | + node_paths = list(set(node_paths)) |
| 78 | + |
| 79 | + ctx.file_action( |
| 80 | + output = ctx.outputs.executable, |
| 81 | + executable = True, |
| 82 | + content = BASH_TEMPLATE.format( |
| 83 | + node_paths = ":".join(node_paths), |
| 84 | + node = node.short_path, |
| 85 | + node_bin_path = node.dirname, |
| 86 | + script_path = script.short_path, |
| 87 | + mocha = mocha_path, |
| 88 | + mocha_args = " ".join(ctx.attr.mocha_args), |
| 89 | + ), |
| 90 | + ) |
| 91 | + |
| 92 | + #print("node_paths %s" % "\n".join(node_paths)) |
| 93 | + |
| 94 | + runfiles = [node, script] + inputs + srcs |
| 95 | + |
| 96 | + return struct( |
| 97 | + runfiles = ctx.runfiles( |
| 98 | + files = runfiles, |
| 99 | + ), |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +mocha_test = rule( |
| 104 | + mocha_test_impl, |
| 105 | + attrs = { |
| 106 | + "main": attr.label( |
| 107 | + single_file = True, |
| 108 | + allow_files = True, |
| 109 | + #allow_files = _js_filetype, |
| 110 | + ), |
| 111 | + "data": attr.label_list( |
| 112 | + allow_files = True, |
| 113 | + ), |
| 114 | + "deps": attr.label_list( |
| 115 | + providers = ["node_library"], |
| 116 | + ), |
| 117 | + "modules": attr.label_list( |
| 118 | + allow_files = _modules_filetype, |
| 119 | + ), |
| 120 | + "_node": attr.label( |
| 121 | + default = Label("@org_pubref_rules_node_toolchain//:node_tool"), |
| 122 | + single_file = True, |
| 123 | + allow_files = True, |
| 124 | + executable = True, |
| 125 | + cfg = "host", |
| 126 | + ), |
| 127 | + "mocha": attr.label( |
| 128 | + default = Label("@npm_mocha//:bin/mocha"), |
| 129 | + allow_files = True, |
| 130 | + single_file = True, |
| 131 | + ), |
| 132 | + "mocha_modules": attr.label( |
| 133 | + default = Label("@npm_mocha//:modules"), |
| 134 | + ), |
| 135 | + "mocha_args": attr.string_list(), |
| 136 | + }, |
| 137 | + test = True, |
| 138 | +) |
0 commit comments