refactor(bazel): remove @angular/bazel protractor rule now provided by @bazel/protractor (#32485)

BREAKING CHANGE:
Angular bazel users using protractor_web_test_suite from @angular/bazel npm package should now switch to the @bazel/protractor npm package.

This should impact very few users and the user's that are impacted have a very easy upgrade path to switch to fetching the protractor_web_test_suite rule via the @bazel/protractor npm package.

PR Close #32485
This commit is contained in:
Greg Magolan 2019-09-04 18:31:59 -07:00 committed by Matias Niemelä
parent 21245887e6
commit 9448828b0d
33 changed files with 0 additions and 1014 deletions

View File

@ -45,7 +45,6 @@ merge:
- "packages/bazel/src/api-extractor/**" - "packages/bazel/src/api-extractor/**"
- "packages/bazel/src/builders/**" - "packages/bazel/src/builders/**"
- "packages/bazel/src/ng_package/**" - "packages/bazel/src/ng_package/**"
- "packages/bazel/src/protractor/**"
- "packages/bazel/src/schematics/**" - "packages/bazel/src/schematics/**"
- "packages/compiler-cli/ngcc/**" - "packages/compiler-cli/ngcc/**"
- "packages/docs/**" - "packages/docs/**"

View File

@ -11,7 +11,6 @@ def benchmark_test(name, server, deps, tags = []):
name = name, name = name,
configuration = "//:protractor-perf.conf.js", configuration = "//:protractor-perf.conf.js",
data = [ data = [
"//packages/bazel/src/protractor/utils",
"//packages/benchpress", "//packages/benchpress",
], ],
on_prepare = "//modules/benchmarks:start-server.js", on_prepare = "//modules/benchmarks:start-server.js",

View File

@ -11,7 +11,6 @@ npm_package(
"//packages/bazel/src/builders:package_assets", "//packages/bazel/src/builders:package_assets",
"//packages/bazel/src/ng_package:package_assets", "//packages/bazel/src/ng_package:package_assets",
"//packages/bazel/src/ngc-wrapped:package_assets", "//packages/bazel/src/ngc-wrapped:package_assets",
"//packages/bazel/src/protractor:package_assets",
"//packages/bazel/src/schematics:package_assets", "//packages/bazel/src/schematics:package_assets",
"//packages/bazel/third_party/github.com/bazelbuild/bazel/src/main/protobuf:package_assets", "//packages/bazel/third_party/github.com/bazelbuild/bazel/src/main/protobuf:package_assets",
], ],
@ -32,7 +31,6 @@ npm_package(
"//packages/bazel/src/builders", "//packages/bazel/src/builders",
"//packages/bazel/src/ng_package:lib", "//packages/bazel/src/ng_package:lib",
"//packages/bazel/src/ngc-wrapped:ngc_lib", "//packages/bazel/src/ngc-wrapped:ngc_lib",
"//packages/bazel/src/protractor/utils",
"//packages/bazel/src/schematics/ng-add", "//packages/bazel/src/schematics/ng-add",
"//packages/bazel/src/schematics/ng-new", "//packages/bazel/src/schematics/ng-new",
], ],

View File

@ -6,7 +6,6 @@ skylark_doc(
"//packages/bazel/src:ng_module.bzl", "//packages/bazel/src:ng_module.bzl",
"//packages/bazel/src:ng_rollup_bundle.bzl", "//packages/bazel/src:ng_rollup_bundle.bzl",
"//packages/bazel/src/ng_package:ng_package.bzl", "//packages/bazel/src/ng_package:ng_package.bzl",
"//packages/bazel/src/protractor:protractor_web_test.bzl",
], ],
format = "html", format = "html",
site_root = "/bazel-builds", site_root = "/bazel-builds",

View File

