test.sh 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. TEST_OK=0
  3. TEST_FAIL=0
  4. function test_end {
  5. echo ""
  6. echo ""
  7. printf "%-40s" "$1 test failures found"
  8. if [ $1 -gt 0 ]; then
  9. log_fail
  10. else
  11. log_ok
  12. fi
  13. return "$1"
  14. }
  15. function test_start {
  16. TEST_OK=0
  17. TEST_FAIL=0
  18. }
  19. function log_ok {
  20. echo -e "[ \033[0;32mPASS\033[0m ]"
  21. }
  22. function log_fail {
  23. echo -e "[ \033[0;31mFAIL\033[0m ]"
  24. }
  25. function expect_failure {
  26. temp_file=$(mktemp)
  27. printf "%-40s" "$1"
  28. shift
  29. $@ &> log.txt
  30. if [ $? -ne 0 ]; then
  31. log_ok
  32. TEST_OK=$((TEST_OK+1))
  33. else
  34. log_fail
  35. #cat ${temp_file}
  36. TEST_FAIL=$((TEST_FAIL+1))
  37. fi
  38. rm ${temp_file}
  39. }
  40. function expect_success {
  41. temp_file=$(mktemp)
  42. printf "%-40s" "$1"
  43. shift
  44. $@ &> ${temp_file}
  45. if [ $? -ne 0 ]; then
  46. log_fail
  47. #cat ${temp_file}
  48. TEST_FAIL=$((TEST_FAIL+1))
  49. else
  50. log_ok
  51. TEST_OK=$((TEST_OK+1))
  52. fi
  53. rm ${temp_file}
  54. }
  55. function test_commands {
  56. PNGSUITE="../resources/pngsuite/"
  57. for i in $(find $PNGSUITE -type f -name '*.png' | sort | grep -v pngsuite/x); do
  58. echo $0 expect_success "$(basename $i)" php read.php "$i"
  59. done
  60. for i in $(find $PNGSUITE -type f -name '*.png' | sort | grep pngsuite/x); do
  61. echo $0 expect_failure "$(basename $i)" php read.php "$i"&
  62. done
  63. }
  64. set -u -o pipefail
  65. if [ $# -eq 0 ]; then
  66. mkdir -p out/
  67. test_start
  68. test_commands | parallel --no-notice
  69. test_end $?
  70. else
  71. subcmd=$1
  72. shift
  73. test_start
  74. if [ $subcmd == "expect_success" ]; then
  75. expect_success $@
  76. elif [ $subcmd == "expect_failure" ]; then
  77. expect_failure $@
  78. fi
  79. exit $TEST_FAIL
  80. fi