about summary refs log tree commit diff
path: root/third_party/bazel/rules_haskell/tests/inline_tests.bzl
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-07-04T10·18+0100
committerVincent Ambo <tazjin@google.com>2019-07-04T10·18+0100
commitf723b8b878a3c4a4687b9e337a875500bebb39b1 (patch)
treee85204cf042c355e90cff61c111e7d8cd15df311 /third_party/bazel/rules_haskell/tests/inline_tests.bzl
parent2eb1dc26e42ffbdc168f05ef744bd4b4f3e4c36f (diff)
feat(third_party/bazel): Check in rules_haskell from Tweag r/17
Diffstat (limited to 'third_party/bazel/rules_haskell/tests/inline_tests.bzl')
-rw-r--r--third_party/bazel/rules_haskell/tests/inline_tests.bzl90
1 files changed, 90 insertions, 0 deletions
diff --git a/third_party/bazel/rules_haskell/tests/inline_tests.bzl b/third_party/bazel/rules_haskell/tests/inline_tests.bzl
new file mode 100644
index 000000000000..b1ba001c6ec7
--- /dev/null
+++ b/third_party/bazel/rules_haskell/tests/inline_tests.bzl
@@ -0,0 +1,90 @@
+# features sh_inline_test and py_inline_test,
+# which are like their respective builtin rules,
+# but their scripts can be given inline as a string.
+
+load("@bazel_skylib//lib:shell.bzl", "shell")
+
+def quote_make_variables(s):
+    """Quote all genrule “Make” Variables in a string."""
+    return s.replace("$", "$$")
+
+def target_from_string(name, string):
+    """Write a skylark string to a target."""
+    native.genrule(
+        name = name + "-file",
+        outs = [name],
+        # this is exceptionally ugly.
+        cmd = """echo -n {quoted} > $(@)""".format(
+            # but should at least be quoted right
+            quoted = shell.quote(quote_make_variables(string)),
+        ),
+    )
+
+bash_runfiles_boilerplate = """\
+# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
+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 ---
+"""
+
+def sh_inline_test(name, script, **kwargs):
+    """Like sh_test, but instead of srcs takes the shell script
+    as verbatim bazel string. The bash runfiles are in scope,
+    using `rlocation` works by default.
+    """
+    script_name = name + ".sh"
+    script = bash_runfiles_boilerplate + script
+
+    target_from_string(script_name, script)
+
+    deps = kwargs.pop("deps", [])
+
+    native.sh_test(
+        name = name,
+        srcs = [script_name],
+        deps = ["@bazel_tools//tools/bash/runfiles"] + deps,
+        **kwargs
+    )
+
+python_runfiles_boilerplate = """
+from bazel_tools.tools.python.runfiles import runfiles
+r = runfiles.Create()
+"""
+
+def py_inline_test(name, script, **kwargs):
+    """Like py_test, but instead of srcs takes the shell script
+    as verbatim bazel string. The python runfiles are in scope
+    as the `r` variable. Use `r.Rlocation()`
+    """
+    script_name = name + ".py"
+    script = python_runfiles_boilerplate + script
+
+    target_from_string(script_name, script)
+
+    deps = kwargs.pop("deps", [])
+    srcs = kwargs.pop("srcs", [])
+
+    native.py_test(
+        name = name,
+        srcs = [script_name] + srcs,
+        main = script_name,
+        deps = ["@bazel_tools//tools/python/runfiles"] + deps,
+        **kwargs
+    )