@ -8,17 +8,10 @@ Users should not load files under "/src"
""" """
load("//packages/bazel/src/ng_package:ng_package.bzl", _ng_package = "ng_package") load("//packages/bazel/src/ng_package:ng_package.bzl", _ng_package = "ng_package")
load(
"//packages/bazel/src/protractor:protractor_web_test.bzl",
_protractor_web_test = "protractor_web_test",
_protractor_web_test_suite = "protractor_web_test_suite",
)
load("//packages/bazel/src:ng_module.bzl", _ng_module = "ng_module_macro") load("//packages/bazel/src:ng_module.bzl", _ng_module = "ng_module_macro")
ng_module = _ng_module ng_module = _ng_module
ng_package = _ng_package ng_package = _ng_package
protractor_web_test = _protractor_web_test
protractor_web_test_suite = _protractor_web_test_suite
# DO NOT ADD PUBLIC API without including in the documentation generation # DO NOT ADD PUBLIC API without including in the documentation generation
# Run `yarn bazel build //packages/bazel/docs` to verify # Run `yarn bazel build //packages/bazel/docs` to verify

View File

@ -1,9 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
module.exports = require('./src/protractor/utils');

View File

@ -1,11 +0,0 @@
package(default_visibility = ["//visibility:public"])
exports_files([
"protractor.conf.js",
"protractor_web_test.bzl",
])
filegroup(
name = "package_assets",
srcs = glob(["*"]),
)

View File

