Skip to content

Commit 7bacaa1

Browse files
committed
Add run-tests.sh script to automate Python unit testing
1 parent 501ec96 commit 7bacaa1

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

scripts/run-tests.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# run-tests.sh - Runs Python unit tests for the server
4+
5+
# Define color codes
6+
GREEN='\033[0;32m'
7+
YELLOW='\033[1;33m'
8+
RED='\033[0;31m'
9+
NC='\033[0m' # No Color
10+
11+
# Store initial directory
12+
INITIAL_DIR=$(pwd)
13+
14+
# Function to handle errors
15+
handle_error() {
16+
echo -e "${RED}Error: $1${NC}"
17+
cd "$INITIAL_DIR"
18+
exit 1
19+
}
20+
21+
echo -e "${GREEN}Running Python unit tests...${NC}"
22+
23+
# Check if we're in scripts directory and navigate accordingly
24+
if [[ $(basename $(pwd)) == "scripts" ]]; then
25+
SCRIPT_DIR=$(pwd)
26+
cd ..
27+
else
28+
SCRIPT_DIR="./scripts"
29+
fi
30+
31+
# Verify we're in the project root
32+
if [[ ! -d "server" ]]; then
33+
handle_error "Not in project root directory. Please run from the project root or scripts directory."
34+
fi
35+
36+
# Check if virtual environment exists
37+
if [[ ! -d "server/venv" ]]; then
38+
echo -e "${YELLOW}Virtual environment not found. Setting up environment first...${NC}"
39+
if ! bash "$SCRIPT_DIR/setup-environment.sh"; then
40+
handle_error "Failed to set up environment"
41+
fi
42+
fi
43+
44+
echo -e "${YELLOW}Activating virtual environment...${NC}"
45+
46+
# Navigate to server directory
47+
cd server || handle_error "server directory not found"
48+
49+
# Activate virtual environment
50+
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
51+
# Windows
52+
if ! source venv/Scripts/activate; then
53+
handle_error "Failed to activate Python virtual environment"
54+
fi
55+
else
56+
# macOS/Linux
57+
if ! source venv/bin/activate; then
58+
handle_error "Failed to activate Python virtual environment"
59+
fi
60+
fi
61+
62+
echo -e "${YELLOW}Running tests...${NC}"
63+
64+
# Run tests with verbose output
65+
if python -m unittest; then
66+
echo -e "${GREEN}All tests passed!${NC}"
67+
EXIT_CODE=0
68+
else
69+
echo -e "${RED}Some tests failed!${NC}"
70+
EXIT_CODE=1
71+
fi
72+
73+
# Deactivate virtual environment
74+
deactivate
75+
76+
# Return to initial directory
77+
cd "$INITIAL_DIR"
78+
79+
exit $EXIT_CODE

0 commit comments

Comments
 (0)