2019-11-15 04:32:04 +00:00
|
|
|
#!/usr/bin/env bash
|
2021-07-16 14:15:31 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
2019-11-15 04:32:04 +00:00
|
|
|
|
2020-06-09 15:03:35 +01:00
|
|
|
# This script is executed by GitHub Actions for every successful push (on any branch, PR or not).
|
2019-11-15 04:32:04 +00:00
|
|
|
# It runs some basic tests on pages. If the build is also a PR, additional
|
|
|
|
# checks are run through the check-pr script, and any message or error is sent
|
|
|
|
# to tldr-bot to be commented on the PR.
|
|
|
|
#
|
|
|
|
# NOTE: must be run from the repository root directory to correctly work!
|
|
|
|
# NOTE: `set -e` is applied conditionally only if needed.
|
|
|
|
|
2021-09-16 00:10:16 +01:00
|
|
|
# check if a command is available to run in the system
|
|
|
|
function exists {
|
|
|
|
command -v "$1" >/dev/null 2>&1
|
2021-09-03 16:17:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Wrapper around black as it outputs everything to stderr,
|
|
|
|
# but we want to only print if there are actual errors, and not
|
|
|
|
# the "All done!" success message.
|
|
|
|
function run_black {
|
2023-04-13 22:58:23 +01:00
|
|
|
target_black_version=$(awk -F '==' '$1 == "black" { print $2 }' < requirements.txt)
|
|
|
|
|
|
|
|
if grep -qw black <<< "$(pip3 --disable-pip-version-check list)"; then
|
|
|
|
errs=$(python3 -m black scripts --check --required-version ${target_black_version} 2>&1 || true)
|
|
|
|
fi
|
|
|
|
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ -z $errs ]]; then
|
2023-11-13 14:22:16 +00:00
|
|
|
# skip the black check if the command is not available in the system.
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ $CI != true ]] && ! exists black; then
|
2023-04-13 22:58:23 +01:00
|
|
|
echo "Skipping black check, command not available."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
errs=$(black scripts --check --required-version ${target_black_version} 2>&1 || true)
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ${errs} == *"does not match the running version"* ]]; then
|
|
|
|
echo -e "Skipping black check, required version not available, try running: pip3 install -r requirements.txt"
|
2021-09-16 00:10:16 +01:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2023-11-13 14:22:16 +00:00
|
|
|
# We want to ignore the exit code from black on failure so that we can
|
2021-09-03 16:17:51 +01:00
|
|
|
# do the conditional printing below
|
|
|
|
if [[ ${errs} != "All done!"* ]]; then
|
|
|
|
echo -e "${errs}" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
2019-11-15 04:32:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 00:10:16 +01:00
|
|
|
function run_flake8 {
|
2023-11-13 14:22:16 +00:00
|
|
|
# skip flake8 check if the command is not available in the system.
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ $CI != true ]] && ! exists flake8; then
|
2021-09-16 00:10:16 +01:00
|
|
|
echo "Skipping flake8 check, command not available."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
flake8 scripts
|
|
|
|
}
|
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
function run_pytest {
|
|
|
|
# skip pytest check if the command is not available in the system.
|
|
|
|
if [[ $CI != true ]] && ! exists pytest; then
|
|
|
|
echo "Skipping pytest check, command not available."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
errs=$(pytest scripts/*.py 2>&1 || true)
|
|
|
|
if [[ ${errs} == *"failed"* ]]; then
|
|
|
|
echo -e "${errs}" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-11-13 14:22:16 +00:00
|
|
|
# Default test function, run by `npm test`.
|
2021-09-16 00:10:16 +01:00
|
|
|
function run_tests {
|
2022-11-24 14:16:42 +00:00
|
|
|
find pages* -name '*.md' -exec markdownlint {} +
|
2021-09-16 00:10:16 +01:00
|
|
|
tldr-lint ./pages
|
|
|
|
for f in ./pages.*; do
|
2024-05-05 12:31:02 +01:00
|
|
|
checks="TLDR104"
|
2023-11-18 10:48:52 +00:00
|
|
|
if [[ -L $f ]]; then
|
|
|
|
continue
|
2024-05-05 12:31:02 +01:00
|
|
|
elif [[ $f == *ar* || $f == *bn* || $f == *fa* || $f == *hi* || $f == *ja* || $f == *ko* || $f == *lo* || $f == *ml* || $f == *ne* || $f == *ta* || $f == *th* || $f == *tr* ]]; then
|
|
|
|
checks+=",TLDR003,TLDR004,TLDR015"
|
2023-11-18 10:48:52 +00:00
|
|
|
elif [[ $f == *zh* || $f == *zh_TW* ]]; then
|
2024-05-05 12:31:02 +01:00
|
|
|
checks+=",TLDR003,TLDR004,TLDR005,TLDR015"
|
2023-11-13 14:22:16 +00:00
|
|
|
fi
|
2023-11-18 10:48:52 +00:00
|
|
|
tldr-lint --ignore $checks "${f}"
|
2021-09-16 00:10:16 +01:00
|
|
|
done
|
|
|
|
run_black
|
|
|
|
run_flake8
|
2024-05-18 01:33:40 +01:00
|
|
|
run_pytest
|
2021-09-16 00:10:16 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 15:03:35 +01:00
|
|
|
# Special test function for GitHub Actions pull request builds.
|
2019-11-27 17:22:38 +00:00
|
|
|
# Runs run_tests collecting errors for tldr-bot.
|
|
|
|
function run_tests_pr {
|
2019-11-27 17:25:16 +00:00
|
|
|
errs=$(run_tests 2>&1)
|
2019-11-15 04:32:04 +00:00
|
|
|
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ -n $errs ]]; then
|
2019-11-15 04:32:04 +00:00
|
|
|
echo -e "Test failed!\n$errs\n" >&2
|
|
|
|
echo 'Sending errors to tldr-bot.' >&2
|
2019-11-18 21:58:03 +00:00
|
|
|
echo -n "$errs" | python3 scripts/send-to-bot.py report-errors
|
2019-11-15 04:32:04 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-06-09 15:03:35 +01:00
|
|
|
# Additional checks for GitHub Actions pull request builds.
|
2019-11-15 04:32:04 +00:00
|
|
|
# Only taken as suggestions, does not make the build fail.
|
2019-11-27 17:22:38 +00:00
|
|
|
function run_checks_pr {
|
2019-11-27 17:25:16 +00:00
|
|
|
msgs=$(bash scripts/check-pr.sh)
|
2019-11-15 04:32:04 +00:00
|
|
|
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ -n $msgs ]]; then
|
2019-11-15 04:32:04 +00:00
|
|
|
echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2
|
|
|
|
echo 'Sending check results to tldr-bot.' >&2
|
2019-11-18 21:58:03 +00:00
|
|
|
echo -n "$msgs" | python3 scripts/send-to-bot.py report-check-results
|
2019-11-15 04:32:04 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
###################################
|
|
|
|
# MAIN
|
|
|
|
###################################
|
|
|
|
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then
|
2019-11-27 17:22:38 +00:00
|
|
|
run_checks_pr
|
|
|
|
run_tests_pr
|
2019-11-15 04:32:04 +00:00
|
|
|
else
|
|
|
|
set -e
|
2019-11-27 17:22:38 +00:00
|
|
|
run_tests
|
2019-11-15 04:32:04 +00:00
|
|
|
fi
|
|
|
|
|
2021-09-16 04:32:13 +01:00
|
|
|
echo 'Test ran successfully!'
|