-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improved support for various execution contexts. * Move node_modules tree creation into own rule. * parse yarn modules: create a node_binary rules foreach executable target in the module. * Redo mocha_test as an sh_test. * Add the node_test rule.
- Loading branch information
Showing
37 changed files
with
1,233 additions
and
505 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
test_all: | ||
(cd tests/helloworld && bazel test //:helloworld_test) | ||
(cd tests/lyrics && bazel test //:lyrics_test) | ||
(cd tests/express && bazel test //:server_test) | ||
(cd tests/namespace && bazel test //:question_test) | ||
(cd tests/typescript && bazel test //:typescript_test) | ||
(cd tests/mocha && bazel test //:test) | ||
(cd tests/mocha && bazel test //tests:test) | ||
test_helloworld: | ||
(cd tests/helloworld && bazel test //...) | ||
|
||
test_lyrics: | ||
(cd tests/lyrics && bazel test //...) | ||
|
||
test_express: | ||
(cd tests/express && bazel test //...) | ||
|
||
test_namespace: | ||
(cd tests/namespace && bazel test //...) | ||
|
||
test_typescript: | ||
(cd tests/typescript && bazel test //...) | ||
|
||
test_webpack: | ||
(cd tests/webpack && bazel test //...) | ||
|
||
test_mocha: | ||
(cd tests/mocha && bazel test //...) | ||
|
||
test_all: test_helloworld test_lyrics test_express test_namespace test_typescript test_webpack test_mocha |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ workspace(name = "org_pubref_rules_node") | |
load("//node:rules.bzl", "node_repositories") | ||
|
||
node_repositories() | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files([ | ||
"internal/mocha_test.sh", | ||
]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,56 @@ | ||
load("//node:internal/node_module.bzl", "node_module") | ||
load("//node:internal/node_binary.bzl", "copy_modules", "binary_attrs") | ||
|
||
|
||
def _create_launcher(ctx, output_dir, node, mocha): | ||
entry_module = ctx.attr.entrypoint.node_module | ||
# if package is under root | ||
entrypoint = '%s_test/node_modules/%s' % (ctx.label.name, entry_module.name) | ||
# if test is under inner package | ||
if ctx.label.package: | ||
entrypoint = '%s/%s' % (ctx.label.package, entrypoint) | ||
cmd = [ | ||
node.short_path, | ||
] + ctx.attr.node_args + [ | ||
mocha.short_path, | ||
] + ctx.attr.mocha_args + [ | ||
entrypoint, | ||
] + ctx.attr.script_args + [ | ||
'$@', | ||
] | ||
|
||
lines = [ | ||
'#!/usr/bin/env bash', | ||
'set -e', | ||
' '.join(cmd) | ||
] | ||
ctx.file_action( | ||
output = ctx.outputs.executable, | ||
executable = True, | ||
content = '\n'.join(lines), | ||
load("//node:internal/node_modules.bzl", "node_modules") | ||
|
||
def mocha_test( | ||
name = None, | ||
# Main test script entrypoint | ||
main = None, | ||
# Additional module deps for the test | ||
deps = [], | ||
# The mocha binary executable target | ||
mocha_bin = "@mocha_modules//:mocha_bin", | ||
# The script runner for the sh_test | ||
script = "@org_pubref_rules_node//node:internal/mocha_test.sh", | ||
# Any additional args to pass directly to mocha | ||
args = [], | ||
# Test size | ||
size = "small", | ||
# Test visibility | ||
visibility = None, | ||
# Remainder of args go to 'node_module' | ||
**kwargs): | ||
|
||
"""Given a rule name and a main test script entrypoint file, package | ||
that test script up as a module, then package that module in a | ||
node_modules tree. Run a bash script that invokes the mocha_bin | ||
executable with the name of the testable entrypoint module. | ||
""" | ||
|
||
node_module( | ||
name = name + "_module", | ||
main = main, | ||
visibility = visibility, | ||
**kwargs | ||
) | ||
|
||
|
||
def mocha_test_impl(ctx): | ||
output_dir = ctx.label.name + '_test' | ||
node = ctx.executable._node | ||
mocha = ctx.executable._mocha_bin | ||
|
||
all_deps = ctx.attr.deps + [ctx.attr.entrypoint] | ||
files = copy_modules(ctx, output_dir, all_deps) | ||
|
||
_create_launcher(ctx, output_dir, node, mocha) | ||
|
||
mocha_deps_all = ctx.attr._mocha_deps.node_module | ||
transitive_mocha_files = mocha_deps_all.files.to_list() | ||
for dep in mocha_deps_all.transitive_deps: | ||
transitive_mocha_files += dep.files.to_list() | ||
|
||
runfiles = [ | ||
node, | ||
mocha, | ||
ctx.outputs.executable | ||
] + transitive_mocha_files + files | ||
|
||
return struct( | ||
runfiles = ctx.runfiles( | ||
files = runfiles, | ||
collect_data = True, | ||
), | ||
node_modules( | ||
name = name + "_modules", | ||
target = name + "_modules", | ||
visibility = visibility, | ||
deps = deps + [name + "_module"], | ||
) | ||
|
||
|
||
_mocha_test = rule( | ||
mocha_test_impl, | ||
attrs = binary_attrs + { | ||
"_mocha_bin": attr.label( | ||
default = Label("@mocha_modules//:mocha_bin"), | ||
allow_files = True, | ||
executable = True, | ||
cfg = "host", | ||
), | ||
"_mocha_deps": attr.label( | ||
providers = ["node_module"], | ||
default = Label("@mocha_modules//:_all_"), | ||
), | ||
"mocha_args": attr.string_list(), | ||
}, | ||
test = True, | ||
) | ||
|
||
|
||
def mocha_test(name = None, main = None, entrypoint = None, node_args = [], mocha_args = [], deps = [], visibility = None, size = "small", **kwargs): | ||
|
||
if not entrypoint: | ||
if not main: | ||
fail('Either an entrypoint node_module or a main script file must be specified') | ||
entrypoint = name + '_module' | ||
node_module( | ||
name = entrypoint, | ||
main = main, | ||
deps = [], | ||
visibility = visibility, | ||
**kwargs | ||
) | ||
|
||
_mocha_test( | ||
|
||
native.sh_test( | ||
name = name, | ||
entrypoint = entrypoint, | ||
deps = deps, | ||
srcs = [script], | ||
args = args + [ | ||
"{name}_modules/node_modules/{name}_module".format(name = name), | ||
], | ||
data = [ | ||
mocha_bin, | ||
name + "_modules", | ||
], | ||
size = size, | ||
node_args = node_args, | ||
mocha_args = mocha_args, | ||
visibility = visibility, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
set -eu | ||
external/mocha_modules/mocha_bin $@ |
Oops, something went wrong.