Skip to main content

sh_test

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

Details

NOTE: This rule is not currently supported on Windows.

Function Signature

def sh_test(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
_inject_test_env: str = "prelude//test/tools:inject_test_env",
_remote_test_execution_toolchain: str = "gh_facebook_buck2_shims_meta//:remote_test_execution",
_test_toolchain: str = "gh_facebook_buck2_shims_meta//:test",
args: list[str] = [],
constraint_overrides: list[None | str] = [],
contacts: list[str] = [],
default_host_platform: None | str = None,
deps: list[str] = [],
env: dict[str, str] = {},
labels: list[str] = [],
licenses: list[str] = [],
list_args: None | list[str] = None,
list_env: None | dict[str, str] = None,
platform_override: None | str = None,
remote_execution: None | str | dict[str, None | bool | int | str | list[dict[str, str]] | dict[str, str | list[str]]] = None,
resources: list[str] = [],
run_args: list[str] = [],
run_env: dict[str, str] = {},
run_test_separately: bool = False,
test: None | str = None,
test_rule_timeout_ms: None | int = None,
type: None | str = None,
) -> None

Parameters

  • name: (required)

    name of the target

  • default_target_platform: (defaults to: None)

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

  • target_compatible_with: (defaults to: [])

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

  • compatible_with: (defaults to: [])

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

  • exec_compatible_with: (defaults to: [])

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

  • visibility: (defaults to: [])

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

  • within_view: (defaults to: ["PUBLIC"])

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

  • metadata: (defaults to: {})

    a key-value map of metadata associated with this target

  • tests: (defaults to: [])

    a list of targets that provide tests for this one

  • modifiers: (defaults to: [])

    an array of modifiers associated with this target

  • args: (defaults to: [])

    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.

  • contacts: (defaults to: [])

    A list of organizational contacts for this rule. These could be individuals who you would contact in the event of a failure or other issue with the rule.

    contacts = [ 'Joe Sixpack', 'Erika Mustermann' ]
  • env: (defaults to: {})

    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.

  • labels: (defaults to: [])

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query().

  • licenses: (defaults to: [])

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query

  • test: (defaults to: None)

    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: (defaults to: None)

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

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