File size: 1,653 Bytes
a506cc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import tempfile
import pytest

# Ensure app is discoverable on python path
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
APP_DIR = os.path.join(BASE_DIR, "app")
if APP_DIR not in sys.path:
    sys.path.insert(0, APP_DIR)
if BASE_DIR not in sys.path:
    sys.path.insert(0, BASE_DIR)

# Strictly isolate test execution from any live database and default directories BEFORE imports
_SESSION_TMP = tempfile.mkdtemp(prefix="atlas_test_isolation_")
_SESSION_DATA = os.path.join(_SESSION_TMP, "data")
_SESSION_UPLOADS = os.path.join(_SESSION_TMP, "uploads")
_SESSION_STATIC = os.path.join(_SESSION_TMP, "static")
os.makedirs(_SESSION_DATA, exist_ok=True)
os.makedirs(_SESSION_UPLOADS, exist_ok=True)
os.makedirs(_SESSION_STATIC, exist_ok=True)

os.environ["DATABASE_URL"] = ""
os.environ["POSTGRES_URL"] = ""
os.environ["POSTGRESQL_URL"] = ""
os.environ["ATLAS_DATA_DIR"] = _SESSION_DATA
os.environ["ATLAS_UPLOAD_DIR"] = _SESSION_UPLOADS


@pytest.fixture(autouse=True)
def setup_isolated_test_env(tmp_path, monkeypatch):
    """Enforces completely isolated local directories and SQLite database for each test."""
    data_dir = str(tmp_path / "data")
    upload_dir = str(tmp_path / "uploads")
    static_dir = str(tmp_path / "static")
    os.makedirs(data_dir, exist_ok=True)
    os.makedirs(upload_dir, exist_ok=True)
    os.makedirs(static_dir, exist_ok=True)

    monkeypatch.setenv("DATABASE_URL", "")
    monkeypatch.setenv("POSTGRES_URL", "")
    monkeypatch.setenv("POSTGRESQL_URL", "")
    monkeypatch.setenv("ATLAS_DATA_DIR", data_dir)
    monkeypatch.setenv("ATLAS_UPLOAD_DIR", upload_dir)

    yield