-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrun_test.sh
More file actions
executable file
Β·144 lines (116 loc) Β· 4.02 KB
/
Copy pathrun_test.sh
File metadata and controls
executable file
Β·144 lines (116 loc) Β· 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# Common test script for all OpenAEV collectors.
# Usage:
# ./run_test.sh <collector_dir> Run tests for a single collector
# ./run_test.sh <dir1> <dir2> ... Run tests for multiple collectors
# ./run_test.sh Auto-discover and run all collector tests
#
# Environment variables:
# RELEASE_REF Base branch for git diff (default: main)
# GITHUB_REF_NAME Current branch name (GitHub Actions)
# CIRCLE_BRANCH Current branch name (CircleCI)
set -e
RELEASE_REF="${RELEASE_REF:-main}"
BRANCH="${CIRCLE_BRANCH:-${GITHUB_REF_NAME:-$RELEASE_REF}}"
REPO_ROOT="$(cd "$(dirname "$0")" && pwd)"
# Use pyoaev main by default
PYOAEV_BRANCH="main"
# Discover collectors with test directories
discover_collectors() {
find "$REPO_ROOT" -maxdepth 2 \( -name "test" -o -name "tests" \) -type d \
| sed "s|^$REPO_ROOT/||; s|/test.*||" \
| sort -u
}
# Detect install arguments for a collector's pyproject.toml
get_install_args() {
local pyproject="$1/pyproject.toml"
local args="--extras prod"
if grep -q '\[tool\.poetry\.group\.test' "$pyproject" 2>/dev/null; then
args="$args --with test"
elif grep -q '\[tool\.poetry\.group\.dev' "$pyproject" 2>/dev/null; then
# microsoft-defender has pytest in dev group
if sed -n '/\[tool\.poetry\.group\.dev/,/\[/p' "$pyproject" | grep -q 'pytest'; then
args="$args --with dev"
fi
fi
echo "$args"
}
# Detect whether the collector uses pytest or unittest
uses_pytest() {
local collector_dir="$1"
local pyproject="$collector_dir/pyproject.toml"
# Check for pytest configuration in pyproject.toml
if grep -q '\[tool\.pytest' "$pyproject" 2>/dev/null; then
return 0
fi
# Check for conftest.py (pytest convention)
if [ -f "$collector_dir/tests/conftest.py" ] || [ -f "$collector_dir/test/conftest.py" ]; then
return 0
fi
# Check if pytest is in any dependency group
if grep -q 'pytest' "$pyproject" 2>/dev/null; then
return 0
fi
return 1
}
# Run tests for a single collector
run_collector_tests() {
local collector="$1"
local collector_dir="$REPO_ROOT/$collector"
echo "==========================================="
echo "Processing: $collector"
echo "==========================================="
echo "π Running tests for $collector"
cd "$collector_dir"
# Ensure Poetry is available
command -v poetry >/dev/null 2>&1 || pip install -q poetry==2.1.3
poetry config installer.re-resolve false 2>/dev/null || true
# Install collector dependencies
local install_args
install_args=$(get_install_args "$collector_dir")
echo "β poetry install $install_args"
poetry install $install_args
# Force-reinstall pyoaev from git (correct branch)
echo "β Installing pyoaev from branch $PYOAEV_BRANCH"
poetry run pip install --force-reinstall -q \
"git+https://github.com/OpenAEV-Platform/client-python.git@$PYOAEV_BRANCH"
# Install coverage tooling
poetry run pip install -q coverage
# Detect test directory
local test_dir=""
if [ -d "test" ]; then
test_dir="test"
elif [ -d "tests" ]; then
test_dir="tests"
fi
# Run tests with the appropriate framework, capturing exit code explicitly
# (set -e is disabled inside `if !` so we must not rely on it here)
local test_rc=0
if uses_pytest "$collector_dir"; then
echo "β Running pytest"
poetry run python -m coverage run -m pytest --junitxml="junit.xml" -q -rA || test_rc=$?
else
echo "β Running unittest"
poetry run python -m coverage run -m unittest discover -s "$test_dir" -v || test_rc=$?
fi
# Generate coverage report even on failure (partial coverage is still useful)
poetry run python -m coverage xml -o coverage.xml || true
cd "$REPO_ROOT"
return $test_rc
}
# --- Main ---
if [ $# -gt 0 ]; then
collectors="$*"
else
collectors=$(discover_collectors)
fi
exit_code=0
for collector in $collectors; do
if run_collector_tests "$collector"; then
echo "β
$collector tests passed"
else
echo "β $collector tests FAILED"
exit_code=1
fi
done
exit $exit_code