getpkg/whatsdirty/test.sh
Your Name 1362f03462
Some checks failed
Build-Test-Publish / build (push) Failing after 26s
'Generic Commit'
2025-06-15 19:24:39 +12:00

141 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
WHATSDIRTY_SCRIPT="$SCRIPT_DIR/whatsdirty.sh"
CONFIG_FILE="$HOME/.config/whatsdirty.conf"
BACKUP_FILE=""
TEST_PASSED=true
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to print test results
print_test() {
local test_name="$1"
local result="$2"
if [ "$result" = "PASS" ]; then
echo -e "${GREEN}${NC} $test_name"
else
echo -e "${RED}${NC} $test_name"
TEST_PASSED=false
fi
}
# Function to cleanup and restore
cleanup() {
if [ -n "$BACKUP_FILE" ] && [ -f "$BACKUP_FILE" ]; then
cp "$BACKUP_FILE" "$CONFIG_FILE"
rm -f "$BACKUP_FILE"
elif [ -n "$BACKUP_FILE" ]; then
# Original didn't exist, so remove the created one
rm -f "$CONFIG_FILE"
fi
}
# Set up trap to restore config on exit
trap cleanup EXIT
echo "Testing whatsdirty.sh configuration behavior..."
echo
# Backup existing config if it exists
if [ -f "$CONFIG_FILE" ]; then
BACKUP_FILE=$(mktemp)
cp "$CONFIG_FILE" "$BACKUP_FILE"
else
# Mark that we need to remove the config file on cleanup
BACKUP_FILE="NONE_EXISTED"
fi
# Remove config to start fresh
rm -f "$CONFIG_FILE"
# Test 1: Running without arguments and no config should fail
echo "Test 1: First run without arguments (should fail)"
OUTPUT=$("$WHATSDIRTY_SCRIPT" 2>&1 || true)
if echo "$OUTPUT" | grep -q "No directory specified and no saved directory found"; then
print_test "First run without args shows error" "PASS"
else
print_test "First run without args shows error" "FAIL"
echo " Expected: 'No directory specified and no saved directory found'"
echo " Got: $OUTPUT"
fi
# Test 2: Running with a valid directory should save config
echo -e "\nTest 2: First run with directory argument"
TEST_DIR="/home/j/code"
if [ -d "$TEST_DIR" ]; then
"$WHATSDIRTY_SCRIPT" "$TEST_DIR" > /dev/null 2>&1
if [ -f "$CONFIG_FILE" ] && [ "$(cat "$CONFIG_FILE")" = "$TEST_DIR" ]; then
print_test "Directory saved to config" "PASS"
else
print_test "Directory saved to config" "FAIL"
fi
else
print_test "Directory saved to config" "SKIP (test directory not found)"
fi
# Test 3: Running without arguments should use saved directory
echo -e "\nTest 3: Subsequent run without arguments"
if "$WHATSDIRTY_SCRIPT" > /dev/null 2>&1; then
print_test "Uses saved directory from config" "PASS"
else
print_test "Uses saved directory from config" "FAIL"
fi
# Test 4: Running with new directory should update config
echo -e "\nTest 4: Run with different directory"
NEW_TEST_DIR="$SCRIPT_DIR/.."
if [ -d "$NEW_TEST_DIR" ]; then
CANONICAL_DIR=$(cd "$NEW_TEST_DIR" && pwd)
"$WHATSDIRTY_SCRIPT" "$NEW_TEST_DIR" > /dev/null 2>&1
if [ -f "$CONFIG_FILE" ] && [ "$(cat "$CONFIG_FILE")" = "$CANONICAL_DIR" ]; then
print_test "Config updated with new directory" "PASS"
else
print_test "Config updated with new directory" "FAIL"
fi
else
print_test "Config updated with new directory" "SKIP (test directory not found)"
fi
# Test 5: Invalid directory should fail
echo -e "\nTest 5: Invalid directory handling"
OUTPUT=$("$WHATSDIRTY_SCRIPT" "/nonexistent/directory" 2>&1 || true)
if echo "$OUTPUT" | grep -q "Directory '/nonexistent/directory' not found"; then
print_test "Invalid directory shows error" "PASS"
else
print_test "Invalid directory shows error" "FAIL"
echo " Expected: 'Directory '/nonexistent/directory' not found'"
echo " Got: $OUTPUT"
fi
# Test 6: Saved directory that no longer exists
echo -e "\nTest 6: Saved directory that no longer exists"
mkdir -p "$(dirname "$CONFIG_FILE")"
echo "/nonexistent/saved/directory" > "$CONFIG_FILE"
OUTPUT=$("$WHATSDIRTY_SCRIPT" 2>&1 || true)
if echo "$OUTPUT" | grep -q "no longer exists"; then
# Check that config was removed
if [ ! -f "$CONFIG_FILE" ]; then
print_test "Handles deleted saved directory" "PASS"
else
print_test "Handles deleted saved directory" "FAIL (config not cleaned up)"
fi
else
print_test "Handles deleted saved directory" "FAIL"
echo " Expected message about directory no longer existing"
echo " Got: $OUTPUT"
fi
# Summary
echo
if [ "$TEST_PASSED" = true ]; then
echo -e "${GREEN}All tests passed!${NC}"
exit 0
else
echo -e "${RED}Some tests failed!${NC}"
exit 1
fi