bazel: karma (watch is broken)

This commit is contained in:
Jason Choi
2016-08-19 23:52:51 -07:00
parent b5f97b0410
commit 47935dd5cd
9 changed files with 195 additions and 52 deletions

59
BUILD
View File

@ -3,6 +3,7 @@ package(default_visibility=["//visibility:public"])
load("//build_defs:nodejs.bzl", "nodejs_binary", "nodejs_test")
load("//build_defs:typescript.bzl", "ts_library", "ts_ext_library")
load("//build_defs:jasmine.bzl", "jasmine_node_test")
load("//build_defs:karma.bzl", "karma_test")
# This imports node_modules targets from a generated file.
load("//build_defs:node_modules_index.bzl", "node_modules_index")
node_modules_index(glob)
@ -607,3 +608,61 @@ test_suite(
name = "jasmine_tests",
tests = [":{}_test".format(p) for p in JASMINE_TESTABLE],
)
ts_library(
name = "empty_module",
srcs = ["modules/empty.ts"],
tsconfig = "modules/tsconfig.json",
)
KARMA_DATA = [
":es6-shim",
":karma-browserstack-launcher",
":karma-chrome-launcher",
":karma-jasmine",
":karma-sauce-launcher",
":karma-sourcemap-loader",
":reflect-metadata",
":source-map",
":systemjs",
]
karma_test(
name = "karma_test",
srcs = [
":core_test_module",
":common_test_module",
":compiler_test_module",
":compiler_test_codegen_js",
":forms_test_module",
":http_test_module",
":platform-browser_test_module",
":platform-browser-dynamic_test_module",
":platform-server_test_module",
":upgrade_test_module",
":empty_module",
"shims_for_IE.js",
"test-main.js",
],
data = KARMA_DATA + [
":angular",
"browser-providers.conf.js",
"tools/karma/reporter.js",
],
config = "karma-js.conf.js",
local = True,
)
karma_test(
name = "router_karma_test",
srcs = [
":router_test_module",
"modules/@angular/router/karma-test-shim.js",
],
data = KARMA_DATA + [
"browser-providers.conf.js",
],
config = "modules/@angular/router/karma.conf.js",
size = "small",
local = True,
)

View File

@ -1,4 +1,5 @@
exports_files([
"nodejs_launcher_template.sh",
"jasmine_launcher_template.sh",
"karma_launcher_template.sh",
])

75
build_defs/karma.bzl Normal file
View File

@ -0,0 +1,75 @@
_KARMA_TEST_ATTRS = {
"srcs": attr.label_list(allow_files=True),
"deps": attr.label_list(),
"data": attr.label_list(allow_files=True, cfg=DATA_CFG),
"config": attr.label(allow_files=True, single_file=True, mandatory=True),
"_karma": attr.label(default=Label("//:karma_bin"), executable=True),
"_launcher_template": attr.label(
default = Label("//build_defs:karma_launcher_template.sh"),
allow_files = True,
single_file = True,
),
}
def _karma_test_impl(ctx, karma_args="--single-run"):
"""
Rule for running Karma tests.
Args:
srcs: The targets containing the spec files.
deps: JavaScript targets which the tests depend on.
data: Data files which the tests depend on.
config: Required. Karma config file to use.
Due to the complexity of Karma config files, this rule does not do the heavy
lifting of creating that config file. Instead, the user has to point to the
right files. Files specified in Karma must be a subset of files depended upon
in the karma_test definition.
This rule additionally creates a <name>_local target, which runs Karma in
watch mode. This can be combined with ibazel for incremental development.
"""
ctx.template_action(
template = ctx.file._launcher_template,
output = ctx.outputs.executable,
substitutions = {
"{{karma}}": ctx.executable._karma.short_path,
"{{config}}": ctx.file.config.short_path,
"{{args}}": karma_args,
},
executable = True,
)
return struct(
files = set([ctx.outputs.executable]),
runfiles = ctx.runfiles(
files = ctx.files.srcs + ctx.files._karma + ctx.files.config,
transitive_files = set(ctx.attr._karma.default_runfiles.files),
collect_data = True,
collect_default = True,
),
)
_karma_test = rule(
implementation = _karma_test_impl,
executable = True,
test = True,
attrs = _KARMA_TEST_ATTRS,
)
def _karma_test_local_impl(ctx):
return _karma_test_impl(ctx, karma_args = "")
_karma_test_local = rule(
implementation = _karma_test_local_impl,
executable = True,
attrs = _KARMA_TEST_ATTRS,
)
def karma_test(*, name, timeout=None, size=None, flaky=None, shard_count=None, local=None,
tags=None, **kwargs):
tags = tags or []
_karma_test(name=name, timeout=timeout, size=size, flaky=flaky, shard_count=shard_count,
local=local, tags=tags, **kwargs)
_karma_test_local(name=name + "_local", tags=tags + ["ibazel_notify_changes"], **kwargs)

