Dotfiles update 2025-12-25 15:45
This commit is contained in:
241
tests/run-tests.zsh
Executable file
241
tests/run-tests.zsh
Executable file
@@ -0,0 +1,241 @@
|
||||
#!/usr/bin/env zsh
|
||||
# ============================================================================
|
||||
# Dotfiles Test Framework
|
||||
# ============================================================================
|
||||
# Simple unit testing for shell functions and scripts.
|
||||
#
|
||||
# Usage:
|
||||
# ./tests/run-tests.zsh # Run all tests
|
||||
# ./tests/run-tests.zsh test_utils # Run specific test file
|
||||
# ============================================================================
|
||||
|
||||
set -e
|
||||
|
||||
# ============================================================================
|
||||
# Configuration
|
||||
# ============================================================================
|
||||
|
||||
SCRIPT_DIR="${0:A:h}"
|
||||
DOTFILES_DIR="${SCRIPT_DIR:h}"
|
||||
TESTS_DIR="$SCRIPT_DIR"
|
||||
|
||||
# Colors
|
||||
RED=$'\033[0;31m'
|
||||
GREEN=$'\033[0;32m'
|
||||
YELLOW=$'\033[1;33m'
|
||||
CYAN=$'\033[0;36m'
|
||||
NC=$'\033[0m'
|
||||
|
||||
# Counters
|
||||
TESTS_RUN=0
|
||||
TESTS_PASSED=0
|
||||
TESTS_FAILED=0
|
||||
CURRENT_TEST=""
|
||||
|
||||
# ============================================================================
|
||||
# Test Framework Functions
|
||||
# ============================================================================
|
||||
|
||||
# Start a test group
|
||||
describe() {
|
||||
echo ""
|
||||
echo -e "${CYAN}▶ $1${NC}"
|
||||
}
|
||||
|
||||
# Run a single test
|
||||
it() {
|
||||
CURRENT_TEST="$1"
|
||||
((TESTS_RUN++))
|
||||
}
|
||||
|
||||
# Assert equality
|
||||
assert_eq() {
|
||||
local actual="$1"
|
||||
local expected="$2"
|
||||
local message="${3:-Values should be equal}"
|
||||
|
||||
if [[ "$actual" == "$expected" ]]; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}Expected:${NC} $expected"
|
||||
echo -e " ${RED}Actual:${NC} $actual"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert not equal
|
||||
assert_ne() {
|
||||
local actual="$1"
|
||||
local not_expected="$2"
|
||||
|
||||
if [[ "$actual" != "$not_expected" ]]; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}Should not equal:${NC} $not_expected"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert command succeeds
|
||||
assert_success() {
|
||||
local cmd="$1"
|
||||
|
||||
if eval "$cmd" &>/dev/null; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}Command failed:${NC} $cmd"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert command fails
|
||||
assert_fail() {
|
||||
local cmd="$1"
|
||||
|
||||
if ! eval "$cmd" &>/dev/null; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}Expected failure but succeeded:${NC} $cmd"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert string contains
|
||||
assert_contains() {
|
||||
local haystack="$1"
|
||||
local needle="$2"
|
||||
|
||||
if [[ "$haystack" == *"$needle"* ]]; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}String should contain:${NC} $needle"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert file exists
|
||||
assert_file_exists() {
|
||||
local file="$1"
|
||||
|
||||
if [[ -f "$file" ]]; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}File not found:${NC} $file"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert directory exists
|
||||
assert_dir_exists() {
|
||||
local dir="$1"
|
||||
|
||||
if [[ -d "$dir" ]]; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}Directory not found:${NC} $dir"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Assert command exists
|
||||
assert_cmd_exists() {
|
||||
local cmd="$1"
|
||||
|
||||
if command -v "$cmd" &>/dev/null; then
|
||||
echo -e " ${GREEN}✓${NC} $CURRENT_TEST"
|
||||
((TESTS_PASSED++))
|
||||
else
|
||||
echo -e " ${RED}✗${NC} $CURRENT_TEST"
|
||||
echo -e " ${RED}Command not found:${NC} $cmd"
|
||||
((TESTS_FAILED++))
|
||||
fi
|
||||
}
|
||||
|
||||
# Skip a test
|
||||
skip() {
|
||||
local reason="${1:-No reason given}"
|
||||
echo -e " ${YELLOW}○${NC} $CURRENT_TEST (SKIPPED: $reason)"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Test Runner
|
||||
# ============================================================================
|
||||
|
||||
run_test_file() {
|
||||
local test_file="$1"
|
||||
source "$test_file"
|
||||
}
|
||||
|
||||
print_summary() {
|
||||
echo ""
|
||||
echo "─────────────────────────────────────────"
|
||||
echo -e "Tests: ${CYAN}$TESTS_RUN${NC}"
|
||||
echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}"
|
||||
echo -e "Failed: ${RED}$TESTS_FAILED${NC}"
|
||||
echo "─────────────────────────────────────────"
|
||||
|
||||
if (( TESTS_FAILED > 0 )); then
|
||||
echo -e "${RED}FAILED${NC}"
|
||||
return 1
|
||||
else
|
||||
echo -e "${GREEN}PASSED${NC}"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Main
|
||||
# ============================================================================
|
||||
|
||||
main() {
|
||||
echo "╔═══════════════════════════════════════╗"
|
||||
echo "║ Dotfiles Test Suite ║"
|
||||
echo "╚═══════════════════════════════════════╝"
|
||||
|
||||
# Source the libraries we're testing
|
||||
source "$DOTFILES_DIR/zsh/lib/bootstrap.zsh" 2>/dev/null || true
|
||||
|
||||
local specific_test="$1"
|
||||
|
||||
if [[ -n "$specific_test" ]]; then
|
||||
# Run specific test file
|
||||
if [[ -f "$TESTS_DIR/${specific_test}.zsh" ]]; then
|
||||
run_test_file "$TESTS_DIR/${specific_test}.zsh"
|
||||
elif [[ -f "$TESTS_DIR/test_${specific_test}.zsh" ]]; then
|
||||
run_test_file "$TESTS_DIR/test_${specific_test}.zsh"
|
||||
else
|
||||
echo "Test file not found: $specific_test"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
# Run all test files
|
||||
for test_file in "$TESTS_DIR"/test_*.zsh(N); do
|
||||
[[ -f "$test_file" ]] || continue
|
||||
echo ""
|
||||
echo -e "${YELLOW}Running: $(basename "$test_file")${NC}"
|
||||
run_test_file "$test_file"
|
||||
done
|
||||
fi
|
||||
|
||||
print_summary
|
||||
}
|
||||
|
||||
# Export functions for test files
|
||||
export -f describe it assert_eq assert_ne assert_success assert_fail
|
||||
export -f assert_contains assert_file_exists assert_dir_exists assert_cmd_exists skip
|
||||
|
||||
main "$@"
|
||||
94
tests/test_config.zsh
Executable file
94
tests/test_config.zsh
Executable file
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env zsh
|
||||
# ============================================================================
|
||||
# Tests for zsh/lib/config.zsh
|
||||
# ============================================================================
|
||||
|
||||
# Source config if not already loaded
|
||||
source "${DOTFILES_DIR:-$HOME/.dotfiles}/zsh/lib/config.zsh" 2>/dev/null
|
||||
|
||||
# ============================================================================
|
||||
# Tests
|
||||
# ============================================================================
|
||||
|
||||
describe "Core configuration variables"
|
||||
|
||||
it "should have DOTFILES_VERSION defined"
|
||||
assert_ne "${DOTFILES_VERSION:-}" ""
|
||||
|
||||
it "should have DOTFILES_DIR defined"
|
||||
assert_ne "${DOTFILES_DIR:-}" ""
|
||||
|
||||
it "should have DOTFILES_BRANCH defined"
|
||||
assert_ne "${DOTFILES_BRANCH:-}" ""
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "Display configuration"
|
||||
|
||||
it "should have DF_WIDTH defined"
|
||||
assert_ne "${DF_WIDTH:-}" ""
|
||||
|
||||
it "should have DF_WIDTH as a number"
|
||||
[[ "${DF_WIDTH:-66}" =~ ^[0-9]+$ ]] && assert_success "true" || assert_fail "DF_WIDTH not a number"
|
||||
|
||||
it "should have MOTD_STYLE defined"
|
||||
assert_ne "${MOTD_STYLE:-}" ""
|
||||
|
||||
it "should have ENABLE_MOTD defined"
|
||||
assert_ne "${ENABLE_MOTD:-}" ""
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "Theme configuration"
|
||||
|
||||
it "should have ZSH_THEME_NAME defined"
|
||||
assert_ne "${ZSH_THEME_NAME:-}" ""
|
||||
|
||||
it "should have THEME_TIMER_THRESHOLD defined"
|
||||
assert_ne "${THEME_TIMER_THRESHOLD:-}" ""
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "Feature toggles"
|
||||
|
||||
it "should have ENABLE_SMART_SUGGESTIONS defined"
|
||||
assert_ne "${ENABLE_SMART_SUGGESTIONS:-}" ""
|
||||
|
||||
it "should have ENABLE_COMMAND_PALETTE defined"
|
||||
assert_ne "${ENABLE_COMMAND_PALETTE:-}" ""
|
||||
|
||||
it "should have ENABLE_VAULT defined"
|
||||
assert_ne "${ENABLE_VAULT:-}" ""
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "Path configuration"
|
||||
|
||||
it "should have valid DOTFILES_DIR path"
|
||||
assert_dir_exists "${DOTFILES_DIR:-$HOME/.dotfiles}"
|
||||
|
||||
it "should have dotfiles.conf in DOTFILES_DIR"
|
||||
if [[ -d "${DOTFILES_DIR:-$HOME/.dotfiles}" ]]; then
|
||||
assert_file_exists "${DOTFILES_DIR:-$HOME/.dotfiles}/dotfiles.conf"
|
||||
else
|
||||
skip "DOTFILES_DIR not found"
|
||||
fi
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "df_config helper function"
|
||||
|
||||
it "should have df_config function defined"
|
||||
if typeset -f df_config &>/dev/null; then
|
||||
assert_success "true"
|
||||
else
|
||||
skip "df_config not defined in this version"
|
||||
fi
|
||||
|
||||
it "should return default for undefined variable"
|
||||
if typeset -f df_config &>/dev/null; then
|
||||
local result=$(df_config "UNDEFINED_VAR_12345" "default_value")
|
||||
assert_eq "$result" "default_value"
|
||||
else
|
||||
skip "df_config not defined"
|
||||
fi
|
||||
111
tests/test_utils.zsh
Executable file
111
tests/test_utils.zsh
Executable file
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env zsh
|
||||
# ============================================================================
|
||||
# Tests for zsh/lib/utils.zsh
|
||||
# ============================================================================
|
||||
|
||||
# Source utils if not already loaded
|
||||
source "${DOTFILES_DIR:-$HOME/.dotfiles}/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
# ============================================================================
|
||||
# Tests
|
||||
# ============================================================================
|
||||
|
||||
describe "df_cmd_exists"
|
||||
|
||||
it "should return true for existing command (ls)"
|
||||
assert_success "df_cmd_exists ls"
|
||||
|
||||
it "should return false for non-existent command"
|
||||
assert_fail "df_cmd_exists this_command_does_not_exist_12345"
|
||||
|
||||
it "should work with common tools"
|
||||
assert_success "df_cmd_exists git"
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "df_print functions"
|
||||
|
||||
it "should have df_print_success defined"
|
||||
assert_success "typeset -f df_print_success"
|
||||
|
||||
it "should have df_print_error defined"
|
||||
assert_success "typeset -f df_print_error"
|
||||
|
||||
it "should have df_print_warning defined"
|
||||
assert_success "typeset -f df_print_warning"
|
||||
|
||||
it "should have df_print_step defined"
|
||||
assert_success "typeset -f df_print_step"
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "df_in_git_repo"
|
||||
|
||||
it "should detect git repository in dotfiles dir"
|
||||
(
|
||||
cd "${DOTFILES_DIR:-$HOME/.dotfiles}"
|
||||
if [[ -d .git ]]; then
|
||||
assert_success "df_in_git_repo"
|
||||
else
|
||||
skip "Not a git repo"
|
||||
fi
|
||||
)
|
||||
|
||||
it "should return false in /tmp"
|
||||
(
|
||||
cd /tmp
|
||||
assert_fail "df_in_git_repo"
|
||||
)
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "df_ensure_dir"
|
||||
|
||||
it "should create directory if it doesn't exist"
|
||||
local test_dir="/tmp/dotfiles_test_$$"
|
||||
df_ensure_dir "$test_dir"
|
||||
assert_dir_exists "$test_dir"
|
||||
rmdir "$test_dir" 2>/dev/null
|
||||
|
||||
it "should not fail if directory exists"
|
||||
df_ensure_dir "/tmp"
|
||||
assert_success "true"
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "_df_hline"
|
||||
|
||||
it "should create a line of specified width"
|
||||
local line=$(_df_hline "=" 10)
|
||||
assert_eq "${#line}" "10"
|
||||
|
||||
it "should use specified character"
|
||||
local line=$(_df_hline "-" 5)
|
||||
assert_eq "$line" "-----"
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "Color variables"
|
||||
|
||||
it "should have DF_GREEN defined"
|
||||
assert_ne "$DF_GREEN" ""
|
||||
|
||||
it "should have DF_RED defined"
|
||||
assert_ne "$DF_RED" ""
|
||||
|
||||
it "should have DF_NC (reset) defined"
|
||||
assert_ne "$DF_NC" ""
|
||||
|
||||
it "should have DF_CYAN defined"
|
||||
assert_ne "$DF_CYAN" ""
|
||||
|
||||
# ============================================================================
|
||||
|
||||
describe "Configuration variables"
|
||||
|
||||
it "should have DOTFILES_DIR defined"
|
||||
assert_ne "${DOTFILES_DIR:-}" ""
|
||||
|
||||
it "should have DF_WIDTH defined with reasonable value"
|
||||
local width="${DF_WIDTH:-66}"
|
||||
(( width >= 40 && width <= 120 )) && assert_success "true" || assert_fail "DF_WIDTH out of range"
|
||||
Reference in New Issue
Block a user