test.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bash
  2. # This script is executed by GitHub Actions for every successful push (on any branch, PR or not).
  3. # It runs some basic tests on pages. If the build is also a PR, additional
  4. # checks are run through the check-pr script, and any message or error is sent
  5. # to tldr-bot to be commented on the PR.
  6. #
  7. # NOTE: must be run from the repository root directory to correctly work!
  8. # NOTE: `set -e` is applied conditionally only if needed.
  9. # Default test function, ran by `npm test`.
  10. function run_tests {
  11. markdownlint pages*/**/*.md
  12. tldr-lint ./pages
  13. }
  14. # Special test function for GitHub Actions pull request builds.
  15. # Runs run_tests collecting errors for tldr-bot.
  16. function run_tests_pr {
  17. errs=$(run_tests 2>&1)
  18. if [ -n "$errs" ]; then
  19. echo -e "Test failed!\n$errs\n" >&2
  20. echo 'Sending errors to tldr-bot.' >&2
  21. echo -n "$errs" | python3 scripts/send-to-bot.py report-errors
  22. exit 1
  23. fi
  24. }
  25. # Additional checks for GitHub Actions pull request builds.
  26. # Only taken as suggestions, does not make the build fail.
  27. function run_checks_pr {
  28. msgs=$(bash scripts/check-pr.sh)
  29. if [ -n "$msgs" ]; then
  30. echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2
  31. echo 'Sending check results to tldr-bot.' >&2
  32. echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results
  33. fi
  34. }
  35. ###################################
  36. # MAIN
  37. ###################################
  38. if [ "$CI" = "true" ] && [ "$GITHUB_REPOSITORY" = "tldr-pages/tldr" ] && [ "$PULL_REQUEST_ID" != "" ]; then
  39. run_checks_pr
  40. run_tests_pr
  41. else
  42. set -e
  43. run_tests
  44. fi
  45. echo 'Test ran succesfully!'