1
0

test.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. # skip black check if command is not available in the system.
  19. if [ "$CI" != "true" ] && ! exists black; then
  20. echo "Skipping black check, command not available."
  21. return 0
  22. fi
  23. # we want to ignore the exit code from black on failure, so that we can
  24. # do the conditional printing below
  25. errs=$(black scripts --check 2>&1 || true)
  26. if [[ ${errs} != "All done!"* ]]; then
  27. echo -e "${errs}" >&2
  28. return 1
  29. fi
  30. }
  31. function run_flake8 {
  32. # skip flake8 check if command is not available in the system.
  33. if [ "$CI" != "true" ] && ! exists flake8; then
  34. echo "Skipping flake8 check, command not available."
  35. return 0
  36. fi
  37. flake8 scripts
  38. }
  39. # Default test function, ran by `npm test`.
  40. function run_tests {
  41. markdownlint pages*/**/*.md
  42. tldr-lint ./pages
  43. for f in ./pages.*; do
  44. tldr-lint --ignore "TLDR003,TLDR004,TLDR005,TLDR015,TLDR104" "${f}"
  45. done
  46. run_black
  47. run_flake8
  48. }
  49. # Special test function for GitHub Actions pull request builds.
  50. # Runs run_tests collecting errors for tldr-bot.
  51. function run_tests_pr {
  52. errs=$(run_tests 2>&1)
  53. if [ -n "$errs" ]; then
  54. echo -e "Test failed!\n$errs\n" >&2
  55. echo 'Sending errors to tldr-bot.' >&2
  56. echo -n "$errs" | python3 scripts/send-to-bot.py report-errors
  57. exit 1
  58. fi
  59. }
  60. # Additional checks for GitHub Actions pull request builds.
  61. # Only taken as suggestions, does not make the build fail.
  62. function run_checks_pr {
  63. msgs=$(bash scripts/check-pr.sh)
  64. if [ -n "$msgs" ]; then
  65. echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2
  66. echo 'Sending check results to tldr-bot.' >&2
  67. echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results
  68. fi
  69. }
  70. ###################################
  71. # MAIN
  72. ###################################
  73. if [ "$CI" = "true" ] && [ "$GITHUB_REPOSITORY" = "tldr-pages/tldr" ] && [ "$PULL_REQUEST_ID" != "" ]; then
  74. run_checks_pr
  75. run_tests_pr
  76. else
  77. set -e
  78. run_tests
  79. fi
  80. echo 'Test ran successfully!'