View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -e
if [[ -z "${RUNFILES}" ]]; then
case "${0}" in
/*) self="${0}" ;;
*) self="${PWD}/${0}" ;;
esac
if [[ -n "${TEST_SRCDIR}" ]]; then
export RUNFILES="${TEST_SRCDIR}/angular"
elif [[ -d "${self}.runfiles" ]]; then
export RUNFILES="${self}.runfiles/angular"
else
echo "Runfiles directory not found." >&2
exit 1
fi
fi
cd "${RUNFILES}" && "${RUNFILES}/{{karma}}" start "${RUNFILES}/{{config}}" {{args}} "$@"

View File

@ -11,7 +11,8 @@ module.exports = function(config) {
files: [
// Sources and specs.
// Loaded through the System loader, in `test-main.js`.
{pattern: 'dist/all/@angular/**/*.js', included: false, watched: true},
{pattern: 'modules/@angular/**/*.js', included: false, watched: true},
{pattern: 'modules/@angular/**/*.js.map', included: false, watched: true},
'node_modules/core-js/client/core.js',
// include Angular v1 for upgrade module testing
@ -30,20 +31,19 @@ module.exports = function(config) {
'node_modules/systemjs/dist/system.src.js',
{pattern: 'node_modules/rxjs/**', included: false, watched: false, served: true},
'node_modules/reflect-metadata/Reflect.js',
'tools/build/file2modulename.js',
// 'tools/build/file2modulename.js',
'test-main.js',
{pattern: 'dist/all/empty.*', included: false, watched: false},
{pattern: 'modules/empty.*', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser/test/static_assets/**', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser/test/browser/static_assets/**', included: false, watched: false}
],
exclude: [
'dist/all/@angular/**/e2e_test/**',
'dist/all/@angular/examples/**',
'dist/all/@angular/router/**',
'dist/all/@angular/compiler-cli/**',
'dist/all/angular1_router.js',
'dist/all/@angular/platform-browser/testing/e2e_util.js'
'modules/@angular/**/e2e_test/**',
'modules/@angular/examples/**',
'modules/@angular/compiler-cli/**',
'modules/angular1_router.js',
'modules/@angular/platform-browser/testing/e2e_util.js'
],
customLaunchers: browserProvidersConf.customLaunchers,

View File

@ -14,8 +14,7 @@ function isSpecFile(path) {
}
function isBuiltFile(path) {
var builtPath = '/base/dist/';
return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath);
return isJsFile(path);
}
var allSpecFiles = Object.keys(window.__karma__.files)
@ -31,7 +30,7 @@ System.config(
{
map: {
'rxjs': 'node_modules/rxjs',
'@angular': 'dist/all/@angular'
'@angular': 'modules/@angular'
},
packages: {
'@angular/core': {

View File

@ -12,7 +12,6 @@ module.exports = function(config) {
// Polyfills.
'node_modules/core-js/client/core.js',
'node_modules/reflect-metadata/Reflect.js',
'shims_for_IE.js',
// System.js for module loading
'node_modules/systemjs/dist/system-polyfills.js',
@ -35,28 +34,28 @@ module.exports = function(config) {
{pattern: 'modules/@angular/router/karma-test-shim.js', included: true, watched: true },
// Angular modules
{pattern: 'dist/all/@angular/core/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/core/src/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/core/testing/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/core/*.js', included: false, watched: false},
{pattern: 'modules/@angular/core/src/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/core/testing/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/common/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/common/src/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/common/testing/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/common/*.js', included: false, watched: false},
{pattern: 'modules/@angular/common/src/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/common/testing/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/compiler/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/compiler/src/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/compiler/testing/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/compiler/*.js', included: false, watched: false},
{pattern: 'modules/@angular/compiler/src/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/compiler/testing/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/platform-browser/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/platform-browser/src/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/platform-browser/testing/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser/*.js', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser/src/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser/testing/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/platform-browser-dynamic/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/platform-browser-dynamic/src/**/*.js', included: false, watched: false},
{pattern: 'dist/all/@angular/platform-browser-dynamic/testing/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser-dynamic/*.js', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser-dynamic/src/**/*.js', included: false, watched: false},
{pattern: 'modules/@angular/platform-browser-dynamic/testing/**/*.js', included: false, watched: false},
// Router
{pattern: 'dist/all/@angular/router/**/*.js', included: false, watched: true}
{pattern: 'modules/@angular/router/**/*.js', included: false, watched: true}
],
customLaunchers: browserProvidersConf.customLaunchers,
@ -77,12 +76,7 @@ module.exports = function(config) {
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
captureTimeout: 60000,
browserDisconnectTimeout : 60000,
browserDisconnectTolerance : 3,
browserNoActivityTimeout : 60000
});
singleRun: false
})
};

