Skip to content

Commit c08e089

Browse files
committed
Address Copilot PR feedback and add seriallocal marking
1 parent d510acf commit c08e089

3 files changed

Lines changed: 47 additions & 43 deletions

File tree

scripts/test-local.sh

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ COVERAGE_REPORT="term"
2525
KEEP_RUNNING=false
2626
SELECTED_CORES=""
2727
INCLUDE_LOCAL_CORES=false
28-
TEST_FILES=""
28+
TEST_FILES=()
2929
PARALLEL=false
3030
PARALLEL_WORKERS="auto"
3131

@@ -102,7 +102,7 @@ while [[ $# -gt 0 ]]; do
102102
usage
103103
exit 1
104104
fi
105-
TEST_FILES="$TEST_FILES $1"
105+
TEST_FILES+=("$1")
106106
shift
107107
;;
108108
--help)
@@ -542,37 +542,37 @@ main() {
542542

543543
# Run pytest
544544
# Build pytest command
545-
PYTEST_CMD="pytest"
545+
PYTEST_ARGS=(pytest)
546546
# and the specific pytest command for running serial pickle tests
547-
SERIAL_PYTEST_CMD="pytest -m seriallocal"
547+
SERIAL_PYTEST_ARGS=(pytest -m seriallocal)
548548
# Only add -n0 if pytest-xdist is available; otherwise, plain pytest is already serial
549549
if python - << 'EOF' >/dev/null 2>&1
550550
import xdist # noqa: F401
551551
EOF
552552
then
553-
SERIAL_PYTEST_CMD="$SERIAL_PYTEST_CMD -n0"
553+
SERIAL_PYTEST_ARGS+=(-n0)
554554
fi
555555

556556
# Add test files if specified
557-
if [ -n "$TEST_FILES" ]; then
558-
PYTEST_CMD="$PYTEST_CMD $TEST_FILES"
559-
print_message $BLUE "Test files specified: $TEST_FILES"
557+
if [ ${#TEST_FILES[@]} -gt 0 ]; then
558+
PYTEST_ARGS+=("${TEST_FILES[@]}")
559+
print_message $BLUE "Test files specified: ${TEST_FILES[*]}"
560560
# and turn off serial local tests, so we run only selected files
561561
run_serial_local_tests=false
562562
fi
563563

564564
# Add markers if needed (only if no specific test files were given)
565-
if [ -z "$TEST_FILES" ]; then
565+
if [ ${#TEST_FILES[@]} -eq 0 ]; then
566566
# Check if we selected all cores - if so, run all tests without marker filtering
567567
all_cores="memory mongo pickle redis s3 sql"
568568
selected_sorted=$(echo "$SELECTED_CORES" | tr ' ' '\n' | sort | tr '\n' ' ' | xargs)
569569
all_sorted=$(echo "$all_cores" | tr ' ' '\n' | sort | tr '\n' ' ' | xargs)
570570

571571
if [ "$selected_sorted" != "$all_sorted" ]; then
572-
PYTEST_CMD="$PYTEST_CMD -m \"$pytest_markers\""
572+
PYTEST_ARGS+=(-m "$pytest_markers")
573573
else
574574
print_message $BLUE "Running all tests without markers since all cores are selected"
575-
PYTEST_CMD="$PYTEST_CMD -m \"not seriallocal\""
575+
PYTEST_ARGS+=(-m "not seriallocal")
576576
run_serial_local_tests=true
577577
fi
578578
else
@@ -582,19 +582,19 @@ EOF
582582
all_sorted=$(echo "$all_cores" | tr ' ' '\n' | sort | tr '\n' ' ' | xargs)
583583

584584
if [ "$selected_sorted" != "$all_sorted" ]; then
585-
PYTEST_CMD="$PYTEST_CMD -m \"$pytest_markers\""
585+
PYTEST_ARGS+=(-m "$pytest_markers")
586586
fi
587587
fi
588588

589589
# Add verbose flag if needed
590590
if [ "$VERBOSE" = true ]; then
591-
PYTEST_CMD="$PYTEST_CMD -v"
592-
SERIAL_PYTEST_CMD="$SERIAL_PYTEST_CMD -v"
591+
PYTEST_ARGS+=(-v)
592+
SERIAL_PYTEST_ARGS+=(-v)
593593
fi
594594

595595
# Add parallel testing options if requested
596596
if [ "$PARALLEL" = true ]; then
597-
PYTEST_CMD="$PYTEST_CMD -n $PARALLEL_WORKERS"
597+
PYTEST_ARGS+=(-n "$PARALLEL_WORKERS")
598598

599599
# Show parallel testing info
600600
if [ "$PARALLEL_WORKERS" = "auto" ]; then
@@ -610,16 +610,16 @@ EOF
610610
fi
611611

612612
# Add coverage options
613-
PYTEST_CMD="$PYTEST_CMD --cov=cachier --cov-report=$COVERAGE_REPORT"
614-
SERIAL_PYTEST_CMD="$SERIAL_PYTEST_CMD --cov=cachier --cov-report=$COVERAGE_REPORT --cov-append"
613+
PYTEST_ARGS+=(--cov=cachier --cov-report="$COVERAGE_REPORT")
614+
SERIAL_PYTEST_ARGS+=(--cov=cachier --cov-report="$COVERAGE_REPORT" --cov-append)
615615

616616
# Print and run the command
617-
print_message $BLUE "Running: $PYTEST_CMD"
618-
eval $PYTEST_CMD
617+
print_message $BLUE "Running: $(printf '%q ' "${PYTEST_ARGS[@]}")"
618+
"${PYTEST_ARGS[@]}"
619619

620620
if [ "$run_serial_local_tests" = true ]; then
621-
print_message $BLUE "Running serial local tests (pickle, memory) with: $SERIAL_PYTEST_CMD"
622-
eval $SERIAL_PYTEST_CMD
621+
print_message $BLUE "Running serial local tests (pickle, memory) with: $(printf '%q ' "${SERIAL_PYTEST_ARGS[@]}")"
622+
"${SERIAL_PYTEST_ARGS[@]}"
623623
else
624624
print_message $BLUE "Skipping serial local tests (pickle, memory) since not requested"
625625
fi

tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ the corresponding Docker service started beforehand.
371371
1. **Import Errors**: Install backend-specific requirements
372372

373373
```bash
374-
pip install -r tests/redis_requirements.txt
374+
pip install -r tests/requirements_redis.txt
375375
```
376376

377377
2. **Docker Not Running**: Start Docker Desktop or daemon

tests/conftest.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22

33
import logging
44
import os
5+
import re
56
from urllib.parse import parse_qs, unquote, urlencode, urlparse, urlunparse
67

78
import pytest
89

910
logger = logging.getLogger(__name__)
1011

1112

13+
def _worker_schema_name(worker_id: str) -> str | None:
14+
"""Return a safe SQL schema name for an xdist worker ID."""
15+
match = re.fullmatch(r"gw(\d+)", worker_id)
16+
if match is None:
17+
return None
18+
return f"test_worker_{match.group(1)}"
19+
20+
1221
@pytest.fixture(autouse=True)
1322
def inject_worker_schema_for_sql_tests(monkeypatch, request):
1423
"""Automatically inject worker-specific schema into SQL connection string.
1524
16-
This fixture enables parallel SQL test execution by giving each pytest- xdist worker its own PostgreSQL schema,
25+
This fixture enables parallel SQL test execution by giving each pytest-xdist worker its own PostgreSQL schema,
1726
preventing table creation conflicts.
1827
1928
"""
@@ -34,7 +43,11 @@ def inject_worker_schema_for_sql_tests(monkeypatch, request):
3443

3544
if "postgresql" in original_url:
3645
# Create worker-specific schema name
37-
schema_name = f"test_worker_{worker_id.replace('gw', '')}"
46+
schema_name = _worker_schema_name(worker_id)
47+
if schema_name is None:
48+
logger.warning("Unexpected worker ID for SQL schema isolation: %s", worker_id)
49+
yield
50+
return
3851

3952
# Parse the URL
4053
parsed = urlparse(original_url)
@@ -118,8 +131,12 @@ def isolated_cache_directory(tmp_path, monkeypatch, request, worker_id):
118131

119132
monkeypatch.setattr(cachier.config._global_params, "cache_dir", str(cache_dir))
120133

121-
# Also set environment variable as a backup
122-
monkeypatch.setenv("CACHIER_TEST_CACHE_DIR", str(cache_dir))
134+
135+
def pytest_collection_modifyitems(items):
136+
"""Mark local backends as serial-local for the split test runner flow."""
137+
for item in items:
138+
if "memory" in item.keywords or "pickle" in item.keywords:
139+
item.add_marker(pytest.mark.seriallocal)
123140

124141

125142
@pytest.fixture(scope="session", autouse=True)
@@ -140,7 +157,10 @@ def cleanup_test_schemas(request):
140157
original_url = os.environ.get("SQLALCHEMY_DATABASE_URL", "")
141158

142159
if "postgresql" in original_url:
143-
schema_name = f"test_worker_{worker_id.replace('gw', '')}"
160+
schema_name = _worker_schema_name(worker_id)
161+
if schema_name is None:
162+
logger.warning("Unexpected worker ID for SQL schema cleanup: %s", worker_id)
163+
return
144164

145165
try:
146166
from sqlalchemy import create_engine, text
@@ -174,19 +194,3 @@ def cleanup_test_schemas(request):
174194
except Exception as e:
175195
# If cleanup fails, it's not critical
176196
logger.debug(f"Failed to cleanup schema {schema_name}: {e}")
177-
178-
179-
def pytest_addoption(parser):
180-
"""Add custom command line options for parallel testing."""
181-
parser.addoption(
182-
"--parallel",
183-
action="store_true",
184-
default=False,
185-
help="Run tests in parallel using pytest-xdist",
186-
)
187-
parser.addoption(
188-
"--parallel-workers",
189-
action="store",
190-
default="auto",
191-
help="Number of parallel workers (default: auto)",
192-
)

0 commit comments

Comments
 (0)