#!/bin/bash set -uo pipefail # Tests contrib enabling against fixture APT trees: classic sources.list, # deb822 .sources, contrib already present, and repeated runs. # # tests/test-classroom-dvd-css-setup.sh ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" SCRIPT="$ROOT/scripts/classroom-dvd-css-setup" WORK="$(mktemp -d)" trap 'rm -rf "$WORK"' EXIT passed=0 failed=0 check() { local label="$1" expected="$2" actual="$3" if [[ "$actual" == "$expected" ]]; then echo " ok $label" passed=$((passed + 1)) else echo " FAIL $label" echo " expected: $expected" echo " got: $actual" failed=$((failed + 1)) fi } # A fixture APT tree. Prints the root. fixture() { local name="$1" local root="$WORK/$name" rm -rf "$root" mkdir -p "$root/etc/apt/sources.list.d" echo "$root" } enable_contrib() { CLASSROOM_APT_ROOT="$1" python3 "$SCRIPT" --enable-contrib-only 2>&1 } # ------------------------------------------------- 1. classic sources.list echo "1. classic /etc/apt/sources.list" root="$(fixture classic)" cat > "$root/etc/apt/sources.list" <<'LIST' # Debian Trixie deb http://deb.debian.org/debian/ trixie main non-free-firmware deb-src http://deb.debian.org/debian/ trixie main non-free-firmware deb http://security.debian.org/debian-security trixie-security main non-free-firmware deb http://deb.debian.org/debian/ trixie-updates main non-free-firmware LIST enable_contrib "$root" > /dev/null check "binary entry gains contrib" \ "deb http://deb.debian.org/debian/ trixie main non-free-firmware contrib" \ "$(sed -n '2p' "$root/etc/apt/sources.list")" check "source entry gains contrib" \ "deb-src http://deb.debian.org/debian/ trixie main non-free-firmware contrib" \ "$(sed -n '3p' "$root/etc/apt/sources.list")" check "security entry gains contrib" \ "deb http://security.debian.org/debian-security trixie-security main non-free-firmware contrib" \ "$(sed -n '5p' "$root/etc/apt/sources.list")" check "comment line untouched" "# Debian Trixie" "$(sed -n '1p' "$root/etc/apt/sources.list")" check "line count unchanged" "6" "$(wc -l < "$root/etc/apt/sources.list")" check "contrib appears once per line" "0" \ "$(grep -c 'contrib.*contrib' "$root/etc/apt/sources.list")" # ------------------------------------------------------------ 2. deb822 echo "2. deb822 .sources" root="$(fixture deb822)" cat > "$root/etc/apt/sources.list.d/debian.sources" <<'SOURCES' Types: deb deb-src URIs: http://deb.debian.org/debian/ Suites: trixie trixie-updates Components: main non-free-firmware Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg Types: deb URIs: http://security.debian.org/debian-security/ Suites: trixie-security Components: main non-free-firmware Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg SOURCES enable_contrib "$root" > /dev/null check "first stanza gains contrib" \ "Components: main non-free-firmware contrib" \ "$(sed -n '4p' "$root/etc/apt/sources.list.d/debian.sources")" check "second stanza gains contrib" \ "Components: main non-free-firmware contrib" \ "$(sed -n '10p' "$root/etc/apt/sources.list.d/debian.sources")" check "other fields untouched" \ "URIs: http://deb.debian.org/debian/" \ "$(sed -n '2p' "$root/etc/apt/sources.list.d/debian.sources")" check "stanza separator kept" "" \ "$(sed -n '6p' "$root/etc/apt/sources.list.d/debian.sources")" # ------------------------------------------------ 3. contrib already there echo "3. contrib already enabled" root="$(fixture already)" cat > "$root/etc/apt/sources.list" <<'LIST' deb http://deb.debian.org/debian/ trixie main contrib non-free-firmware LIST cat > "$root/etc/apt/sources.list.d/extra.sources" <<'SOURCES' Types: deb URIs: http://deb.debian.org/debian/ Suites: trixie Components: main contrib SOURCES before_list="$(cat "$root/etc/apt/sources.list")" before_sources="$(cat "$root/etc/apt/sources.list.d/extra.sources")" out="$(enable_contrib "$root")" check "reports nothing to do" "[classroom-dvd-css-setup] contrib was already enabled" "$out" check "sources.list byte-identical" "$before_list" "$(cat "$root/etc/apt/sources.list")" check ".sources byte-identical" "$before_sources" "$(cat "$root/etc/apt/sources.list.d/extra.sources")" # ------------------------------------------------------- 4. repeated runs echo "4. repeated runs are idempotent" root="$(fixture repeat)" cat > "$root/etc/apt/sources.list" <<'LIST' deb http://deb.debian.org/debian/ trixie main non-free-firmware LIST cat > "$root/etc/apt/sources.list.d/debian.sources" <<'SOURCES' Types: deb URIs: http://deb.debian.org/debian/ Suites: trixie Components: main SOURCES enable_contrib "$root" > /dev/null first_list="$(cat "$root/etc/apt/sources.list")" first_sources="$(cat "$root/etc/apt/sources.list.d/debian.sources")" for _ in 1 2 3; do enable_contrib "$root" > /dev/null done check "sources.list unchanged after 4 runs" "$first_list" "$(cat "$root/etc/apt/sources.list")" check ".sources unchanged after 4 runs" "$first_sources" "$(cat "$root/etc/apt/sources.list.d/debian.sources")" check "exactly one contrib in sources.list" "1" \ "$(grep -o 'contrib' "$root/etc/apt/sources.list" | wc -l)" check "exactly one contrib in .sources" "1" \ "$(grep -o 'contrib' "$root/etc/apt/sources.list.d/debian.sources" | wc -l)" check "no repository was added" "1" "$(wc -l < "$root/etc/apt/sources.list")" # ------------------------------------------- 5. third-party repos untouched echo "5. third-party repositories are left alone" root="$(fixture thirdparty)" cat > "$root/etc/apt/sources.list" <<'LIST' deb http://deb.debian.org/debian/ trixie main LIST chrome='deb [arch=amd64 signed-by=/etc/apt/keyrings/google-chrome.gpg] https://dl.google.com/linux/chrome/deb/ stable main' echo "$chrome" > "$root/etc/apt/sources.list.d/google-chrome.list" enable_contrib "$root" > /dev/null check "chrome repository untouched" "$chrome" \ "$(cat "$root/etc/apt/sources.list.d/google-chrome.list")" check "debian entry still updated" \ "deb http://deb.debian.org/debian/ trixie main contrib" \ "$(cat "$root/etc/apt/sources.list")" # ---------------------------------------------------- 6. awkward real lines echo "6. options, comments and flat repositories" root="$(fixture awkward)" cat > "$root/etc/apt/sources.list" <<'LIST' deb [arch=amd64 signed-by=/usr/share/keyrings/debian-archive-keyring.gpg] http://deb.debian.org/debian/ trixie main deb http://deb.debian.org/debian/ trixie main # indented, with a comment deb http://deb.debian.org/debian/ ./ # deb http://deb.debian.org/debian/ trixie main LIST enable_contrib "$root" > /dev/null check "bracketed options preserved" \ "deb [arch=amd64 signed-by=/usr/share/keyrings/debian-archive-keyring.gpg] http://deb.debian.org/debian/ trixie main contrib" \ "$(sed -n '1p' "$root/etc/apt/sources.list")" check "indent and trailing comment preserved" \ " deb http://deb.debian.org/debian/ trixie main contrib # indented, with a comment" \ "$(sed -n '2p' "$root/etc/apt/sources.list")" check "flat repository untouched" \ "deb http://deb.debian.org/debian/ ./" \ "$(sed -n '3p' "$root/etc/apt/sources.list")" check "commented-out entry untouched" \ "# deb http://deb.debian.org/debian/ trixie main" \ "$(sed -n '4p' "$root/etc/apt/sources.list")" # ------------------------------------------------------------- summary echo echo "passed: $passed failed: $failed" [[ "$failed" -eq 0 ]]