diff --git a/rules.bzl b/rules.bzl index 240f287..eec1d9d 100644 --- a/rules.bzl +++ b/rules.bzl @@ -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( @@ -57,7 +51,7 @@ bats_test = rule( "_bats": attr.label( default = Label("@bats_core//:bats"), executable = True, - cfg = "host", + cfg = "exec", ), }, test = True,