diff options
| author | Danilo M. <danix@danix.xyz> | 2026-09-18 17:09:28 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-09-18 17:09:28 +0200 |
| commit | 1101fc73ee2c599b351115a032ece5e8b21ecefa (patch) | |
| tree | 23a445040a8df3d0ee2db25d45b22d7efd76054b | |
| parent | 26f9d6ad31c1e56876e2634fbaa2f1eb217ac0c3 (diff) | |
| download | breaktimer-1101fc73ee2c599b351115a032ece5e8b21ecefa.tar.gz breaktimer-1101fc73ee2c599b351115a032ece5e8b21ecefa.zip | |
test: cover the config file path
Two cases carry the weight: an override takes effect, and a missing config
still yields the built-in defaults. The second is what every user without a
config file runs, so it is the regression worth pinning.
No daemon is started. Every assertion goes through the config verb, which
exits immediately, so the check is instant and cannot leave a stray process.
| -rwxr-xr-x | test-breaktimer-config.sh | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/test-breaktimer-config.sh b/test-breaktimer-config.sh new file mode 100755 index 0000000..db918fa --- /dev/null +++ b/test-breaktimer-config.sh @@ -0,0 +1,85 @@ +#!/bin/bash +# +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, see <https://www.gnu.org/licenses/>. +# +# The one runnable check for the config file. Points XDG_CONFIG_HOME and +# XDG_RUNTIME_DIR at temporary directories, so it never reads the user's +# config and never touches a live daemon. No daemon is started: every +# assertion goes through the `config` verb, which exits immediately. +# +# Usage: ./test-breaktimer-config.sh (exit 0 = all passed) + +set -u + +here="$(cd "$(dirname "$0")" && pwd)" +bt="$here/breaktimer.sh" +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT +export XDG_CONFIG_HOME="$tmp" +export XDG_RUNTIME_DIR="$tmp" + +pass=0 +fail=0 + +check() { + local label="$1" want="$2" got="$3" + if [[ "$want" == "$got" ]]; then + printf 'ok %s\n' "$label" + pass=$((pass + 1)) + else + printf 'FAIL %s: want %q, got %q\n' "$label" "$want" "$got" + fail=$((fail + 1)) + fi +} + +# Read one KEY=value line out of the config verb. +cfg() { "$bt" config | grep "^$1=" | cut -d= -f2-; } + +# No config file: the built-in defaults stand. This is the case every user +# without a config file exercises, so it is the regression that would hurt. +check "no config, MICRO_MIN default" "30" "$(cfg MICRO_MIN)" +check "no config, BREAK_MIN default" "3" "$(cfg BREAK_MIN)" +check "no config, reported as absent" "no" "$(cfg CONFIG_LOADED)" + +# A config file overrides the variable it names. +printf 'MICRO_MIN=45\n' > "$tmp/breaktimer.conf" +check "config overrides MICRO_MIN" "45" "$(cfg MICRO_MIN)" +check "config reported as loaded" "yes" "$(cfg CONFIG_LOADED)" + +# and leaves every variable it does not name alone. A config that silently +# reset the other values would be worse than no config at all. +check "unnamed value keeps its default" "3" "$(cfg BREAK_MIN)" +check "unnamed window keeps its default" "09:00" "$(cfg WORK_START)" + +# Several values at once, including a string with a colon and a path. +cat > "$tmp/breaktimer.conf" <<'CONF' +MICRO_MIN=25 +BREAK_MIN=5 +WORK_START="08:30" +SOUND_MICRO="/tmp/nonexistent.opus" +CONF +check "multi: MICRO_MIN" "25" "$(cfg MICRO_MIN)" +check "multi: BREAK_MIN" "5" "$(cfg BREAK_MIN)" +check "multi: quoted time" "08:30" "$(cfg WORK_START)" +check "multi: path" "/tmp/nonexistent.opus" "$(cfg SOUND_MICRO)" + +# An empty config file is not an error: it is a file the user has not filled +# in yet, and every default must survive it. +: > "$tmp/breaktimer.conf" +check "empty config keeps defaults" "30" "$(cfg MICRO_MIN)" +check "empty config still reports loaded" "yes" "$(cfg CONFIG_LOADED)" + +printf '\n%d passed, %d failed\n' "$pass" "$fail" +[[ "$fail" -eq 0 ]] |
