Rewrite configure-zepp-timer to be testable and add tests

This commit is contained in:
2026-03-26 00:43:24 +01:00
parent 6917c58f52
commit 87de4c8dd8
3 changed files with 253 additions and 45 deletions
+89 -44
View File
@@ -2,57 +2,102 @@
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
CONFIG="$XDG_CONFIG_HOME/huami-token.env"
if [ -s "$CONFIG" ]; then
# shellcheck disable=SC1090
source "$CONFIG"
fi
_load_existing_config() {
if [ -s "$1" ]; then
# shellcheck disable=SC1090
source "$1"
fi
}
readarray -t CREDS < <(dialog --output-fd 1 --defaultno --title "Provide Zepp login credentials" --visit-items --keep-tite --insecure --mixedform "Please enter your credentials:" 8 60 2 "Username/Email:" 1 1 "$ZEPP_USER" 1 18 38 0 0 "Password:" 2 1 "$ZEPP_PASS" 2 18 38 0 1)
if [ ${#CREDS[@]} -eq 0 ]; then
echo "Cancelled. $CONFIG was not changed."
exit 0;
fi
ZEPP_USER="${CREDS[0]//\\n|\\t|\\r/}"
ZEPP_PASS="${CREDS[1]//\\n|\\t|\\r/}"
if [ -z "$ZEPP_USER" ] || [ -z "$ZEPP_PASS" ]; then
echo "No username or password were provided. Not changing $CONFIG"
exit 1;
fi
term_width=$(tput cols)
( huami-token --method amazfit --email "$ZEPP_USER" --password "$ZEPP_PASS" ) 2>&1 | dialog --keep-tite --title "Remote check" --programbox "Trying to log into Zepp" 32 "$term_width"
auth_result=${PIPESTATUS[0]}
if [ "$auth_result" -ne "0" ]; then
echo Failed to login with the given user and password. Aborting.
exit 1
fi
if [ "$ZEPP_AGPS_TARGET_DIR" == "%h" ]; then
TARGET_DIR=$HOME
else
TARGET_DIR=$ZEPP_AGPS_TARGET_DIR
fi
TARGET_DIR=$(dialog --output-fd 1 --erase-on-exit --visit-items --keep-tite --clear --title "Choose a directory to put the downloaded files into" --dselect "$TARGET_DIR/" 3 60)
if [ "${PIPESTATUS[0]}" -ne 0 ]; then
echo "Cancelled. $CONFIG was not changed."
exit 0;
fi
TARGET_DIR=${TARGET_DIR//\\n|\\t|\\r/}
if [ -n "$TARGET_DIR" ]; then
if [ "$(realpath "${TARGET_DIR}")" == "$HOME" ]; then
_request_credentials() {
local -a CREDS
CREDS=( $(__credentials_dialog "$1" "$2") )
if [ ${#CREDS[@]} -eq 0 ]; then
echo "Cancelled. $CONFIG was not changed."
exit 0
fi
ZEPP_USER="${CREDS[0]//\\n|\\t|\\r/}"
ZEPP_PASS="${CREDS[1]//\\n|\\t|\\r/}"
if [ -z "$ZEPP_USER" ] || [ -z "$ZEPP_PASS" ]; then
echo "Username or password wasn't provided. Not changing $CONFIG"
exit 1
fi
}
_test_credentials() {
local auth_result
auth_result=$(__call_huami_token $1 $2)
if [ -z "$auth_result" ] || [ "$auth_result" -ne "0" ]; then
echo Failed to login with the given user and password. Aborting.
exit 1
fi
}
_request_target_directory() {
local TARGET_DIR
if [ "$1" == "%h" ]; then
TARGET_DIR=$HOME
else
TARGET_DIR=$1
fi
TARGET_DIR=$(__target_directory_dialog "$TARGET_DIR")
if [ -z "$TARGET_DIR" ]; then
echo "Cancelled. $CONFIG was not changed."
exit 0
fi
TARGET_DIR=${TARGET_DIR//\\n|\\t|\\r/}
if [ -n "$TARGET_DIR" ] && [ "$(realpath "${TARGET_DIR}")" == "$HOME" ]; then
TARGET_DIR=%h
else
TARGET_DIR=$(realpath "$TARGET_DIR")
fi
else
TARGET_DIR=%h
fi
if [ ! -d "$XDG_CONFIG_HOME" ]; then
mkdir "$XDG_CONFIG_HOME"
fi
cat << EOF > "$CONFIG"
ZEPP_AGPS_TARGET_DIR="$TARGET_DIR"
}
_save_config() {
if [ ! -d "$XDG_CONFIG_HOME" ]; then
mkdir "$XDG_CONFIG_HOME"
fi
cat <<EOF >"$CONFIG"
# Generated by configure-zepp-timer
ZEPP_USER=$ZEPP_USER
ZEPP_PASS=$ZEPP_PASS
ZEPP_AGPS_TARGET_DIR=$TARGET_DIR
ZEPP_AGPS_TARGET_DIR=$ZEPP_AGPS_TARGET_DIR
EOF
}
echo "The config at $CONFIG was updated"
__credentials_dialog() {
local RET
readarray -t RET < <(dialog --output-fd 1 --defaultno --title "Provide Zepp login credentials" --visit-items --keep-tite --insecure --mixedform "Please enter your credentials:" 8 60 2 "Username/Email:" 1 1 "$1" 1 18 38 0 0 "Password:" 2 1 "$2" 2 18 38 0 1)
echo ${RET[@]@Q}
}
__call_huami_token() {
huami-token --method amazfit --email $1 --password $2 |& dialog --title 'Remote check' --keep-tite --programbox 'Trying to log into Zepp' 32 "$(tput cols)" 3>&1 1>&2 2>&3 3>&-
echo "${PIPESTATUS[0]}"
}
__target_directory_dialog() {
local RET
RET="$(dialog --output-fd 1 --erase-on-exit --visit-items --keep-tite --clear --title "Choose a directory to put the downloaded files into" --dselect "$1/" 3 60)"
if [ "${PIPESTATUS[0]}" -ne 0 ]; then
RET=""
fi
echo "$RET"
}
main() {
_load_existing_config "$CONFIG"
_request_credentials "$ZEPP_USER" "$ZEPP_PASS"
_test_credentials "$ZEPP_USER" "$ZEPP_PASS"
_request_target_directory "$ZEPP_AGPS_TARGET_DIR"
_save_config
echo "The config at $CONFIG was updated"
}
# Check if the script is being run directly, not sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
+1 -1
View File
@@ -22,7 +22,7 @@ BuildOption(install): -l %{modname}
BuildOption(generate_buildrequires): -x dev
BuildArch: noarch
BuildRequires: python3-devel pyproject-srpm-macros systemd-rpm-macros
BuildRequires: python3-devel pyproject-srpm-macros systemd-rpm-macros bats-assert bats-mock bats-file
Requires: dialog coreutils
Obsoletes: hf-cli hf-cli-user-systemd
+163
View File
@@ -0,0 +1,163 @@
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"/../configure-zepp-timer)"
TEST_TEMP_DIR="$(temp_make)"
export CONFIG="$TEST_TEMP_DIR/test-config.env"
cat <<EOF >"$CONFIG"
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
temp_del "$TEST_TEMP_DIR"
}
bats::on_failure() {
echo "Test run output:"
echo "$output"
}
# bats file_tags=configure
@test "The happy path" {
unset -f __credentials_dialog __call_huami_token __target_directory_dialog
stub __credentials_dialog "fnuggi ugh : printf 'gurli\npass1234'"
stub __call_huami_token 'printf 0'
stub __target_directory_dialog "echo \"$TEST_TEMP_DIR\""
run main
assert_output "The config at $TEST_TEMP_DIR/test-config.env was updated"
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=gurli"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=pass1234"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=$TEST_TEMP_DIR"
}
# bats file_tags=configure
@test "User cancels credential dialog" {
unset -f __credentials_dialog
stub __credentials_dialog "fnuggi ugh : printf ''"
run main
assert_output "Cancelled. $TEST_TEMP_DIR/test-config.env was not changed."
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=fnuggi"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=ug"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=/tmp/tmp"
}
# bats file_tags=configure
@test "No username was entered" {
unset -f __credentials_dialog
stub __credentials_dialog "fnuggi ugh : printf '\npass1234'"
run main
assert_output "Username or password wasn't provided. Not changing $TEST_TEMP_DIR/test-config.env"
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=fnuggi"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=ugh"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=/tmp/tmp"
}
# bats file_tags=configure
@test "No password was entered" {
unset -f __credentials_dialog
stub __credentials_dialog "fnuggi ugh : printf 'gurli\n'"
run main
assert_output "Username or password wasn't provided. Not changing $TEST_TEMP_DIR/test-config.env"
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=fnuggi"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=ugh"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=/tmp/tmp"
}
# bats file_tags=configure
@test "Username/password couldn't log in" {
unset -f __credentials_dialog __call_huami_token
stub __credentials_dialog "fnuggi ugh : printf 'gurli\npass1234'"
stub __call_huami_token 'printf 1'
run main
assert_output "Failed to login with the given user and password. Aborting."
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=fnuggi"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=ugh"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=/tmp/tmp"
}
# bats file_tags=configure
@test "User cancels target directory dialog" {
unset -f __credentials_dialog __call_huami_token __target_directory_dialog
stub __credentials_dialog "fnuggi ugh : printf 'gurli\npass1234'"
stub __call_huami_token 'printf 0'
stub __target_directory_dialog 'echo ""'
run main
assert_output "Cancelled. $TEST_TEMP_DIR/test-config.env was not changed."
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=fnuggi"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=ugh"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=/tmp/tmp"
}
# bats file_tags=configure
@test "The path to the home directory is converted to the systemd version, %h" {
unset -f __credentials_dialog __call_huami_token __target_directory_dialog
stub __credentials_dialog "fnuggi ugh : printf 'gurli\npass1234'"
stub __call_huami_token 'printf 0'
stub __target_directory_dialog "echo $HOME"
run main
assert_output "The config at $TEST_TEMP_DIR/test-config.env was updated"
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=gurli"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=pass1234"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=%h"
}
# bats file_tags=configure
@test "An existing systemd home path, %h, is converted to a real path in the target directory dialog" {
cat <<EOF >"$CONFIG"
ZEPP_USER=fnuggi
ZEPP_PASS=ugh
ZEPP_AGPS_TARGET_DIR=%h
EOF
unset -f __credentials_dialog __call_huami_token __target_directory_dialog
stub __credentials_dialog "fnuggi ugh : printf 'gurli\npass1234'"
stub __call_huami_token 'printf 0'
stub __target_directory_dialog "$HOME : echo /tmp/tmp"
run main
assert_output "The config at $TEST_TEMP_DIR/test-config.env was updated"
assert_file_exists "$TEST_TEMP_DIR/test-config.env"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_USER=gurli"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_PASS=pass1234"
assert_file_contains "$TEST_TEMP_DIR/test-config.env" "ZEPP_AGPS_TARGET_DIR=/tmp/tmp"
}