1
0

test.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: MIT
  3. # This script is executed by GitHub Actions for every successful push (on any branch, PR or not).
  4. # It runs some basic tests on pages. If the build is also a PR, additional
  5. # checks are run through the check-pr script, and any message or error is sent
  6. # to tldr-bot to be commented on the PR.
  7. #
  8. # NOTE: must be run from the repository root directory to correctly work!
  9. # NOTE: `set -e` is applied conditionally only if needed.
  10. # check if a command is available to run in the system
  11. function exists {
  12. command -v "$1" >/dev/null 2>&1
  13. }
  14. # Wrapper around black as it outputs everything to stderr,
  15. # but we want to only print if there are actual errors, and not
  16. # the "All done!" success message.
  17. function run_black {
  18. target_black_version=$(awk -F '==' '$1 == "black" { print $2 }' < requirements.txt)
  19. if grep -qw black <<< "$(pip3 --disable-pip-version-check list)"; then
  20. errs=$(python3 -m black scripts --check --required-version ${target_black_version} 2>&1 || true)
  21. fi
  22. if [[ -z $errs ]]; then
  23. # skip the black check if the command is not available in the system.
  24. if [[ $CI != true ]] && ! exists black; then
  25. echo "Skipping black check, command not available."
  26. return 0
  27. fi
  28. errs=$(black scripts --check --required-version ${target_black_version} 2>&1 || true)
  29. fi
  30. if [[ ${errs} == *"does not match the running version"* ]]; then
  31. echo -e "Skipping black check, required version not available, try running: pip3 install -r requirements.txt"
  32. return 0
  33. fi
  34. # We want to ignore the exit code from black on failure so that we can
  35. # do the conditional printing below
  36. if [[ ${errs} != "All done!"* ]]; then
  37. echo -e "${errs}" >&2
  38. return 1
  39. fi
  40. }
  41. function run_flake8 {
  42. # skip flake8 check if the command is not available in the system.
  43. if [[ $CI != true ]] && ! exists flake8; then
  44. echo "Skipping flake8 check, command not available."
  45. return 0
  46. fi
  47. flake8 scripts
  48. }
  49. function run_pytest {
  50. # skip pytest check if the command is not available in the system.
  51. if [[ $CI != true ]] && ! exists pytest; then
  52. echo "Skipping pytest check, command not available."
  53. return 0
  54. fi
  55. errs=$(pytest scripts/*.py 2>&1 || true)
  56. if [[ ${errs} == *"failed"* ]]; then
  57. echo -e "${errs}" >&2
  58. return 1
  59. fi
  60. }
  61. # Default test function, run by `npm test`.
  62. function run_tests {
  63. find pages* -name '*.md' -exec markdownlint {} +
  64. tldr-lint ./pages
  65. for f in ./pages.*; do
  66. checks="TLDR104"
  67. if [[ -L $f ]]; then
  68. continue
  69. elif [[ $f == *ar* || $f == *bn* || $f == *fa* || $f == *hi* || $f == *ja* || $f == *ko* || $f == *lo* || $f == *ml* || $f == *ne* || $f == *ta* || $f == *th* || $f == *tr* ]]; then
  70. checks+=",TLDR003,TLDR004,TLDR015"
  71. elif [[ $f == *zh* || $f == *zh_TW* ]]; then
  72. checks+=",TLDR003,TLDR004,TLDR005,TLDR015"
  73. fi
  74. tldr-lint --ignore $checks "${f}"
  75. done
  76. run_black
  77. run_flake8
  78. run_pytest
  79. }
  80. # Special test function for GitHub Actions pull request builds.
  81. # Runs run_tests collecting errors for tldr-bot.
  82. function run_tests_pr {
  83. errs=$(run_tests 2>&1)
  84. if [[ -n $errs ]]; then
  85. echo -e "Test failed!\n$errs\n" >&2
  86. echo 'Sending errors to tldr-bot.' >&2
  87. echo -n "$errs" | python3 scripts/send-to-bot.py report-errors
  88. exit 1
  89. fi
  90. }
  91. # Additional checks for GitHub Actions pull request builds.
  92. # Only taken as suggestions, does not make the build fail.
  93. function run_checks_pr {
  94. msgs=$(bash scripts/check-pr.sh)
  95. if [[ -n $msgs ]]; then
  96. echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2
  97. echo 'Sending check results to tldr-bot.' >&2
  98. echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results
  99. fi
  100. }
  101. ###################################
  102. # MAIN
  103. ###################################
  104. if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then
  105. run_checks_pr
  106. run_tests_pr
  107. else
  108. set -e
  109. run_tests
  110. fi
  111. echo 'Test ran successfully!'