Skip to content

Commit

Permalink
refactor(jasmine): cleanup jasmine_node_test to no longer depend on r…
Browse files Browse the repository at this point in the history
…equire of the manifest path (#1796)

Use runfiles helper instead.
  • Loading branch information
gregmagolan authored Apr 6, 2020
1 parent f56bfd0 commit 9d91154
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 74 deletions.
56 changes: 0 additions & 56 deletions internal/common/devmode_js_sources.bzl

This file was deleted.

39 changes: 36 additions & 3 deletions packages/jasmine/src/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,42 @@ These rules let you run tests outside of a browser. This is typically faster
than launching a test in Karma, for example.
"""

load("@build_bazel_rules_nodejs//internal/common:devmode_js_sources.bzl", "devmode_js_sources")
load("@build_bazel_rules_nodejs//:providers.bzl", "JSNamedModuleInfo")
load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_test")

def _devmode_js_sources_impl(ctx):
depsets = []
for src in ctx.attr.srcs:
if JSNamedModuleInfo in src:
depsets.append(src[JSNamedModuleInfo].sources)
if hasattr(src, "files"):
depsets.append(src.files)
sources = depset(transitive = depsets)

ctx.actions.write(ctx.outputs.manifest, "".join([
f.short_path + "\n"
for f in sources.to_list()
if f.path.endswith(".js") or f.path.endswith(".mjs")
]))

return [DefaultInfo(files = sources)]

"""Rule to get devmode js sources from deps.
Outputs a manifest file with the sources listed.
"""
_devmode_js_sources = rule(
implementation = _devmode_js_sources_impl,
attrs = {
"srcs": attr.label_list(
allow_files = True,
),
},
outputs = {
"manifest": "%{name}.MF",
},
)

def jasmine_node_test(
name,
srcs = [],
Expand Down Expand Up @@ -62,9 +95,9 @@ def jasmine_node_test(
jasmine_entry_point: A label providing the `@bazel/jasmine` entry point.
**kwargs: Remaining arguments are passed to the test rule
"""
devmode_js_sources(
_devmode_js_sources(
name = "%s_devmode_srcs" % name,
deps = srcs + deps,
srcs = srcs + deps,
testonly = 1,
tags = tags,
)
Expand Down
27 changes: 12 additions & 15 deletions packages/jasmine/src/jasmine_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function main(args) {
throw new Error('expected argument missing');
}


// first args is always the path to the manifest
const manifest = runfiles.resolveWorkspaceRelative(readArg());
// second is always a flag to enable coverage or not
Expand All @@ -87,17 +86,9 @@ function main(args) {
.filter(l => l.length > 0)
// Filter out files from node_modules
.filter(f => !IS_NODE_MODULE.test(f))

const sourceFiles = allFiles
// Filter out all .spec and .test files so we only report
// coverage against the source files
.filter(f => !IS_TEST_FILE.test(f))
// the jasmine_runner.js gets in here as a file to run
.filter(f => !f.endsWith('jasmine_runner.js'))
.map(f => runfiles.resolve(f))
// the reporting lib resolves the relative path to our cwd instead of
// using the absolute one so match it here
.map(f => path.relative(cwd, f))
// Use runfiles resolve to resolve the file path that
// bazel passes to the runner to its absolute path
.map(f => runfiles.resolveWorkspaceRelative(f))

allFiles
// Filter here so that only files ending in `spec.js` and `test.js`
Expand Down Expand Up @@ -133,14 +124,22 @@ function main(args) {
// so we need to add it back
jrunner.configureDefaultReporter({});


let covExecutor;
let covDir;
if (enableCoverage) {
// lazily pull these deps in for only when we want to collect coverage
const crypto = require('crypto');
const Execute = require('v8-coverage/src/execute');

// Filter out all .spec and .test files so we only report coverage against the source files
const sourceFiles = allFiles
.filter(f => !IS_TEST_FILE.test(f))
// the jasmine_runner.js gets in here as a file to run
.filter(f => !f.endsWith('jasmine_runner.js'))
// the reporting lib resolves the relative path to our cwd instead of
// using the absolute one so match it here
.map(f => path.relative(cwd, f))

// make a tmpdir inside our tmpdir for just this run
covDir = path.join(process.env['TEST_TMPDIR'], String(crypto.randomBytes(4).readUInt32LE(0)));
covExecutor = new Execute({include: sourceFiles, exclude: []});
Expand Down Expand Up @@ -178,8 +177,6 @@ function main(args) {
} else {
process.exit(exitCode);
}


});

if (TOTAL_SHARDS) {
Expand Down

0 comments on commit 9d91154

Please sign in to comment.