test.sh 3.2 KB

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