test.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. # Default test function, run by `npm test`.
  50. function run_tests {
  51. find pages* -name '*.md' -exec markdownlint {} +
  52. tldr-lint ./pages
  53. for f in ./pages.*; do
  54. checks="TLDR003,TLDR004,TLDR015,TLDR104"
  55. if [[ -L $f ]]; then
  56. continue
  57. elif [[ $f == *zh* || $f == *zh_TW* ]]; then
  58. checks+=",TLDR005"
  59. fi
  60. tldr-lint --ignore $checks "${f}"
  61. done
  62. run_black
  63. run_flake8
  64. }
  65. # Special test function for GitHub Actions pull request builds.
  66. # Runs run_tests collecting errors for tldr-bot.
  67. function run_tests_pr {
  68. errs=$(run_tests 2>&1)
  69. if [[ -n $errs ]]; then
  70. echo -e "Test failed!\n$errs\n" >&2
  71. echo 'Sending errors to tldr-bot.' >&2
  72. echo -n "$errs" | python3 scripts/send-to-bot.py report-errors
  73. exit 1
  74. fi
  75. }
  76. # Additional checks for GitHub Actions pull request builds.
  77. # Only taken as suggestions, does not make the build fail.
  78. function run_checks_pr {
  79. msgs=$(bash scripts/check-pr.sh)
  80. if [[ -n $msgs ]]; then
  81. echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2
  82. echo 'Sending check results to tldr-bot.' >&2
  83. echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results
  84. fi
  85. }
  86. ###################################
  87. # MAIN
  88. ###################################
  89. if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then
  90. run_checks_pr
  91. run_tests_pr
  92. else
  93. set -e
  94. run_tests
  95. fi
  96. echo 'Test ran successfully!'