|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +# Record the start time for unit tests for later log review |
| 4 | +function do_test_record_start_time() { |
| 5 | + TEST_START_TIME=$(date -Is -u) |
| 6 | +} |
| 7 | + |
| 8 | +# Run unit tests through phpunit |
| 9 | +# |
| 10 | +# Parameters |
| 11 | +# 1: CONTAINER container_desc to run on |
| 12 | +# |
| 13 | +function do_test_unit_tests() { |
| 14 | + local CONTAINER="$1" |
| 15 | + docker exec "${CONTAINER}" sh -c "vendor/bin/phpunit --testdox" |
| 16 | +} |
| 17 | + |
| 18 | +# Lint .php files for obvious errors |
| 19 | +# |
| 20 | +# Parameters |
| 21 | +# 1: CONTAINER container_desc to run on |
| 22 | +# |
| 23 | +function do_test_lint() { |
| 24 | + local CONTAINER="$1" |
| 25 | + docker exec "${CONTAINER}" sh -c "find . -name '*.php' | grep -v '/vendor/' | xargs -n 1 -d '\\n' php -l" |
| 26 | +} |
| 27 | + |
| 28 | +## Check links on live local server using linkinator |
| 29 | +# |
| 30 | +# Parameters |
| 31 | +# 1: baseURL the top level URL for the site |
| 32 | +# 2: testPath path under baseURL to start testing, e.g. / |
| 33 | +# 3[,4..]: skipPaths list of paths (under baseURL) to skip crawling, optional |
| 34 | +# |
| 35 | +function do_test_links() { |
| 36 | + local baseURL="$1" |
| 37 | + local testPath="$2" |
| 38 | + shift 2 |
| 39 | + local skipPaths skip skipParams=() |
| 40 | + if [[ $# -gt 1 ]]; then |
| 41 | + skipPaths=("$*") |
| 42 | + else |
| 43 | + skipPaths=() |
| 44 | + fi |
| 45 | + |
| 46 | + for skip in "${skipPaths[@]}"; do |
| 47 | + skipParams+=(--skip "^${baseURL}${skip}") |
| 48 | + done |
| 49 | + |
| 50 | + npx https://github.com/keymanapp/linkinator \ |
| 51 | + "${baseURL}${testPath}" \ |
| 52 | + --clean-urls \ |
| 53 | + --concurrency 50 \ |
| 54 | + --format json \ |
| 55 | + --output-filename linkinator-results.json \ |
| 56 | + --skip "^(?!${baseURL})" \ |
| 57 | + "${skipParams[@]}" \ |
| 58 | + --recurse \ |
| 59 | + --redirects verify \ |
| 60 | + --retry-errors \ |
| 61 | + --root-path "${baseURL}" |
| 62 | +} |
| 63 | + |
| 64 | +# Print summary of results from linkinator |
| 65 | +function do_test_print_link_report() { |
| 66 | + echo ---------------------------------------------------------------------- |
| 67 | + echo Link check summary |
| 68 | + echo ---------------------------------------------------------------------- |
| 69 | + # Emit full JSON detail for broken links (may not be necessary) |
| 70 | + jq '.links[] | select(.state != "OK")' < linkinator-results.json |
| 71 | + echo |
| 72 | + echo |
| 73 | + # Emit a summary report |
| 74 | + jq -r '.links[] | select(.state != "OK") | "\(.state)[\(.status)]: \(.parent) --> \(.url)"' < linkinator-results.json |
| 75 | +} |
| 76 | + |
| 77 | +# Scan logs recorded on container since start of tests to find any reported PHP |
| 78 | +# errors (note, depends on 'php7' string) |
| 79 | +# |
| 80 | +# Parameters |
| 81 | +# 1: CONTAINER container_desc to run on |
| 82 | +# |
| 83 | +function do_test_print_container_error_logs() { |
| 84 | + local CONTAINER="$1" |
| 85 | + if docker container logs "${CONTAINER}" --since "${TEST_START_TIME}" 2>&1 | grep -q 'php7'; then |
| 86 | + echo 'PHP reported errors or warnings:' |
| 87 | + docker container logs "${CONTAINER}" --since "${TEST_START_TIME}" 2>&1 | grep 'php7' |
| 88 | + return 1 |
| 89 | + else |
| 90 | + echo 'No PHP errors found' |
| 91 | + return 0 |
| 92 | + fi |
| 93 | +} |
0 commit comments