From 492152f91a5d5668b384916321ae1b59c72bfb43 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Wed, 24 Jun 2026 16:34:02 +0200 Subject: Add cache_decision + tests Implements cache_decision which inspects $PKG_CACHE/// and returns "cached", "bump:OLD:NEW", or "new". Empty PKG_CACHE disables the cache. Tests in test-logic.sh cover all four outcomes and are driven pure (no VM needed). Co-Authored-By: Claude Opus 4.8 --- sbo-batch-test | 36 ++++++++++++++++++++++++++++++++++++ test-logic.sh | 19 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/sbo-batch-test b/sbo-batch-test index a9e3d14..1e0020c 100755 --- a/sbo-batch-test +++ b/sbo-batch-test @@ -306,6 +306,42 @@ update_base() { echo "Base patched." } +# ============================================================================= +# Package cache. Persistent, on-disk store of built packages so unchanged deps +# are installed from cache instead of rebuilt. Layout mirrors the SBo tree: +# $PKG_CACHE///---_.txz +# One .txz per prog dir (the latest tested build). Key is prog+version; build +# number, arch, and tag do not affect a hit. Empty PKG_CACHE disables caching. +# ============================================================================= + +# Echo the version field of a cached package filename. is a basename like +# prog-1.2-x86_64-1_danix.txz; prog may itself contain dashes, so strip the known +# prog prefix first, then take the field up to the next dash. +_cache_ver_of() { + local prog="$1" base="$2" + base="${base#"$prog"-}" # drop "prog-" + echo "${base%%-*}" # version is up to the next dash +} + +# cache_decision -> echoes: cached | bump:OLD:NEW | new +cache_decision() { + local cat="$1" prog="$2" version="$3" + [[ -z "$PKG_CACHE" ]] && { echo new; return; } + local dir="$PKG_CACHE/$cat/$prog" + local f newest="" + for f in "$dir/$prog"-*.t?z; do + [[ -e "$f" ]] || continue + [[ -z "$newest" || "$f" -nt "$newest" ]] && newest="$f" + done + [[ -z "$newest" ]] && { echo new; return; } + local have; have="$(_cache_ver_of "$prog" "$(basename "$newest")")" + if [[ "$have" == "$version" ]]; then + echo cached + else + echo "bump:$have:$version" + fi +} + # ============================================================================= # SBo tree lookup. Find the SlackBuild dir for a prog name across roots. # Echoes the dir path, returns 0 if found, 1 otherwise. diff --git a/test-logic.sh b/test-logic.sh index 13d97f8..7575ea7 100755 --- a/test-logic.sh +++ b/test-logic.sh @@ -104,6 +104,25 @@ else bad "cascade dead: b=${blocked[b]:-0} a=${blocked[a]:-0}" fi +# --- package cache ---------------------------------------------------------- +PKG_CACHE=$(mktemp -d) +mkc() { mkdir -p "$PKG_CACHE/$1/$2"; : > "$PKG_CACHE/$1/$2/$3"; } + +# exact version match -> cached +mkc net libfoo "libfoo-1.1-x86_64-1_danix.txz" +[[ "$(cache_decision net libfoo 1.1)" == "cached" ]] && ok "cache hit on version match" || bad "cache_decision should be cached, got [$(cache_decision net libfoo 1.1)]" + +# different version cached -> bump:old:new +[[ "$(cache_decision net libfoo 1.2)" == "bump:1.1:1.2" ]] && ok "cache bump reported" || bad "cache_decision bump wrong, got [$(cache_decision net libfoo 1.2)]" + +# nothing cached -> new +[[ "$(cache_decision net libbar 1.0)" == "new" ]] && ok "cache new for absent prog" || bad "cache_decision should be new, got [$(cache_decision net libbar 1.0)]" + +# empty PKG_CACHE disables -> new +PKG_CACHE_SAVE="$PKG_CACHE"; PKG_CACHE="" +[[ "$(cache_decision net libfoo 1.1)" == "new" ]] && ok "empty PKG_CACHE disables (new)" || bad "disabled cache should be new, got [$(cache_decision net libfoo 1.1)]" +PKG_CACHE="$PKG_CACHE_SAVE" + # --- result ----------------------------------------------------------------- echo echo "$pass passed, $fail failed" -- cgit v1.2.3