Skip to content

Commit

Permalink
Add support for $(location) in nodejs_binary#entry_point
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Oct 31, 2017
1 parent 490d9af commit 3bf2820
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ nodejs_binary(
"@//:node_modules",
"main.js",
],
entry_point = "workspace_name/main.js",
entry_point = "$(location :main.js)",
args = ["--node_options=--expose-gc"],
)
```
Expand Down
18 changes: 16 additions & 2 deletions examples/program/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test")
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test", "nodejs_binary")
load("@build_bazel_rules_nodejs//:internal/typescript_mock.bzl", "mock_typescript_lib")

# Make the jasmine library available at runtime by exposing our node_modules
Expand All @@ -25,9 +25,23 @@ filegroup(
srcs = ["index.js"],
)

nodejs_binary(
name = "bin",
entry_point = "$(location :index.js)",
data = [":program", ":index.js"],
node_modules = ":node_modules",
)

jasmine_node_test(
name = "test",
srcs = glob(["*.spec.js"]),
deps = [":program", ":mock_ts_lib"],
node_modules = "//:node_modules"
node_modules = ":node_modules",
)

py_test(
name = "bin_test",
srcs = ["bin_test.py"],
deps = ["@build_bazel_rules_nodejs//internal:runfiles"],
data = [":bin"],
)
18 changes: 18 additions & 0 deletions examples/program/bin_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from subprocess import check_output
from sys import platform
import unittest

from build_bazel_rules_nodejs.internal.runfiles import resolve_runfile

class BinTest(unittest.TestCase):

def testRuns(self):
if platform == "win32" or platform == "win64":
program = 'program_example/bin.exe'
else:
program = 'program_example/bin'
result = check_output([resolve_runfile(program)])
self.assertEqual(result, '2\n')

if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions examples/program/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ function increment(n) {
}

exports.increment = increment;

if (require.main === module) {
console.log(increment(1));
}
6 changes: 6 additions & 0 deletions internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ package(default_visibility = ["//visibility:public"])

exports_files(["node_launcher.sh", "node_loader.js", "jasmine_runner.js"])

py_library(
name = "runfiles",
srcs = ["runfiles.py"],
)

py_test(
name = "check_bazel_version_test",
srcs = ["check_bazel_version_test.py"],
deps = [":runfiles"],
data = [":check_bazel_version.bzl"],
size = "small",
)
14 changes: 1 addition & 13 deletions internal/check_bazel_version_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import os
import unittest

def resolve_runfile(path):
if os.getenv('RUNFILES_MANIFEST_ONLY') != "1":
return os.path.join(os.environ['TEST_SRCDIR'], path)

manifest = os.getenv('RUNFILES_MANIFEST_FILE')
with open(manifest) as f:
for line in f.readlines():
if line.split()[0] == path:
return line.split()[1]
raise "Cannot find %s in manifest %s" % (path, manifest)

from runfiles import resolve_runfile

class MockSkylark:
"""A class that returns mocked bazel version and fail().
Expand Down
3 changes: 1 addition & 2 deletions internal/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ def jasmine_node_test(name, srcs, data = [], deps = [], **kwargs):
data += srcs + deps
data += [Label("//internal:jasmine_runner.js")]
data += [":%s_devmode_srcs.MF" % name]
entry_point = "build_bazel_rules_nodejs/internal/jasmine_runner.js"

nodejs_test(
name = name,
data = data,
entry_point = entry_point,
entry_point = "$(location //internal:jasmine_runner.js)",
templated_args = ["$(location :%s_devmode_srcs.MF)" % name],
testonly = 1,
**kwargs
Expand Down
2 changes: 1 addition & 1 deletion internal/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _write_loader_script(ctx):
output=ctx.outputs.loader,
substitutions={
"TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings),
"TEMPLATED_entry_point": ctx.attr.entry_point,
"TEMPLATED_entry_point": expand_location_into_runfiles(ctx, ctx.attr.entry_point),
"TEMPLATED_label_package": ctx.attr.node_modules.label.package,
"TEMPLATED_workspace_name": (
ctx.label.workspace_root.split("/")[1]
Expand Down
12 changes: 12 additions & 0 deletions internal/runfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

def resolve_runfile(path):
if os.getenv('RUNFILES_MANIFEST_ONLY') != "1":
return os.path.join(os.environ['TEST_SRCDIR'], path)

manifest = os.getenv('RUNFILES_MANIFEST_FILE')
with open(manifest) as f:
for line in f.readlines():
if line.split()[0] == path:
return line.split()[1]
raise "Cannot find %s in manifest %s" % (path, manifest)

0 comments on commit 3bf2820

Please sign in to comment.