fix(dfm): update traps and tests (#124)

* fix(dfm): update traps and tests

* fix(dfm): initialize defaults and secure tests

* fix(tests): secure helper quoting and extend install coverage

* fix(utils): avoid double extension when resolving command

* fix(tests): quote paths and add strict mode

* fix(utils): escape function name in regex
This commit is contained in:
2025-06-11 23:44:52 +03:00
parent 76076fdaa4
commit 1d0ea5ace4
6 changed files with 111 additions and 73 deletions

17
tests/dfm/helpers.bash Normal file
View File

@@ -0,0 +1,17 @@
run_with_dfm() {
local cmd="$*"
run env \
PROJECT_ROOT="$PROJECT_ROOT" \
DFM_CMD_DIR="$PROJECT_ROOT/local/dfm/cmd" \
DFM_LIB_DIR="$PROJECT_ROOT/local/dfm/lib" \
DOTFILES="${DOTFILES:-$PROJECT_ROOT}" \
NO_AUTOMATION="${NO_AUTOMATION:-1}" \
TEMP_DIR="$TEMP_DIR" \
bash -c 'set -e
cmd="$1"
source "$PROJECT_ROOT/local/dfm/lib/common.sh"
source "$PROJECT_ROOT/local/dfm/lib/utils.sh"
set +e
eval "$cmd"' bash "$cmd"
}

View File

@@ -1,34 +1,56 @@
load "$BATS_TEST_DIRNAME/helpers.bash"
setup() {
set -euo pipefail
PROJECT_ROOT="$BATS_TEST_DIRNAME/../.."
TEMP_DIR="$(mktemp -d)"
export TEMP_DIR
}
teardown() {
[[ -n "${TEMP_DIR:-}" ]] && rm -rf "$TEMP_DIR"
}
@test "list_available_commands shows commands" {
run bash -c "export DFM_CMD_DIR=$PROJECT_ROOT/local/dfm/cmd; export DFM_LIB_DIR=$PROJECT_ROOT/local/dfm/lib; export TEMP_DIR=\$(mktemp -d); source $PROJECT_ROOT/local/dfm/lib/common.sh; source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; main::list_available_commands"
run_with_dfm main::list_available_commands
[ "$status" -eq 0 ]
echo "$output" | grep -q "Available commands"
echo "$output" | grep -q "install"
}
@test "interactive confirm returns 0 on yes" {
run bash -c "source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; utils::interactive::confirm \"Proceed?\" <<< \"y\"; echo \$?"
run_with_dfm 'utils::interactive::confirm "Proceed?" <<< "y"; echo $?'
[ "$status" -eq 0 ]
[ "${lines[-1]}" = "0" ]
}
@test "interactive confirm returns 1 on no" {
run bash -c "source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; utils::interactive::confirm \"Proceed?\" <<< \"n\"; echo \$?"
run_with_dfm 'utils::interactive::confirm "Proceed?" <<< "n"; echo $?'
[ "$status" -eq 0 ]
[ "${lines[-1]}" = "1" ]
}
@test "execute_command runs function" {
run bash -c "export DFM_CMD_DIR=$PROJECT_ROOT/local/dfm/cmd; export DFM_LIB_DIR=$PROJECT_ROOT/local/dfm/lib; export TEMP_DIR=\$(mktemp -d); source $PROJECT_ROOT/local/dfm/lib/common.sh; source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; main::execute_command install fonts"
run_with_dfm "main::execute_command install fonts"
[ "$status" -eq 0 ]
echo "$output" | grep -q "Installing fonts"
}
@test "execute_command fails on missing function" {
run bash -c "export DFM_CMD_DIR=$PROJECT_ROOT/local/dfm/cmd; export DFM_LIB_DIR=$PROJECT_ROOT/local/dfm/lib; export TEMP_DIR=\$(mktemp -d); source $PROJECT_ROOT/local/dfm/lib/common.sh; source $PROJECT_ROOT/local/dfm/lib/utils.sh; set +e; main::execute_command install nofunc >/dev/null 2>&1; echo \$?"
[ "$status" -eq 0 ]
[ "${lines[-1]}" = "1" ]
run_with_dfm "main::execute_command install nofunc >/dev/null 2>&1"
[ "$status" -eq 1 ]
}
@test "install all respects skip options" {
run_with_dfm "main::execute_command install all --no-brew --no-cargo --no-automation"
[ "$status" -eq 0 ]
echo "$output" | grep -q "Installing fonts"
[[ "$output" != *"Installing Homebrew"* ]]
[[ "$output" != *"Rust and cargo packages"* ]]
}
@test "get_function_description returns description" {
run_with_dfm "main::get_function_description \"$PROJECT_ROOT/local/dfm/cmd/install.sh\" fonts"
[ "$status" -eq 0 ]
echo "$output" | grep -q "Install all configured fonts"
}