aboutsummaryrefslogtreecommitdiffstats
path: root/test-breaktimer-config.sh
blob: db918faf03ae6455748a0c72fd1ba051e9909d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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 ]]