Skip to content

Commit

Permalink
Fixes and get all tests passing (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Jun 3, 2019
1 parent ed6a68c commit 0a63964
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 97 deletions.
10 changes: 0 additions & 10 deletions e2e/jasmine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
jasmine_node_test(
name = "test",
srcs = ["test.spec.js"],
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
],
)

jasmine_node_test(
name = "shared_env_test",
srcs = ["jasmine_shared_env_test.spec.js"],
bootstrap = ["e2e_jasmine/jasmine_shared_env_bootstrap.js"],
data = ["jasmine_shared_env_bootstrap.js"],
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
"@npm//jasmine",
"@npm//jasmine-core",
"@npm//zone.js",
Expand All @@ -30,8 +24,4 @@ jasmine_node_test(
"coverage_source.js",
],
coverage = True,
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
],
)
2 changes: 1 addition & 1 deletion e2e/ts_library/some_module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ nodejs_binary(
":main",
":some_module",
],
entry_point = ":main.js",
entry_point = ":main.ts",
)

sh_test(
Expand Down
19 changes: 1 addition & 18 deletions e2e/typescript_3.1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary", "rollup_bundle")
load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle")
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

Expand Down Expand Up @@ -57,23 +57,6 @@ jasmine_node_test(
],
)

nodejs_binary(
name = "main_js",
data = [
":main",
],
entry_point = "e2e_typescript_3_1/main.js",
)

nodejs_binary(
name = "main_js_sm",
data = [
":main",
"@npm//source-map-support",
],
entry_point = "e2e_typescript_3_1/main.js",
)

rollup_bundle(
name = "bundle",
entry_point = "main",
Expand Down
61 changes: 50 additions & 11 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def _write_loader_script(ctx):
if len(ctx.attr.entry_point.files) != 1:
fail("labels in entry_point must contain exactly one file")

entry_point_path = expand_path_into_runfiles(ctx, ctx.file.entry_point.short_path)

# If the entry point specified is a typescript file then set the entry
# point to the corresponding .js file
if entry_point_path.endswith(".ts"):
entry_point_path = entry_point_path[:-3] + ".js"

ctx.actions.expand_template(
template = ctx.file._loader_template,
output = ctx.outputs.loader,
Expand All @@ -97,7 +104,7 @@ def _write_loader_script(ctx):
"TEMPLATED_bootstrap": "\n " + ",\n ".join(
["\"" + d + "\"" for d in ctx.attr.bootstrap],
),
"TEMPLATED_entry_point": expand_path_into_runfiles(ctx, ctx.file.entry_point.short_path),
"TEMPLATED_entry_point": entry_point_path,
"TEMPLATED_gen_dir": ctx.genfiles_dir.path,
"TEMPLATED_install_source_map_support": str(ctx.attr.install_source_map_support).lower(),
"TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings),
Expand Down Expand Up @@ -172,7 +179,11 @@ def _nodejs_binary_impl(ctx):
is_executable = True,
)

runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args, ctx.file.entry_point] + ctx.files._node_runfiles, transitive = [sources])
runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args] + ctx.files._node_runfiles, transitive = [sources])

# entry point is only needed in runfiles if it is a .js file
if ctx.file.entry_point.short_path.endswith(".js"):
runfiles = depset([ctx.file.entry_point], transitive = [runfiles])

