Remove usages of systemctl and fix paths

This commit is contained in:
2026-04-05 12:03:43 +02:00
parent 09ddc0f7ce
commit 2a1686e14d
4 changed files with 57 additions and 63 deletions
+11 -17
View File
@@ -1,39 +1,33 @@
#!/usr/bin/env bash
function _is_timer_enabled {
echo "$(systemctl is-enabled --user huami-token.timer)"
}
function _disable_timer {
systemctl disable --now --user huami-token.timer
}
DROPIN_FILE="$1/huami-token.service.d/01-set-working-directory.conf"
function disable_unit_and_exit() {
if [ "$(_is_timer_enabled)" == "enabled" ]; then
_disable_timer
local TIMER_LINK="$XDG_CONFIG_HOME/systemd/user/timers.target.wants/huami-token.timer"
if [ -h "$TIMER_LINK" ]; then
rm -f "$TIMER_LINK"
fi
if [ -f "$DROPIN_PATH" ]; then
rm -f "$DROPIN_PATH"
if [ -f "$DROPIN_FILE" ]; then
rm -f "$DROPIN_FILE"
fi
exit 0
}
main() {
DROPIN_PATH="$1/01-WorkingDirectory.conf"
local XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
local CONFIG="$XDG_CONFIG_HOME/huami-token.env"
# 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
# shellcheck disable=SC1090
source "$CONFIG"
if [ -z "${ZEPP_AGPS_TARGET_DIR//\\n|\\t|\\r/}" ]; then
if [ -z "${ZEPP_AGPS_TARGET_DIR//[$'\t'$'\n'$'\r']/}" ]; then
disable_unit_and_exit
fi
echo "WorkingDirectory=${ZEPP_AGPS_TARGET_DIR}" >"$DROPIN_PATH"
mkdir -p "$(dirname "$DROPIN_FILE")"
echo "WorkingDirectory=${ZEPP_AGPS_TARGET_DIR}" >"$DROPIN_FILE"
}
# Check if the script is being run directly, not sourced
+1
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bats
load 'test_helper'
bats_load_library 'bats-mock'
setup() {
load ../configurators/configure-zepp-timer
+45 -45
View File
@@ -7,104 +7,104 @@ setup() {
common_setup
export XDG_CONFIG_HOME="$TEST_TEMP_DIR/.config"
export ENV_FILE="$XDG_CONFIG_HOME/huami-token.env"
export DROPIN_DIR="$TEST_TEMP_DIR/huami-token.service.d"
export DROPIN_FILE="$DROPIN_DIR/01-set-working-directory.conf"
export TIMER_DIR="$XDG_CONFIG_HOME/systemd/user/timers.target.wants"
export TIMER_LINK="$TIMER_DIR/huami-token.timer"
mkdir -p "$TIMER_DIR"
mkdir -p "$XDG_CONFIG_HOME"
touch "$TEST_TEMP_DIR/dummy-timer.target"
}
# bats file_tags=generate
@test "The happy path" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
cat <<EOF >"$ENV_FILE"
ZEPP_USER=fnuggi
ZEPP_PASS=ugh
ZEPP_AGPS_TARGET_DIR=/tmp/tmp
EOF
unset -f _is_timer_disabled _disable_timer
ln -sf "$TEST_TEMP_DIR/dummy-timer.target" "$TIMER_LINK"
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"
assert_file_exists "$DROPIN_FILE"
assert_file_contains "$DROPIN_FILE" "WorkingDirectory=/tmp/tmp"
assert_link_exists "$TIMER_LINK"
}
# bats file_tags=generate
@test "The systemd conf is updated with new value" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
cat <<EOF >"$ENV_FILE"
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
ln -sf "$TEST_TEMP_DIR/dummy-timer.target" "$TIMER_LINK"
mkdir -p "$DROPIN_DIR"
echo "WorkingDirectory=/tmp/tmp" > "$DROPIN_FILE"
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"
assert_file_exists "$DROPIN_FILE"
assert_file_contains "$DROPIN_FILE" "WorkingDirectory=/tmp/ugh"
assert_link_exists "$TIMER_LINK"
}
# 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
stub _is_timer_enabled 'echo disabled'
@test "Config file is missing, the timer status was disabled" {
rm -f "$ENV_FILE"
mkdir -p "$DROPIN_DIR"
echo "WorkingDirectory=/tmp/tmp" > "$DROPIN_FILE"
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
assert_file_not_exists "$DROPIN_FILE"
assert_file_not_exists "$TIMER_LINK"
}
# 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'
@test "Config file is missing, the timer status was enabled" {
rm -f "$ENV_FILE"
ln -sf "$TEST_TEMP_DIR/dummy-timer.target" "$TIMER_LINK"
mkdir -p "$DROPIN_DIR"
echo "WorkingDirectory=/tmp/tmp" > "$DROPIN_FILE"
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
unstub _disable_timer
assert_file_not_exists "$DROPIN_FILE"
assert_file_not_exists "$TIMER_LINK"
}
# bats file_tags=generate
@test "No target dir is set, the timer is disabled" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
@test "No target dir is set, the timer status was disabled" {
cat <<EOF >"$ENV_FILE"
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
stub _is_timer_enabled 'echo disabled'
mkdir -p "$DROPIN_DIR"
echo "WorkingDirectory=/tmp/tmp" > "$DROPIN_FILE"
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
assert_file_not_exists "$DROPIN_FILE"
assert_file_not_exists "$TIMER_LINK"
}
# bats file_tags=generate
@test "No target dir is set, the timer is enabled" {
cat <<EOF >"$XDG_CONFIG_HOME/huami-token.env"
@test "No target dir is set, the timer status was enabled" {
cat <<EOF >"$ENV_FILE"
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'
mkdir -p "$DROPIN_DIR"
echo "WorkingDirectory=/tmp/tmp" > "$DROPIN_FILE"
ln -sf "$TEST_TEMP_DIR/dummy-timer.target" "$TIMER_LINK"
run main "$TEST_TEMP_DIR"
assert_file_not_exists "$TEST_TEMP_DIR/01-WorkingDirectory.conf"
unstub _is_timer_enabled
unstub _disable_timer
assert_file_not_exists "$DROPIN_FILE"
assert_file_not_exists "$TIMER_LINK"
}
-1
View File
@@ -1,7 +1,6 @@
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'
setup_file() {