diff --git a/macro_compilation_failure_tests/assert_macro_compilation_failure.sh b/macro_compilation_failure_tests/assert_macro_compilation_failure.sh index 1828f1d3..f7385667 100755 --- a/macro_compilation_failure_tests/assert_macro_compilation_failure.sh +++ b/macro_compilation_failure_tests/assert_macro_compilation_failure.sh @@ -1,55 +1,85 @@ #!/usr/bin/env bash -# Within failure_contracts, we have contracts that should fail compilation due to code in the macros -# This script will test that compilation fails for each of the contracts in failure_contracts +# File: test_failure_contracts.sh +# Description: This script tests that compilation fails for contracts within the +# 'failure_contracts' directory, as they contain code designed to break compilation +# (e.g., due to code generated by macros). + +# --- Configuration and Environment --- + +# Resolve the absolute path to the repository root. REPO=$(git rev-parse --show-toplevel) + +# Define the path to the Nargo binary. Allow override via environment variable. +# :? makes sure $NARGO is set, if not, it uses the default path. NARGO=${NARGO:-"$REPO/noir/noir-repo/target/release/nargo"} +FAILURE_CONTRACTS_DIR="$(dirname "${BASH_SOURCE[0]}")/failure_contracts" -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -NC='\033[0m' # No Color +# --- Colors for Output --- +readonly RED='\033[0;31m' +readonly GREEN='\033[0;32m' +readonly YELLOW='\033[0;33m' +readonly NC='\033[0m' # No Color -# Initialize counters +# --- Counters --- total_tests=0 passed_tests=0 -# Function to test compilation failure +# --- Core Function --- + +# Function to test compilation failure for a given contract directory. test_compilation_failure() { - local contract_dir=$1 + local contract_dir="$1" ((total_tests++)) - echo "Testing compilation failure for: $contract_dir" + echo -e "${YELLOW}-> Testing compilation failure for: $(basename "$contract_dir")${NC}" + + # Use pushd/popd for cleaner directory management and error handling. + pushd "$contract_dir" > /dev/null - # Try to compile the contract - if cd "$contract_dir" && $NARGO compile --pedantic-solving 2>/dev/null; then - echo -e "${RED}❌ Test failed: Compilation succeeded when it should have failed for $contract_dir${NC}" - cd - > /dev/null + # Try to compile the contract. + # We redirect stderr (2>) to a temporary file instead of /dev/null to inspect the error message if needed. + # The '|| true' prevents the whole script from exiting if the command fails, which is expected here. + if "$NARGO" compile --pedantic-solving 2> compile_error.log; then + # Check if the compilation succeeded (exit code 0). This is the failure case. + echo -e "${RED}❌ Test FAILED: Compilation succeeded when it should have failed.${NC}" + popd > /dev/null return 1 else - echo -e "${GREEN}✓ Test passed: Compilation failed as expected for $contract_dir${NC}" - cd - > /dev/null + # Check if the compilation failed (non-zero exit code). This is the success case. + echo -e "${GREEN}✓ Test PASSED: Compilation failed as expected.${NC}" + # Cleanup the temporary error log file. + rm -f compile_error.log ((passed_tests++)) + popd > /dev/null return 0 fi } -# Get the directory where the script is located -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -FAILURE_CONTRACTS_DIR="$SCRIPT_DIR/failure_contracts" +# --- Main Execution --- + +# Check if the Nargo binary exists before running tests +if [ ! -x "$NARGO" ]; then + echo -e "${RED}Error: Nargo binary not found at '$NARGO'. Please build it or set the NARGO environment variable.${NC}" + exit 1 +fi -# Test each contract in the failure_contracts directory +# Test each subdirectory (contract) in the failure_contracts directory for contract in "$FAILURE_CONTRACTS_DIR"/*; do if [ -d "$contract" ]; then test_compilation_failure "$contract" fi done -# Print summary -echo -e "\nTest Summary:" -echo -e "Total tests: $total_tests" -echo -e "Passed tests: $passed_tests" +# --- Summary and Exit --- +echo -e "\n--- Test Summary ---" +echo -e "Total tests: ${total_tests}" +echo -e "Passed tests: ${passed_tests}" echo -e "Failed tests: $((total_tests - passed_tests))" -# Exit with failure if any test didn't pass -[ "$total_tests" -eq "$passed_tests" ] || exit 1 +# Exit with status 0 if all tests passed, 1 otherwise. +if [ "$total_tests" -eq "$passed_tests" ]; then + exit 0 +else + exit 1 +fi