'Generic Commit'
Some checks failed
Build-Test-Publish / build (push) Failing after 26s

This commit is contained in:
Your Name 2025-06-15 19:24:39 +12:00
parent 25aff1baab
commit 1362f03462
2 changed files with 182 additions and 4 deletions

141
whatsdirty/test.sh Executable file
View File

@ -0,0 +1,141 @@
#!/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

View File

@ -8,11 +8,48 @@
# for each git repo print the name of the repo, the datestamp of the last commit,
# the nubmer of changes since then, and whether it's clean or not.
# if no argument is provided, use the current directory
DIR="${1:-.}"
# Configuration file path
CONFIG_FILE="$HOME/.config/whatsdirty.conf"
# make directory canonical
DIR=$(cd "$DIR" 2>/dev/null && pwd) || { echo "Error: Directory '$1' not found"; exit 1; }
# Function to save directory to config
save_directory() {
local dir="$1"
mkdir -p "$(dirname "$CONFIG_FILE")"
echo "$dir" > "$CONFIG_FILE"
}
# Function to load directory from config
load_directory() {
if [ -f "$CONFIG_FILE" ]; then
cat "$CONFIG_FILE"
fi
}
# Determine which directory to use
if [ $# -eq 0 ]; then
# No argument provided - try to load from config
SAVED_DIR=$(load_directory)
if [ -z "$SAVED_DIR" ]; then
echo "Error: No directory specified and no saved directory found."
echo "Usage: $0 <directory>"
echo "The specified directory will be saved for future use."
exit 1
fi
DIR="$SAVED_DIR"
else
# Argument provided - use it and save to config
DIR="$1"
# Validate and canonicalize the directory first
DIR=$(cd "$DIR" 2>/dev/null && pwd) || { echo "Error: Directory '$1' not found"; exit 1; }
save_directory "$DIR"
fi
# Make sure directory is canonical (in case loaded from config)
DIR=$(cd "$DIR" 2>/dev/null && pwd) || {
echo "Error: Saved directory '$DIR' no longer exists";
rm -f "$CONFIG_FILE"
exit 1;
}
# Color codes
RED='\033[0;31m'