Skip to content

Commit 13b5f83

Browse files
committed
chore: linkinator
Based-on: keymanapp/keyman.com#788 Test-bot: skip
1 parent a7641b7 commit 13b5f83

4 files changed

Lines changed: 139 additions & 52 deletions

File tree

.github/workflows/ci.yml

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ jobs:
88
runs-on: ubuntu-latest
99
env:
1010
KEYMANHOSTS_TIER: TIER_TEST
11+
CONTAINER_DESC: web-keyman-com-app
12+
CONTAINER_PORT: 8057
13+
1114
# Composer started complaining about running as root, but we don't care in a GHA
1215
COMPOSER_ALLOW_SUPERUSER: 1
1316

@@ -25,49 +28,57 @@ jobs:
2528
fail-fast: true
2629

2730
#
28-
# Finally, run the tests; note that this is almost all replicated in build.sh; here logging is reduced (TODO sync)
31+
# Run tests -- these step definitions are identical across:
32+
# help.keyman.com, keyman.com, keymanweb.com, api.keyman.com
2933
#
30-
- name: PHP unit tests
34+
- name: Test setup
35+
shell: bash
36+
run: |
37+
source ./resources/commands.inc.sh
38+
set -e
39+
do_test_record_start_time
40+
echo "TEST_START_TIME=${TEST_START_TIME}" >> "$GITHUB_ENV"
41+
42+
- name: PHP test
43+
if: ${{ !cancelled() }}
3144
shell: bash
3245
run: |
33-
docker exec web-keyman-com-app sh -c "vendor/bin/phpunit --testdox"
46+
source ./resources/commands.inc.sh
47+
set -e
48+
do_test_unit_tests "$CONTAINER_DESC"
3449
3550
- name: Lint
51+
if: ${{ !cancelled() }}
3652
shell: bash
3753
run: |
38-
( set +e; set +o pipefail; find . -name '*.php' | grep -v '/vendor/' | xargs -n 1 -d '\n' php -l | grep -v "No syntax errors detected"; exit ${PIPESTATUS[2]} )
54+
source ./resources/commands.inc.sh
55+
set -e
56+
do_test_lint "$CONTAINER_DESC"
3957
4058
- name: Check broken links
59+
if: ${{ !cancelled() }}
4160
shell: bash
4261
run: |
43-
set +e;
44-
set +o pipefail;
45-
npx broken-link-checker http://localhost:8057 --ordered --recursive --requests 50 --host-requests 50 -e --filter-level 3 | \
46-
grep -E "BROKEN|Getting links from" | \
47-
grep -B 1 "BROKEN"
48-
exit ${PIPESTATUS[0]}
62+
source ./resources/commands.inc.sh
63+
set -e
64+
do_test_links "http://localhost:${CONTAINER_PORT}"
4965
50-
- name: Check PHP errors
51-
shell: bash
66+
# We split the reporting of broken links into a separate step for ease of
67+
# viewing because the broken links are otherwise hidden in a sea of good
68+
# links in a very long report
69+
70+
- name: Report on broken links
71+
if: ${{ !cancelled() }}
5272
run: |
53-
CONTAINER=`docker container ls -l -q`
54-
if docker container logs $CONTAINER 2>&1 | grep -q 'php7'; then
55-
echo 'PHP reported errors or warnings:'
56-
docker container logs $CONTAINER 2>&1 | grep 'php7'
57-
exit 1
58-
else
59-
echo 'No PHP errors found'
60-
exit 0
61-
fi
62-
#
63-
# If any of the tests fail, let's grab a bit more detail on the environment
64-
#
65-
- name: Report errors
66-
if: ${{ failure() }}
73+
source ./resources/commands.inc.sh
74+
set -e
75+
do_test_print_link_report
76+
77+
- name: Check PHP errors
78+
if: ${{ !cancelled() }}
6779
shell: bash
6880
run: |
69-
CONTAINER=`docker container ls -l -q`
70-
echo "--- tier.txt ---"
71-
cat tier.txt
72-
echo "--- PHP errors in Docker log ---"
73-
docker container logs $CONTAINER 2>&1 | grep 'php7'
81+
source ./resources/commands.inc.sh
82+
set -e
83+
do_test_print_container_error_logs "$CONTAINER_DESC"
84+

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ cdn/deploy
44
/vendor
55
.data/
66

7+
# unit test artifacts
8+
linkinator-results.json
9+
.phpunit.result.cache
10+
711
# Shared files are bootstrapped:
812
resources/bootstrap.inc.sh
913
resources/.bootstrap-version

build.sh

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,34 @@ builder_describe \
3232

3333
builder_parse "$@"
3434

