run_examples.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. RED='\033[0;31m'
  3. GREEN='\033[0;32m'
  4. NC='\033[0m' # No Color
  5. TAGFAIL=$RED'[FAIL]'$NC
  6. TAGPASS=$GREEN'[PASS]'$NC
  7. PROJECT_DIR=$(dirname $(cargo locate-project | awk -F\" '{print $4}'))
  8. # Some of the examples assume that the working directory is the project root
  9. # and it never hurts to force consistency regardless
  10. cd $PROJECT_DIR
  11. EXAMPLES_DIR=tests
  12. if [ -z "$TOOLCHAIN" ]; then
  13. TOOLCHAIN=$(rustc --version | sed 's/rustc [0-9\.\-]*\(.*\) (.*)/\1/')
  14. fi
  15. # Create expected output for fn-root-vars
  16. echo $HOME > $EXAMPLES_DIR/fn-root-vars.out # Overwrite previous file
  17. echo '${x::1B}]0;${USER}: ${PWD}${x::07}${c::0x55,bold}${USER}${c::default}:${c::0x4B}${SWD}${c::default}# ${c::reset}' >> $EXAMPLES_DIR/fn-root-vars.out
  18. id -u $USER >> $EXAMPLES_DIR/fn-root-vars.out
  19. echo >> $EXAMPLES_DIR/fn-root-vars.out
  20. test() {
  21. # Replace .ion with .out in file name
  22. EXPECTED_OUTPUT_FILE=$(echo $1 | sed 's/\..\+/\.out/')
  23. # Compare real and expected output
  24. if target/debug/ion "${@:2}" 2>&1 | diff - "$EXPECTED_OUTPUT_FILE"; then
  25. echo -e "Test ${1} ${TAGPASS}";
  26. return 0;
  27. else
  28. echo -e "Test ${1} ${TAGFAIL}";
  29. return 1;
  30. fi
  31. }
  32. test_generic() {
  33. ls -1 $EXAMPLES_DIR/*.ion | xargs -P $CPU_CORES -n 1 -I {} bash -c "test {} {} 1"
  34. }
  35. test_params() {
  36. ls -1 $EXAMPLES_DIR/*.params | xargs -P $CPU_CORES -n 1 -I {} bash -c "IFS=$'\\n'; test {} "'$(< {})'
  37. }
  38. set -e -u
  39. export -f test
  40. export TAGFAIL
  41. export TAGPASS
  42. export EXAMPLES_DIR
  43. CPU_CORES=$(nproc --all)
  44. # Build debug binary
  45. # Check if the variabl $RUSTUP is set. If yes the $TOOLCHAIN can be choosen via cargo, otherwise cargo might not now
  46. # about that feature.
  47. if [ -v RUSTUP ] && [ "${RUSTUP}" -eq 1 ]; then
  48. cargo +$TOOLCHAIN build
  49. else
  50. cargo build
  51. fi
  52. test_generic
  53. test_params
  54. set +u