Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Adds ts_web_test_suite using rules_webtesting web_test_suite & browsers
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 196145021
  • Loading branch information
gregmagolan authored and alexeagle committed May 10, 2018
1 parent c7b6880 commit d25f3ce
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 154 deletions.
9 changes: 7 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ platforms:
build_targets:
- "..."
test_flags:
- "--test_tag_filters=-browser:Chrome"
# TODO(gregmagolan): shared libs needed by chrome & firefox not available on ubuntu1404
- "--test_tag_filters=-browser:chromium-local,-browser:firefox-local"
test_targets:
- "..."
ubuntu1604:
Expand All @@ -15,14 +16,18 @@ platforms:
build_targets:
- "..."
test_flags:
- "--test_tag_filters=-browser:Chrome"
# TODO(gregmagolan): shared libs needed by chrome & firefox not available on ubuntu1604
- "--test_tag_filters=-browser:chromium-local,-browser:firefox-local"
test_targets:
- "..."
macos:
run_targets:
- "@yarn//:yarn"
build_targets:
- "..."
test_flags:
# TODO(gregmagolan): firefox unknown breakage on macos target here; does work locally on mac
- "--test_tag_filters=-browser:firefox-local"
test_targets:
- "..."
windows:
Expand Down
2 changes: 1 addition & 1 deletion .ci/rules_typescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"parameters": {
"configure": ["$BAZEL run @yarn//:yarn"],
"test_tag_filters": ["-browser:Chrome"]
"test_tag_filters": ["-browser:chromium-local"]
}
},
{
Expand Down
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ skylark_doc(
"//internal:ts_config.bzl",
"//internal/devserver:ts_devserver.bzl",
"//internal/karma:ts_web_test.bzl",
"//internal/karma:ts_web_test_suite.bzl",
"//internal/protobufjs:ts_proto_library.bzl",
],
format = "html",
Expand Down
61 changes: 43 additions & 18 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ workspace(name = "build_bazel_rules_typescript")

http_archive(
name = "build_bazel_rules_nodejs",
url = "https://github.com/bazelbuild/rules_nodejs/archive/092404e3b47e1144ecfc2937d3729b717b1052bf.zip",
strip_prefix = "rules_nodejs-092404e3b47e1144ecfc2937d3729b717b1052bf",
sha256 = "5e3dd3f76a043687939a14ce6aee3049f8bd97d2cd885ef2105ac344d05213a3",
url = "https://github.com/bazelbuild/rules_nodejs/archive/0.8.0.zip",
strip_prefix = "rules_nodejs-0.8.0",
sha256 = "4e40dd49ae7668d245c3107645f2a138660fcfd975b9310b91eda13f0c973953",
)

load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")
Expand All @@ -37,16 +37,29 @@ yarn_install(
# @build_bazel_rules_typescript_node//:bin/npm
# - The yarn package manager:
# @yarn//:yarn
node_repositories(package_json = ["//:package.json"])
node_repositories(
package_json = ["//:package.json"],
preserve_symlinks = True)

load("@build_bazel_rules_typescript//:defs.bzl", "ts_setup_workspace")
http_archive(
name = "io_bazel_rules_go",
sha256 = "feba3278c13cde8d67e341a837f69a029f698d7a27ddbb2a202be7a10b22142a",
urls = [
"http://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/0.10.3/rules_go-0.10.3.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/0.10.3/rules_go-0.10.3.tar.gz"
],
)

ts_setup_workspace()
http_archive(
name = "io_bazel",
url = "https://github.com/bazelbuild/bazel/releases/download/0.9.0/bazel-0.9.0-dist.zip",
sha256 = "efb28fed4ffcfaee653e0657f6500fc4cbac61e32104f4208da385676e76312a",
)

http_archive(
name = "io_bazel_rules_go",
url = "https://github.com/bazelbuild/rules_go/releases/download/0.7.1/rules_go-0.7.1.tar.gz",
sha256 = "341d5eacef704415386974bc82a1783a8b7ffbff2ab6ba02375e1ca20d9b031c",
name = "bazel_gazelle",
sha256 = "d03625db67e9fb0905bbd206fa97e32ae9da894fe234a493e7517fd25faec914",
url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.1/bazel-gazelle-0.10.1.tar.gz",
)

load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
Expand All @@ -55,12 +68,30 @@ go_rules_dependencies()

go_register_toolchains()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()

http_archive(
name = "io_bazel",
url = "https://github.com/bazelbuild/bazel/releases/download/0.9.0/bazel-0.9.0-dist.zip",
sha256 = "efb28fed4ffcfaee653e0657f6500fc4cbac61e32104f4208da385676e76312a",
name = "io_bazel_rules_webtesting",
url = "https://github.com/bazelbuild/rules_webtesting/archive/ca7b8062d9cf4ef2fde9193c7d37a0764c4262d7.zip",
strip_prefix = "rules_webtesting-ca7b8062d9cf4ef2fde9193c7d37a0764c4262d7",
sha256 = "28c73cf9d310fa6dba30e66bdb98071341c99c3feb8662f2d3883a632de97d72",
)

load("@io_bazel_rules_webtesting//web:repositories.bzl", "browser_repositories", "web_test_repositories")

web_test_repositories()

browser_repositories(
chromium = True,
firefox = True,
)

load("@build_bazel_rules_typescript//:defs.bzl", "ts_setup_workspace")

ts_setup_workspace()

#############################################
# Dependencies for generating documentation #
#############################################
Expand All @@ -74,12 +105,6 @@ http_archive(
load("@io_bazel_rules_sass//sass:sass.bzl", "sass_repositories")
sass_repositories()

http_archive(
name = "bazel_skylib",
url = "https://github.com/bazelbuild/bazel-skylib/archive/0.3.1.zip",
strip_prefix = "bazel-skylib-0.3.1",
)

http_archive(
name = "io_bazel_skydoc",
url = "https://github.com/bazelbuild/skydoc/archive/0ef7695c9d70084946a3e99b89ad5a99ede79580.zip",
Expand Down
4 changes: 3 additions & 1 deletion defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ load("//internal:build_defs.bzl", _ts_library = "ts_library")
load("//internal:ts_config.bzl", _ts_config = "ts_config")
load("//internal/devserver:ts_devserver.bzl", _ts_devserver = "ts_devserver_macro")
load("//internal/karma:ts_web_test.bzl", _ts_web_test = "ts_web_test_macro")
load("//internal/karma:ts_web_test_suite.bzl", _ts_web_test_suite = "ts_web_test_suite")
load("//internal/protobufjs:ts_proto_library.bzl", _ts_proto_library = "ts_proto_library")

ts_setup_workspace = _ts_setup_workspace
ts_library = _ts_library
ts_config = _ts_config
ts_devserver = _ts_devserver
# TODO(alexeagle): make ts_web_test work in google3
# TODO(alexeagle): make ts_web_test && ts_web_test_suite work in google3
ts_web_test = _ts_web_test
ts_web_test_suite = _ts_web_test_suite
ts_proto_library = _ts_proto_library
# DO NOT ADD MORE rules here unless they appear in the generated docsite.
# Run yarn skydoc to re-generate the docsite.
8 changes: 6 additions & 2 deletions examples/protocol_buffers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@build_bazel_rules_typescript//:defs.bzl",
"ts_library",
"ts_proto_library",
"ts_web_test",
"ts_web_test_suite",
"ts_devserver",
)

Expand Down Expand Up @@ -31,10 +31,14 @@ ts_library(
deps = [":car"],
)

ts_web_test(
ts_web_test_suite(
name = "test",
deps = ["test_lib"],
bootstrap = ["@build_bazel_rules_typescript//:protobufjs_bootstrap_scripts"],
browsers = [
"@io_bazel_rules_webtesting//browsers:chromium-local",
"@io_bazel_rules_webtesting//browsers:firefox-local",
],
)

ts_library(
Expand Down
8 changes: 6 additions & 2 deletions examples/testing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test")
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test_suite")

ts_library(
name = "lib",
Expand All @@ -12,9 +12,13 @@ ts_library(
testonly = 1,
)

ts_web_test(
ts_web_test_suite(
name = "testing",
deps = [
":tests",
],
browsers = [
"@io_bazel_rules_webtesting//browsers:chromium-local",
"@io_bazel_rules_webtesting//browsers:firefox-local",
],
)
1 change: 1 addition & 0 deletions internal/karma/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports_files([
"karma.conf.js",
# Exported to be consumed for generating skydoc.
"ts_web_test.bzl",
"ts_web_test_suite.bzl",
])

load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
Expand Down
30 changes: 23 additions & 7 deletions internal/karma/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@ const path = require('path');
const fs = require('fs');
const tmp = require('tmp');

// When karma is configured to use Chrome it will look for a CHROME_BIN
// environment variable. This line points Karma to use puppeteer instead.
// See
// https://github.com/karma-runner/karma-chrome-launcher/blob/master/README.md#headless-chromium-with-puppeteer
process.env.CHROME_BIN = require('puppeteer').executablePath();
const browsers = [];
if (process.env['WEB_TEST_METADATA']) {
const webTestMetadata = require(process.env['WEB_TEST_METADATA']);
const webTestNamedFiles = webTestMetadata['webTestFiles'][0]['namedFiles'];
if (webTestNamedFiles['CHROMIUM']) {
// When karma is configured to use Chrome it will look for a CHROME_BIN
// environment variable.
process.env.CHROME_BIN = path.join("external", webTestNamedFiles['CHROMIUM']);
browsers.push(process.env['DISPLAY'] ? 'Chrome': 'ChromeHeadless');
}
if (webTestNamedFiles['FIREFOX']) {
// When karma is configured to use Firefox it will look for a FIREFOX_BIN
// environment variable.
process.env.FIREFOX_BIN = path.join("external", webTestNamedFiles['FIREFOX']);
browsers.push(process.env['DISPLAY'] ? 'Firefox': 'FirefoxHeadless');
}
}
if (!browsers.length) {
console.warn('No browsers configured. Configuring Karma to use system Chrome.');
browsers.push(process.env['DISPLAY'] ? 'Chrome': 'ChromeHeadless');
}

let files = [
TMPL_bootstrap_files
Expand Down Expand Up @@ -70,7 +86,7 @@ module.exports = function(config) {
}

config.set({
plugins: ['karma-*', 'karma-concat-js', 'karma-sourcemap-loader'],
plugins: ['karma-*', 'karma-concat-js', 'karma-sourcemap-loader', 'karma-chrome-launcher', 'karma-firefox-launcher'],
preprocessors: {
'**/*.js': ['sourcemap']
},
Expand Down Expand Up @@ -100,7 +116,7 @@ module.exports = function(config) {

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: [process.env['DISPLAY'] ? 'TMPL_browser': 'TMPL_headlessbrowser'],
browsers: browsers,

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
Expand Down
4 changes: 2 additions & 2 deletions internal/karma/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"description": "runtime dependences for ts_web_test rules",
"description": "runtime dependences for web test rules",
"devDependencies": {
"jasmine-core": "2.8.0",
"karma": "alexeagle/karma#fa1a84ac881485b5657cb669e9b4e5da77b79f0a",
"karma-chrome-launcher": "2.2.0",
"karma-firefox-launcher": "1.1.0",
"karma-jasmine": "1.1.1",
"karma-sourcemap-loader": "0.3.7",
"karma-requirejs": "1.1.0",
"puppeteer": "1.1.0",
"requirejs": "2.3.5",
"tmp": "0.0.33"
}
Expand Down
13 changes: 6 additions & 7 deletions internal/karma/ts_web_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 Chrome"
"Unit testing in with Karma"

load("@build_bazel_rules_nodejs//internal:node.bzl",
"sources_aspect",
Expand All @@ -20,8 +20,6 @@ load("@build_bazel_rules_nodejs//internal:node.bzl",
load("@build_bazel_rules_nodejs//internal/js_library:js_library.bzl", "write_amd_names_shim")

_CONF_TMPL = "//internal/karma:karma.conf.js"
# TODO(alexeagle): users will need some control over browser; needs design
_BROWSER = "Chrome"

def _ts_web_test_impl(ctx):
conf = ctx.actions.declare_file(
Expand Down Expand Up @@ -81,8 +79,6 @@ def _ts_web_test_impl(ctx):
"TMPL_bootstrap_files": "\n".join([" '%s'," % e for e in bootstrap_entries]),
"TMPL_user_files": "\n".join([" '%s'," % e for e in user_entries]),
"TMPL_workspace_name": ctx.workspace_name,
"TMPL_browser": _BROWSER,
"TMPL_headlessbrowser": "%sHeadless" % _BROWSER,
})

karma_executable_path = ctx.executable._karma.short_path
Expand Down Expand Up @@ -121,6 +117,7 @@ $KARMA ${{ARGV[@]}}
""".format(TMPL_karma = karma_executable_path,
TMPL_conf = conf.short_path))
return [DefaultInfo(
files = depset([ctx.outputs.executable]),
runfiles = ctx.runfiles(
files = ctx.files.srcs + ctx.files.deps + ctx.files.bootstrap + [
conf, amd_names_shim
Expand All @@ -130,11 +127,13 @@ $KARMA ${{ARGV[@]}}
collect_data = True,
collect_default = True,
),
executable = ctx.outputs.executable,
)]

ts_web_test = rule(
implementation = _ts_web_test_impl,
test = True,
executable = True,
attrs = {
"srcs": attr.label_list(
doc = "JavaScript source files",
Expand Down Expand Up @@ -166,7 +165,7 @@ ts_web_test = rule(
)
"""Runs unit tests in a browser.
When executed under `bazel test`, this uses a headless Chrome browser for speed.
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.
Expand Down Expand Up @@ -206,7 +205,7 @@ def ts_web_test_macro(tags = [], data = [], **kwargs):
# 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:%s" % _BROWSER,
"browser:chromium-system",
],
# Our binary dependency must be in data[] for collect_data to pick it up
# FIXME: maybe we can just ask the attr._karma for its runfiles attr
Expand Down
Loading

0 comments on commit d25f3ce

Please sign in to comment.