import importlib import sys from unittest.mock import patch import pytest import delta_barth.constants from delta_barth import _env @patch("delta_barth._env.PY_RUNTIME_FOLDER", "test123456") def test_prepare_env_NoRuntimeFolder(tmp_path): ret = _env.prepare_env(tmp_path) assert ret is None @patch("delta_barth._env.PY_RUNTIME_FOLDER", "base") def test_prepare_env_FailNoInterpreter(tmp_path_factory): mocked_lib_pth = tmp_path_factory.mktemp("path") / "to/base/folder/lib/" mocked_lib_pth.mkdir(parents=True, exist_ok=True) with pytest.raises(FileNotFoundError): _ = _env.prepare_env(mocked_lib_pth) @patch("delta_barth._env.PY_RUNTIME_FOLDER", "base") def test_prepare_env_Success(tmp_path_factory): mocked_lib_pth = tmp_path_factory.mktemp("path") / "to/base/folder/lib/" mocked_lib_pth.mkdir(parents=True, exist_ok=True) rt_path = mocked_lib_pth.parents[1] mocked_interpreter = rt_path / "python.exe" mocked_interpreter.touch() assert mocked_interpreter.exists() ret = _env.prepare_env(mocked_lib_pth) assert ret == rt_path # sys attributes executable = getattr(sys, "executable") assert executable == str(mocked_interpreter) base_executable = getattr(sys, "_base_executable") assert base_executable == str(mocked_interpreter) class MockPath: def __init__(self, *args, **kwargs): self.parent = mocked_lib_pth with patch("pathlib.Path", MockPath): (mocked_lib_pth / "_dummy_data").mkdir(exist_ok=True) importlib.reload(delta_barth.constants) assert delta_barth.constants.DEPLOYMENT_STATUS assert delta_barth.constants.RUNTIME_PATH == rt_path