#!/bin/bash
#
# Offline HTML parser tests: each section crawls a file:// fixture (no network)
# and checks which assets the parser captured and how it rewrote the links.
set -euo pipefail
tmp=$(mktemp -d "${TMPDIR:-/tmp}/httrack_parse.XXXXXX") || exit 1
trap 'rm -rf "$tmp"' EXIT HUP INT QUIT PIPE TERM
# a minimal valid 1x1 GIF, reused for every referenced asset
gif() {
printf 'GIF89a\1\0\1\0\200\0\0\0\0\0\377\377\377!\371\4\1\0\0\0\0,\0\0\0\0\1\0\1\0\0\2\2D\1\0;' >"$1"
}
# crawl into with link rewriting on, no extra fetching
crawl() {
local html="$1" out="$2"
rm -rf "$out"
mkdir -p "$out"
# the crawl's own exit status is irrelevant here; the assertions below check
# the mirrored files, so don't let set -e trip on a non-zero httrack exit
httrack "file://$html" -O "$out" --quiet --near -n >"$out/.log" 2>&1 || true
}
# assert a file with the given basename was saved somewhere under
found() {
test -n "$(find "$2" -type f -name "$1" -print -quit)" ||
! echo "FAIL: expected '$1' to be downloaded under $2" || exit 1
}
# assert NO file with the given basename was saved (e.g. a descriptor token must
# not be mistaken for a URL)
notfound() {
test -z "$(find "$2" -type f -name "$1" -print -quit)" ||
! echo "FAIL: '$1' should not have been downloaded under $2" || exit 1
}
# the mirrored fixture page (under "file/"), not HTTrack's own landing index
savedhtml() {
find "$1" -type f -path '*/file/*' -name index.html -print -quit
}
# srcset on and (#235, #236): every candidate captured and
# rewritten, descriptors preserved, following attributes left intact.
site="$tmp/srcset"
mkdir -p "$site"
for f in a b c d e f g h i j v dz; do gif "$site/$f.gif"; done
# unquoted heredoc: $site expands in the absolute-URL candidate
cat >"$site/index.html" <plain link still works