diff --git a/package.json b/package.json index cbb2a390b..ad58887e4 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "scripts": { "lint-markdown": "markdownlint pages*/**/*.md", "lint-tldr": "tldr-lint ./pages", - "test": "bash -c 'markdownlint pages*/**/*.md && tldr-lint ./pages 2>&1 | tee test_result; test ${PIPESTATUS[0]} -eq 0'", + "test": "bash scripts/test.sh", "build-index": "node ./scripts/build-index.js | tee pages/index.json > index.json" }, "husky": { diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100644 index 000000000..506afaa4c --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +# This script is executed by Travis CI for every successful push (on any branch, PR or not). +# 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. + +# Default test. +function default_test { + markdownlint pages*/**/*.md + tldr-lint ./pages +} + +# Special test for Travis CI pull request builds. +# Runs default_test collecting errors for tldr-bot. +function pr_test { + errs=`default_test 2>&1` + + if [ -n "$errs" ]; then + echo -e "Test failed!\n$errs\n" >&2 + echo 'Sending errors to tldr-bot.' >&2 + echo -n "$errs" | python3 scripts/send_to_bot.py error + exit 1 + fi +} + +# Additional checks for Travis CI pull request builds. +# Only taken as suggestions, does not make the build fail. +function pr_check { + msgs=`bash scripts/check-pr.sh` + + if [ -n "$msgs" ]; then + echo -e "\nCheck PR reported the following message(s):\n$msgs\n" >&2 + echo 'Sending check results to tldr-bot.' >&2 + echo -n "$msgs" | python3 scripts/send_to_bot.py check + fi +} + +################################### +# MAIN +################################### + +if [ "$TRAVIS" = "true" ] && [ "$TRAVIS_REPO_SLUG" = "tldr-pages/tldr" ] && [ "$TRAVIS_PULL_REQUEST" != "false" ]; then + pr_check + pr_test +else + set -e + default_test +fi + +echo 'Test ran succesfully!'