From 8384562a1dcf2b1dbfc3f49403bca15ee56a7b17 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Sat, 9 Nov 2019 10:14:48 -0800 Subject: [PATCH] feat(karma): remove ts_web_test and ts_web_test_suite rules BREAKING CHANGES: The `ts_web_test` and `ts_web_test_suite` rules were duplicates of `karma_web_test` and `karma_web_test_suite` rules minus the `config_file` attribute. The `karma_web_test` and `karma_web_test_suite`, which have identical APIs, should now be used instead. They can be loaded from `load("@npm_bazel_karma//:index.bzl", "karma_web_test")` and `load("@npm_bazel_karma//:index.bzl", "karma_web_test_suite")`. --- examples/angular/README.md | 2 +- .../angular/src/app/hello-world/BUILD.bazel | 4 +- examples/angular/src/initialize_testbed.ts | 2 +- examples/angular/tools/defaults.bzl | 8 +- .../src/app/hello-world/BUILD.bazel | 4 +- .../src/initialize_testbed.ts | 2 +- .../angular_view_engine/tools/defaults.bzl | 8 +- examples/protocol_buffers/BUILD.bazel | 4 +- examples/protocol_buffers/defaults.bzl | 10 +- examples/web_testing/BUILD.bazel | 27 +-- examples/web_testing/defaults.bzl | 28 +-- packages/karma/src/BUILD.bazel | 2 - packages/karma/src/browser_repositories.bzl | 1 + packages/karma/src/index.bzl | 32 ++- packages/karma/src/index.from_src.bzl | 10 - packages/karma/src/karma_web_test.bzl | 75 +++---- packages/karma/src/ts_web_test.bzl | 206 ------------------ packages/karma/src/web_test.bzl | 40 ---- packages/karma/test/defaults.bzl | 10 +- packages/karma/test/karma/BUILD.bazel | 4 +- .../karma/test/karma_typescript/BUILD.bazel | 4 +- .../test/karma_typescript/user_files.spec.js | 2 +- packages/karma/test/stack_trace/BUILD.bazel | 4 +- .../labs/src/protobufjs/ts_proto_library.bzl | 10 +- .../protractor/src/browser_repositories.bzl | 1 + packages/protractor/src/protractor.conf.js | 4 +- 26 files changed, 109 insertions(+), 395 deletions(-) delete mode 100644 packages/karma/src/ts_web_test.bzl delete mode 100644 packages/karma/src/web_test.bzl diff --git a/examples/angular/README.md b/examples/angular/README.md index 35ced91751..727e06ccdc 100644 --- a/examples/angular/README.md +++ b/examples/angular/README.md @@ -79,7 +79,7 @@ $ bazel test //e2e/... ``` In this example, there is a unit test for the `hello-world` component which uses -the `ts_web_test_suite` rule. There are also protractor e2e tests for both the +the `karma_web_test_suite` rule. There are also protractor e2e tests for both the `prodserver` and `devserver` which use the `protractor_web_test_suite` rule. Note that Bazel will only re-run the tests whose inputs changed since the last run. diff --git a/examples/angular/src/app/hello-world/BUILD.bazel b/examples/angular/src/app/hello-world/BUILD.bazel index 0a831316b2..ea6a609ed9 100644 --- a/examples/angular/src/app/hello-world/BUILD.bazel +++ b/examples/angular/src/app/hello-world/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_sass//:defs.bzl", "sass_binary") load("@npm_angular_bazel//:index.bzl", "ng_module") load("@npm_bazel_typescript//:index.bzl", "ts_library") -load("//tools:defaults.bzl", "ts_web_test_suite") +load("//tools:defaults.bzl", "karma_web_test_suite") package(default_visibility = ["//:__subpackages__"]) @@ -52,7 +52,7 @@ ts_library( ], ) -ts_web_test_suite( +karma_web_test_suite( name = "test", srcs = [ # We are manaully adding the bazel generated named-UMD date-fns bundle here as diff --git a/examples/angular/src/initialize_testbed.ts b/examples/angular/src/initialize_testbed.ts index 25cca1d783..e55c05b188 100644 --- a/examples/angular/src/initialize_testbed.ts +++ b/examples/angular/src/initialize_testbed.ts @@ -1,6 +1,6 @@ /** * @fileoverview Provides a script to initialize TestBed before tests are run. - * This file should be included in the "runtime_deps" of a "ts_web_test_suite" + * This file should be included in the "runtime_deps" of a "karma_web_test_suite" * rule. */ import {TestBed} from '@angular/core/testing'; diff --git a/examples/angular/tools/defaults.bzl b/examples/angular/tools/defaults.bzl index 9262a75a22..23f18e9f19 100644 --- a/examples/angular/tools/defaults.bzl +++ b/examples/angular/tools/defaults.bzl @@ -1,9 +1,9 @@ "Set some defaults for karma rules" -load("@npm_bazel_karma//:index.bzl", _ts_web_test_suite = "ts_web_test_suite") +load("@npm_bazel_karma//:index.bzl", _karma_web_test_suite = "karma_web_test_suite") -def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): - _ts_web_test_suite( +def karma_web_test_suite(name, browsers = [], tags = [], **kwargs): + _karma_web_test_suite( name = name, tags = tags + ["native", "no-bazelci"], browsers = browsers, @@ -13,7 +13,7 @@ def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): # BazelCI docker images are missing shares libs to run a subset browser tests: # mac: firefox does not work, chrome works # ubuntu: firefox and chrome do not work --- there are 0 tests to run - _ts_web_test_suite( + _karma_web_test_suite( name = "bazelci_" + name, tags = tags + ["native", "no-circleci"], browsers = [ diff --git a/examples/angular_view_engine/src/app/hello-world/BUILD.bazel b/examples/angular_view_engine/src/app/hello-world/BUILD.bazel index 0a831316b2..ea6a609ed9 100644 --- a/examples/angular_view_engine/src/app/hello-world/BUILD.bazel +++ b/examples/angular_view_engine/src/app/hello-world/BUILD.bazel @@ -1,7 +1,7 @@ load("@io_bazel_rules_sass//:defs.bzl", "sass_binary") load("@npm_angular_bazel//:index.bzl", "ng_module") load("@npm_bazel_typescript//:index.bzl", "ts_library") -load("//tools:defaults.bzl", "ts_web_test_suite") +load("//tools:defaults.bzl", "karma_web_test_suite") package(default_visibility = ["//:__subpackages__"]) @@ -52,7 +52,7 @@ ts_library( ], ) -ts_web_test_suite( +karma_web_test_suite( name = "test", srcs = [ # We are manaully adding the bazel generated named-UMD date-fns bundle here as diff --git a/examples/angular_view_engine/src/initialize_testbed.ts b/examples/angular_view_engine/src/initialize_testbed.ts index 25cca1d783..e55c05b188 100644 --- a/examples/angular_view_engine/src/initialize_testbed.ts +++ b/examples/angular_view_engine/src/initialize_testbed.ts @@ -1,6 +1,6 @@ /** * @fileoverview Provides a script to initialize TestBed before tests are run. - * This file should be included in the "runtime_deps" of a "ts_web_test_suite" + * This file should be included in the "runtime_deps" of a "karma_web_test_suite" * rule. */ import {TestBed} from '@angular/core/testing'; diff --git a/examples/angular_view_engine/tools/defaults.bzl b/examples/angular_view_engine/tools/defaults.bzl index 9262a75a22..23f18e9f19 100644 --- a/examples/angular_view_engine/tools/defaults.bzl +++ b/examples/angular_view_engine/tools/defaults.bzl @@ -1,9 +1,9 @@ "Set some defaults for karma rules" -load("@npm_bazel_karma//:index.bzl", _ts_web_test_suite = "ts_web_test_suite") +load("@npm_bazel_karma//:index.bzl", _karma_web_test_suite = "karma_web_test_suite") -def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): - _ts_web_test_suite( +def karma_web_test_suite(name, browsers = [], tags = [], **kwargs): + _karma_web_test_suite( name = name, tags = tags + ["native", "no-bazelci"], browsers = browsers, @@ -13,7 +13,7 @@ def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): # BazelCI docker images are missing shares libs to run a subset browser tests: # mac: firefox does not work, chrome works # ubuntu: firefox and chrome do not work --- there are 0 tests to run - _ts_web_test_suite( + _karma_web_test_suite( name = "bazelci_" + name, tags = tags + ["native", "no-circleci"], browsers = [ diff --git a/examples/protocol_buffers/BUILD.bazel b/examples/protocol_buffers/BUILD.bazel index 00e513d4c5..56bf00c8c2 100644 --- a/examples/protocol_buffers/BUILD.bazel +++ b/examples/protocol_buffers/BUILD.bazel @@ -4,7 +4,7 @@ load("@npm_bazel_protractor//:index.bzl", "protractor_web_test_suite") load("@npm_bazel_rollup//:index.bzl", "rollup_bundle") load("@npm_bazel_terser//:index.bzl", "terser_minified") load("@npm_bazel_typescript//:index.bzl", "ts_config", "ts_devserver", "ts_library") -load("//:defaults.bzl", "ts_web_test_suite") +load("//:defaults.bzl", "karma_web_test_suite") proto_library( name = "tire_proto", @@ -45,7 +45,7 @@ ts_library( ], ) -ts_web_test_suite( +karma_web_test_suite( name = "test", bootstrap = ["@npm_bazel_labs//protobufjs:bootstrap_scripts"], browsers = [ diff --git a/examples/protocol_buffers/defaults.bzl b/examples/protocol_buffers/defaults.bzl index c7277bcf38..5a007be1de 100644 --- a/examples/protocol_buffers/defaults.bzl +++ b/examples/protocol_buffers/defaults.bzl @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Overrides for ts_web_test_suite to support bazelci testing +"""Overrides for karma_web_test_suite to support bazelci testing """ -load("@npm_bazel_karma//:index.bzl", _ts_web_test_suite = "ts_web_test_suite") +load("@npm_bazel_karma//:index.bzl", _karma_web_test_suite = "karma_web_test_suite") -def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): +def karma_web_test_suite(name, browsers = [], tags = [], **kwargs): # BazelCI docker images are missing shares libs to run a subset browser tests: # mac: firefox does not work, chrome works # ubuntu: firefox and chrome do not work --- there are 0 tests to run @@ -25,7 +25,7 @@ def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): # TODO(gregmagolan): fix underlying issue in bazelci and remove this macro # For CircleCI and local testing - _ts_web_test_suite( + _karma_web_test_suite( name = name, tags = tags + ["no-bazelci"], browsers = browsers, @@ -33,7 +33,7 @@ def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): ) # For BazelCI mac only - _ts_web_test_suite( + _karma_web_test_suite( name = "bazelci_chrome_" + name, tags = tags + ["no-circleci", "no-bazelci-ubuntu", "no-bazelci-windows", "no-local"], browsers = [ diff --git a/examples/web_testing/BUILD.bazel b/examples/web_testing/BUILD.bazel index 1e6be6c959..57c9ab0d4c 100644 --- a/examples/web_testing/BUILD.bazel +++ b/examples/web_testing/BUILD.bazel @@ -1,5 +1,5 @@ load("@npm_bazel_typescript//:index.bzl", "ts_config", "ts_library") -load("//:defaults.bzl", "karma_web_test_suite", "ts_web_test_suite") +load("//:defaults.bzl", "karma_web_test_suite") ts_library( name = "lib", @@ -36,7 +36,7 @@ ts_library( ) karma_web_test_suite( - name = "testing_karma", + name = "config_test", browsers = [ "@io_bazel_rules_webtesting//browsers:chromium-local", "@io_bazel_rules_webtesting//browsers:firefox-local", @@ -56,24 +56,7 @@ karma_web_test_suite( ) karma_web_test_suite( - name = "testing_karma_sauce", - browsers = [ - "@io_bazel_rules_webtesting//browsers/sauce:chrome-win10", - ], - tags = [ - "exclusive", - "sauce", - "native", - # TODO(alexeagle): enable on CI once we have set the SAUCE env variables - "manual", - ], - deps = [ - ":tests", - ], -) - -ts_web_test_suite( - name = "testing", + name = "no_config_test", browsers = [ "@io_bazel_rules_webtesting//browsers:chromium-local", "@io_bazel_rules_webtesting//browsers:firefox-local", @@ -90,8 +73,8 @@ ts_web_test_suite( ], ) -ts_web_test_suite( - name = "testing_sauce", +karma_web_test_suite( + name = "sauce_test", browsers = [ "@io_bazel_rules_webtesting//browsers/sauce:chrome-win10", ], diff --git a/examples/web_testing/defaults.bzl b/examples/web_testing/defaults.bzl index c96bc91ab4..93da9e9753 100644 --- a/examples/web_testing/defaults.bzl +++ b/examples/web_testing/defaults.bzl @@ -12,40 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Overrides for karma_web_test_suite & ts_web_test_suite to support bazelci testing +"""Overrides for karma_web_test_suite to support bazelci testing """ load( "@npm_bazel_karma//:index.bzl", _karma_web_test_suite = "karma_web_test_suite", - _ts_web_test_suite = "ts_web_test_suite", ) -def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): - # BazelCI docker images are missing shares libs to run a subset browser tests: - # mac: firefox does not work, chrome works - # ubuntu: firefox and chrome do not work --- there are 0 tests to run - # windows: firefox (disabled by rules_webtesting) and chrome do not work --- there are 0 tests to run - # TODO(gregmagolan): fix underlying issue in bazelci and remove this macro - - # For CircleCI and local testing - _ts_web_test_suite( - name = name, - tags = tags + ["no-bazelci"], - browsers = browsers, - **kwargs - ) - - # For BazelCI mac only - _ts_web_test_suite( - name = "bazelci_chrome_" + name, - tags = tags + ["no-circleci", "no-bazelci-ubuntu", "no-bazelci-windows", "no-local"], - browsers = [ - "@io_bazel_rules_webtesting//browsers:chromium-local", - ], - **kwargs - ) - def karma_web_test_suite(name, browsers = [], tags = [], **kwargs): # BazelCI docker images are missing shares libs to run a subset browser tests: # mac: firefox does not work, chrome works diff --git a/packages/karma/src/BUILD.bazel b/packages/karma/src/BUILD.bazel index 30363ee439..1c2b93547f 100644 --- a/packages/karma/src/BUILD.bazel +++ b/packages/karma/src/BUILD.bazel @@ -76,8 +76,6 @@ filegroup( "package.bzl", "package.json", "plugins.js", - "ts_web_test.bzl", - "web_test.bzl", ], ) diff --git a/packages/karma/src/browser_repositories.bzl b/packages/karma/src/browser_repositories.bzl index 9391d950a1..707ee1d347 100644 --- a/packages/karma/src/browser_repositories.bzl +++ b/packages/karma/src/browser_repositories.bzl @@ -17,6 +17,7 @@ load("@io_bazel_rules_webtesting//web/versioned:browsers-0.3.2.bzl", _browser_repositories = "browser_repositories") +# TODO(gregmagolan): remove this for 1.0 release def browser_repositories(): print(""" WARNING: @npm_bazel_karma//:browser_repositories.bzl is deprecated. diff --git a/packages/karma/src/index.bzl b/packages/karma/src/index.bzl index adbf15e5ba..49bfa7fd36 100644 --- a/packages/karma/src/index.bzl +++ b/packages/karma/src/index.bzl @@ -20,15 +20,33 @@ load( _karma_web_test = "karma_web_test", _karma_web_test_suite = "karma_web_test_suite", ) -load( - ":ts_web_test.bzl", - _ts_web_test = "ts_web_test", - _ts_web_test_suite = "ts_web_test_suite", -) -ts_web_test = _ts_web_test -ts_web_test_suite = _ts_web_test_suite karma_web_test = _karma_web_test karma_web_test_suite = _karma_web_test_suite # DO NOT ADD MORE rules here unless they appear in the generated docsite. # Run yarn skydoc to re-generate the docsite. + +# TODO(gregmagolan): remove ts_web_test & ts_web_test_suite entirely for 1.0 release +def ts_web_test(**kwargs): + """This rule has been removed. Replace with karma_web_test""" + + fail("""*********** + +The ts_web_test rule has been removed. + +The existing karma_web_test rule with an identical API should be used instead. +It can be loaded from `load("@npm_bazel_karma//:index.bzl", "karma_web_test")`. +************ +""") + +def ts_web_test_suite(**kwargs): + """This rule has been removed. Replace with ts_web_test_suite""" + + fail("""*********** + +The ts_web_test_suite rule has been removed. + +The existing karma_web_test_suite rule with an identical API should be used instead. +It can be loaded from `load("@npm_bazel_karma//:index.bzl", "karma_web_test_suite")`. +************ +""") diff --git a/packages/karma/src/index.from_src.bzl b/packages/karma/src/index.from_src.bzl index 3dae887250..d6cf7d9f7e 100644 --- a/packages/karma/src/index.from_src.bzl +++ b/packages/karma/src/index.from_src.bzl @@ -19,8 +19,6 @@ load( "@npm_bazel_karma//:index.bzl", _karma_web_test = "karma_web_test", _karma_web_test_suite = "karma_web_test_suite", - _ts_web_test = "ts_web_test", - _ts_web_test_suite = "ts_web_test_suite", ) INTERNAL_KARMA_BIN = "@npm_bazel_karma//:karma_bin" @@ -32,11 +30,3 @@ def karma_web_test(karma = INTERNAL_KARMA_BIN, **kwargs): def karma_web_test_suite(karma = INTERNAL_KARMA_BIN, **kwargs): data = kwargs.pop("data", []) + ["@npm_bazel_karma//:karma_plugins"] _karma_web_test_suite(karma = karma, data = data, **kwargs) - -def ts_web_test(karma = INTERNAL_KARMA_BIN, **kwargs): - data = kwargs.pop("data", []) + ["@npm_bazel_karma//:karma_plugins"] - _ts_web_test(karma = karma, data = data, **kwargs) - -def ts_web_test_suite(karma = INTERNAL_KARMA_BIN, **kwargs): - data = kwargs.pop("data", []) + ["@npm_bazel_karma//:karma_plugins"] - _ts_web_test_suite(karma = karma, data = data, **kwargs) diff --git a/packages/karma/src/karma_web_test.bzl b/packages/karma/src/karma_web_test.bzl index 34c79b658e..b481e64a6d 100644 --- a/packages/karma/src/karma_web_test.bzl +++ b/packages/karma/src/karma_web_test.bzl @@ -17,20 +17,38 @@ load("@build_bazel_rules_nodejs//:providers.bzl", "JSNamedModuleInfo", "NpmPacka load("@build_bazel_rules_nodejs//internal/js_library:js_library.bzl", "write_amd_names_shim") load("@io_bazel_rules_webtesting//web:web.bzl", "web_test_suite") load("@io_bazel_rules_webtesting//web/internal:constants.bzl", "DEFAULT_WRAPPED_TEST_TAGS") -load(":web_test.bzl", "COMMON_WEB_TEST_ATTRS") _CONF_TMPL = "//:karma.conf.js" _DEFAULT_KARMA_BIN = "@npm//@bazel/karma/bin:karma" -# Attributes for karma_web_test that are shared with ts_web_test which -# uses Karma under the hood -KARMA_GENERIC_WEB_TEST_ATTRS = dict(COMMON_WEB_TEST_ATTRS, **{ +KARMA_WEB_TEST_ATTRS = { + "srcs": attr.label_list( + doc = "A list of JavaScript test files", + allow_files = [".js"], + ), "bootstrap": attr.label_list( doc = """JavaScript files to include *before* the module loader (require.js). For example, you can include Reflect,js for TypeScript decorator metadata reflection, or UMD bundles for third-party libraries.""", allow_files = [".js"], ), + "config_file": attr.label( + doc = """User supplied Karma configuration file. Bazel will override + certain attributes of this configuration file. Attributes that are + overridden will be outputted to the test log.""", + allow_single_file = True, + ), + "configuration_env_vars": attr.string_list( + doc = """Pass these configuration environment variables to the resulting binary. + Chooses a subset of the configuration environment variables (taken from ctx.var), which also + includes anything specified via the --define flag. + Note, this can lead to different outputs produced by this rule.""", + default = [], + ), + "data": attr.label_list( + doc = "Runtime dependencies", + allow_files = True, + ), "karma": attr.label( doc = "karma binary label", default = Label(_DEFAULT_KARMA_BIN), @@ -52,21 +70,16 @@ KARMA_GENERIC_WEB_TEST_ATTRS = dict(COMMON_WEB_TEST_ATTRS, **{ allow_files = True, aspects = [node_modules_aspect], ), + "deps": attr.label_list( + doc = "Other targets which produce JavaScript such as `ts_library`", + allow_files = True, + aspects = [node_modules_aspect], + ), "_conf_tmpl": attr.label( default = Label(_CONF_TMPL), allow_single_file = True, ), -}) - -# Attributes for karma_web_test that are specific to karma_web_test -KARMA_WEB_TEST_ATTRS = dict(KARMA_GENERIC_WEB_TEST_ATTRS, **{ - "config_file": attr.label( - doc = """User supplied Karma configuration file. Bazel will override - certain attributes of this configuration file. Attributes that are - overridden will be outputted to the test log.""", - allow_single_file = True, - ), -}) +} # Avoid using non-normalized paths (workspace/../other_workspace/path) def _to_manifest_path(ctx, file): @@ -96,8 +109,7 @@ def _write_karma_config(ctx, files, amd_names_shim): config_file = None - # Check for config_file since ts_web_test does not have this attribute - if hasattr(ctx.attr, "config_file") and ctx.attr.config_file: + if ctx.attr.config_file: # TODO: switch to JSModuleInfo when it is available if JSNamedModuleInfo in ctx.attr.config_file: config_file = _filter_js(ctx.attr.config_file[JSNamedModuleInfo].direct_sources.to_list())[0] @@ -185,18 +197,7 @@ def _write_karma_config(ctx, files, amd_names_shim): return configuration -def run_karma_web_test(ctx): - """Internal utility for use by Bazel rule authors. - - Creates an action that can run karma. - This is also used by ts_web_test_rule. - - Args: - ctx: Bazel rule execution context - - Returns: - The runfiles for the generated action. - """ +def _karma_web_test_impl(ctx): files_depsets = [depset(ctx.files.srcs)] for dep in ctx.attr.deps + ctx.attr.runtime_deps: if JSNamedModuleInfo in dep: @@ -267,8 +268,7 @@ $KARMA ${{ARGV[@]}} config_sources = [] - # Check for config_file since ts_web_test does not have this attribute - if hasattr(ctx.attr, "config_file") and ctx.attr.config_file: + if ctx.attr.config_file: # TODO: switch to JSModuleInfo when it is available if JSNamedModuleInfo in ctx.attr.config_file: config_sources = ctx.attr.config_file[JSNamedModuleInfo].sources.to_list() @@ -287,17 +287,12 @@ $KARMA ${{ARGV[@]}} runfiles += ctx.files.static_files runfiles += ctx.files.data - return ctx.runfiles( - files = runfiles, - transitive_files = depset(transitive = [files, node_modules]), - ).merge(ctx.attr.karma[DefaultInfo].data_runfiles) - -def _karma_web_test_impl(ctx): - runfiles = run_karma_web_test(ctx) - return [DefaultInfo( files = depset([ctx.outputs.executable]), - runfiles = runfiles, + runfiles = ctx.runfiles( + files = runfiles, + transitive_files = depset(transitive = [files, node_modules]), + ).merge(ctx.attr.karma[DefaultInfo].data_runfiles), executable = ctx.outputs.executable, )] diff --git a/packages/karma/src/ts_web_test.bzl b/packages/karma/src/ts_web_test.bzl deleted file mode 100644 index f118310476..0000000000 --- a/packages/karma/src/ts_web_test.bzl +++ /dev/null @@ -1,206 +0,0 @@ -# Copyright 2017 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"Unit testing in a browser" - -load("@io_bazel_rules_webtesting//web:web.bzl", "web_test_suite") -load("@io_bazel_rules_webtesting//web/internal:constants.bzl", "DEFAULT_WRAPPED_TEST_TAGS") -load(":karma_web_test.bzl", "KARMA_GENERIC_WEB_TEST_ATTRS", "run_karma_web_test") - -# Using generic karma_web_test attributes under the hood -TS_WEB_TEST_ATTRS = dict(KARMA_GENERIC_WEB_TEST_ATTRS, **{}) - -def _ts_web_test_impl(ctx): - # Using karma_web_test under the hood - runfiles = run_karma_web_test(ctx) - - return [DefaultInfo( - files = depset([ctx.outputs.executable]), - runfiles = runfiles, - executable = ctx.outputs.executable, - )] - -_ts_web_test = rule( - implementation = _ts_web_test_impl, - test = True, - executable = True, - attrs = TS_WEB_TEST_ATTRS, -) - -def ts_web_test( - srcs = [], - deps = [], - data = [], - configuration_env_vars = [], - bootstrap = [], - runtime_deps = [], - static_files = [], - tags = [], - **kwargs): - """Runs unit tests in a browser. - - When executed under `bazel test`, this uses a headless browser for speed. - This is also because `bazel test` allows multiple targets to be tested together, - and we don't want to open a Chrome window on your machine for each one. Also, - under `bazel test` the test will execute and immediately terminate. - - Running under `ibazel test` gives you a "watch mode" for your tests. The rule is - optimized for this case - the test runner server will stay running and just - re-serve the up-to-date JavaScript source bundle. - - To debug a single test target, run it with `bazel run` instead. This will open a - browser window on your computer. Also you can use any other browser by opening - the URL printed when the test starts up. The test will remain running until you - cancel the `bazel run` command. - - This rule will use your system Chrome. Your environment must specify CHROME_BIN - so that the rule will know which Chrome binary to run. - - Currently this rule uses Karma as the test runner under the hood, but this is - an implementation detail. We might switch to another runner like Jest in the future. - - Args: - srcs: A list of JavaScript test files - deps: Other targets which produce JavaScript such as `ts_library` - data: Runtime dependencies - configuration_env_vars: Pass these configuration environment variables to the resulting binary. - Chooses a subset of the configuration environment variables (taken from ctx.var), which also - includes anything specified via the --define flag. - Note, this can lead to different outputs produced by this rule. - bootstrap: JavaScript files to include *before* the module loader (require.js). - For example, you can include Reflect,js for TypeScript decorator metadata reflection, - or UMD bundles for third-party libraries. - runtime_deps: Dependencies which should be loaded after the module loader but before the srcs and deps. - These should be a list of targets which produce JavaScript such as `ts_library`. - The files will be loaded in the same order they are declared by that rule. - static_files: Arbitrary files which are available to be served on request. - Files are served at: - `/base//`, e.g. - `/base/npm_bazel_typescript/examples/testing/static_script.js` - tags: Standard Bazel tags, this macro adds tags for ibazel support as well as - `browser:chromium-system` to allow for filtering on systems with no - system Chrome. - **kwargs: Passed through to `ts_web_test` - """ - - _ts_web_test( - srcs = srcs, - deps = deps, - data = data, - configuration_env_vars = configuration_env_vars, - bootstrap = bootstrap, - runtime_deps = runtime_deps, - static_files = static_files, - tags = tags + [ - # Users don't need to know that this tag is required to run under ibazel - "ibazel_notify_changes", - # Always attach this label to allow filtering, eg. envs w/ no browser - "browser:chromium-system", - ], - **kwargs - ) - -def ts_web_test_suite( - name, - browsers = None, - args = None, - browser_overrides = None, - config = None, - flaky = None, - local = None, - shard_count = None, - size = None, - tags = [], - test_suite_tags = None, - timeout = None, - visibility = None, - web_test_data = [], - wrapped_test_tags = None, - **remaining_keyword_args): - """Defines a test_suite of web_test targets that wrap a ts_web_test target. - - This macro also accepts all parameters in ts_web_test. See ts_web_test docs for - details. - - Args: - name: The base name of the test. - browsers: A sequence of labels specifying the browsers to use. - args: Args for web_test targets generated by this extension. - browser_overrides: Dictionary; optional; default is an empty dictionary. A - dictionary mapping from browser names to browser-specific web_test - attributes, such as shard_count, flakiness, timeout, etc. For example: - {'//browsers:chrome-native': {'shard_count': 3, 'flaky': 1} - '//browsers:firefox-native': {'shard_count': 1, 'timeout': 100}}. - config: Label; optional; Configuration of web test features. - flaky: A boolean specifying that the test is flaky. If set, the test will - be retried up to 3 times (default: 0) - local: boolean; optional. - shard_count: The number of test shards to use per browser. (default: 1) - size: A string specifying the test size. (default: 'large') - tags: A list of test tag strings to apply to each generated web_test_suite target. - This macro adds a couple for ibazel. - test_suite_tags: A list of tag strings for the generated test_suite. - timeout: A string specifying the test timeout (default: computed from size) - visibility: List of labels; optional. - web_test_data: Data dependencies for the web_test_suite. - wrapped_test_tags: A list of test tag strings to use for the wrapped test - **remaining_keyword_args: Arguments for the wrapped test target. - """ - - # Check explicitly for None so that users can set this to the empty list - if wrapped_test_tags == None: - wrapped_test_tags = DEFAULT_WRAPPED_TEST_TAGS - - if browsers == None: - browsers = ["@io_bazel_rules_webtesting//browsers:chromium-local"] - if not "native" in tags: - tags = tags + ["native"] - - size = size or "large" - - wrapped_test_name = name + "_wrapped_test" - - _ts_web_test( - name = wrapped_test_name, - args = args, - flaky = flaky, - local = local, - shard_count = shard_count, - size = size, - tags = wrapped_test_tags, - timeout = timeout, - visibility = ["//visibility:private"], - **remaining_keyword_args - ) - - web_test_suite( - name = name, - launcher = ":" + wrapped_test_name, - args = args, - browsers = browsers, - browser_overrides = browser_overrides, - config = config, - data = web_test_data, - flaky = flaky, - local = local, - shard_count = shard_count, - size = size, - tags = tags + [ - # Users don't need to know that this tag is required to run under ibazel - "ibazel_notify_changes", - ], - test = wrapped_test_name, - test_suite_tags = test_suite_tags, - timeout = timeout, - visibility = visibility, - ) diff --git a/packages/karma/src/web_test.bzl b/packages/karma/src/web_test.bzl deleted file mode 100644 index 13d3871a25..0000000000 --- a/packages/karma/src/web_test.bzl +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2017 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"Common web_test attributes" - -load("@build_bazel_rules_nodejs//:providers.bzl", "node_modules_aspect") - -# Attributes shared by any web_test rule (ts_web_test, karma_web_test, protractor_web_test) -COMMON_WEB_TEST_ATTRS = { - "srcs": attr.label_list( - doc = "A list of JavaScript test files", - allow_files = [".js"], - ), - "configuration_env_vars": attr.string_list( - doc = """Pass these configuration environment variables to the resulting binary. - Chooses a subset of the configuration environment variables (taken from ctx.var), which also - includes anything specified via the --define flag. - Note, this can lead to different outputs produced by this rule.""", - default = [], - ), - "data": attr.label_list( - doc = "Runtime dependencies", - allow_files = True, - ), - "deps": attr.label_list( - doc = "Other targets which produce JavaScript such as `ts_library`", - allow_files = True, - aspects = [node_modules_aspect], - ), -} diff --git a/packages/karma/test/defaults.bzl b/packages/karma/test/defaults.bzl index 77e1984f29..1a179c2eaf 100644 --- a/packages/karma/test/defaults.bzl +++ b/packages/karma/test/defaults.bzl @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Overrides for ts_web_test_suite to support bazelci testing +"""Overrides for karma_web_test_suite to support bazelci testing """ -load("@npm_bazel_karma//:index.from_src.bzl", _ts_web_test_suite = "ts_web_test_suite") +load("@npm_bazel_karma//:index.from_src.bzl", _karma_web_test_suite = "karma_web_test_suite") -def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): +def karma_web_test_suite(name, browsers = [], tags = [], **kwargs): # BazelCI docker images are missing shares libs to run a subset browser tests: # mac: firefox does not work, chrome works # ubuntu: firefox and chrome do not work --- there are 0 tests to run @@ -25,7 +25,7 @@ def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): # TODO(gregmagolan): fix underlying issue in bazelci and remove this macro # For CircleCI and local testing - _ts_web_test_suite( + _karma_web_test_suite( name = name, tags = tags + ["no-bazelci"], browsers = browsers, @@ -33,7 +33,7 @@ def ts_web_test_suite(name, browsers = [], tags = [], **kwargs): ) # For BazelCI mac only - _ts_web_test_suite( + _karma_web_test_suite( name = "bazelci_chrome_" + name, tags = tags + ["no-circleci", "no-bazelci-ubuntu", "no-bazelci-windows", "no-local"], browsers = [ diff --git a/packages/karma/test/karma/BUILD.bazel b/packages/karma/test/karma/BUILD.bazel index f6b50b3bc5..ec73a9231e 100644 --- a/packages/karma/test/karma/BUILD.bazel +++ b/packages/karma/test/karma/BUILD.bazel @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//packages/karma/test:defaults.bzl", "ts_web_test_suite") +load("//packages/karma/test:defaults.bzl", "karma_web_test_suite") -ts_web_test_suite( +karma_web_test_suite( name = "testing", srcs = glob( ["*.js"], diff --git a/packages/karma/test/karma_typescript/BUILD.bazel b/packages/karma/test/karma_typescript/BUILD.bazel index 66a8771f8f..99a7e21f1b 100644 --- a/packages/karma/test/karma_typescript/BUILD.bazel +++ b/packages/karma/test/karma_typescript/BUILD.bazel @@ -14,7 +14,7 @@ load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test") load("@npm_bazel_typescript//:index.from_src.bzl", "ts_library") -load("//packages/karma/test:defaults.bzl", "ts_web_test_suite") +load("//packages/karma/test:defaults.bzl", "karma_web_test_suite") ts_library( name = "lib", @@ -42,7 +42,7 @@ ts_library( ], ) -ts_web_test_suite( +karma_web_test_suite( name = "testing", srcs = [ # JS files only in srcs - eval'ed but not require'd diff --git a/packages/karma/test/karma_typescript/user_files.spec.js b/packages/karma/test/karma_typescript/user_files.spec.js index 48c1b72782..e411408594 100644 --- a/packages/karma/test/karma_typescript/user_files.spec.js +++ b/packages/karma/test/karma_typescript/user_files.spec.js @@ -1,6 +1,6 @@ const fs = require('fs'); -describe('ts_web_test_suite', () => { +describe('karma_web_test_suite', () => { let config; beforeAll(() => { diff --git a/packages/karma/test/stack_trace/BUILD.bazel b/packages/karma/test/stack_trace/BUILD.bazel index a1f3b9e143..78216606f3 100644 --- a/packages/karma/test/stack_trace/BUILD.bazel +++ b/packages/karma/test/stack_trace/BUILD.bazel @@ -13,7 +13,7 @@ # limitations under the License. load("@npm_bazel_typescript//:index.from_src.bzl", "ts_library") -load("//packages/karma/test:defaults.bzl", "ts_web_test_suite") +load("//packages/karma/test:defaults.bzl", "karma_web_test_suite") ts_library( name = "test_lib", @@ -27,7 +27,7 @@ ts_library( # This is a test with failing test. This test is not directly run by CI. # The sh_test below invokes this test and checks the source mapped lines in the # stack trace. -ts_web_test_suite( +karma_web_test_suite( name = "karma_test", browsers = [ "@io_bazel_rules_webtesting//browsers:chromium-local", diff --git a/packages/labs/src/protobufjs/ts_proto_library.bzl b/packages/labs/src/protobufjs/ts_proto_library.bzl index d6c781c5f9..661c126da7 100644 --- a/packages/labs/src/protobufjs/ts_proto_library.bzl +++ b/packages/labs/src/protobufjs/ts_proto_library.bzl @@ -190,17 +190,17 @@ result will be `car.d.ts`. This means our TypeScript code can just name the rule differently from the output file. The JavaScript produced by protobuf.js has a runtime dependency on a support library. -Under devmode (e.g. `ts_devserver`, `ts_web_test_suite`) you'll need to include these scripts +Under devmode (e.g. `ts_devserver`, `karma_web_test_suite`) you'll need to include these scripts in the `bootstrap` phase (before Require.js loads). You can use the label `@npm_bazel_labs//protobufjs:bootstrap_scripts` to reference these scripts -in the `bootstrap` attribute of `ts_web_test_suite` or `ts_devserver`. +in the `bootstrap` attribute of `karma_web_test_suite` or `ts_devserver`. -To complete the example above, you could write a `ts_web_test_suite`: +To complete the example above, you could write a `karma_web_test_suite`: ```python -load("@npm_bazel_karma//:index.bzl", "ts_web_test_suite") +load("@npm_bazel_karma//:index.bzl", "karma_web_test_suite") -ts_web_test_suite( +karma_web_test_suite( name = "test", deps = ["test_lib"], bootstrap = ["@npm_bazel_labs//protobufjs:bootstrap_scripts"], diff --git a/packages/protractor/src/browser_repositories.bzl b/packages/protractor/src/browser_repositories.bzl index 653a81d874..3e3e11f3d6 100644 --- a/packages/protractor/src/browser_repositories.bzl +++ b/packages/protractor/src/browser_repositories.bzl @@ -17,6 +17,7 @@ load("@io_bazel_rules_webtesting//web/versioned:browsers-0.3.2.bzl", _browser_repositories = "browser_repositories") +# TODO(gregmagolan): remove this for 1.0 release def browser_repositories(): print(""" WARNING: @npm_bazel_karma//:browser_repositories.bzl is deprecated. diff --git a/packages/protractor/src/protractor.conf.js b/packages/protractor/src/protractor.conf.js index 6f81d4052c..81dd2254d8 100644 --- a/packages/protractor/src/protractor.conf.js +++ b/packages/protractor/src/protractor.conf.js @@ -100,7 +100,7 @@ if (onPreparePath) { } // Override the user's base protractor configuration as appropriate based on the -// ts_web_test_suite & rules_webtesting WEB_TEST_METADATA attributes +// karma_web_test_suite & rules_webtesting WEB_TEST_METADATA attributes setConf(conf, 'framework', 'jasmine2', 'is set to jasmine2'); const specs = @@ -109,7 +109,7 @@ const specs = setConf(conf, 'specs', specs, 'are determined by the srcs and deps attribute'); // WEB_TEST_METADATA is configured in rules_webtesting based on value -// of the browsers attribute passed to ts_web_test_suite +// of the browsers attribute passed to karma_web_test_suite // We setup the protractor configuration based on the values in this object if (process.env['WEB_TEST_METADATA']) { const webTestMetadata = require(process.env['WEB_TEST_METADATA']);