diff --git a/yarn-project/noir-contracts/Dockerfile.build b/yarn-project/noir-contracts/Dockerfile.build index b946f891009..d82539127fa 100644 --- a/yarn-project/noir-contracts/Dockerfile.build +++ b/yarn-project/noir-contracts/Dockerfile.build @@ -21,3 +21,4 @@ ENV PATH="/usr/src/yarn-project/noir-contracts/.nargo/bin:${PATH}" RUN ./scripts/install_noir.sh RUN ./scripts/install_noir_backend.sh RUN ./scripts/compile_ci.sh +RUN ./scripts/nargo_test_ci.sh diff --git a/yarn-project/noir-contracts/scripts/get_all_libraries.sh b/yarn-project/noir-contracts/scripts/get_all_libraries.sh new file mode 100755 index 00000000000..e1aa684e3fa --- /dev/null +++ b/yarn-project/noir-contracts/scripts/get_all_libraries.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# Utility to get the names of all noir libaries located in ../noir-libs +echo $(ls -d ../noir-libs/*/Nargo.toml | sed -r "s/..\\/noir-libs\\/(.+)\\/Nargo.toml/\\1/") \ No newline at end of file diff --git a/yarn-project/noir-contracts/scripts/nargo_test.sh b/yarn-project/noir-contracts/scripts/nargo_test.sh new file mode 100755 index 00000000000..5c90fef4d20 --- /dev/null +++ b/yarn-project/noir-contracts/scripts/nargo_test.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +# Tests noir contracts, if multiple are provided, then they are testing in parallel, bubbling any testing errors +# +# Usage: +# If testing a single contract: +# ./scripts/test.sh CONTRACT +# If testing multiple contracts: +# ./scripts/test.sh CONTRACT ... +# If testing a library: +# ./scripts/test.sh LIB +# If testing multiple libraries: +# ./scripts/test.sh LIB ... + +ROOT=$(pwd) + +# Get the project type from the first argument +PROJECT_TYPE=$1 +shift + +# Error flag file +error_file="/tmp/error.$$" +# Array of child PIDs +pids=() + +# Handler for SIGCHLD, cleanup if child exit with error +handle_sigchld() { + for pid in "${pids[@]}"; do + # If process is no longer running + if ! kill -0 "$pid" 2>/dev/null; then + # Wait for the process and get exit status + wait "$pid" + status=$? + + # If exit status is error + if [ $status -ne 0 ]; then + # Create error file + touch "$error_file" + fi + fi + done +} + +# Set SIGCHLD handler +trap handle_sigchld SIGCHLD # Trap any ERR signal and call the custom error handler + +test() { + PROJECT_NAME=$1 + + if [ "$PROJECT_TYPE" == "CONTRACT" ]; then + CONTRACT_FOLDER="${PROJECT_NAME}_contract" + echo "Testing contract $PROJECT_NAME..." + cd src/contracts/$CONTRACT_FOLDER + nargo test + else + echo "Testing library $PROJECT_NAME..." + cd ../noir-libs/$PROJECT_NAME + nargo test + fi +} + +echo "Using $(nargo --version)" + +# Build contracts +for PROJECT_NAME in "$@"; do + test $PROJECT_NAME & + pids+=($!) +done + +# Wait for all background processes to finish +wait + +# If error file exists, exit with error +if [ -f "$error_file" ]; then + rm "$error_file" + echo "Error occurred in one or more child processes. Exiting..." + exit 1 +fi diff --git a/yarn-project/noir-contracts/scripts/nargo_test_ci.sh b/yarn-project/noir-contracts/scripts/nargo_test_ci.sh new file mode 100755 index 00000000000..103e92c18b8 --- /dev/null +++ b/yarn-project/noir-contracts/scripts/nargo_test_ci.sh @@ -0,0 +1,7 @@ + +#!/bin/bash + +# Runs tests scripts for all contracts, then for all libraries. + +./scripts/nargo_test.sh CONTRACT $(./scripts/get_all_contracts.sh) +./scripts/nargo_test.sh LIB $(./scripts/get_all_libraries.sh) \ No newline at end of file