Skip to main content

sh_test

name

def name(
*,
name: str,
default_target_platform: None | str = ...,
target_compatible_with: list[str] = ...,
compatible_with: list[str] = ...,
exec_compatible_with: list[str] = ...,
visibility: list[str] = ...,
within_view: list[str] = ...,
metadata: OpaqueMetadata = ...,
tests: list[str] = ...,
modifiers: OpaqueMetadata = ...,
_apple_platforms: dict[str, str] = ...,
_inject_test_env: str = ...,
_remote_test_execution_toolchain: str = ...,
_test_toolchain: str = ...,
args: list[str] = ...,
constraint_overrides: list[str] = ...,
contacts: list[str] = ...,
default_host_platform: None | str = ...,
deps: list[str] = ...,
env: dict[str, str] = ...,
labels: list[str] = ...,
licenses: list[str] = ...,
list_args: None | list[str] = ...,
list_env: None | dict[str, str] = ...,
platform_override: None | str = ...,
remote_execution: None | str | dict[str, None | bool | int | str | list[dict[str, str]] | dict[str, str | list[str]]] = ...,
remote_execution_action_key_providers: None | str = ...,
resources: list[str] = ...,
run_args: list[str] = ...,
run_env: dict[str, str] = ...,
run_test_separately: bool = ...,
test: None | str = ...,
test_rule_timeout_ms: None | int = ...,
type: None | str = ...,
) -> None

A sh_test() is a test rule that can pass results to the test runner by invoking a shell script.

Parameters

  • name: name of the target

  • default_target_platform: specifies the default target platform, used when no platforms are specified on the command line

  • target_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • exec_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with an execution platform

  • visibility: a list of visibility patterns restricting what targets can depend on this one

  • within_view: a list of visibility patterns restricting what this target can depend on

  • metadata: a key-value map of metadata associated with this target

  • tests: a list of targets that provide tests for this one

  • modifiers: an array of modifiers associated with this target

  • args: The list of arguments to invoke this script with. These are literal values, and no shell interpolation is done.

    These can contain string parameter macros , for example, to give the location of a generated binary to the test script.

  • env: Environment variable overrides that should be used when running the script. The key is the variable name, and the value is its value.

    The values can contain string parameter macros such as the location of a generated binary to be used by the test script.

  • test: Either the path to the script (relative to the build file), or a build target. This file must be executable in order to be run.

  • type: If provided, this will be sent to any configured .buckconfig

Details

NOTE: This rule is not currently supported on Windows.

Examples:

This sh_test() fails if a string does not match a value.


# $REPO/BUCK
sh_test(
name = "script_pass",
test = "script.sh",
args = ["--pass"],
)

sh_test(
name = "script_fail",
test = "script.sh",
args = ["--fail"],
)



# Create a simple script that prints out the resource
$ cat > script.sh
#!/bin/sh
for arg in $@; do
if [ "$arg" == "--pass" ]; then
echo "Passed"
exit 0;
fi
done
echo "Failed"
exit 1

# Make sure the script is executable
$ chmod u+x script.sh

# Run the script, and see that one test passes, one fails
$ buck test //:script_pass //:script_fail
FAILURE script.sh sh_test
Building: finished in 0.0 sec (100%) 2/2 jobs, 0 updated
Total time: 0.0 sec
Testing: finished in 0.0 sec (1 PASS/1 FAIL)
RESULTS FOR //:script_fail //:script_pass
FAIL <100ms 0 Passed 0 Skipped 1 Failed //:script_fail
FAILURE script.sh sh_test
====STANDARD OUT====
Failed

PASS <100ms 1 Passed 0 Skipped 0 Failed //:script_pass
TESTS FAILED: 1 FAILURE
Failed target: //:script_fail
FAIL //:script_fail