Skip to content

Commit

Permalink
define shell test prelude (#27)
Browse files Browse the repository at this point in the history
Change-Id: I16491fc3679e923b8880f5d47d8f051b44897e6c
  • Loading branch information
oliverlee authored Jan 5, 2024
1 parent 0fd5b09 commit d8524da
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 74 deletions.
6 changes: 5 additions & 1 deletion rules/sh_binary_template.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def sh_binary_template(
substitutions,
template = None,
out = None,
srcs = []):
srcs = [],
deps = []):
"""
Combines expand_template with sh_binary
Expand All @@ -24,6 +25,8 @@ def sh_binary_template(
Key-value mappings.
srcs: string_label_list
`srcs` used for `sh_binary`
deps: string_label_list
`deps` used for `sh_binary`
"""
if len(srcs) == 1 and template == None:
template = srcs[0]
Expand All @@ -45,4 +48,5 @@ def sh_binary_template(
native.sh_binary(
name = name,
srcs = [binary_src(src) for src in srcs],
deps = deps,
)
12 changes: 1 addition & 11 deletions rules/skytest_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ Test stdout and return code of a unit test for skytest.
"""

load("@rules_cc//cc:defs.bzl", "cc_binary")
load(
"@local_config_info//:defs.bzl",
"BAZEL_BIN",
"BAZEL_EXTERNAL_DIR",
"BAZEL_WORKSPACE_ROOT",
"XDG_CACHE_HOME",
)
load(":sh_binary_template.bzl", "sh_binary_template")

def skytest_test(
Expand Down Expand Up @@ -108,13 +101,10 @@ echo "done" >> $@
elif binary_type == sh_binary_template:
binary_kwargs = {
"substitutions": {
"$BAZEL_BIN": BAZEL_BIN,
"$BAZEL_EXTERNAL_DIR": BAZEL_EXTERNAL_DIR,
"$BAZEL_WORKSPACE_ROOT": BAZEL_WORKSPACE_ROOT,
"$XDG_CACHE_HOME": XDG_CACHE_HOME,
"$CC_BINARY_CXXSTD": std,
"$CC_BINARY_MALLOC": malloc,
},
"deps": ["//test:prelude_sh"],
}
else:
fail("unhandled binary_type: {}".format(binary_type))
Expand Down
28 changes: 28 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
load("//rules:skytest_test.bzl", "skytest_test")
load("//rules:sh_binary_template.bzl", "sh_binary_template")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load(
"@local_config_info//:defs.bzl",
"BAZEL_BIN",
"BAZEL_EXTERNAL_DIR",
"BAZEL_WORKSPACE_ROOT",
"XDG_CACHE_HOME",
)

expand_template(
name = "gen_prelude_sh",
testonly = True,
out = "prelude.sh",
substitutions = {
"$BAZEL_BIN": BAZEL_BIN,
"$BAZEL_EXTERNAL_DIR": BAZEL_EXTERNAL_DIR,
"$BAZEL_WORKSPACE_ROOT": BAZEL_WORKSPACE_ROOT,
"$XDG_CACHE_HOME": XDG_CACHE_HOME,
},
tags = ["manual"],
template = "prelude.sh.tmpl",
visibility = ["//visibility:private"],
)

sh_library(
name = "prelude_sh",
srcs = ["prelude.sh"],
)

skytest_test(
name = "pass_test",
Expand Down
66 changes: 4 additions & 62 deletions test/noreturn_expect.sh.tmpl
Original file line number Diff line number Diff line change
@@ -1,60 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail

TEST_PATH="$(readlink -f ${BASH_SOURCE[0]})"
SCRIPT_NAME="${TEST_PATH##*/test/}}"
WORKSPACE="${SCRIPT_NAME%.*}"

cp "$BAZEL_WORKSPACE_ROOT/.bazeliskrc" .

cp "$BAZEL_WORKSPACE_ROOT/.common.bazelrc" .bazelrc

cat >> .bazelrc <<eof
startup --max_idle_secs=1
build --announce_rc
build --show_timestamps
eof

cat >WORKSPACE.bazel <<EOF
workspace(name = "$WORKSPACE")
local_repository(
name = "skytest",
path = "$BAZEL_WORKSPACE_ROOT",
)
local_repository(
name = "rules_cc",
path = "$BAZEL_EXTERNAL_DIR/rules_cc",
)
local_repository(
name = "bazel_bootlin",
path = "$BAZEL_EXTERNAL_DIR/bazel_bootlin",
)
local_repository(
name = "gcc_toolchain_files",
path = "$BAZEL_EXTERNAL_DIR/gcc_toolchain_files",
)
local_repository(
name = "gcc_toolchain",
path = "$BAZEL_EXTERNAL_DIR/gcc_toolchain",
)
register_toolchains(
"@gcc_toolchain//:toolchain",
)
EOF

cat >BUILD.bazel <<EOF
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
# include_prefix doesn't appear to work with Bazel 7
cc_library(
name = "external_skytest",
includes = ["external"],
deps = ["@skytest"],
)
source test/prelude.sh
prelude "${BASH_SOURCE[0]}"

cat >>BUILD.bazel <<EOF
cc_binary(
name = "noreturn_expect",
srcs = ["noreturn_expect.cpp"],
Expand Down Expand Up @@ -87,12 +37,4 @@ auto main() -> int
}
EOF

XDG_CACHE_HOME="$XDG_CACHE_HOME"
export XDG_CACHE_HOME

$BAZEL_BIN \
--nosystem_rc \
--nohome_rc \
build \
-s \
//:noreturn_expect
bazel build -s //:noreturn_expect
68 changes: 68 additions & 0 deletions test/prelude.sh.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
set -euo pipefail

function prelude() {

TEST_PATH="$(readlink -f $1)"
SCRIPT_NAME="${TEST_PATH##*/test/}}"
WORKSPACE="${SCRIPT_NAME%.*}"

cat >> .bazelrc <<EOF
startup --max_idle_secs=1
build --announce_rc
build --show_timestamps
EOF

cp "$BAZEL_WORKSPACE_ROOT/.bazeliskrc" .

cp "$BAZEL_WORKSPACE_ROOT/.common.bazelrc" .bazelrc

cat > WORKSPACE.bazel <<EOF
workspace(name = "$WORKSPACE")
local_repository(
name = "skytest",
path = "$BAZEL_WORKSPACE_ROOT",
)
local_repository(
name = "rules_cc",
path = "$BAZEL_EXTERNAL_DIR/rules_cc",
)
local_repository(
name = "bazel_bootlin",
path = "$BAZEL_EXTERNAL_DIR/bazel_bootlin",
)
local_repository(
name = "gcc_toolchain_files",
path = "$BAZEL_EXTERNAL_DIR/gcc_toolchain_files",
)
local_repository(
name = "gcc_toolchain",
path = "$BAZEL_EXTERNAL_DIR/gcc_toolchain",
)
register_toolchains(
"@gcc_toolchain//:toolchain",
)
EOF

cat >BUILD.bazel <<EOF
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
# include_prefix doesn't appear to work with Bazel 7
cc_library(
name = "external_skytest",
includes = ["external"],
deps = ["@skytest"],
)
EOF

}

function bazel() {

XDG_CACHE_HOME="$XDG_CACHE_HOME" \
$BAZEL_BIN --nosystem_rc --nohome_rc $@

}

0 comments on commit d8524da

Please sign in to comment.