blob: dfc36befdf3870f86960dbbdcec2f72a87cb9179 [file]
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
load(":rule.bzl", "linked")
# Use the node binary supplied by the bazel toolchain
# Normally the nodejs_binary rule would do this,
# but we want to have a more minimal test fixture
genrule(
name = "replace_node_path",
srcs = [":run_program.sh"],
outs = ["run_program_with_node.sh"],
# Remove leading external/ segment to account for --nolegacy_external_runfiles
cmd = "sed s#NODE_PATH#$(NODE_PATH)# $< | sed s#external/## > $@",
toolchains = ["@node16_toolchains//:resolved_toolchain"],
)
# A program from a *_test rule with a module mapped package that will
# by linked as "runfiles" mapping by downstream rules
sh_test(
name = "some_program",
srcs = ["some_program.sh"],
data = [
"//internal/linker/test/integration/transitive_static_linked_pkg",
],
)
# Make our program executable and include the linker
# The runfiles here are only the ones included with the program itself
sh_binary(
name = "run_program",
testonly = True,
srcs = ["run_program_with_node.sh"],
data = [
":program.js",
# Include `:some_program` as a data dep so we get the
# transitive_static_linked_pkg "runfiles" mapping transitively. This test verifies that
# `bazel-out/darwin-fastbuild/bin/internal/linker/test/integration/run_program.runfiles`
# also contains the runfiles from
# `bazel-out/darwin-fastbuild/bin/internal/linker/test/integration/some_program.runfiles`
# as it has `:some_program` as a data dep and that the linker will correctly link
# "runfiles" module mappings from `:some_program` in `:run_program` under the
":some_program",
"//internal/linker:index.js",
"//internal/runfiles:index.js",
"//internal/runfiles:runfile_helper_main.js",
"//internal/linker/test/integration/static_linked_pkg",
"//internal/linker/test/integration/static_linked_scoped_pkg",
"//internal/linker/test/integration/absolute_import:index.js",
"//third_party/github.com/bazelbuild/bazel/tools/bash/runfiles",
"@node16_toolchains//:resolved_toolchain",
],
)
# How a users rule might want to run a node program
linked(
name = "example",
testonly = True,
out = "actual",
program = ":run_program",
deps = [
":run_program",
# NB: Verify that a direct dep on :some_program and a transitive dep on
# the same via :run_program works with the linker. These should both
# bring in the transitive_static_linked mapping to
# ["runfiles", "build_bazel_rules_nodejs/internal/linker/test/integration/transitive_static_linked_pkg"].
# This tests that the linker does "runfiles" mappings for both *_binary target
# and *_test targets.
":some_program",
"//internal/linker/test/integration/dynamic_linked_pkg",
"//internal/linker/test/integration/dynamic_linked_scoped_pkg",
"@npm//semver",
],
)
# Test that the linker can handle duplicate module mappings. "src" and "bin"
# mappings will win over the "runfiles" mapping that comes from a dep on a
# *_binary or *_test rule.
linked(
name = "example_with_conflicts",
testonly = True,
out = "actual_with_conflicts",
program = ":run_program",
# do not sort
deps = [
# NB: reference the copy of index.js in the output folder
# Intentinally include this before static_linked_pkg as order matters for the linker.
# The order here exercises a different code path in the linker conflict resolution logic
# than `example_with_conflicts_alt`.
":run_program",
# NB: static_linked maps to both
# ["runfiles", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_pkg"] and
# ["bin", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_pkg"]
# as the "runfiles" mapping comes from `:run_program`
"//internal/linker/test/integration/static_linked_pkg",
# NB: @linker_scoped/static_linked maps to both
# ["runfiles", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_scoped_pkg"] and
# ["src", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_scoped_pkg"]
# as the "runfiles" mapping comes from `:run_program`
"//internal/linker/test/integration/static_linked_scoped_pkg",
"//internal/linker/test/integration/dynamic_linked_pkg",
"//internal/linker/test/integration/dynamic_linked_scoped_pkg",
"@npm//semver",
],
)
linked(
name = "example_with_conflicts_alt",
testonly = True,
out = "actual_with_conflicts_alt",
program = ":run_program",
# do not sort
deps = [
# NB: static_linked maps to both
# ["runfiles", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_pkg"] and
# ["bin", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_pkg"]
# as the "runfiles" mapping comes from `:run_program`
"//internal/linker/test/integration/static_linked_pkg",
# NB: @linker_scoped/static_linked maps to both
# ["runfiles", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_scoped_pkg"] and
# ["src", "build_bazel_rules_nodejs/internal/linker/test/integration/static_linked_scoped_pkg"]
# as the "runfiles" mapping comes from `:run_program`
"//internal/linker/test/integration/static_linked_scoped_pkg",
# Intentinally include this before static_linked_pkg as order matters for the linker.
# The order here exercises a different code path in the linker conflict resolution logic
# than `example_with_conflicts`.
":run_program",
"//internal/linker/test/integration/dynamic_linked_pkg",
"//internal/linker/test/integration/dynamic_linked_scoped_pkg",
"@npm//semver",
],
)
generated_file_test(
# default rule in this package
name = "integration",
src = "golden.txt",
generated = "actual",
)
generated_file_test(
# default rule in this package
name = "integration_conflicts",
src = "golden.txt",
generated = "actual_with_conflicts",
)
generated_file_test(
# default rule in this package
name = "integration_conflicts_alt",
src = "golden.txt",
generated = "actual_with_conflicts_alt",
)