return [DefaultInfo(
executable = ctx.outputs.script,
Expand Down Expand Up @@ -215,32 +226,60 @@ _NODEJS_EXECUTABLE_ATTRS = {
"entry_point": attr.label(
doc = """The script which should be executed first, usually containing a main function.
The `entry_point` accepts a target's name as an entry point.
If the target is a rule, it should produce the JavaScript entry file that will be passed to the nodejs_binary rule).
If the entry JavaScript file belongs to the same package (as the BUILD file),
you can simply reference it by its relative name to the package directory:
```
nodejs_binary(
name = "my_binary",
...
entry_point = ":file.js",
)
```
You can specify the entry point as a typescript file so long as you also include
the ts_library target in data:
```
ts_library(
name = "main",
srcs = ["main.ts"],
)
nodejs_binary(
name = "bin",
data = [":main"]
entry_point = ":main.ts",
)
```
The rule will use the corresponding `.js` output of the ts_library rule as the entry point.
If the entry point target is a rule, it should produce the JavaScript entry file that will be passed to the nodejs_binary rule).
For example:
```
filegroup(
name = "entry_file",
srcs = ["workspace/path/to/entry/file"]
srcs = ["main.js"],
)
nodejs_binary(
name = "my_binary",
...
entry_point = ":entry_file",
)
```
If the entry JavaScript file belongs to the same package (as the BUILD file),
you can simply reference it by its relative name to the package directory:
The entry_point can also be a label in another workspace:
```
nodejs_binary(
name = "my_binary",
...
entry_point = ":file.js",
name = "history-server",
entry_point = "@npm//node_modules/history-server:modules/cli.js",
data = ["@npm//history-server"],
)
```
""",
mandatory = True,
allow_single_file = True,
Expand Down
2 changes: 1 addition & 1 deletion internal/npm_install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ nodejs_binary(
":browserify-wrapped.js",
"//third_party/github.com/browserify/browserify:sources",
],
entry_point = "build_bazel_rules_nodejs/internal/npm_install/browserify-wrapped.js",
entry_point = ":browserify-wrapped.js",
install_source_map_support = False,
visibility = ["//visibility:public"],
)
2 changes: 1 addition & 1 deletion packages/create/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ nodejs_test(
":npm_package",
"@npm//minimist",
],
entry_point = "build_bazel_rules_nodejs/packages/create/test.js",
entry_point = ":test.js",
)

# TODO(alexeagle): add e2e testing by running bazel in a newly created project
Expand Down
2 changes: 1 addition & 1 deletion packages/jasmine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ npm_package(
)

js_library(
name = "jasmine_runner",
name = "jasmine__pkg",
srcs = [
"index.js",
"src/jasmine_runner.js",
Expand Down
17 changes: 10 additions & 7 deletions packages/jasmine/src/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,25 @@ def jasmine_node_test(
expected_exit_code = 0,
tags = [],
coverage = False,
jasmine = "@npm//@bazel/jasmine",
jasmine = "@npm//node_modules/@bazel/jasmine:index.js",
**kwargs):
"""Runs tests in NodeJS using the Jasmine test runner.
To debug the test, see debugging notes in `nodejs_test`.
Args:
name: name of the resulting label
name: Name of the resulting label
srcs: JavaScript source files containing Jasmine specs
data: Runtime dependencies which will be loaded while the test executes
deps: Other targets which produce JavaScript, such as ts_library
expected_exit_code: The expected exit code for the test. Defaults to 0.
tags: bazel tags applied to test
jasmine: a label providing the @bazel/jasmine npm dependency
coverage: Enables code coverage collection and reporting
**kwargs: remaining arguments are passed to the test rule
tags: Bazel tags applied to test
coverage: Enables code coverage collection and reporting. Defaults to False.
jasmine: A label providing the @bazel/jasmine npm dependency. Defaults
to "@npm//@bazel/jasmine" so that the correct jasmine & jasmine-core
dependencies are resolved unless they are overwritten in a bootstrap
file.
**kwargs: Remaining arguments are passed to the test rule
"""
devmode_js_sources(
name = "%s_devmode_srcs" % name,
Expand All @@ -53,7 +56,7 @@ def jasmine_node_test(
tags = tags,
)

all_data = data + srcs + deps + [jasmine]
all_data = data + srcs + deps + [Label(jasmine).relative(":jasmine__pkg")]

all_data += [":%s_devmode_srcs.MF" % name]
all_data += [Label("@bazel_tools//tools/bash/runfiles")]
Expand Down
16 changes: 8 additions & 8 deletions packages/jasmine/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jasmine_node_test(
# target if they are not using the default @npm//@bazel/jasmine
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
],
)
Expand All @@ -23,7 +23,7 @@ jasmine_node_test(
# target if they are not using the default @npm//@bazel/jasmine
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
],
)
Expand All @@ -37,7 +37,7 @@ jasmine_node_test(
# target if they are not using the default @npm//@bazel/jasmine
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
],
)
Expand All @@ -51,7 +51,7 @@ jasmine_node_test(
# target if they are not using the default @npm//@bazel/jasmine
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
],
)
Expand All @@ -66,7 +66,7 @@ jasmine_node_test(
jasmine = "@npm_bazel_jasmine//:index.js",
shard_count = 3,
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
],
)
Expand All @@ -78,7 +78,7 @@ jasmine_node_test(
jasmine = "@npm_bazel_jasmine//:index.js",
shard_count = 2,
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
],
)
Expand All @@ -102,7 +102,7 @@ jasmine_node_test(
# to return a 'incomplete' status
tags = ["manual"],
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
],
)
Expand All @@ -116,7 +116,7 @@ jasmine_node_test(
coverage = True,
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"//:jasmine_runner",
"//:jasmine__pkg",
"@npm//jasmine",
"@npm//v8-coverage",
],
Expand Down
3 changes: 3 additions & 0 deletions packages/labs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"bugs": {
"url": "https://github.com/bazelbuild/rules_nodejs/issues"
},
"bin": {
"webpack": "./webpack/src/cli.js"
},
"devDependencies": {
"@bazel/typescript": "file:../../dist/npm_bazel_typescript",
"@types/jasmine": "^3.3.9",
Expand Down
13 changes: 11 additions & 2 deletions packages/labs/webpack/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BEGIN-INTERNAL
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary", "npm_package")

npm_package(
name = "webpack",
Expand All @@ -10,9 +10,18 @@ npm_package(
replacements = {"#@external\\s": ""},
visibility = ["//:__pkg__"],
deps = [
"//webpack/src:cli",
"//webpack/src:cli_lib",
"//webpack/src:package_contents",
],
)

nodejs_binary(
name = "cli",
data = [
"//webpack/src:cli_lib",
"@npm//webpack",
],
entry_point = "//webpack/src:cli.ts",
visibility = ["//visibility:public"],
)
# END-INTERNAL
26 changes: 3 additions & 23 deletions packages/labs/webpack/src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")

# BEGIN-INTERNAL
load("@npm_bazel_typescript//:defs.bzl", "ts_library")

exports_files(["cli.ts"])

ts_library(
name = "cli_lib",
srcs = glob(["*.ts"]),
visibility = ["//:__subpackages__"],
deps = [
"@npm//@types",
],
Expand All @@ -20,24 +21,3 @@ filegroup(
visibility = ["//webpack:__subpackages__"],
)
# END-INTERNAL

nodejs_binary(
name = "cli",
data = [
# BEGIN-INTERNAL
# For local development, we depend on the TypeScript output
":cli_lib",
# END-INTERNAL

# For external usage, we depend on the published .JS files
#@external "@npm//@bazel/labs",
"@npm//webpack",
],
# BEGIN-INTERNAL
# For local development, our module has this AMD name
entry_point = ":cli_lib",
# END-INTERNAL
# For external usage, we resolve the module from node_modules
#@external entry_point = "@bazel/labs/webpack/src:cli_lib",
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 0a63964

Please sign in to comment.