@ -1,177 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const DEBUG = false;
const configPath = 'TMPL_config';
const onPreparePath = 'TMPL_on_prepare';
const workspace = 'TMPL_workspace';
const server = 'TMPL_server';
if (DEBUG)
console.info(`Protractor test starting with:
cwd: ${process.cwd()}
configPath: ${configPath}
onPreparePath: ${onPreparePath}
workspace: ${workspace}
server: ${server}`);
// Helper function to warn when a user specified value is being overwritten
function setConf(conf, name, value, msg) {
if (conf[name] && conf[name] !== value) {
console.warn(
`Your protractor configuration specifies an option which is overwritten by Bazel: '${name}' ${msg}`);
}
conf[name] = value;
}
function mergeCapabilities(conf, capabilities) {
if (conf.capabilities) {
if (conf.capabilities.browserName === capabilities.browserName) {
// there are capabilities to merge
if (capabilities.browserName === 'chrome') {
conf.capabilities.chromeOptions = conf.capabilities.chromeOptions || {};
conf.capabilities.chromeOptions.binary = capabilities.chromeOptions.binary;
conf.capabilities.chromeOptions.args = conf.capabilities.chromeOptions.args || [];
conf.capabilities.chromeOptions.args.push(...capabilities.chromeOptions.args);
console.warn(
`Your protractor configuration specifies capabilities for browser '${conf.capabilities.browserName}'
which will be merged with capabilities provided by Bazel resulting in:`,
JSON.stringify(conf.capabilities, null, 2));
} else {
// TODO(gmagolan): implement firefox support for protractor
throw new Error(
`Unexpected browserName ${capabilities.browserName} for capabilities merging`);
}
} else {
console.warn(
`Your protractor configuration specifies capabilities for browser '${conf.capabilities.browserName}' which will be overwritten by Bazel`);
conf.capabilities = capabilities;
}
} else {
conf.capabilities = capabilities;
}
}
let conf = {};
// Import the user's base protractor configuration if specified
if (configPath) {
const baseConf = require(configPath);
if (!baseConf.config) {
throw new Error('Invalid base protractor configuration. Expected config to be exported.');
}
conf = baseConf.config;
if (DEBUG) console.info(`Base protractor configuration: ${JSON.stringify(conf, null, 2)}`);
}
// Import the user's on prepare function if specified
if (onPreparePath) {
const onPrepare = require(onPreparePath);
if (typeof onPrepare === 'function') {
const original = conf.onPrepare;
conf.onPrepare = function() {
return Promise.resolve(original ? original() : null)
.then(() => Promise.resolve(onPrepare({workspace, server})));
};
} else {
throw new Error(
'Invalid protractor on_prepare script. Expected a function as the default export.');
}
}
// Override the user's base protractor configuration as appropriate based on the
// ts_web_test_suite & rules_webtesting WEB_TEST_METADATA attributes
setConf(conf, 'framework', 'jasmine2', 'is set to jasmine2');
const specs =
[TMPL_specs].map(s => require.resolve(s)).filter(s => /(\b|_)(spec|test)\.js$/.test(s));
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
// 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']);
if (DEBUG) console.info(`WEB_TEST_METADATA: ${JSON.stringify(webTestMetadata, null, 2)}`);
if (webTestMetadata['environment'] === 'sauce') {
// If a sauce labs browser is chosen for the test such as
// "@io_bazel_rules_webtesting//browsers/sauce:chrome-win10"
// than the 'environment' will equal 'sauce'.
// We expect that a SAUCE_USERNAME and SAUCE_ACCESS_KEY is available
// from the environment for this test to run
// TODO(gmagolan): implement sauce labs support for protractor
throw new Error('Saucelabs not yet support by protractor_web_test_suite.');
// if (!process.env.SAUCE_USERNAME || !process.env.SAUCE_ACCESS_KEY) {
// console.error('Make sure the SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables are
// set.');
// process.exit(1);
// }
// setConf(conf, 'sauceUser', process.env.SAUCE_USERNAME, 'is determined by the SAUCE_USERNAME
// environment variable');
// setConf(conf, 'sauceKey', process.env.SAUCE_ACCESS_KEY, 'is determined by the
// SAUCE_ACCESS_KEY environment variable');
} else if (webTestMetadata['environment'] === 'local') {
// When a local chrome or firefox browser is chosen such as
// "@io_bazel_rules_webtesting//browsers:chromium-local" or
// "@io_bazel_rules_webtesting//browsers:firefox-local"
// then the 'environment' will equal 'local' and
// 'webTestFiles' will contain the path to the binary to use
const webTestNamedFiles = webTestMetadata['webTestFiles'][0]['namedFiles'];
const headless = !process.env['DISPLAY'];
if (webTestNamedFiles['CHROMIUM']) {
const chromeBin = require.resolve(webTestNamedFiles['CHROMIUM']);
const chromeDriver = require.resolve(webTestNamedFiles['CHROMEDRIVER']);
// The sandbox needs to be disabled, because it causes Chrome to crash on some environments.
// See: http://chromedriver.chromium.org/help/chrome-doesn-t-start
const args = ['--no-sandbox'];
if (headless) {
args.push('--headless', '--disable-gpu');
}
setConf(conf, 'directConnect', true, 'is set to true for chrome');
setConf(conf, 'chromeDriver', chromeDriver, 'is determined by the browsers attribute');
mergeCapabilities(conf, {
browserName: 'chrome',
chromeOptions: {
binary: chromeBin,
args: args,
}
});
}
if (webTestNamedFiles['FIREFOX']) {
// TODO(gmagolan): implement firefox support for protractor
throw new Error('Firefox not yet support by protractor_web_test_suite');
// const firefoxBin = require.resolve(webTestNamedFiles['FIREFOX'])
// const args = [];
// if (headless) {
// args.push("--headless")
// args.push("--marionette")
// }
// setConf(conf, 'seleniumAddress', process.env.WEB_TEST_HTTP_SERVER.trim() + "/wd/hub", 'is
// configured by Bazel for firefox browser')
// mergeCapabilities(conf, {
// browserName: "firefox",
// 'moz:firefoxOptions': {
// binary: firefoxBin,
// args: args,
// }
// }, 'is determined by the browsers attribute');
}
} else {
console.warn(`Unknown WEB_TEST_METADATA environment '${webTestMetadata['environment']}'`);
}
}
// Export the complete protractor configuration
if (DEBUG) console.info(`Protractor configuration: ${JSON.stringify(conf, null, 2)}`);
exports.config = conf;

View File

