Created
November 27, 2018 08:01
-
-
Save dicarlo2/3f35d9ef183a0cfe856997b7122a105a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("//packages/bazel/rules_common:defs.bzl", | |
"module_mappings_runtime_aspect", | |
"collect_node_modules_aspect", | |
"sources_aspect", | |
"test_sources_aspect", | |
"short_path_to_manifest_path", | |
"get_ci") | |
load("//packages/bazel/rules_nodejs/internal/node:node.bzl", "nodejs_binary_impl", | |
"NODE_EXECUTABLE_OUTPUTS", "BASE_NODE_EXECUTABLE_ATTRS", "NODE_TOOLCHAINS") | |
def _collect_test_sources(ctx): | |
return depset(transitive=[d.node_test_sources for d in ctx.attr.data if hasattr(d, "node_test_sources")]) | |
def _node_jest_test_impl(ctx): | |
test_sources = _collect_test_sources(ctx).to_list() | |
ctx.actions.expand_template( | |
template=ctx.file._jest_template, | |
output=ctx.outputs.jest, | |
substitutions={ | |
"TEMPLATED_env": "\"" + ctx.attr.env + "\"", | |
"TEMPLATED_ci": "true" if get_ci(ctx) else "false", | |
"TEMPLATED_filePaths": "[" + ",\n ".join( | |
["\"" + f.short_path + "\"" for f in test_sources]) + "]", | |
"TEMPLATED_update": "true" if ctx.attr.update_snapshots else "false", | |
}, | |
) | |
return nodejs_binary_impl( | |
ctx, | |
entry_point=short_path_to_manifest_path( | |
ctx, ctx.outputs.jest.short_path), | |
files=[ctx.outputs.jest], | |
) | |
node_jest_test = rule( | |
_node_jest_test_impl, | |
attrs=dict(BASE_NODE_EXECUTABLE_ATTRS, **{ | |
"data": attr.label_list( | |
allow_files=True, | |
aspects=[sources_aspect, test_sources_aspect, module_mappings_runtime_aspect, collect_node_modules_aspect]), | |
"env": attr.string( | |
default="node", | |
values=["node", "jsdom"] | |
), | |
"update_snapshots": attr.bool( | |
default=False, | |
), | |
"_jest_template": attr.label( | |
default=Label( | |
"//packages/bazel/rules_nodejs/internal/jest:jest.js"), | |
allow_files=True, | |
single_file=True), | |
}), | |
test=True, | |
outputs=dict(NODE_EXECUTABLE_OUTPUTS, **{ | |
"jest": "%{name}_jest.js", | |
}), | |
toolchains=NODE_TOOLCHAINS, | |
) | |
def _node_jest_test_macro_base(name, data=[], args=[], visibility=None, tags=[], **kwargs): | |
node_jest_test( | |
name="%s_bin" % name, | |
data=data + ["@bazel_tools//tools/bash/runfiles", | |
"@npm//jest", | |
"@npm//jest-cli", | |
"@npm//fs-extra"], | |
testonly=1, | |
tags=["manual"], | |
visibility=["//visibility:private"], | |
**kwargs | |
) | |
native.sh_test( | |
name=name, | |
args=args, | |
tags=tags, | |
visibility=visibility, | |
srcs=[":%s_bin.sh" % name], | |
data=[":%s_bin" % name], | |
) | |
def node_jest_test_macro(name, tags=[], **kwargs): | |
_node_jest_test_macro_base( | |
name=name, | |
tags=tags, | |
**kwargs | |
) | |
_node_jest_test_macro_base( | |
name="%s_update" % name, | |
update_snapshots=True, | |
tags=tags + ["manual", "update-jest-snapshot"], | |
**kwargs | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment