diff --git a/rules/maprule_private.bzl b/rules/maprule_private.bzl index 0b4b3af..10c9d48 100644 --- a/rules/maprule_private.bzl +++ b/rules/maprule_private.bzl @@ -454,12 +454,15 @@ def _maprule_main(ctx, strategy): inputs = depset(direct = [src], transitive = [common_srcs, inputs_from_tools]), outputs = foreach_src_outs_dicts[src].values(), # The custom envmap contains envvars specific to the current "src", such as MAPRULE_SRC. - env = common_envmap + _custom_envmap( - ctx, - strategy, - src_placeholders_dicts[src], - foreach_src_outs_dicts[src], - add_env, + env = dicts.add( + common_envmap, + _custom_envmap( + ctx, + strategy, + src_placeholders_dicts[src], + foreach_src_outs_dicts[src], + add_env, + ), ), command = ctx.attr.cmd, progress_message = progress_message, diff --git a/tests/maprule/BUILD b/tests/maprule/BUILD new file mode 100644 index 0000000..d33039e --- /dev/null +++ b/tests/maprule/BUILD @@ -0,0 +1,47 @@ +# This package aids testing the 'maprule' rule. + +load("//rules:maprule.bzl", "bash_maprule") + +package(default_testonly = 1) + +sh_test( + name = "maprule_tests", + srcs = ["maprule_tests.sh"], + data = [ + # Use DefaultInfo.files from 'mr_bash' (via 'file_deps'). + ":file_deps", + "//tests:unittest.bash", + ], + deps = ["@bazel_tools//tools/bash/runfiles"], +) + +filegroup( + name = "file_deps", + # Use DefaultInfo.files from 'mr_bash'. + srcs = [":mr_bash"], +) + +bash_maprule( + name = "mr_bash", + srcs = ["common.txt"], + add_env = { + "TOOL": "$(location :mr_bash_tool)", + }, + # TODO(laszlocsomor): add quotes around $MAPRULE_TOOL after + # https://github.com/bazelbuild/bazel/issues/7454 is fixed. + cmd = "$MAPRULE_TOOL", + foreach_srcs = [ + "foo.txt", + "b/bar.txt", + ], + outs_templates = { + "OUT1": "{src}.out1", + "OUT2": "out2/{src_name_noext}", + }, + tools = [":mr_bash_tool"], +) + +sh_binary( + name = "mr_bash_tool", + srcs = ["mr_bash_tool.sh"], +) diff --git a/tests/maprule/b/bar.txt b/tests/maprule/b/bar.txt new file mode 100644 index 0000000..24ec369 --- /dev/null +++ b/tests/maprule/b/bar.txt @@ -0,0 +1 @@ +bar file diff --git a/tests/maprule/common.txt b/tests/maprule/common.txt new file mode 100644 index 0000000..b9e06a5 --- /dev/null +++ b/tests/maprule/common.txt @@ -0,0 +1 @@ +common file diff --git a/tests/maprule/foo.txt b/tests/maprule/foo.txt new file mode 100644 index 0000000..3f9a7b1 --- /dev/null +++ b/tests/maprule/foo.txt @@ -0,0 +1 @@ +foo file diff --git a/tests/maprule/maprule_tests.sh b/tests/maprule/maprule_tests.sh new file mode 100755 index 0000000..660021b --- /dev/null +++ b/tests/maprule/maprule_tests.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# --- begin runfiles.bash initialization --- +set -euo pipefail +if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + if [[ -f "$0.runfiles_manifest" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" + elif [[ -f "$0.runfiles/MANIFEST" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" + elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then + export RUNFILES_DIR="$0.runfiles" + fi +fi +if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then + source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" +elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ + "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" +else + echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" + exit 1 +fi +# --- end runfiles.bash initialization --- + +source "$(rlocation bazel_skylib/tests/unittest.bash)" \ + || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } + +function test_bash_maprule() { + local -r out1foo="$(rlocation bazel_skylib/tests/maprule/mr_bash_out/tests/maprule/foo.txt.out1)" + local -r out1bar="$(rlocation bazel_skylib/tests/maprule/mr_bash_out/tests/maprule/b/bar.txt.out1)" + local -r out2foo="$(rlocation bazel_skylib/tests/maprule/mr_bash_out/out2/foo)" + local -r out2bar="$(rlocation bazel_skylib/tests/maprule/mr_bash_out/out2/bar)" + + cat "$out1foo" | tr '\n\r' ' ' > "$TEST_log" + expect_log "^tests/maprule/common.txt *tests/maprule/foo.txt *$" + + cat "$out1bar" | tr '\n\r' ' ' > "$TEST_log" + expect_log "^tests/maprule/common.txt *tests/maprule/b/bar.txt *$" + + cat "$out2foo" | tr '\n\r' ' ' > "$TEST_log" + expect_log "^common file *foo file *$" + + cat "$out2bar" | tr '\n\r' ' ' > "$TEST_log" + expect_log "^common file *bar file *$" +} + +run_suite "maprule test suite" diff --git a/tests/maprule/mr_bash_tool.sh b/tests/maprule/mr_bash_tool.sh new file mode 100755 index 0000000..843eb63 --- /dev/null +++ b/tests/maprule/mr_bash_tool.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -euo pipefail + +echo "$MAPRULE_SRCS" "$MAPRULE_SRC" > "$MAPRULE_OUT1" +cat "$MAPRULE_SRCS" "$MAPRULE_SRC" > "$MAPRULE_OUT2"