test.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. # Default test function, ran by `npm test`.
  11. function run_tests {
  12. markdownlint pages*/**/*.md
  13. tldr-lint ./pages
  14. for f in ./pages.*; do
  15. tldr-lint --ignore "TLDR003,TLDR004,TLDR005,TLDR015,TLDR104" ${f}
  16. done
  17. }
  18. # Special test function for GitHub Actions pull request builds.
  19. # Runs run_tests collecting errors for tldr-bot.
  20. function run_tests_pr {
  21. errs=$(run_tests 2>&1)
  22. if [ -n "$errs" ]; then
  23. echo -e "Test failed!\n$errs\n" >&2
  24. echo 'Sending errors to tldr-bot.' >&2
  25. echo -n "$errs" | python3 scripts/send-to-bot.py report-errors
  26. exit 1
  27. fi
  28. }
  29. # Additional checks for GitHub Actions pull request builds.
  30. # Only taken as suggestions, does not make the build fail.
  31. function run_checks_pr {
  32. msgs=$(bash scripts/check-pr.sh)
  33. if [ -n "$msgs" ]; then
  34. echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2
  35. echo 'Sending check results to tldr-bot.' >&2
  36. echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results
  37. fi
  38. }
  39. ###################################
  40. # MAIN
  41. ###################################
  42. if [ "$CI" = "true" ] && [ "$GITHUB_REPOSITORY" = "tldr-pages/tldr" ] && [ "$PULL_REQUEST_ID" != "" ]; then
  43. run_checks_pr
  44. run_tests_pr
  45. else
  46. set -e
  47. run_tests
  48. fi
  49. echo 'Test ran succesfully!'