test-ref 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. set -eo pipefail
  3. FAILED_TXT="${RED}[FAILED]${NC}"
  4. SUCCESS_TXT="${GREEN}[SUCCESS]${NC}"
  5. # Check $TESTS_DIR is given
  6. if [[ -z "$TESTS_DIR" ]]; then
  7. echo "Must provide \$TESTS_DIR env variable"
  8. exit 1
  9. fi
  10. # Check $INVOCATION_ID is given: we use it as cache key to be sure compiled tester-lcmp is actual (see file $TESTS_DIR/tester-lcmp/tester-lcmp.$INVOCATION_ID)
  11. if [[ -z "$INVOCATION_ID" ]]; then
  12. echo "Must provide \$INVOCATION_ID env variable"
  13. exit 1
  14. fi
  15. mkdir -p "$TESTS_DIR"/tester-lcmp
  16. # If compiled tester-lcmp is not actual
  17. if [ ! -f "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" ]; then
  18. # Clear
  19. rm -f "$TESTS_DIR"/tester-lcmp/*
  20. # Compile it
  21. CPP_INCLUDE_DIR_=$CPP_INCLUDE_DIR
  22. export CPP_INCLUDE_DIR=$TESTS_DIR/lib
  23. bash "$TESTS_DIR"/scripts/compile "$TESTS_DIR"/src/tester-lcmp.cpp
  24. export CPP_INCLUDE_DIR=$CPP_INCLUDE_DIR_
  25. # Move from current dir to /tester-lcmp
  26. if [ ! -d ./tester-lcmp ]; then
  27. mv -f ./tester-lcmp "$TESTS_DIR"/tester-lcmp
  28. else
  29. mv -f ./tester-lcmp.exe "$TESTS_DIR"/tester-lcmp
  30. fi
  31. # Create file to inform that it was compiled to this $INVOCATION_ID
  32. touch "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID"
  33. fi
  34. # First argument as a subdirectory in ./refs
  35. ref_dir=$1
  36. if [[ -z "$ref_dir" ]]; then
  37. echo "Must provide \$1 for test-ref"
  38. exit 1
  39. fi
  40. refs=refs/$1
  41. shift 1
  42. # Check if we don't have invocation reference files
  43. if [ ! -d "$refs" ]; then
  44. if [[ "$TEST_REF_FORBID_GEN_REFS" == "true" ]]; then
  45. echo "You forgot to run push ref files for invocation: "$*""
  46. echo "Run test locally, it will produce ref files and push it into the repo"
  47. exit 1
  48. fi
  49. # Create them
  50. mkdir -p "$refs"
  51. echo Generating tester refs: "$*"
  52. exit_code=0
  53. # shellcheck disable=SC2048
  54. $* 1>"$refs"/stdout 2>"$refs"/stderr || exit_code=$?
  55. echo $exit_code >"$refs"/exit_code
  56. echo -e "${SUCCESS_TXT} generated execution reference files (exit code: $exit_code)\n"
  57. else
  58. # Do invocation
  59. echo Testing refs: "$*"
  60. exit_code=0
  61. # shellcheck disable=SC2048
  62. $* 1>"$refs"/stdout.aux 2>"$refs"/stderr.aux || exit_code=$?
  63. echo $exit_code >"$refs"/exit_code.aux
  64. echo ---
  65. cat "$refs"/stdout.aux
  66. echo ---
  67. cat "$refs"/stderr.aux
  68. echo ---
  69. cat "$refs"/exit_code.aux
  70. echo ---
  71. # Check exit code is the same
  72. tester_lcmp_exit_code=0
  73. "$TESTS_DIR"/tester-lcmp/tester-lcmp "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" "$refs"/exit_code.aux "$refs"/exit_code 2>tester-lcmp.out || tester_lcmp_exit_code=$?
  74. if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
  75. echo -e "${FAILED_TXT} exit_code mismatched: found $(cat < "$refs"/exit_code.aux) but expected $(cat "$refs"/exit_code)\n"
  76. cat tester-lcmp.out
  77. rm -f tester-lcmp.out
  78. rm -f "$refs"/*.aux
  79. exit 1
  80. fi
  81. echo $exit_code >"$refs"/exit_code.aux
  82. # Check stdout is the same
  83. "$TESTS_DIR"/tester-lcmp/tester-lcmp "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" "$refs"/stdout.aux "$refs"/stdout 2>tester-lcmp.out || tester_lcmp_exit_code=$?
  84. if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
  85. echo -e "${FAILED_TXT} stdout mismatched\n"
  86. cat tester-lcmp.out
  87. rm -f tester-lcmp.out
  88. rm -f "$refs"/*.aux
  89. exit 1
  90. fi
  91. # Check stderr is the same
  92. "$TESTS_DIR"/tester-lcmp/tester-lcmp "$TESTS_DIR"/tester-lcmp/tester-lcmp."$INVOCATION_ID" "$refs"/stderr.aux "$refs"/stderr 2>tester-lcmp.out || tester_lcmp_exit_code=$?
  93. if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
  94. echo -e "${FAILED_TXT} stderr mismatched\n"
  95. cat tester-lcmp.out
  96. rm -f tester-lcmp.out
  97. rm -f "$refs"/*.aux
  98. exit 1
  99. fi
  100. # Invocation is the same!
  101. echo -e "${SUCCESS_TXT} execution matched with reference (exit code: $exit_code)\n"
  102. rm -f tester-lcmp.out
  103. rm -f "$refs"/*.aux
  104. fi