test-ref 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. stdout_size=$(wc -c < "$refs"/stdout.aux)
  64. stderr_size=$(wc -c < "$refs"/stderr.aux)
  65. echo "Program exit code: $exit_code, stdout size: $stdout_size bytes, stderr size: $stderr_size bytes"
  66. echo $exit_code >"$refs"/exit_code.aux
  67. # Check exit code is the same
  68. tester_lcmp_exit_code=0
  69. "$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=$?
  70. if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
  71. echo -e "${FAILED_TXT} exit_code mismatched: found $(cat < "$refs"/exit_code.aux) but expected $(cat "$refs"/exit_code)\n"
  72. cat tester-lcmp.out
  73. rm -f tester-lcmp.out
  74. rm -f "$refs"/*.aux
  75. exit 1
  76. fi
  77. echo $exit_code >"$refs"/exit_code.aux
  78. # Check stdout is the same
  79. "$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=$?
  80. if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
  81. echo -e "${FAILED_TXT} stdout mismatched\n"
  82. cat tester-lcmp.out
  83. rm -f tester-lcmp.out
  84. rm -f "$refs"/*.aux
  85. exit 1
  86. fi
  87. # Check stderr is the same
  88. "$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=$?
  89. if [[ ! "$tester_lcmp_exit_code" == "0" ]]; then
  90. echo -e "${FAILED_TXT} stderr mismatched\n"
  91. cat tester-lcmp.out
  92. rm -f tester-lcmp.out
  93. rm -f "$refs"/*.aux
  94. exit 1
  95. fi
  96. # Invocation is the same!
  97. echo -e "${SUCCESS_TXT} execution matched with reference (exit code: $exit_code)\n"
  98. rm -f tester-lcmp.out
  99. rm -f "$refs"/*.aux
  100. fi