Rewrite huami-token-generator to be testable and add tests

This commit is contained in:
2026-03-26 00:43:24 +01:00
parent 87de4c8dd8
commit 7ca46045d2
4 changed files with 159 additions and 23 deletions
+34 -17
View File
@@ -1,25 +1,42 @@
#!/usr/bin/env bash
DROPIN_PATH="$1/01-WorkingDirectory.conf"
function _is_timer_enabled {
echo "$(systemctl is-enabled --user huami-token.timer)"
}
function _disable_timer {
systemctl disable --now --user huami-token.timer
}
function disable_unit_and_exit() {
if [ "$(systemctl is-enabled --user huami-token.timer)" == "enabled" ]; then
systemctl disable --now --user huami-token.timer
if [ -f "$DROPIN_PATH" ]; then
rm -f "$DROPIN_PATH"
fi
if [ "$(_is_timer_enabled)" == "enabled" ]; then
_disable_timer
fi
if [ -f "$DROPIN_PATH" ]; then
rm -f "$DROPIN_PATH"
fi
exit 0
}
# Generate the WorkingDir value for the huami-token user service unit
CONFIG=${XDG_CONFIG_HOME:-$HOME/.config}/huami-token.env
if [ ! -s "$CONFIG" ]; then
disable_unit
fi
main() {
DROPIN_PATH="$1/01-WorkingDirectory.conf"
# shellcheck disable=SC1090
source "$CONFIG"
if [ -z "${ZEPP_AGPS_TARGET_DIR//\\n|\\t|\\r/}" ]; then
disable_unit
fi
# Generate the WorkingDir value for the huami-token user service unit
local CONFIG
CONFIG=${XDG_CONFIG_HOME:-$HOME/.config}/huami-token.env
if [ ! -s "$CONFIG" ]; then
disable_unit_and_exit
fi
echo "WorkingDirectory=${ZEPP_AGPS_TARGET_DIR}" > "$DROPIN_PATH"
# shellcheck disable=SC1090
source "$CONFIG"
if [ -z "${ZEPP_AGPS_TARGET_DIR//\\n|\\t|\\r/}" ]; then
disable_unit_and_exit
fi
echo "WorkingDirectory=${ZEPP_AGPS_TARGET_DIR}" >"$DROPIN_PATH"
}
# Check if the script is being run directly, not sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
+1 -1
View File
@@ -5,5 +5,5 @@ ConditionFileNotEmpty=%E/huami-token.env
[Service]
Type=oneshot
EnvironmentFile=%E/.config/huami-token.env
EnvironmentFile=%E/huami-token.env
ExecStart=/usr/bin/huami-token --method amazfit --gps --email "${ZEPP_USER}" --password "${ZEPP_PASS}"
+1 -5
View File
@@ -18,13 +18,9 @@ ZEPP_USER=fnuggi
ZEPP_PASS=ugh
ZEPP_AGPS_TARGET_DIR=/tmp/tmp
EOF
pushd "$TEST_TEMP_DIR" || fail
}
teardown() {
popd || echo "Couldn't return to original folder. Check where you are with pwd and be careful"
unstub __target_directory_dialog
unstub __call_huami_token
unstub __credentials_dialog
@@ -32,7 +28,7 @@ teardown() {
}
bats::on_failure() {
echo "Test run output:"
echo "Failed test run output:"
echo "$output"
}
+123
View File
@@ -0,0 +1,123 @@
setup_file() {
BATS_TEST_TIMEOUT=3
}
setup() {
bats_require_minimum_version 1.5.0
bats_load_library 'bats-support'
bats_load_library 'bats-assert'
bats_load_library 'bats-mock'
bats_load_library 'bats-file'
source "$(readlink -f "$BATS_TEST_DIRNAME"/../huami-token-generator)"
TEST_TEMP_DIR="$(temp_make)"
export XDG_CONFIG_HOME="$TEST_TEMP_DIR/.config"
mkdir -p "$XDG_CONFIG_HOME"
}
teardown() {
temp_del "$TEST_TEMP_DIR"
}
# bats file_tags=generate
@test "The happy path" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
ZEPP_USER=fnuggi
ZEPP_PASS=ugh
ZEPP_AGPS_TARGET_DIR=/tmp/tmp
EOF
unset -f _is_timer_disabled _disable_timer
run main "$TEST_TEMP_DIR"
assert_file_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
assert_file_contains "$TEST_TEMP_DIR/01-WorkingDirectory.conf" "WorkingDirectory=/tmp/tmp"
}
# bats file_tags=generate
@test "The systemd conf is updated with new value" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
ZEPP_USER=fnuggi
ZEPP_PASS=ugh
ZEPP_AGPS_TARGET_DIR=/tmp/ugh
EOF
echo "WorkingDirectory=/tmp/tmp" > "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unset -f _is_timer_disabled _disable_timer
run main "$TEST_TEMP_DIR"
assert_file_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
assert_file_contains "$TEST_TEMP_DIR/01-WorkingDirectory.conf" "WorkingDirectory=/tmp/ugh"
}
# bats file_tags=generate
@test "Config file is missing, the timer is disabled" {
rm -f "$XDG_CONFIG_HOME/huami-token.env"
echo "WorkingDirectory=/tmp/tmp" > "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unset -f _is_timer_enabled _disable_timer
stub _is_timer_enabled 'echo disabled'
stub _disable_timer ''
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
unstub _disable_timer
}
# bats file_tags=generate
@test "Config file is missing, the timer is enabled" {
rm -f "$XDG_CONFIG_HOME/huami-token.env"
echo "WorkingDirectory=/tmp/tmp" > "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unset -f _is_timer_enabled _disable_timer
stub _is_timer_enabled 'echo enabled'
stub _disable_timer 'true'
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
unstub _disable_timer
}
# bats file_tags=generate
@test "No target dir is set, the timer is disabled" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
ZEPP_USER=fnuggi
ZEPP_PASS=ugh
ZEPP_AGPS_TARGET_DIR=
EOF
echo "WorkingDirectory=/tmp/tmp" > "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unset -f _is_timer_enabled _disable_timer
stub _is_timer_enabled 'echo disabled'
stub _disable_timer ''
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
unstub _disable_timer
}
# bats file_tags=generate
@test "No target dir is set, the timer is enabled" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
ZEPP_USER=fnuggi
ZEPP_PASS=ugh
ZEPP_AGPS_TARGET_DIR=
EOF
echo "WorkingDirectory=/tmp/tmp" > "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unset -f _is_timer_enabled _disable_timer
stub _is_timer_enabled 'echo enabled'
stub _disable_timer 'true'
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
unstub _disable_timer
}