Skip to content

feat: Add an executable_test rule and use it in write_source_file #1090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ stardoc_with_diff_test(
bzl_library_target = "@aspect_bazel_lib//lib:diff_test",
)

stardoc_with_diff_test(
name = "executable_test",
bzl_library_target = "@aspect_bazel_lib//lib:executable_test",
)

stardoc_with_diff_test(
name = "expand_make_vars",
bzl_library_target = "@aspect_bazel_lib//lib:expand_make_vars",
Expand Down
33 changes: 33 additions & 0 deletions docs/executable_test.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions docs/write_source_files.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ bzl_library(
deps = ["//lib/private:diff_test"],
)

#keep
bzl_library(
name = "executable_test",
srcs = ["executable_test.bzl"],
deps = ["//lib/private:executable_test"],
)

bzl_library(
name = "write_source_files",
srcs = ["write_source_files.bzl"],
Expand Down
8 changes: 8 additions & 0 deletions lib/executable_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""A test rule that checks the executable permission on a file or directory."""

load(
"//lib/private:executable_test.bzl",
_executable_test = "executable_test",
)

executable_test = _executable_test
13 changes: 13 additions & 0 deletions lib/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ exports_files(
[
"diff_test_tmpl.sh",
"diff_test_tmpl.bat",
"executable_test_tmpl.sh",
"executable_test_tmpl.bat",
"modify_mtree.awk",
"parse_status_file.jq",
"parse_status_file.yq",
Expand Down Expand Up @@ -114,6 +116,16 @@ bzl_library(
],
)

#keep
bzl_library(
name = "executable_test",
srcs = ["executable_test.bzl"],
visibility = ["//lib:__subpackages__"],
deps = [
":directory_path",
],
)

bzl_library(
name = "docs",
srcs = ["docs.bzl"],
Expand Down Expand Up @@ -247,6 +259,7 @@ bzl_library(
deps = [
":diff_test",
":directory_path",
":executable_test",
":fail_with_message_test",
":utils",
],
Expand Down
94 changes: 94 additions & 0 deletions lib/private/executable_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""A test rule that checks the executable permission on a file or directory."""

load(":directory_path.bzl", "DirectoryPathInfo")

def _runfiles_path(f):
if f.root.path:
return f.path[len(f.root.path) + 1:] # generated file
else:
return f.path # source file

def _executable_test_impl(ctx):
is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])

if DirectoryPathInfo in ctx.attr.file:
file = ctx.attr.file1[DirectoryPathInfo].directory
file_path = "/".join([_runfiles_path(file), ctx.attr.file[DirectoryPathInfo].path])
else:
if len(ctx.files.file) != 1:
fail("file must be a single file or a target that provides a DirectoryPathInfo")
file = ctx.files.file[0]
file_path = _runfiles_path(file)

if is_windows:
test_suffix = "-test.bat"
template = ctx.file._executable_test_tmpl_bat
else:
test_suffix = "-test.sh"
template = ctx.file._executable_test_tmpl_sh

test_bin = ctx.actions.declare_file(ctx.label.name + test_suffix)
ctx.actions.expand_template(
template = template,
output = test_bin,
substitutions = {
"{name}": ctx.attr.name,
"{fail_msg}": ctx.attr.failure_message,
"{file}": file_path,
"{executable}": "true" if ctx.attr.executable else "",
"{build_file_path}": ctx.build_file_path,
},
is_executable = True,
)

return DefaultInfo(
executable = test_bin,
files = depset(direct = [test_bin]),
runfiles = ctx.runfiles(files = [test_bin, file]),
)

