test(realestate): fix mktemp deprecation + narrow exception in conftest
Replace deprecated mktemp with mkstemp to eliminate TOCTOU race, narrow OSError catches to PermissionError (Windows SQLite lock intent only), and add a deferred-import comment for clarity. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,10 @@ import tempfile
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
# 테스트 임시 DB 경로를 import 전에 주입
|
# 테스트 임시 DB 경로를 import 전에 주입
|
||||||
_TMP_DB = tempfile.mktemp(suffix=".db")
|
# mkstemp으로 충돌 없는 고유 경로 확보 후 SQLite가 직접 생성하도록 즉시 삭제
|
||||||
|
_fd, _TMP_DB = tempfile.mkstemp(suffix=".db")
|
||||||
|
os.close(_fd)
|
||||||
|
os.unlink(_TMP_DB) # let SQLite create it fresh on first init_db()
|
||||||
os.environ["REALESTATE_DB_PATH"] = _TMP_DB
|
os.environ["REALESTATE_DB_PATH"] = _TMP_DB
|
||||||
|
|
||||||
# app 패키지 import 가능하게 PYTHONPATH 보정
|
# app 패키지 import 가능하게 PYTHONPATH 보정
|
||||||
@@ -14,13 +17,17 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def _clean_db():
|
def _clean_db():
|
||||||
"""각 테스트마다 DB 초기화."""
|
"""각 테스트마다 DB 초기화."""
|
||||||
|
try:
|
||||||
if os.path.exists(_TMP_DB):
|
if os.path.exists(_TMP_DB):
|
||||||
os.remove(_TMP_DB)
|
os.remove(_TMP_DB)
|
||||||
|
except PermissionError:
|
||||||
|
pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시
|
||||||
|
# deferred to ensure REALESTATE_DB_PATH is set before first module load
|
||||||
from app.db import init_db
|
from app.db import init_db
|
||||||
init_db()
|
init_db()
|
||||||
yield
|
yield
|
||||||
try:
|
try:
|
||||||
if os.path.exists(_TMP_DB):
|
if os.path.exists(_TMP_DB):
|
||||||
os.remove(_TMP_DB)
|
os.remove(_TMP_DB)
|
||||||
except OSError:
|
except PermissionError:
|
||||||
pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시
|
pass # Windows: SQLite 파일 잠금 해제 전 삭제 실패 무시
|
||||||
|
|||||||
Reference in New Issue
Block a user