about summary refs log tree commit diff
path: root/third_party/bazel/rules_haskell/tests/inline_tests.bzl
blob: b1ba001c6ec750032e0fd7c53d45567b9473ae05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
    )