View File

@ -11,13 +11,11 @@ System.config({
baseURL: '/base',
defaultJSExtensions: true,
map: {
'benchpress/*': 'dist/js/dev/es5/benchpress/*.js',
'@angular': 'dist/all/@angular',
// 'benchpress/*': 'modules/benchpress/*.js',
'@angular': 'modules/@angular',
'rxjs': 'node_modules/rxjs',
'parse5/index': 'dist/all/empty.js',
'@angular/platform-server/src/parse5_adapter': 'dist/all/empty.js',
'angular2/*': 'dist/all/angular2/*.js',
'angular2/src/alt_router/router_testing_providers': 'dist/all/angular2/src/alt_router/router_testing_providers.js'
'parse5/index': 'modules/empty.js',
'@angular/platform-server/src/parse5_adapter': 'modules/empty.js'
},
packages: {
'@angular/core': {
@ -41,7 +39,7 @@ System.config({
main: 'index.js',
defaultExtension: 'js'
},
'@angular/router': {
'@angular/router-deprecated': {
main: 'index.js',
defaultExtension: 'js'
},
@ -65,7 +63,8 @@ System.config({
main: 'index.js',
defaultExtension: 'js'
}
}
},
transpiler: null,
});
@ -84,14 +83,9 @@ System.import('@angular/core/testing')
return Promise.all(
Object.keys(window.__karma__.files) // All files served by Karma.
.filter(onlySpecFiles)
.map(window.file2moduleName) // Normalize paths to module names.
// .map(window.file2moduleName) // Normalize paths to module names.
.map(function(path) {
return System.import(path).then(function(module) {
if (module.hasOwnProperty('main')) {
module.main();
} else {
throw new Error('Module ' + path + ' does not implement main() method.');
}
});
}));
})

View File

@ -33,7 +33,7 @@ var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) {
line: line,
column: column
});
return process.cwd() + "/modules/" + original.source + ":" + original.line + ":" + original.column;
return "modules/" + original.source + ":" + original.line + ":" + original.column;
} catch (e) {
console.warn('SourceMap position not found for trace: %s', msg);
}