Files
omarchy/test/shell.d/region-test.sh
T

114 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"
test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT
china_sed="$ROOT/default/region/china/pacman.sed"
expected_stable_omarchy='Server = https://pkgs.omarchy.cn/stable/$arch
Server = https://pkgs.omarchy.org/stable/$arch'
actual=$(sed -f "$china_sed" "$ROOT/default/pacman/pacman-stable.conf" | sed -n '/^\[omarchy\]/,/^$/p' | grep '^Server')
[[ $actual == "$expected_stable_omarchy" ]] || fail "stable [omarchy] lists pkgs.omarchy.cn before pkgs.omarchy.org" "$actual"
pass "stable [omarchy] lists pkgs.omarchy.cn before pkgs.omarchy.org"
declare -A mirrors=([stable]=stable-mirror [rc]=rc-mirror [edge]=mirror)
for channel in stable rc edge; do
host=${mirrors[$channel]}
expected="Server = https://$host.omarchy.cn/\$repo/os/\$arch
Server = https://$host.omarchy.org/\$repo/os/\$arch"
actual=$(sed -f "$china_sed" "$ROOT/default/pacman/mirrorlist-$channel")
[[ $actual == "$expected" ]] || fail "$channel mirrorlist lists $host.omarchy.cn first" "$actual"
pass "$channel mirrorlist lists $host.omarchy.cn first"
for file in "pacman-$channel.conf" "mirrorlist-$channel"; do
original=$(<"$ROOT/default/pacman/$file")
stripped=$(sed -f "$china_sed" "$ROOT/default/pacman/$file" | grep -v '^Server = https://[a-z-]*\.omarchy\.cn/')
[[ $stripped == "$original" ]] || fail "$file only gains .cn servers" "$(diff <(echo "$original") <(echo "$stripped"))"
org_count=$(grep -c '^Server = https://[a-z-]*\.omarchy\.org/' "$ROOT/default/pacman/$file")
cn_count=$(sed -f "$china_sed" "$ROOT/default/pacman/$file" | grep -c '^Server = https://[a-z-]*\.omarchy\.cn/')
((org_count > 0 && org_count == cn_count)) || fail "$file gets one .cn twin per .org server" "org=$org_count cn=$cn_count"
done
pass "$channel templates only gain one .cn twin per .org server"
done
region_bin="$test_tmp/omarchy-region"
region_file="$test_tmp/region"
sed "s|/etc/omarchy/region|$region_file|" "$ROOT/bin/omarchy-region" >"$region_bin"
chmod +x "$region_bin"
[[ $("$region_bin") == "Global" ]] || fail "no region file reads as Global"
pass "no region file reads as Global"
echo china >"$region_file"
[[ $("$region_bin") == "China" ]] || fail "china region file reads as China"
pass "china region file reads as China"
echo mars >"$region_file"
if output=$("$region_bin" 2>&1); then
fail "an unknown region fails" "$output"
fi
[[ $output == "Unknown region in $region_file: mars" ]] || fail "an unknown region names the file and value" "$output"
pass "an unknown region fails and names the file and value"
stub_bin="$test_tmp/bin"
calls="$test_tmp/calls"
mkdir -p "$stub_bin"
for command in omarchy-pkg-add rime-ice-installer; do
printf '#!/bin/bash\necho "%s $*" >>"%s"\n' "$command" "$calls" >"$stub_bin/$command"
done
printf '#!/bin/bash\necho "$STUB_REGION"\n' >"$stub_bin/omarchy-region"
chmod +x "$stub_bin"/*
PATH="$stub_bin:$PATH"
region_setup="$test_tmp/region.sh"
sed "s|/etc/omarchy|$test_tmp/etc-omarchy|g" "$ROOT/install/config/region.sh" >"$region_setup"
: >"$calls"
OMARCHY_REGION=china bash -e "$region_setup"
[[ $(<"$test_tmp/etc-omarchy/region") == "china" ]] || fail "install records the China region"
[[ $(<"$calls") == "omarchy-pkg-add fcitx5-configtool fcitx5-rime librime opencc rime-ice-installer" ]] ||
fail "China installs the Rime input method packages" "$(<"$calls")"
for package in fcitx5-configtool fcitx5-rime librime opencc rime-ice-installer; do
grep -qx "$package" "$ROOT/install/omarchy-other.packages" || fail "the ISO mirror carries $package"
done
pass "install records the China region and installs the Rime packages the ISO carries"
: >"$calls"
OMARCHY_REGION=global bash -e "$region_setup"
[[ ! -e $test_tmp/etc-omarchy/region ]] || fail "install records Global by leaving no region file"
[[ ! -s $calls ]] || fail "Global installs no input method packages" "$(<"$calls")"
OMARCHY_REGION=china bash -e "$region_setup"
env -u OMARCHY_REGION bash -e "$region_setup"
[[ ! -e $test_tmp/etc-omarchy/region ]] || fail "an installer that passes no region installs Global"
pass "Global and an unset region leave no region file"
if output=$(OMARCHY_REGION=mars bash -e "$region_setup" 2>&1); then
fail "install rejects an unknown region" "$output"
fi
[[ $output == "Unknown OMARCHY_REGION: mars" ]] || fail "install names the unknown region" "$output"
pass "install rejects an unknown region"
user_region="$ROOT/install/user/region.sh"
: >"$calls"
STUB_REGION=China OMARCHY_SETUP_CONTEXT=iso-chroot bash -e "$user_region"
[[ $(<"$calls") == "rime-ice-installer --yes --asset-dir /opt/packages/rime-ice" ]] ||
fail "a China install sets up Rime Ice from the ISO's assets" "$(<"$calls")"
pass "a China install sets up Rime Ice from the ISO's assets"
: >"$calls"
output=$(STUB_REGION=China OMARCHY_SETUP_CONTEXT=provision-owner bash -e "$user_region")
[[ ! -s $calls && $output == "Skipping Rime Ice setup in provision-owner: only the ISO bundles its assets" ]] ||
fail "first-boot setup says why it skips Rime Ice" "$output $(<"$calls")"
pass "first-boot setup says why it skips Rime Ice"
: >"$calls"
output=$(STUB_REGION=Global OMARCHY_SETUP_CONTEXT=iso-chroot bash -e "$user_region")
[[ ! -s $calls && -z $output ]] || fail "a Global install leaves the input method alone" "$output $(<"$calls")"
pass "a Global install leaves the input method alone"