| load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "nodejs_binary", "nodejs_test", "npm_package_bin") |
| load("@npm//typescript:index.bzl", "tsc") |
| load("//:index.bzl", "js_library") |
| load("//internal/node:node_repositories.bzl", "BUILT_IN_NODE_PLATFORMS") |
| load("//packages/jasmine:index.bzl", "jasmine_node_test") |
| load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file") |
| load("//third_party/github.com/bazelbuild/bazel-skylib:rules/write_file.bzl", "write_file") |
| load(":nodejs_toolchain_test.bzl", "nodejs_toolchain_test") |
| |
| # You can have a nodejs_binary with no node_modules attribute |
| # and no fine grained deps |
| nodejs_binary( |
| name = "no_deps", |
| data = ["no-deps.js"], |
| entry_point = ":no-deps.js", |
| ) |
| |
| # You can have a nodejs_binary with a //:node_modules in deps |
| nodejs_binary( |
| name = "has_deps_legacy", |
| data = [ |
| "has-deps.js", |
| "@fine_grained_deps_yarn//:node_modules", |
| ], |
| entry_point = ":has-deps.js", |
| ) |
| |
| # You can have a nodejs_binary with no node_modules attribute |
| # and fine grained deps |
| nodejs_binary( |
| name = "has_deps", |
| data = [ |
| "has-deps.js", |
| "@fine_grained_deps_yarn//typescript", |
| ], |
| entry_point = ":has-deps.js", |
| ) |
| |
| # You can have a nodejs_binary with both a //:node_modules and |
| # and fine grained deps so long as they come from the same root |
| nodejs_binary( |
| name = "has_deps_hybrid", |
| data = [ |
| "has-deps.js", |
| "@fine_grained_deps_yarn//:node_modules", |
| "@fine_grained_deps_yarn//typescript", |
| ], |
| entry_point = ":has-deps.js", |
| ) |
| |
| filegroup( |
| name = "entry_file", |
| srcs = ["no-deps.js"], |
| ) |
| |
| nodejs_binary( |
| name = "has_entry_file", |
| entry_point = ":entry_file", |
| ) |
| |
| js_library( |
| name = "module_name_lib", |
| package_name = "build_bazel_rules_nodejs/internal/node/test", |
| srcs = ["module-name.js"], |
| ) |
| |
| nodejs_binary( |
| name = "module_name_test", |
| data = [":module_name_lib"], |
| entry_point = ":module-name.js", |
| ) |
| |
| jasmine_node_test( |
| name = "env_test", |
| srcs = [":env.spec.js"], |
| data = [ |
| ":dump_build_env.json", |
| ":dump_build_env_alt.json", |
| ":dump_build_env_attr_json", |
| ], |
| ) |
| |
| nodejs_test( |
| name = "define_var", |
| configuration_env_vars = [ |
| "SOME_TEST_ENV", |
| "SOME_OTHER_ENV", |
| ], |
| entry_point = ":define.spec.js", |
| ) |
| |
| nodejs_test( |
| name = "mode_resolution_test", |
| data = [ |
| "//internal/node/test/lib1", |
| "//internal/node/test/lib2", |
| "@npm//node_resolve_index", |
| "@npm//node_resolve_index_2", |
| "@npm//node_resolve_index_3", |
| "@npm//node_resolve_index_4", |
| "@npm//node_resolve_main", |
| "@npm//node_resolve_main_2", |
| "@npm//node_resolve_nested_main", |
| ], |
| entry_point = ":module_resolution.spec.js", |
| # this is a test for the patched module resolver |
| templated_args = ["--bazel_patch_module_resolver"], |
| ) |
| |
| nodejs_test( |
| name = "data_resolution_test", |
| data = [ |
| "data/data.json", |
| ], |
| entry_point = ":data_resolution.spec.js", |
| ) |
| |
| nodejs_test( |
| name = "bootstrap_legacy_location_test", |
| data = [ |
| "bootstrap.js", |
| "@npm//tmp", |
| ], |
| entry_point = ":bootstrap.spec.js", |
| # Also ensure that legacy "$(rlocation $(...))" usage is preserved so that users |
| # don't have to upgrade to "$$(rlocation $(...))" |
| templated_args = ["--node_options=--require=$(rlocation $(location :bootstrap.js))"], |
| ) |
| |
| nodejs_test( |
| name = "bootstrap_patch_test", |
| data = ["bootstrap_with_patch.js"], |
| entry_point = ":bootstrap.spec.js", |
| templated_args = ["--node_options=--require=$$(rlocation $(location :bootstrap_with_patch.js))"], |
| ) |
| |
| # Special case when $(location) is for a root file |
| nodejs_test( |
| name = "bootstrap_legacy_location_root_test", |
| data = ["//:bootstrap.js"], |
| entry_point = ":bootstrap.spec.js", |
| templated_args = ["--node_options=--require=$$(rlocation $(location //:bootstrap.js))"], |
| ) |
| |
| nodejs_test( |
| name = "bootstraps_legacy_location_test", |
| data = [ |
| "bootstrap1.js", |
| "bootstrap2.js", |
| ], |
| entry_point = ":bootstraps.spec.js", |
| templated_args = [ |
| "--node_options=--require=$$(rlocation $(location :bootstrap1.js))", |
| "--node_options=--require=$$(rlocation $(location :bootstrap2.js))", |
| ], |
| ) |
| |
| nodejs_test( |
| name = "bootstrap_rootpath_test", |
| data = [ |
| "bootstrap.js", |
| "@npm//tmp", |
| ], |
| entry_point = ":bootstrap.spec.js", |
| templated_args = ["--node_options=--require=$$(rlocation $(rootpath :bootstrap.js))"], |
| ) |
| |
| # Special case when $(rootpath) is for a root file |
| nodejs_test( |
| name = "bootstrap_rootpath_root_test", |
| data = ["//:bootstrap.js"], |
| entry_point = ":bootstrap.spec.js", |
| templated_args = ["--node_options=--require=$$(rlocation $(rootpath //:bootstrap.js))"], |
| ) |
| |
| nodejs_test( |
| name = "bootstraps_rootpath_test", |
| data = [ |
| "bootstrap1.js", |
| "bootstrap2.js", |
| ], |
| entry_point = ":bootstraps.spec.js", |
| templated_args = [ |
| "--node_options=--require=$$(rlocation $(rootpath :bootstrap1.js))", |
| "--node_options=--require=$$(rlocation $(rootpath :bootstrap2.js))", |
| ], |
| ) |
| |
| # Also test resolution from built files. |
| copy_file( |
| name = "module_resolution_lib", |
| src = "module_resolution.spec.js", |
| out = "module_resolution_built.spec.js", |
| ) |
| |
| copy_file( |
| name = "data_resolution_lib", |
| src = "data_resolution.spec.js", |
| out = "data_resolution_built.spec.js", |
| ) |
| |
| # This rule creates a file that alphabetically comes before any source file in this |
| # package. This genfile can be then set up as runfile to verify that the "loader.js" |
| # properly determines the local workspace path without incorrectly using the genfile as base |
| # for the local workspace path. See: https://github.com/bazelbuild/rules_nodejs/issues/352 |
| write_file( |
| name = "genfile-runfile", |
| out = "#LEADING.txt", |
| ) |
| |
| nodejs_test( |
| name = "mode_resolution_built_test", |
| data = [ |
| "//internal/node/test/lib1", |
| "//internal/node/test/lib2", |
| "@npm//node_resolve_index", |
| "@npm//node_resolve_index_2", |
| "@npm//node_resolve_index_3", |
| "@npm//node_resolve_index_4", |
| "@npm//node_resolve_main", |
| "@npm//node_resolve_main_2", |
| "@npm//node_resolve_nested_main", |
| ], |
| entry_point = ":module_resolution_built.spec.js", |
| # TODO: passes locally without this flag but fails on CircleCI |
| templated_args = ["--bazel_patch_module_resolver"], |
| ) |
| |
| nodejs_test( |
| name = "data_resolution_built_test", |
| data = [ |
| "data/data.json", |
| ":genfile-runfile", |
| ], |
| entry_point = ":data_resolution_built.spec.js", |
| # TODO: fails on Windows without this flag |
| templated_args = ["--bazel_patch_module_resolver"], |
| ) |
| |
| npm_package_bin( |
| name = "run_terser", |
| outs = ["minified.js"], |
| args = [ |
| "$(execpath terser_input.js)", |
| "--output", |
| "$@", |
| ], |
| data = ["terser_input.js"], |
| package = "terser", |
| ) |
| |
| # captures stdout of terser_input.js to minified.stdout.js (note args do not include --output flag) |
| npm_package_bin( |
| name = "run_terser_stdout", |
| args = [ |
| "$(execpath terser_input.js)", |
| ], |
| data = ["terser_input.js"], |
| package = "terser", |
| stdout = "minified.stdout.js", |
| ) |
| |
| generated_file_test( |
| name = "stdout_output_test", |
| src = "stdout_output_test.golden", |
| generated = ":minified.stdout.js", |
| ) |
| |
| # capture stderr to diagnostics.out, then analyze it in terser_diagnostics_stats printing some stats |
| # stdout is captured into greeter.min.js (again, without the --output $@ flags) |
| # the exit code is also capture to a file with exit_code_out, allowing downstream actions to read it |
| npm_package_bin( |
| name = "diagnostics_producing_bin", |
| args = [ |
| "$(execpath terser_input_with_diagnostics.js)", |
| "--compress", |
| "--verbose", |
| "--warn", |
| ], |
| data = ["terser_input_with_diagnostics.js"], |
| exit_code_out = "exit.code", |
| package = "terser", |
| stderr = "diagnostics.out", |
| stdout = "greeter.min.js", |
| ) |
| |
| nodejs_binary( |
| name = "terser_diagnostics_stats", |
| data = [ |
| "diagnostics_producing_bin", |
| "terser_diagnostics_stats.js", |
| ], |
| entry_point = "terser_diagnostics_stats.js", |
| ) |
| |
| # When we build with --define=VERBOSE_LOGS=1 there is some extra stuff printed to stderr |
| # We don't care to assert about that, and it has machine-local paths so we can't assert |
| # on the exact content either. |
| genrule( |
| name = "scrub_verbose_logs_stderr", |
| srcs = [":diagnostics.out"], |
| outs = [":diagnostics.scrubbed.out"], |
| cmd = "grep -v 'bazel node patches enabled' $< > $@", |
| ) |
| |
| generated_file_test( |
| name = "stderr_output_test", |
| src = "stderr_output_test.golden", |
| generated = ":diagnostics.scrubbed.out", |
| ) |
| |
| generated_file_test( |
| name = "exit_code_test", |
| src = "exit_code_test.golden", |
| generated = ":exit.code", |
| ) |
| |
| nodejs_binary( |
| name = "copy_to_directory", |
| entry_point = "copy_to_directory.js", |
| ) |
| |
| npm_package_bin( |
| name = "dir_output", |
| args = [ |
| "--output-dir", |
| "$(@D)", |
| "$(execpath terser_input.js)", |
| ], |
| data = ["terser_input.js"], |
| output_dir = True, |
| tool = ":copy_to_directory", |
| ) |
| |
| jasmine_node_test( |
| name = "npm_package_bin_test", |
| srcs = ["npm_package_bin.spec.js"], |
| data = [ |
| "dir_output", |
| "minified.js", |
| ], |
| ) |
| |
| [nodejs_toolchain_test( |
| name = "nodejs_toolchain_%s_test" % platform, |
| platform = platform, |
| # must be run with --platforms=@build_bazel_rules_nodejs//toolchains/node:PLATFORM |
| # where PLATFORM is one of BUILT_IN_NODE_PLATFORMS |
| tags = ["manual"], |
| ) for platform in BUILT_IN_NODE_PLATFORMS] |
| |
| nodejs_binary( |
| name = "expand_variables", |
| entry_point = "expand_variables.js", |
| ) |
| |
| npm_package_bin( |
| name = "expand_variables_ouput", |
| outs = ["expand_variables.out"], |
| args = [ |
| "$@", |
| "$(COMPILATION_MODE)", |
| "$(TARGET_CPU)", |
| "$(BINDIR)", |
| "$(SOME_TEST_ENV)", |
| "somearg$$", |
| "some0arg", |
| ], |
| tool = ":expand_variables", |
| ) |
| |
| jasmine_node_test( |
| name = "expand_variables_test", |
| srcs = [":expand_variables.spec.js"], |
| data = [":expand_variables.out"], |
| templated_args = [ |
| "$(rootpath :expand_variables.out)", |
| "$(COMPILATION_MODE)", |
| "$(TARGET_CPU)", |
| "$(BINDIR)", |
| "$(SOME_TEST_ENV)", |
| # Should preserve lone $ in templated_args |
| "somearg$", |
| "some$#arg", |
| ], |
| ) |
| |
| nodejs_binary( |
| name = "dump_build_env_attr", |
| data = ["dump_build_env.js"], |
| entry_point = "dump_build_env.js", |
| env = { |
| "LOC": "$(location :dump_build_env.js)", |
| }, |
| ) |
| |
| nodejs_binary( |
| name = "dump_build_env", |
| entry_point = "dump_build_env.js", |
| ) |
| |
| nodejs_binary( |
| name = "dump_build_env_alt", |
| data = ["@npm//tmp"], |
| entry_point = "dump_build_env.js", |
| ) |
| |
| npm_package_bin( |
| name = "dump_build_env_json", |
| outs = ["dump_build_env.json"], |
| args = ["$@"], |
| tool = ":dump_build_env", |
| ) |
| |
| npm_package_bin( |
| name = "dump_build_env_alt_json", |
| outs = ["dump_build_env_alt.json"], |
| args = ["$@"], |
| tool = ":dump_build_env_alt", |
| ) |
| |
| npm_package_bin( |
| name = "dump_build_env_attr_json", |
| outs = ["dump_build_env_attr.json"], |
| args = ["$@"], |
| env = { |
| "FOO": "BAR", |
| }, |
| tool = ":dump_build_env_attr", |
| ) |
| |
| nodejs_binary( |
| name = "test_runfiles_helper", |
| data = [":test_runfiles_helper.golden"], |
| entry_point = "test_runfiles_helper.js", |
| ) |
| |
| npm_package_bin( |
| name = "test_runfiles_helper_ouput", |
| outs = ["test_runfiles_helper.out"], |
| args = ["$@"], |
| tool = ":test_runfiles_helper", |
| ) |
| |
| generated_file_test( |
| name = "test_runfiles_helper_test", |
| src = "test_runfiles_helper.golden", |
| generated = ":test_runfiles_helper.out", |
| ) |
| |
| nodejs_test( |
| name = "fail_test", |
| entry_point = "fail.spec.js", |
| expected_exit_code = 55, |
| ) |
| |
| nodejs_test( |
| name = "fail_bootstrap_fail_test", |
| data = ["bootstrap_fail.js"], |
| entry_point = "fail.spec.js", |
| expected_exit_code = 33, |
| templated_args = ["--node_options=--require=$$(rlocation $(location :bootstrap_fail.js))"], |
| ) |
| |
| nodejs_test( |
| name = "fail_bootstrap_test", |
| data = [ |
| "bootstrap.js", |
| "@npm//tmp", |
| ], |
| entry_point = "fail.spec.js", |
| expected_exit_code = 55, |
| templated_args = ["--node_options=--require=$$(rlocation $(location :bootstrap.js))"], |
| ) |
| |
| nodejs_test( |
| name = "fail_bootstrap_with_patch_test", |
| data = ["bootstrap_with_patch.js"], |
| entry_point = "fail.spec.js", |
| expected_exit_code = 55, |
| templated_args = ["--node_options=--require=$$(rlocation $(location :bootstrap_with_patch.js))"], |
| ) |
| |
| nodejs_test( |
| name = "binary_with_no_arguments", |
| data = ["empty_args_fail.js"], |
| entry_point = "empty_args_fail.js", |
| expected_exit_code = 0, |
| ) |
| |
| tsc( |
| name = "main_lib", |
| outs = [ |
| "main.js", |
| ], |
| args = [ |
| "-p", |
| "$(execpath tsconfig.json)", |
| "--outDir", |
| # $(RULEDIR) is a shorthand for the dist/bin directory where Bazel requires we write outputs |
| "$(RULEDIR)", |
| ], |
| data = [ |
| "main.ts", |
| "tsconfig.json", |
| ], |
| ) |
| |
| nodejs_test( |
| name = "main_test", |
| data = [":main_lib"], |
| entry_point = ":main.js", |
| ) |
| |
| # Test that we can run a nodejs_binary with --bazel_patch_module_resolver with exports_directories_only = True |
| nodejs_binary( |
| name = "protractor_directory_artifacts_version", |
| data = ["@npm_directory_artifacts//protractor"], |
| entry_point = {"@npm_directory_artifacts//:node_modules/protractor": "bin/protractor"}, |
| tags = ["requires-runfiles"], |
| templated_args = [ |
| "--bazel_patch_module_resolver", |
| "--version", |
| ], |
| ) |
| |
| npm_package_bin( |
| name = "protractor_directory_artifacts_version_output", |
| stdout = "protractor_directory_artifacts_version.out", |
| tags = ["requires-runfiles"], |
| tool = ":protractor_directory_artifacts_version", |
| ) |
| |
| generated_file_test( |
| name = "protractor_directory_artifacts_version_test", |
| src = "protractor_directory_artifacts_version.golden", |
| generated = ":protractor_directory_artifacts_version.out", |
| tags = ["requires-runfiles"], |
| ) |