diff --git a/scripts/check-links.sh b/scripts/check-links.sh index 1376ee5..1494fd7 100755 --- a/scripts/check-links.sh +++ b/scripts/check-links.sh @@ -2,6 +2,7 @@ set -uo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$ROOT" || exit 1 WORK_DIR="$ROOT/.linkcheck-work" USER_AGENT="hivecommons-link-checker/1.0 (+https://hivecommons.dev)" EXTERNAL_WARN=0 @@ -83,6 +84,11 @@ extract_redirect_targets >> "$WORK_DIR/links.tsv" sort -u "$WORK_DIR/links.tsv" > "$WORK_DIR/links.dedup.tsv" +if [[ ! -s "$WORK_DIR/links.dedup.tsv" ]]; then + echo "ERROR no links found under $ROOT; refusing to pass without checking anything" + exit 1 +fi + internal_errors=0 external_errors=0 checked=0 diff --git a/scripts/check-links.test.sh b/scripts/check-links.test.sh index 1c4825f..da89ed2 100755 --- a/scripts/check-links.test.sh +++ b/scripts/check-links.test.sh @@ -193,6 +193,20 @@ run_checker "$root" expect_rc 0 "dedup site passes" expect_out 'Checked 1 unique links\.' "three identical links counted once" +echo "case: result does not depend on the caller's cwd" +root="$(new_site other-cwd)" +echo 'x' > "$root/index.html" +OUT="$(cd "$TMP" && PATH="$TMP/bin:$PATH" bash "$root/scripts/check-links.sh" 2>&1)"; RC=$? +expect_rc 1 "broken link still fails when run from another directory" +expect_out 'ERROR internal target missing: index\.html -> /nope' "page links are extracted from outside the root" +expect_no_out 'No such file' "no grep path errors" + +echo "case: a site with no links to check fails closed" +root="$(new_site empty)" +run_checker "$root" +expect_rc 1 "no pages/links is an error, not a pass" +expect_out 'no links found' "explains why" + echo echo "check-links self-test: $passes passed, $failures failed" (( failures == 0 ))