35+
source resources/commands.inc.sh
36+
3537
function test_docker_container() {
36-
# Note: ci.yml replicates these
38+
local CONTAINER_DESC="$1"
39+
local CONTAINER_PORT="$2"
40+
local LINK_RESULT
3741
echo "TIER_TEST" > tier.txt
3842

39-
# Run unit tests
40-
docker exec $KEYMANWEB_CONTAINER_DESC sh -c "vendor/bin/phpunit --testdox"
41-
42-
# Lint .php files for obvious errors
43-
docker exec $KEYMANWEB_CONTAINER_DESC sh -c "find . -name '*.php' | grep -v '/vendor/' | xargs -n 1 -d '\\n' php -l"
44-
45-
# Check all internal links
46-
# NOTE: link checker runs on host rather than in docker image
47-
npx broken-link-checker http://localhost:${PORT_KEYMANWEB_COM} --ordered --recursive --host-requests 10 -e --filter-level 3
48-
49-
# Check for errors
50-
if docker container logs $KEYMANWEB_CONTAINER_DESC 2>&1 | grep -q 'php7'; then
51-
echo 'PHP reported errors or warnings:'
52-
docker container logs $KEYMANWEB_CONTAINER_DESC 2>&1 | grep 'php7'
53-
exit 1
54-
else
55-
echo 'No PHP errors found'
56-
exit 0
57-
fi
43+
# from commands.inc.sh
44+
45+
do_test_record_start_time
46+
47+
builder_echo blue "---- PHP unit tests"
48+
do_test_unit_tests "${CONTAINER_DESC}"
49+
50+
builder_echo blue "---- Lint PHP files"
51+
do_test_lint "${CONTAINER_DESC}"
52+
53+
builder_echo blue "---- Testing links"
54+
LINK_RESULT=0
55+
do_test_links "http://localhost:${CONTAINER_PORT}" || LINK_RESULT=$?
56+
builder_echo blue "Done checking links; linkinator exit code: ${LINK_RESULT}"
57+
do_test_print_link_report
58+
59+
do_test_print_container_error_logs "${CONTAINER_DESC}"
60+
61+
rm tier.txt
62+
return "$LINK_RESULT"
5863
}
5964

6065
builder_run_action configure bootstrap_configure
@@ -63,4 +68,4 @@ builder_run_action stop stop_docker_container $KEYMANWEB_IMAGE_NAME $KEYM
6368
builder_run_action build build_docker_container $KEYMANWEB_IMAGE_NAME $KEYMANWEB_CONTAINER_NAME
6469
builder_run_action start start_docker_container $KEYMANWEB_IMAGE_NAME $KEYMANWEB_CONTAINER_NAME $KEYMANWEB_CONTAINER_DESC $HOST_KEYMANWEB_COM $PORT_KEYMANWEB_COM $BUILDER_CONFIGURATION
6570

66-
builder_run_action test test_docker_container
71+
builder_run_action test test_docker_container $KEYMANWEB_CONTAINER_DESC $PORT_KEYMANWEB_COM

resources/commands.inc.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
function do_test_unit_tests() {
10+
local CONTAINER="$1"
11+
docker exec "${CONTAINER}" sh -c "vendor/bin/phpunit --testdox"
12+
}
13+
14+
# Lint .php files for obvious errors
15+
function do_test_lint() {
16+
local CONTAINER="$1"
17+
docker exec "${CONTAINER}" sh -c "find . -name '*.php' | grep -v '/vendor/' | xargs -n 1 -d '\\n' php -l"
18+
}
19+
20+
# Check links on live local server using linkinator
21+
function do_test_links() {
22+
local baseURL="$1"
23+
24+
set +e
25+
npx https://github.com/keymanapp/linkinator \
26+
"${baseURL}/" \
27+
--clean-urls \
28+
--concurrency 50 \
29+
--format json \
30+
--output-filename linkinator-results.json \
31+
--skip "^(?!${baseURL})" \
32+
--recurse \
33+
--redirects verify \
34+
--retry-errors \
35+
--root-path "${baseURL}"
36+
local RESULT=$?
37+
set -e
38+
39+
return "${RESULT}"
40+
}
41+
42+
# Print summary of results from linkinator
43+
function do_test_print_link_report() {
44+
echo ----------------------------------------------------------------------
45+
echo Link check summary
46+
echo ----------------------------------------------------------------------
47+
# Emit full JSON detail for broken links (may not be necessary)
48+
jq '.links[] | select(.state != "OK")' < linkinator-results.json
49+
echo
50+
echo
51+
# Emit a summary report
52+
jq -r '.links[] | select(.state != "OK") | "\(.state)[\(.status)]: \(.parent) --> \(.url)"' < linkinator-results.json
53+
}
54+
55+
# Scan logs recorded on container since start of tests to find any reported PHP
56+
# errors (note, depends on 'php7' string)
57+
function do_test_print_container_error_logs() {
58+
local CONTAINER="$1"
59+
if docker container logs "${CONTAINER}" --since "${TEST_START_TIME}" 2>&1 | grep -q 'php7'; then
60+
echo 'PHP reported errors or warnings:'
61+
docker container logs "${CONTAINER}" --since "${TEST_START_TIME}" 2>&1 | grep 'php7'
62+
return 1
63+
else
64+
echo 'No PHP errors found'
65+
return 0
66+
fi
67+
}

0 commit comments

Comments
 (0)