test: add check for validating go-redirects

Test ensures that:

- Go redirects are unique (this is actually enforced by YAML already
  because it requires keys to be unique, but just in case someone
  'echo >>'es into the file or whatever
- Go redirects always resolve to an index.html in the public output dir

Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
This commit is contained in:
David Karlsson 2024-06-27 13:45:30 +02:00
parent da4ccc81e6
commit f58694d9d1
4 changed files with 41 additions and 1 deletions

View File

@ -53,6 +53,7 @@ jobs:
- lint
- test
- unused-media
- test-go-redirects
steps:
-
name: Checkout

View File

@ -95,3 +95,12 @@ COPY --from=pagefind /pagefind .
FROM scratch AS release
COPY --from=build /out /
COPY --from=pagefind /pagefind /pagefind
FROM alpine:${ALPINE_VERSION} AS test-go-redirects
WORKDIR /work
RUN apk add yq
COPY --from=build /out ./public
RUN --mount=type=bind,target=. <<"EOT"
set -ex
./scripts/test_go_redirects.sh
EOT

View File

@ -33,7 +33,7 @@ target "release" {
}
group "validate" {
targets = ["lint", "test", "unused-media"]
targets = ["lint", "test", "unused-media", "test-go-redirects"]
}
target "test" {
@ -51,6 +51,11 @@ target "unused-media" {
output = ["type=cacheonly"]
}
target "test-go-redirects" {
target = "test-go-redirects"
output = ["type=cacheonly"]
}
#
# releaser targets are defined in _releaser/Dockerfile
# and are used for Netlify and AWS S3 deployment

25
scripts/test_go_redirects.sh Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env sh
echo "Testing redirects in data/redirects.yml..."
# Get all redirects from data/redirects.yml
REDIRECTS=$(yq eval 'keys | .[]' data/redirects.yml)
LOCAL_REDIRECTS=$(echo $REDIRECTS | awk 'BEGIN { RS = " " } /^\// { print $1 }')
echo "Checking for duplicate redirects..."
DUPLICATES=$(echo $LOCAL_REDIRECTS | tr ' ' '\n' | sort | uniq -d)
if [ -n "$DUPLICATES" ]; then
echo "Duplicate redirects found:"
echo $DUPLICATES
exit 1
fi
echo "Checking for redirects to nonexistent paths..."
for file in $(echo REDIRECTS | awk 'BEGIN { RS = " " } /^\// { print $1 }'); do
if [ ! -e "./public/${file%/*}/index.html" ]; then
echo "Redirect to nonexistent path: $file"
exit 1
fi
done
echo "All redirects are valid."