Skip to content

Commit

Permalink
re-write rules.bzl
Browse files Browse the repository at this point in the history
  • Loading branch information
jmelahman authored and filmil committed Nov 24, 2021
1 parent f52b781 commit 881d221
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions rules.bzl
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
# From:
# From:
# https://stackoverflow.com/questions/47192668/idiomatic-retrieval-of-the-bazel-execution-path#

def _dirname(path):
prefix, _, _ = path.rpartition("/")
return prefix.rstrip("/")

def _test_files(bats, srcs):
return '"{bats_bin}" {test_paths}'.format(
bats_bin = bats.short_path,
test_paths = " ".join(['"{}"'.format(s.short_path) for s in srcs]),
)

def _bats_test_impl(ctx):
runfiles = ctx.runfiles(
files = ctx.files.srcs,
collect_data = True,
)
tests = ["\"{}\"".format(f.short_path) for f in ctx.files.srcs]
path = ["$PWD/" + _dirname(b.short_path) for b in ctx.files.deps]
l = ['export {}="{}"'.format(
key, ctx.expand_location(val, ctx.attr.deps)) for key, val in ctx.attr.env.items()]
env = "\n".join(l)

sep = ctx.configuration.host_path_separator

BASH_TEMPLATE = """#!/usr/bin/env bash
set -e
export TMPDIR="$TEST_TMPDIR"
export PATH="{bats_bins_path}":$PATH
{env}
"{bats}" {test_paths}
"""
content = BASH_TEMPLATE.format(
bats = ctx.executable._bats.short_path,
bats_bins_path = sep.join(path),
env=env,
test_paths = " ".join(tests),
content = "\n".join(
["#!/usr/bin/env bash"] +
["set -e"] +
["export TMPDIR=\"$TEST_TMPDIR\""] +
["export PATH=\"{bats_bins_path}\":$PATH".format(bats_bins_path = sep.join(path))] +
[
'export {}="{}"'.format(key, ctx.expand_location(val, ctx.attr.deps))
for key, val in ctx.attr.env.items()
] +
[_test_files(ctx.executable._bats, ctx.files.srcs)]
)
ctx.actions.write(
output = ctx.outputs.executable,
is_executable = True,
content = content,
)
runfiles = runfiles.merge(ctx.attr._bats.default_runfiles)
return DefaultInfo(
runfiles = runfiles,
)
runfiles = ctx.runfiles(
files = ctx.files.srcs,
collect_data = True,
).merge(ctx.attr._bats.default_runfiles)
return [DefaultInfo(runfiles = runfiles)]


bats_test = rule(
Expand All @@ -57,7 +51,7 @@ bats_test = rule(
"_bats": attr.label(
default = Label("@bats_core//:bats"),
executable = True,
cfg = "host",
cfg = "exec",
),
},
test = True,
Expand Down

0 comments on commit 881d221

Please sign in to comment.