Buck: sh_test()

sh_test()

This is liable to change in the future.

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

NOTE: This rule is not currently supported on Windows.

Arguments

  • name (required) #

    The name of the sh_test.

  • test (required) #

    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.

  • 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.

  • 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.

  • type (defaults to None) #

    If provided, this will be sent to any configured external runner

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule as a dependency, for example, by listing it in their deps or exported_deps attributes. For more information, see visibility.

  • 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.

  • 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 attrfilter.

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 [email protected]; 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