diff --git a/README.md b/README.md index cd4e446b59..e43daedff8 100644 --- a/README.md +++ b/README.md @@ -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"], ) ``` diff --git a/examples/program/BUILD.bazel b/examples/program/BUILD.bazel index 1ac14475e2..4ee7fa94dc 100644 --- a/examples/program/BUILD.bazel +++ b/examples/program/BUILD.bazel @@ -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 @@ -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"], ) diff --git a/examples/program/bin_test.py b/examples/program/bin_test.py new file mode 100644 index 0000000000..660a8b4c72 --- /dev/null +++ b/examples/program/bin_test.py @@ -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() diff --git a/examples/program/index.js b/examples/program/index.js index 88ade89fb7..97f6ab2e2a 100644 --- a/examples/program/index.js +++ b/examples/program/index.js @@ -3,3 +3,7 @@ function increment(n) { } exports.increment = increment; + +if (require.main === module) { + console.log(increment(1)); +} diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index bdf048cd89..9f7dc945c3 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -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", ) diff --git a/internal/check_bazel_version_test.py b/internal/check_bazel_version_test.py index 30672383af..620a3e9a11 100644 --- a/internal/check_bazel_version_test.py +++ b/internal/check_bazel_version_test.py @@ -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(). diff --git a/internal/jasmine_node_test.bzl b/internal/jasmine_node_test.bzl index 84ee3fb817..ab736a0f1e 100644 --- a/internal/jasmine_node_test.bzl +++ b/internal/jasmine_node_test.bzl @@ -9,14 +9,15 @@ def jasmine_node_test(name, srcs, data = [], deps = [], **kwargs): ) all_data = data + srcs + deps - all_data += [Label("//internal:jasmine_runner.js")] - all_data += [":%s_devmode_srcs.MF" % name] - entry_point = "build_bazel_rules_nodejs/internal/jasmine_runner.js" + all_data += [ + "//internal:jasmine_runner.js", + ":%s_devmode_srcs.MF" % name, + ] nodejs_test( name = name, data = all_data, - entry_point = entry_point, + entry_point = "$(location //internal:jasmine_runner.js)", templated_args = ["$(location :%s_devmode_srcs.MF)" % name], testonly = 1, **kwargs diff --git a/internal/node.bzl b/internal/node.bzl index 3e68ef9d32..8835509455 100644 --- a/internal/node.bzl +++ b/internal/node.bzl @@ -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] diff --git a/internal/runfiles.py b/internal/runfiles.py new file mode 100644 index 0000000000..cd2d1df55f --- /dev/null +++ b/internal/runfiles.py @@ -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)