Optimized git
Some checks failed
Some checks failed
This commit is contained in:
84
tests/README.md
Normal file
84
tests/README.md
Normal file
@@ -0,0 +1,84 @@
|
||||
# HyperTerm Tests
|
||||
|
||||
Test suite for HyperTerm prompt functionality and performance.
|
||||
|
||||
## Test Files
|
||||
|
||||
### test_prompt.sh
|
||||
Comprehensive test suite with full functionality testing.
|
||||
|
||||
**Features:**
|
||||
- Individual function testing
|
||||
- Performance benchmarking
|
||||
- Git state testing
|
||||
- Interactive test mode (skipped in CI)
|
||||
- Non-interactive mode for CI/automation
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
cd tests
|
||||
bash test_prompt.sh # Interactive mode
|
||||
bash test_prompt.sh --non-interactive # Non-interactive mode (for CI)
|
||||
```
|
||||
|
||||
### quick_test.sh
|
||||
Fast validation test for immediate feedback.
|
||||
|
||||
**Features:**
|
||||
- Quick function validation
|
||||
- Basic performance check
|
||||
- Works in current directory or creates temp repo
|
||||
|
||||
**Usage:**
|
||||
```bash
|
||||
cd tests
|
||||
bash quick_test.sh
|
||||
```
|
||||
|
||||
## Test Environment
|
||||
|
||||
Tests create isolated environments to avoid affecting your working directory:
|
||||
- Temporary git repositories in `/tmp/`
|
||||
- Various git states (clean, dirty, staged, untracked)
|
||||
- Automatic cleanup on exit
|
||||
|
||||
## Performance Testing
|
||||
|
||||
Both test scripts include performance measurements:
|
||||
- Multiple iterations for accurate timing
|
||||
- Average execution time per prompt call
|
||||
- Performance classification (Excellent < 10ms, Good < 50ms)
|
||||
|
||||
## Git States Tested
|
||||
|
||||
- Clean repository
|
||||
- Modified files
|
||||
- Untracked files
|
||||
- Staged files
|
||||
- Mixed states
|
||||
- Ahead/behind tracking
|
||||
|
||||
## Requirements
|
||||
|
||||
- Git installed and available in PATH
|
||||
- Bash 4.0 or later
|
||||
- HyperTerm core files in `../hyperterm/core/`
|
||||
|
||||
## Logging
|
||||
|
||||
All tests use structured logging:
|
||||
- `[INFO]` - General information
|
||||
- `[SUCCESS]` - Successful operations
|
||||
- `[WARN]` - Warnings
|
||||
- `[ERROR]` - Errors
|
||||
|
||||
## Interactive Mode
|
||||
|
||||
The full test suite includes an interactive mode with commands:
|
||||
- `status` - Show git status
|
||||
- `prompt` - Show full prompt
|
||||
- `perf` - Run performance test
|
||||
- `states` - Test different git states
|
||||
- `modify` - Create test modifications
|
||||
- `clean` - Clean working directory
|
||||
- `exit` - Exit interactive mode
|
||||
176
tests/quick_test.sh
Normal file
176
tests/quick_test.sh
Normal file
@@ -0,0 +1,176 @@
|
||||
#!/bin/bash
|
||||
# Quick Prompt Test - Fast validation
|
||||
# Simple test for immediate feedback
|
||||
# shellcheck disable=SC1090,SC2034,SC2155
|
||||
|
||||
set -e
|
||||
|
||||
# Logging
|
||||
log_info() { echo "[INFO] $*"; }
|
||||
log_error() { echo "[ERROR] $*"; }
|
||||
log_success() { echo "[SUCCESS] $*"; }
|
||||
|
||||
# Configuration
|
||||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
readonly PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
|
||||
# Basic colors fallback
|
||||
setup_colors() {
|
||||
RESET='\033[0m'
|
||||
BOLD='\033[1m'
|
||||
RED='\033[31m'
|
||||
GREEN='\033[32m'
|
||||
YELLOW='\033[33m'
|
||||
BLUE='\033[34m'
|
||||
CYAN='\033[36m'
|
||||
WHITE='\033[37m'
|
||||
GREY='\033[90m'
|
||||
}
|
||||
|
||||
# Load functions
|
||||
load_functions() {
|
||||
log_info "Loading HyperTerm functions"
|
||||
|
||||
# Load colors if available
|
||||
local colors_file="$PROJECT_ROOT/hyperterm/core/colors.sh"
|
||||
if [[ -f "$colors_file" ]]; then
|
||||
source "$colors_file"
|
||||
else
|
||||
setup_colors
|
||||
fi
|
||||
|
||||
# Load git functions
|
||||
local git_file="$PROJECT_ROOT/hyperterm/core/git.sh"
|
||||
|
||||
if [[ -f "$git_file" ]]; then
|
||||
source "$git_file"
|
||||
log_success "Git functions loaded"
|
||||
return 0
|
||||
else
|
||||
log_error "Git functions not found"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Test in current directory
|
||||
test_current_directory() {
|
||||
if git rev-parse --git-dir >/dev/null 2>&1; then
|
||||
log_success "Current directory is a git repository"
|
||||
|
||||
echo "Current git state:"
|
||||
echo -n " Branch: "
|
||||
_get_git_branch
|
||||
echo ""
|
||||
|
||||
echo -n " Status: "
|
||||
if command -v _get_git_status_fast >/dev/null 2>&1; then
|
||||
_get_git_status_fast
|
||||
else
|
||||
_prompt_get_git_status
|
||||
fi
|
||||
echo ""
|
||||
|
||||
echo -n " Full prompt: "
|
||||
__prompt_git
|
||||
echo ""
|
||||
|
||||
return 0
|
||||
else
|
||||
log_info "Current directory is not a git repository"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Create temporary test
|
||||
test_with_temp_repo() {
|
||||
log_info "Creating temporary git repository for testing"
|
||||
|
||||
local temp_dir="/tmp/quick_test_$$"
|
||||
mkdir -p "$temp_dir"
|
||||
|
||||
(
|
||||
cd "$temp_dir"
|
||||
git init --quiet
|
||||
git config user.name "Test User"
|
||||
git config user.email "test@example.com"
|
||||
echo "# Quick Test" > README.md
|
||||
echo "test content" > file.txt
|
||||
git add README.md
|
||||
git commit -m "Initial commit" --quiet
|
||||
|
||||
# Create changes
|
||||
echo "modified" >> file.txt
|
||||
echo "untracked" > new.txt
|
||||
|
||||
log_success "Temporary repository created with changes"
|
||||
|
||||
echo "Test results:"
|
||||
echo -n " Branch: "
|
||||
_get_git_branch
|
||||
echo ""
|
||||
|
||||
echo -n " Status: "
|
||||
if command -v _get_git_status_fast >/dev/null 2>&1; then
|
||||
_get_git_status_fast
|
||||
else
|
||||
_prompt_get_git_status
|
||||
fi
|
||||
echo ""
|
||||
|
||||
echo -n " Full prompt: "
|
||||
__prompt_git
|
||||
echo ""
|
||||
)
|
||||
|
||||
rm -rf "$temp_dir"
|
||||
log_info "Temporary repository cleaned up"
|
||||
}
|
||||
|
||||
# Performance check
|
||||
quick_performance_check() {
|
||||
log_info "Running quick performance check"
|
||||
|
||||
local iterations=10
|
||||
local start_time end_time duration
|
||||
|
||||
start_time=$(date +%s%N)
|
||||
for ((i=1; i<=iterations; i++)); do
|
||||
__prompt_git >/dev/null 2>&1
|
||||
done
|
||||
end_time=$(date +%s%N)
|
||||
|
||||
duration=$(( (end_time - start_time) / 1000000 ))
|
||||
local avg_duration=$(( duration / iterations ))
|
||||
|
||||
echo "Performance: ${avg_duration}ms average (${iterations} iterations)"
|
||||
|
||||
if [[ $avg_duration -lt 50 ]]; then
|
||||
log_success "Performance is good"
|
||||
elif [[ $avg_duration -lt 100 ]]; then
|
||||
log_info "Performance is acceptable"
|
||||
else
|
||||
log_error "Performance may need optimization"
|
||||
fi
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
log_info "HyperTerm Quick Test"
|
||||
|
||||
if ! load_functions; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! test_current_directory; then
|
||||
test_with_temp_repo
|
||||
fi
|
||||
|
||||
quick_performance_check
|
||||
|
||||
log_success "Quick test completed"
|
||||
}
|
||||
|
||||
# Execute if run directly
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
main "$@"
|
||||
fi
|
||||
304
tests/test_prompt.sh
Normal file
304
tests/test_prompt.sh
Normal file
@@ -0,0 +1,304 @@
|
||||
#!/bin/bash
|
||||
# HyperTerm Prompt Test Suite
|
||||
# Organized testing environment for prompt functionality
|
||||
# shellcheck disable=SC1090,SC2034,SC2155
|
||||
|
||||
set -e
|
||||
|
||||
# Logging functions
|
||||
log_info() { echo "[INFO] $*"; }
|
||||
log_warn() { echo "[WARN] $*"; }
|
||||
log_error() { echo "[ERROR] $*"; }
|
||||
log_success() { echo "[SUCCESS] $*"; }
|
||||
|
||||
# Configuration
|
||||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
readonly PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
readonly TEST_DIR="/tmp/hyperterm_test_$(date +%s)"
|
||||
|
||||
# Cleanup function
|
||||
cleanup() {
|
||||
if [[ -n "${TEST_DIR:-}" && -d "$TEST_DIR" ]]; then
|
||||
rm -rf "$TEST_DIR"
|
||||
log_info "Test directory cleaned: $TEST_DIR"
|
||||
fi
|
||||
}
|
||||
|
||||
# Setup test environment
|
||||
setup_test_env() {
|
||||
log_info "Setting up test environment"
|
||||
|
||||
mkdir -p "$TEST_DIR"
|
||||
cd "$TEST_DIR"
|
||||
|
||||
# Create test git repository
|
||||
git init --quiet
|
||||
git config user.name "Test User"
|
||||
git config user.email "test@example.com"
|
||||
echo "# Test Repository" > README.md
|
||||
echo "test content" > test.txt
|
||||
git add README.md
|
||||
git commit -m "Initial commit" --quiet
|
||||
|
||||
# Create various git states for testing
|
||||
echo "modified content" >> test.txt
|
||||
echo "untracked file" > untracked.txt
|
||||
echo "staged content" > staged.txt
|
||||
git add staged.txt
|
||||
|
||||
log_success "Test environment created at: $TEST_DIR"
|
||||
log_info "Git states created: modified, untracked, staged files"
|
||||
}
|
||||
|
||||
# Load hyperterm functions
|
||||
load_hyperterm_functions() {
|
||||
log_info "Loading HyperTerm functions"
|
||||
|
||||
# Load colors
|
||||
local colors_file="$PROJECT_ROOT/hyperterm/core/colors.sh"
|
||||
if [[ -f "$colors_file" ]]; then
|
||||
source "$colors_file"
|
||||
log_success "Colors loaded from: $colors_file"
|
||||
else
|
||||
log_warn "Colors file not found, using fallback"
|
||||
# Fallback colors
|
||||
RESET='\033[0m'
|
||||
BOLD='\033[1m'
|
||||
RED='\033[31m'
|
||||
GREEN='\033[32m'
|
||||
YELLOW='\033[33m'
|
||||
BLUE='\033[34m'
|
||||
CYAN='\033[36m'
|
||||
WHITE='\033[37m'
|
||||
GREY='\033[90m'
|
||||
fi
|
||||
|
||||
# Load git functions
|
||||
local git_file="$PROJECT_ROOT/hyperterm/core/git.sh"
|
||||
|
||||
if [[ -f "$git_file" ]]; then
|
||||
source "$git_file"
|
||||
log_success "Git functions loaded"
|
||||
return 0
|
||||
else
|
||||
log_error "Git functions not found"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Test individual functions
|
||||
test_individual_functions() {
|
||||
log_info "Testing individual functions"
|
||||
|
||||
echo "Branch detection:"
|
||||
if command -v _get_git_branch >/dev/null 2>&1; then
|
||||
local branch
|
||||
branch="$(_get_git_branch)"
|
||||
echo " Result: $branch"
|
||||
log_success "Branch detection working"
|
||||
else
|
||||
log_error "Branch detection function not found"
|
||||
fi
|
||||
|
||||
echo "Git progress detection:"
|
||||
if command -v _get_git_progress >/dev/null 2>&1; then
|
||||
local progress
|
||||
progress="$(_get_git_progress)"
|
||||
echo " Result: ${progress:-"(none)"}"
|
||||
log_success "Progress detection working"
|
||||
else
|
||||
log_error "Progress detection function not found"
|
||||
fi
|
||||
|
||||
echo "Git status:"
|
||||
if command -v _get_git_status_fast >/dev/null 2>&1; then
|
||||
local status
|
||||
status="$(_get_git_status_fast)"
|
||||
echo " Result: $status"
|
||||
log_success "Fast git status working"
|
||||
elif command -v _prompt_get_git_status >/dev/null 2>&1; then
|
||||
local status
|
||||
status="$(_prompt_get_git_status)"
|
||||
echo " Result: $status"
|
||||
log_success "Original git status working"
|
||||
else
|
||||
log_error "No git status function found"
|
||||
fi
|
||||
}
|
||||
|
||||
# Performance benchmark
|
||||
run_performance_test() {
|
||||
log_info "Running performance benchmark"
|
||||
|
||||
if ! command -v __prompt_git >/dev/null 2>&1; then
|
||||
log_error "Prompt function not available"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local iterations=50
|
||||
local start_time end_time duration
|
||||
|
||||
log_info "Running $iterations iterations"
|
||||
|
||||
start_time=$(date +%s%N)
|
||||
for ((i=1; i<=iterations; i++)); do
|
||||
__prompt_git >/dev/null 2>&1
|
||||
done
|
||||
end_time=$(date +%s%N)
|
||||
|
||||
duration=$(( (end_time - start_time) / 1000000 ))
|
||||
local avg_duration=$(( duration / iterations ))
|
||||
|
||||
echo "Performance Results:"
|
||||
echo " Total time: ${duration}ms"
|
||||
echo " Average per call: ${avg_duration}ms"
|
||||
echo " Iterations: $iterations"
|
||||
|
||||
if [[ $avg_duration -lt 10 ]]; then
|
||||
log_success "Performance: Excellent (< 10ms per call)"
|
||||
elif [[ $avg_duration -lt 50 ]]; then
|
||||
log_success "Performance: Good (< 50ms per call)"
|
||||
else
|
||||
log_warn "Performance: Slow (>= 50ms per call)"
|
||||
fi
|
||||
}
|
||||
|
||||
# Test different git states
|
||||
test_git_states() {
|
||||
log_info "Testing different git states"
|
||||
|
||||
# Clean state
|
||||
git checkout -- . >/dev/null 2>&1
|
||||
git clean -fd >/dev/null 2>&1
|
||||
echo "Clean repository:"
|
||||
__prompt_git
|
||||
echo ""
|
||||
|
||||
# Modified files
|
||||
echo "modified" >> test.txt
|
||||
echo "Modified files:"
|
||||
__prompt_git
|
||||
echo ""
|
||||
|
||||
# Untracked files
|
||||
echo "untracked" > new_file.txt
|
||||
echo "With untracked files:"
|
||||
__prompt_git
|
||||
echo ""
|
||||
|
||||
# Staged files
|
||||
git add new_file.txt
|
||||
echo "With staged files:"
|
||||
__prompt_git
|
||||
echo ""
|
||||
|
||||
log_success "Git states testing completed"
|
||||
}
|
||||
|
||||
# Interactive test mode
|
||||
interactive_mode() {
|
||||
log_info "Entering interactive test mode"
|
||||
echo "Available commands:"
|
||||
echo " status - Show current git status"
|
||||
echo " prompt - Show full prompt"
|
||||
echo " perf - Run performance test"
|
||||
echo " states - Test different git states"
|
||||
echo " modify - Modify files for testing"
|
||||
echo " clean - Clean working directory"
|
||||
echo " help - Show this help"
|
||||
echo " exit - Exit interactive mode"
|
||||
echo ""
|
||||
|
||||
while true; do
|
||||
echo -n "test> "
|
||||
read -r command
|
||||
|
||||
case "$command" in
|
||||
"status")
|
||||
if command -v _get_git_status_fast >/dev/null 2>&1; then
|
||||
_get_git_status_fast
|
||||
else
|
||||
_prompt_get_git_status
|
||||
fi
|
||||
echo ""
|
||||
;;
|
||||
"prompt")
|
||||
__prompt_git
|
||||
echo ""
|
||||
;;
|
||||
"perf")
|
||||
run_performance_test
|
||||
;;
|
||||
"states")
|
||||
test_git_states
|
||||
;;
|
||||
"modify")
|
||||
echo "Creating test modifications"
|
||||
echo "change $(date)" >> test.txt
|
||||
echo "new file $(date)" > "file_$(date +%s).txt"
|
||||
log_success "Files modified"
|
||||
;;
|
||||
"clean")
|
||||
git checkout -- . >/dev/null 2>&1
|
||||
git clean -fd >/dev/null 2>&1
|
||||
log_success "Working directory cleaned"
|
||||
;;
|
||||
"help")
|
||||
echo "Available commands: status, prompt, perf, states, modify, clean, help, exit"
|
||||
;;
|
||||
"exit")
|
||||
log_info "Exiting interactive mode"
|
||||
break
|
||||
;;
|
||||
"")
|
||||
# Empty command, continue
|
||||
;;
|
||||
*)
|
||||
log_warn "Unknown command: $command (type 'help' for available commands)"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
log_info "Starting HyperTerm Prompt Test Suite"
|
||||
|
||||
# Setup
|
||||
setup_test_env
|
||||
|
||||
if ! load_hyperterm_functions; then
|
||||
log_error "Failed to load HyperTerm functions"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run tests
|
||||
test_individual_functions
|
||||
echo ""
|
||||
run_performance_test
|
||||
echo ""
|
||||
test_git_states
|
||||
echo ""
|
||||
|
||||
# Interactive mode (only if not in CI or non-interactive mode)
|
||||
if [[ "$1" != "--non-interactive" && "$1" != "-n" && -z "${CI:-}" ]]; then
|
||||
log_info "All automated tests completed"
|
||||
echo "Enter interactive mode? (y/N)"
|
||||
read -r response
|
||||
if [[ "$response" =~ ^[Yy]$ ]]; then
|
||||
interactive_mode
|
||||
fi
|
||||
else
|
||||
log_info "Running in non-interactive mode, skipping interactive prompt"
|
||||
fi
|
||||
|
||||
log_success "Test suite completed successfully"
|
||||
}
|
||||
|
||||
# Trap cleanup on exit
|
||||
trap cleanup EXIT
|
||||
|
||||
# Execute main function if script is run directly
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
main "$@"
|
||||
fi
|
||||
Reference in New Issue
Block a user