test.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. function run_shellcheck {
  62. # skip shellcheck check if the command is not available in the system.
  63. if [[ $CI != true ]] && ! exists shellcheck; then
  64. echo "Skipping shellcheck check, command not available."
  65. return 0
  66. fi
  67. shellcheck --enable require-double-brackets,avoid-nullary-conditions,quote-safe-variables scripts/*.sh
  68. }
  69. # Default test function, run by `npm test`.
  70. function run_tests {
  71. find pages* -name '*.md' -exec markdownlint {} +
  72. tldr-lint ./pages
  73. for f in ./pages.*; do
  74. checks="TLDR104"
  75. # Skip the `pages.en` symlink.
  76. [[ -h $f ]] && continue
  77. case $f in
  78. *ar*|*bn*|*fa*|*hi*|*ja*|*ko*|*lo*|*ml*|*ne*|*ta*|*th*|*tr*)
  79. checks+=",TLDR003,TLDR004,TLDR015"
  80. ;;
  81. *zh*)
  82. checks+=",TLDR003,TLDR004,TLDR005,TLDR015"
  83. ;;
  84. esac
  85. tldr-lint --ignore "$checks" "$f"
  86. done
  87. run_black
  88. run_flake8
  89. run_pytest
  90. run_shellcheck
  91. }
  92. # Special test function for GitHub Actions pull request builds.
  93. # Runs run_tests collecting errors for tldr-bot.
  94. function run_tests_pr {
  95. errs=$(run_tests 2>&1)
  96. if [[ -n $errs ]]; then
  97. echo -e "Test failed!\n$errs\n" >&2
  98. echo 'Sending errors to tldr-bot.' >&2
  99. echo -n "$errs" | python3 scripts/send-to-bot.py report-errors
  100. exit 1
  101. fi
  102. }
  103. # Additional checks for GitHub Actions pull request builds.
  104. # Only taken as suggestions, does not make the build fail.
  105. function run_checks_pr {
  106. msgs=$(bash scripts/check-pr.sh)
  107. if [[ -n $msgs ]]; then
  108. echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2
  109. echo 'Sending check results to tldr-bot.' >&2
  110. echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results
  111. fi
  112. }
  113. ###################################
  114. # MAIN
  115. ###################################
  116. if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then
  117. run_checks_pr
  118. run_tests_pr
  119. else
  120. set -e
  121. run_tests
  122. fi
  123. echo 'Test ran successfully!'