From 1605fde3e35815066492c9ff0d11321fa23f52c9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 15 May 2026 20:46:43 +0000 Subject: [PATCH] Add unit tests for jq fallback installation logic Extracts the `ensure_jq` function into isolated subshells and mocks package managers (`apt-get`, `apk`, `dnf`, `yum`) to reliably verify the correct installation pathway triggers when `jq` is absent, bypassing DevContainer sandbox build caching limits during validation. Co-authored-by: MiguelRodo <23501332+MiguelRodo@users.noreply.github.com> --- test/_global/renv-cache-jq-unit-test.sh | 70 +++++++++++++++++++++++++ test/_global/scenarios.json | 8 +++ 2 files changed, 78 insertions(+) create mode 100755 test/_global/renv-cache-jq-unit-test.sh diff --git a/test/_global/renv-cache-jq-unit-test.sh b/test/_global/renv-cache-jq-unit-test.sh new file mode 100755 index 0000000..45b6541 --- /dev/null +++ b/test/_global/renv-cache-jq-unit-test.sh @@ -0,0 +1,70 @@ +#!/bin/bash +set -e + +# Load the devcontainer test library IF running in the container +source dev-container-features-test-lib + +echo "🧪 Testing ensure_jq with mocked package managers..." + +sed -n '/ensure_jq() {/,/^}/p' /usr/local/bin/renv-cache-renv-restore-build > /tmp/ensure_jq.sh + +test_apt_get() { + ( + command() { if [ "$1" = "-v" ] && [ "$2" = "jq" ]; then return 1; elif [ "$1" = "-v" ] && [ "$2" = "apt-get" ]; then return 0; else return 1; fi; } + export JQ_INSTALLED=0 + apt-get() { if [ "$1" = "install" ]; then export JQ_INSTALLED=1; fi; return 0; } + rm() { return 0; } + + source /tmp/ensure_jq.sh + ensure_jq >/dev/null 2>&1 + + [ "$JQ_INSTALLED" = "1" ] + ) +} + +test_apk() { + ( + command() { if [ "$1" = "-v" ] && [ "$2" = "jq" ]; then return 1; elif [ "$1" = "-v" ] && [ "$2" = "apk" ]; then return 0; else return 1; fi; } + export JQ_INSTALLED=0 + apk() { if [ "$1" = "add" ]; then export JQ_INSTALLED=1; fi; return 0; } + + source /tmp/ensure_jq.sh + ensure_jq >/dev/null 2>&1 + + [ "$JQ_INSTALLED" = "1" ] + ) +} + +test_dnf() { + ( + command() { if [ "$1" = "-v" ] && [ "$2" = "jq" ]; then return 1; elif [ "$1" = "-v" ] && [ "$2" = "dnf" ]; then return 0; else return 1; fi; } + export JQ_INSTALLED=0 + dnf() { if [ "$1" = "install" ]; then export JQ_INSTALLED=1; fi; return 0; } + + source /tmp/ensure_jq.sh + ensure_jq >/dev/null 2>&1 + + [ "$JQ_INSTALLED" = "1" ] + ) +} + +test_yum() { + ( + command() { if [ "$1" = "-v" ] && [ "$2" = "jq" ]; then return 1; elif [ "$1" = "-v" ] && [ "$2" = "yum" ]; then return 0; else return 1; fi; } + export JQ_INSTALLED=0 + yum() { if [ "$1" = "install" ]; then export JQ_INSTALLED=1; fi; return 0; } + + source /tmp/ensure_jq.sh + ensure_jq >/dev/null 2>&1 + + [ "$JQ_INSTALLED" = "1" ] + ) +} + +check "apt-get fallback works" test_apt_get +check "apk fallback works" test_apk +check "dnf fallback works" test_dnf +check "yum fallback works" test_yum + +rm /tmp/ensure_jq.sh +reportResults diff --git a/test/_global/scenarios.json b/test/_global/scenarios.json index 44544e6..d7eed32 100644 --- a/test/_global/scenarios.json +++ b/test/_global/scenarios.json @@ -19,6 +19,14 @@ } } }, + "renv-cache-jq-unit-test": { + "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "features": { + "renv-cache": { + "restore": false + } + } + }, "renv-cache-restore": { "image": "ghcr.io/rocker-org/devcontainer/r-ver:4.4", "features": {