Skip to content

Commit df4837e

Browse files
committed
feat: shared site testing script and move to linkinator
Relates-to: keymanapp/keyman.com#788 Test-bot: skip
1 parent 959f876 commit df4837e

3 files changed

Lines changed: 147 additions & 1 deletion

File tree

.bootstrap-registry

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ KeymanHosts.php
2020
KeymanSentry.php
2121
KeymanVersion.php
2222
MarkdownHost.php
23+
tests.inc.sh

_common/docker.inc.sh

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Common docker functions.
22

3+
source _common/tests.inc.sh
4+
35
function get_docker_image_id() {
46
local IMAGE_NAME=$1
57
echo "$(docker images -q $IMAGE_NAME)"
@@ -153,4 +155,54 @@ function start_docker_container() {
153155
fi
154156

155157
builder_echo green "Listening on http://$HOST:$PORT"
156-
}
158+
}
159+
160+
#
161+
# Test PHP site in a docker container
162+
#
163+
# Parameters:
164+
# 1: CONTAINER_DESC desc of container to test
165+
# 2: CONTAINER_PORT localhost http port for container
166+
# 3: TEST_PATH path to start link check at, under http://localhost:CONTAINER_PORT
167+
# 4..: SKIP_PATHS paths to skip crawling in link check, optional
168+
#
169+
# Builder options --no-unit-test, --no-lint, --no-link-check are honoured
170+
#
171+
function test_docker_container() {
172+
local CONTAINER_DESC="$1"
173+
local CONTAINER_PORT="$2"
174+
local TEST_PATH="$3"
175+
shift 3
176+
local SKIP_PATHS=("$*")
177+
178+
local LINK_RESULT
179+
echo "TIER_TEST" > tier.txt
180+
181+
# from commands.inc.sh
182+
183+
do_test_record_start_time
184+
185+
if ! builder_has_option --no-unit-test; then
186+
builder_echo blue "---- PHP unit tests"
187+
do_test_unit_tests "${CONTAINER_DESC}"
188+
fi
189+
190+
if ! builder_has_option --no-lint; then
191+
builder_echo blue "---- Lint PHP files"
192+
do_test_lint "${CONTAINER_DESC}"
193+
fi
194+
195+
if ! builder_has_option --no-link-check; then
196+
builder_echo blue "---- Testing links"
197+
198+
LINK_RESULT=0
199+
do_test_links "http://localhost:${CONTAINER_PORT}" "$TEST_PATH" "${SKIP_PATHS[@]}" || LINK_RESULT=$?
200+
builder_echo blue "Done checking links; linkinator exit code: ${LINK_RESULT}"
201+
do_test_print_link_report
202+
203+
do_test_print_container_error_logs "${CONTAINER_DESC}"
204+
fi
205+
206+
rm tier.txt
207+
return "$LINK_RESULT"
208+
}

_common/tests.inc.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 ${builder_extra_params[*]}"
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

Comments
 (0)