@ -1,354 +0,0 @@
# Copyright Google Inc. All Rights Reserved.
#
# Use of this source code is governed by an MIT-style license that can be
# found in the LICENSE file at https://angular.io/license
"Run end-to-end tests with Protractor"
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("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//internal/common:expand_into_runfiles.bzl", "expand_path_into_runfiles")
load("@build_bazel_rules_nodejs//internal/common:sources_aspect.bzl", "sources_aspect")
_CONF_TMPL = "//packages/bazel/src/protractor:protractor.conf.js"
def _short_path_to_manifest_path(ctx, short_path):
if short_path.startswith("../"):
return short_path[3:]
else:
return ctx.workspace_name + "/" + short_path
def _protractor_web_test_impl(ctx):
configuration = ctx.actions.declare_file(
"%s.conf.js" % ctx.label.name,
sibling = ctx.outputs.executable,
)
files = depset(ctx.files.srcs)
for d in ctx.attr.deps:
if hasattr(d, "node_sources"):
files = depset(transitive = [files, d.node_sources])
elif hasattr(d, "files"):
files = depset(transitive = [files, d.files])
specs = [
expand_path_into_runfiles(ctx, f.short_path)
for f in files.to_list()
]
configuration_sources = []
if ctx.file.configuration:
configuration_sources = [ctx.file.configuration]
if hasattr(ctx.attr.configuration, "node_sources"):
configuration_sources = ctx.attr.configuration.node_sources.to_list()
configuration_file = ctx.file.configuration
if hasattr(ctx.attr.configuration, "typescript"):
configuration_file = ctx.attr.configuration.typescript.es5_sources.to_list()[0]
on_prepare_sources = []
if ctx.file.on_prepare:
on_prepare_sources = [ctx.file.on_prepare]
if hasattr(ctx.attr.on_prepare, "node_sources"):
on_prepare_sources = ctx.attr.on_prepare.node_sources.to_list()
on_prepare_file = ctx.file.on_prepare
if hasattr(ctx.attr.on_prepare, "typescript"):
on_prepare_file = ctx.attr.on_prepare.typescript.es5_sources.to_list()[0]
ctx.actions.expand_template(
output = configuration,
template = ctx.file._conf_tmpl,
substitutions = {
"TMPL_config": expand_path_into_runfiles(ctx, configuration_file.short_path) if configuration_file else "",
"TMPL_on_prepare": expand_path_into_runfiles(ctx, on_prepare_file.short_path) if on_prepare_file else "",
"TMPL_server": ctx.executable.server.short_path if ctx.executable.server else "",
"TMPL_specs": "\n".join([" '%s'," % e for e in specs]),
"TMPL_workspace": ctx.workspace_name,
},
)
runfiles = [configuration] + configuration_sources + on_prepare_sources
ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = """#!/usr/bin/env bash
# Immediately exit if any command fails.
set -e
if [ -e "$RUNFILES_MANIFEST_FILE" ]; then
while read line; do
declare -a PARTS=($line)
if [ "${{PARTS[0]}}" == "{TMPL_protractor}" ]; then
readonly PROTRACTOR=${{PARTS[1]}}
elif [ "${{PARTS[0]}}" == "{TMPL_conf}" ]; then
readonly CONF=${{PARTS[1]}}
fi
done < $RUNFILES_MANIFEST_FILE
else
readonly PROTRACTOR=../{TMPL_protractor}
readonly CONF=../{TMPL_conf}
fi
export HOME=$(mktemp -d)
# Print the protractor version in the test log
PROTRACTOR_VERSION=$($PROTRACTOR --version)
echo "Protractor $PROTRACTOR_VERSION"
# Run the protractor binary
$PROTRACTOR $CONF
""".format(
TMPL_protractor = _short_path_to_manifest_path(ctx, ctx.executable.protractor.short_path),
TMPL_conf = _short_path_to_manifest_path(ctx, configuration.short_path),
),
)
return [DefaultInfo(
files = depset([ctx.outputs.executable]),
runfiles = ctx.runfiles(
files = runfiles,
transitive_files = files,
# Propagate protractor_bin and its runfiles
collect_data = True,
collect_default = True,
),
executable = ctx.outputs.executable,
)]
_protractor_web_test = rule(
implementation = _protractor_web_test_impl,
test = True,
executable = True,
attrs = {
"srcs": attr.label_list(
doc = "A list of JavaScript test files",
allow_files = [".js"],
),
"configuration": attr.label(
doc = "Protractor configuration file",
allow_single_file = True,
aspects = [sources_aspect],
),
"data": attr.label_list(
doc = "Runtime dependencies",
),
"on_prepare": attr.label(
doc = """A file with a node.js script to run once before all tests run.
If the script exports a function which returns a promise, protractor
will wait for the promise to resolve before beginning tests.""",
allow_single_file = True,
aspects = [sources_aspect],
),
"protractor": attr.label(
doc = "Protractor executable target (set by protractor_web_test macro)",
executable = True,
cfg = "target",
allow_files = True,
),
"server": attr.label(
doc = "Optional server executable target",
executable = True,
cfg = "target",
allow_files = True,
),
"deps": attr.label_list(
doc = "Other targets which produce JavaScript such as `ts_library`",
allow_files = True,
aspects = [sources_aspect],
),
"_conf_tmpl": attr.label(
default = Label(_CONF_TMPL),
allow_single_file = True,
),
},
)
def protractor_web_test(
name,
configuration = None,
on_prepare = None,
srcs = [],
deps = [],
data = [],
server = None,
tags = [],
protractor = "@npm//:node_modules/protractor/bin/protractor",
**kwargs):
"""Runs a protractor test in a browser.
Args:
name: The name of the test
configuration: Protractor configuration file.
on_prepare: A file with a node.js script to run once before all tests run.
If the script exports a function which returns a promise, protractor
will wait for the promise to resolve before beginning tests.
srcs: JavaScript source files
deps: Other targets which produce JavaScript such as `ts_library`
data: Runtime dependencies
server: Optional server executable target
tags: Standard Bazel tags, this macro adds one for ibazel
protractor: Protractor entry_point. Defaults to @npm//:node_modules/protractor/bin/protractor
but should be changed to @your_npm_workspace//:node_modules/protractor/bin/protractor if
you are not using @npm for your npm dependencies.
**kwargs: passed through to `_protractor_web_test`
"""
protractor_bin_name = name + "_protractor_bin"
nodejs_binary(
name = protractor_bin_name,
entry_point = protractor,
data = srcs + deps + data,
testonly = 1,
visibility = ["//visibility:private"],
)
# Our binary dependency must be in data[] for collect_data to pick it up
# FIXME: maybe we can just ask :protractor_bin_name for its runfiles attr
web_test_data = data + [":" + protractor_bin_name]
if server:
web_test_data += [server]
_protractor_web_test(
name = name,
configuration = configuration,
on_prepare = on_prepare,
srcs = srcs,
deps = deps,
data = web_test_data,
server = server,
protractor = protractor_bin_name,
tags = tags + [
# Users don't need to know that this tag is required to run under ibazel
"ibazel_notify_changes",
],
**kwargs
)
def protractor_web_test_suite(
name,
configuration = None,
on_prepare = None,
srcs = [],
deps = [],
data = [],
server = None,
browsers = ["@io_bazel_rules_webtesting//browsers:chromium-local"],
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,
protractor = "@npm//:node_modules/protractor/bin/protractor",
**remaining_keyword_args):
"""Defines a test_suite of web_test targets that wrap a protractor_web_test target.
Args:
name: The base name of the test.
configuration: Protractor configuration file.
on_prepare: A file with a node.js script to run once before all tests run.
If the script exports a function which returns a promise, protractor
will wait for the promise to resolve before beginning tests.
srcs: JavaScript source files
deps: Other targets which produce JavaScript such as `ts_library`
data: Runtime dependencies
server: Optional server executable target
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 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.
wrapped_test_tags: A list of test tag strings to use for the wrapped test
protractor: Protractor entry_point. Defaults to @npm//:node_modules/protractor/bin/protractor
but should be changed to @your_npm_workspace//:node_modules/protractor/bin/protractor if
you are not using @npm for your npm dependencies.
**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
size = size or "large"
wrapped_test_name = name + "_wrapped_test"
protractor_bin_name = name + "_protractor_bin"
# Users don't need to know that this tag is required to run under ibazel
tags = tags + ["ibazel_notify_changes"]
nodejs_binary(
name = protractor_bin_name,
entry_point = protractor,
data = srcs + deps + data,
testonly = 1,
visibility = ["//visibility:private"],
)
# Our binary dependency must be in data[] for collect_data to pick it up
# FIXME: maybe we can just ask the :protractor_bin_name for its runfiles attr
web_test_data = web_test_data + [":" + protractor_bin_name]
if server:
web_test_data += [server]
_protractor_web_test(
name = wrapped_test_name,
configuration = configuration,
on_prepare = on_prepare,
srcs = srcs,
deps = deps,
data = web_test_data,
server = server,
protractor = protractor_bin_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,
test = wrapped_test_name,
test_suite_tags = test_suite_tags,
timeout = timeout,
visibility = visibility,
)

View File

@ -1,12 +0,0 @@
package(default_visibility = ["//visibility:public"])
load("@npm_bazel_typescript//:index.bzl", "ts_library")
ts_library(
name = "utils",
srcs = ["index.ts"],
module_name = "@angular/bazel/protractor-utils",
node_modules = "@npm//typescript:typescript__typings",
tsconfig = ":tsconfig.json",
deps = ["@npm//@types/node"],
)

View File

@ -1,101 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as child_process from 'child_process';
import * as net from 'net';
import * as path from 'path';
export function isTcpPortFree(port: number): Promise<boolean> {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.on('error', (e) => { resolve(false); });
server.on('close', () => { resolve(true); });
server.listen(port, () => { server.close(); });
});
}
export function isTcpPortBound(port: number): Promise<boolean> {
return new Promise((resolve, reject) => {
const client = new net.Socket();
client.once('connect', () => { resolve(true); });
client.once('error', (e) => { resolve(false); });
client.connect(port);
});
}
export async function findFreeTcpPort(): Promise<number> {
const range = {
min: 32768,
max: 60000,
};
for (let i = 0; i < 100; i++) {
let port = Math.floor(Math.random() * (range.max - range.min) + range.min);
if (await isTcpPortFree(port)) {
return port;
}
}
throw new Error('Unable to find a free port');
}
// Interface for config parameter of the protractor_web_test_suite onPrepare function
export interface OnPrepareConfig {
// The workspace name
workspace: string;
// The server binary to run
server: string;
}
export function waitForServer(port: number, timeout: number): Promise<boolean> {
return isTcpPortBound(port).then(isBound => {
if (!isBound) {
if (timeout <= 0) {
throw new Error('Timeout waiting for server to start');
}
const wait = Math.min(timeout, 500);
return new Promise((res, rej) => setTimeout(res, wait))
.then(() => waitForServer(port, timeout - wait));
}
return true;
});
}
// Return type from runServer function
export interface ServerSpec {
// Port number that the server is running on
port: number;
}
/**
* Runs the specified server binary from a given workspace and waits for the server
* being ready. The server binary will be resolved from the Bazel runfiles. Note that
* the server will be launched with a random free port in order to support test concurrency
* with Bazel.
*/
export async function runServer(
workspace: string, serverTarget: string, portFlag: string, serverArgs: string[],
timeout = 5000): Promise<ServerSpec> {
const serverPath = require.resolve(`${workspace}/${serverTarget}`);
const port = await findFreeTcpPort();
// Start the Bazel server binary with a random free TCP port.
const serverProcess = child_process.spawn(
serverPath, serverArgs.concat([portFlag, port.toString()]), {stdio: 'inherit'});
// In case the process exited with an error, we want to propagate the error.
serverProcess.on('exit', exitCode => {
if (exitCode !== 0) {
throw new Error(`Server exited with error code: ${exitCode}`);
}
});
// Wait for the server to be bound to the given port.
await waitForServer(port, timeout);
return {port};
}

View File

@ -1,7 +0,0 @@
{
"compilerOptions": {
"noImplicitAny": true,
"lib": ["es2015"],
"types": ["node"]
}
}

View File

@ -1,72 +0,0 @@
load("//packages/bazel:index.bzl", "protractor_web_test_suite")
load("@npm_bazel_typescript//:index.bzl", "ts_config", "ts_devserver")
load("@build_bazel_rules_nodejs//:defs.bzl", "http_server", "rollup_bundle")
load("//tools:defaults.bzl", "ts_library")
ts_config(
name = "tsconfig-test",
src = "tsconfig-test.json",
deps = [":tsconfig.json"],
)
ts_library(
name = "app",
srcs = ["app.ts"],
tsconfig = ":tsconfig.json",
)
ts_devserver(
name = "devserver",
serving_path = "/bundle.min.js",
static_files = ["index.html"],
deps = [":app"],
)
rollup_bundle(
name = "bundle",
entry_point = ":app.ts",
deps = [":app"],
)
http_server(
name = "prodserver",
data = [
"index.html",
":bundle.min.js",
],
)
ts_library(
name = "ts_spec",
testonly = True,
srcs = ["test.spec.ts"],
tsconfig = ":tsconfig-test",
deps = [
"@npm//@types/selenium-webdriver",
"@npm//protractor",
],
)
protractor_web_test_suite(
name = "prodserver_test",
configuration = ":conf.js",
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = ":on-prepare.js",
server = ":prodserver",
deps = [
":ts_spec",
"@npm//protractor",
],
)
protractor_web_test_suite(
name = "devserver_test",
configuration = ":conf.js",
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = ":on-prepare.js",
server = ":devserver",
deps = [
":ts_spec",
"@npm//protractor",
],
)

View File

@ -1,12 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const el: HTMLDivElement = document.createElement('div');
el.innerText = 'Hello, Protractor';
el.className = 'ts1';
document.body.appendChild(el);

View File

@ -1,11 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
exports.config = {
onPrepare: function() { global.userOnPrepareGotCalled = true; }
};

View File

@ -1,9 +0,0 @@
<!doctype html>
<html>
<head>
<title>protractor test</title>
</head>
<body>
<script src="/bundle.min.js"></script>
</body>
</html>

View File

@ -1,22 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const protractorUtils = require('@angular/bazel/protractor-utils');
const protractor = require('protractor');
module.exports = function(config) {
if (!global.userOnPrepareGotCalled) {
throw new Error('Expecting user configuration onPrepare to have been called');
}
const portFlag = /prodserver(\.exe)?$/.test(config.server) ? '-p' : '-port';
return protractorUtils.runServer(config.workspace, config.server, portFlag, [])
.then(serverSpec => {
const serverUrl = `http://localhost:${serverSpec.port}`;
protractor.browser.baseUrl = serverUrl;
});
};

View File

@ -1,28 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*
* @fileoverview A small demo of how to run a protractor test.
*/
import {ExpectedConditions, browser, by, element} from 'protractor';
// This test uses Protractor without Angular, so disable Angular features
browser.waitForAngularEnabled(false);
describe('app', () => {
beforeAll(() => {
browser.get('');
browser.wait(ExpectedConditions.presenceOf(element(by.css('div.ts1'))), 100000);
});
it('should display: Hello, Protractor', (done) => {
const div = element(by.css('div.ts1'));
div.getText().then(t => expect(t).toEqual(`Hello, Protractor`));
done();
});
});

View File

@ -1,6 +0,0 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["jasmine"]
}
}

View File

@ -1,6 +0,0 @@
{
"compilerOptions": {
"lib": ["dom", "es2015"],
"types": []
}
}

View File

@ -1,29 +0,0 @@
load("//tools:defaults.bzl", "jasmine_node_test", "nodejs_binary", "ts_library")
ts_library(
name = "protractor_utils_tests_lib",
testonly = True,
srcs = ["index_test.ts"],
deps = [
"//packages/bazel/src/protractor/utils",
],
)
nodejs_binary(
name = "fake-devserver",
testonly = True,
data = [
"fake-devserver.js",
"@npm//minimist",
],
entry_point = ":fake-devserver.ts",
)
jasmine_node_test(
name = "protractor_utils_tests",
size = "small",
srcs = [":protractor_utils_tests_lib"],
data = [
":fake-devserver",
],
)

View File

@ -1,23 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const http = require('http');
const minimist = require('minimist');
const {port} = minimist(process.argv);
const server = new http.Server();
// Basic request handler so that it could respond to fake requests.
server.on('request', (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Running');
});
server.listen(port);
console.info('Server running on port:', port);

View File

@ -1,17 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {runServer} from '../../src/protractor/utils';
describe('Bazel protractor utils', () => {
it('should be able to start devserver', async() => {
// Test will automatically time out if the server couldn't be launched as expected.
await runServer('angular', 'packages/bazel/test/protractor-utils/fake-devserver', '--port', []);
});
});

View File

@ -1,34 +0,0 @@
load("//packages/bazel:index.bzl", "protractor_web_test_suite")
load("//tools:defaults.bzl", "ts_library")
ts_library(
name = "ts_spec",
testonly = True,
srcs = ["test.spec.ts"],
tsconfig = ":tsconfig.json",
deps = [
"@npm//@types/selenium-webdriver",
"@npm//protractor",
],
)
ts_library(
name = "ts_conf",
testonly = True,
srcs = ["conf.ts"],
tsconfig = ":tsconfig.json",
deps = [
"//packages/bazel/src/protractor/utils",
"@npm//protractor",
],
)
protractor_web_test_suite(
name = "test",
configuration = ":ts_conf",
data = ["//packages/bazel/src/protractor/utils"],
deps = [
":ts_spec",
"@npm//protractor",
],
)

View File

@ -1,30 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as protractorUtils from '@angular/bazel/protractor-utils';
import {browser} from 'protractor';
const http = require('http');
exports.config = {
onPrepare() {
return protractorUtils.findFreeTcpPort().then(port => {
const app = new http.Server();
app.on('request', (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World');
res.end('\n');
});
browser.baseUrl = `http://localhost:${port}`;
return new Promise(resolve => { app.listen(port, () => { resolve(); }); });
});
}
};

View File

@ -1,20 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*
* @fileoverview A small demo of how to run a protractor test.
*/
import {$, browser} from 'protractor';
describe('Basic test', () => {
it('should say hello world', () => {
browser.waitForAngularEnabled(false);
browser.get('/');
expect($('body').getText()).toContain('Hello World');
});
});

View File

@ -1,6 +0,0 @@
{
"compilerOptions": {
"lib": ["es2015"],
"types": ["jasmine"]
}
}

View File

@ -52,7 +52,6 @@ ts_devserver(
protractor_web_test_suite( protractor_web_test_suite(
name = "protractor_tests", name = "protractor_tests",
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = ":start-server.js", on_prepare = ":start-server.js",
server = ":devserver", server = ":devserver",
deps = [ deps = [

View File

@ -69,7 +69,6 @@ ts_devserver(
protractor_web_test_suite( protractor_web_test_suite(
name = "protractor_tests", name = "protractor_tests",
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = ":start-server.js", on_prepare = ":start-server.js",
server = ":devserver", server = ":devserver",
deps = [ deps = [

View File

@ -52,7 +52,6 @@ ts_devserver(
protractor_web_test_suite( protractor_web_test_suite(
name = "protractor_tests", name = "protractor_tests",
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = ":start-server.js", on_prepare = ":start-server.js",
server = ":devserver", server = ":devserver",
deps = [ deps = [

View File

@ -51,7 +51,6 @@ ts_devserver(
protractor_web_test_suite( protractor_web_test_suite(
name = "protractor_tests", name = "protractor_tests",
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = "start-server.js", on_prepare = "start-server.js",
server = ":devserver", server = ":devserver",
deps = [ deps = [

View File

@ -51,7 +51,6 @@ ts_devserver(
protractor_web_test_suite( protractor_web_test_suite(
name = "protractor_tests", name = "protractor_tests",
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = "start-server.js", on_prepare = "start-server.js",
server = ":devserver", server = ":devserver",
deps = [ deps = [

View File

@ -61,7 +61,6 @@ def create_upgrade_example_targets(name, srcs, e2e_srcs, entry_module, assets =
protractor_web_test_suite( protractor_web_test_suite(
name = "%s_protractor" % name, name = "%s_protractor" % name,
data = ["//packages/bazel/src/protractor/utils"],
on_prepare = "//packages/examples/upgrade:start-server.js", on_prepare = "//packages/examples/upgrade:start-server.js",
server = ":devserver", server = ":devserver",
deps = [ deps = [