_executable_test = rule(
attrs = {
"failure_message": attr.string(),
"file": attr.label(
allow_files = True,
mandatory = True,
),
"executable": attr.bool(
mandatory = True,
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
"_executable_test_tmpl_sh": attr.label(
default = ":executable_test_tmpl.sh",
allow_single_file = True,
),
"_executable_test_tmpl_bat": attr.label(
default = ":executable_test_tmpl.bat",
allow_single_file = True,
),
},
test = True,
implementation = _executable_test_impl,
)

def executable_test(name, file, executable, size = "small", **kwargs):
"""A test that checks the executable permission on a file or directory.

The test succeeds if the executable permission matches <code>executable</code>.

On Windows, the test always succeeds.

Args:
name: The name of the test rule.
file: Label of the file to check.
executable: Boolean; whether the file should be executable.
size: standard attribute for tests
**kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
"""
_executable_test(
name = name,
file = file,
executable = executable,
size = size,
**kwargs
)
5 changes: 5 additions & 0 deletions lib/private/executable_test_tmpl.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@rem @generated by @aspect_bazel_lib//lib/private:executable_test.bzl
@echo off
:: No way to check the executable permission on Windows, so the test always
:: succeeds.
exit /b 0
81 changes: 81 additions & 0 deletions lib/private/executable_test_tmpl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
escape() {
echo "$1" |
sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g' |
awk 1 ORS='&#10;' # preserve newlines
}
fail() {
cat <<EOF >"${XML_OUTPUT_FILE:-/dev/null}"
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="$(escape "{name}")" tests="1" failures="1">
<testsuite name="$(escape "{name}")" tests="1" failures="1" id="0">
<testcase name="$(escape "{name}")" assertions="1" status="failed">
<failure message="$(escape "$1")" type="diff"></failure>
</testcase>
</testsuite>
</testsuites>
EOF
echo >&2 "FAIL: $1"
exit 1
}
resolve_exec_root() {
local RUNFILES_PARENT
RUNFILES_PARENT=$(dirname "$RUNFILES_DIR")
local BIN_DIR
BIN_DIR="${RUNFILES_PARENT%$BUILD_FILE_DIR}"
local EXEC_ROOT
EXEC_ROOT=$(dirname $(dirname $(dirname "${BIN_DIR}")))

echo -n "$EXEC_ROOT"
}
find_file() {
local F_RAW="$1"
local F="$2"
local RF=

if [[ -f "$TEST_SRCDIR/$F1" || -d "$TEST_SRCDIR/$F" ]]; then
RF="$TEST_SRCDIR/$F"
elif [[ -d "${RUNFILES_DIR:-/dev/null}" && "${RUNFILES_MANIFEST_ONLY:-}" != 1 ]]; then
EXEC_ROOT=$(resolve_exec_root)
if [[ -e "$EXEC_ROOT/$F_RAW" ]]; then
RF="$EXEC_ROOT/$F_RAW"
else
RF="$RUNFILES_DIR/$F1"
fi
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
RF="$(grep -F -m1 "$F " "$RUNFILES_MANIFEST_FILE" | sed 's/^[^ ]* //')"
else
echo >&2 "ERROR: could not find \"${F_RAW}\""
exit 1
fi

echo -n "$RF"
}
BUILD_FILE_DIR="$(dirname "{build_file_path}")"
F1="{file}"
[[ "$F1" =~ ^external/ ]] && F1="${F1#external/}" || F1="$TEST_WORKSPACE/$F1"
RF1="$(find_file {file} "$F1")"
DF1=
[[ ! -d "$RF1" ]] || DF1=1
if [[ "$DF1" ]]; then
if [[ -n '{executable}' ]]; then
if [[ -n "$(find -L "$RF1" ! -executable ! -type d)" ]]; then
fail "some files in directory \"{file}\" are not executable, but should be. {fail_msg}"
fi
else
if [[ -n "$(find -L "$RF1" -executable ! -type d)" ]]; then
fail "some files in directory \"{file}\" are executable, but shouldn't be. {fail_msg}"
fi
fi
else
if [[ -n '{executable}' ]]; then
if [[ ! -x "$RF1" ]]; then
fail "file \"{file}\" is not executable, but should be. {fail_msg}"
fi
else
if [[ -x "$RF1" ]]; then
fail "file \"{file}\" is executable, but shouldn't be. {fail_msg}"
fi
fi
fi
Loading
Loading