|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import ssl |
| 5 | + |
| 6 | +from tidy3d.config import Env, get_manager, reload_config |
| 7 | +from tidy3d.config import config as config_wrapper |
| 8 | + |
| 9 | + |
| 10 | +def test_env_tracks_profile_switch(config_manager): |
| 11 | + """Regression: Env should mirror manager profile switches.""" |
| 12 | + |
| 13 | + del config_manager # ensure fixture runs, avoid lint for unused variable |
| 14 | + try: |
| 15 | + config_wrapper.switch_profile("dev") |
| 16 | + assert config_wrapper.profile == "dev" |
| 17 | + assert Env.current.name == "dev" |
| 18 | + assert str(Env.current.web_api_endpoint) == str(config_wrapper.web.api_endpoint) |
| 19 | + finally: |
| 20 | + reload_config(profile="default") |
| 21 | + |
| 22 | + |
| 23 | +def test_env_pending_overrides_apply_on_activation(mock_config_dir, config_manager): |
| 24 | + """Queued overrides should land once the corresponding profile is activated.""" |
| 25 | + |
| 26 | + del mock_config_dir |
| 27 | + manager = config_manager |
| 28 | + try: |
| 29 | + config_wrapper.switch_profile("default") |
| 30 | + assert manager.profile == "default" |
| 31 | + |
| 32 | + Env.dev.enable_caching = False |
| 33 | + Env.dev.ssl_version = ssl.TLSVersion.TLSv1_2 |
| 34 | + |
| 35 | + # Pending overrides should not touch the active profile yet. |
| 36 | + default_web = manager.get_section("web") |
| 37 | + assert default_web.enable_caching is True |
| 38 | + assert default_web.ssl_version is None |
| 39 | + |
| 40 | + Env.dev.active() |
| 41 | + current_manager = get_manager() |
| 42 | + assert current_manager.profile == "dev" |
| 43 | + dev_web = current_manager.get_section("web") |
| 44 | + assert dev_web.enable_caching is False |
| 45 | + assert dev_web.ssl_version == ssl.TLSVersion.TLSv1_2 |
| 46 | + assert Env.current.enable_caching is False |
| 47 | + assert Env.current.ssl_version == ssl.TLSVersion.TLSv1_2 |
| 48 | + finally: |
| 49 | + reload_config(profile="default") |
| 50 | + |
| 51 | + |
| 52 | +def test_env_vars_follow_profile_switch(mock_config_dir, monkeypatch, config_manager): |
| 53 | + """Environment variables applied via Env should restore previous values on switch.""" |
| 54 | + |
| 55 | + del mock_config_dir |
| 56 | + del config_manager # ensure fixture executes without lint complaints |
| 57 | + try: |
| 58 | + config_wrapper.switch_profile("default") |
| 59 | + monkeypatch.setenv("TIDY3D_TEST_VAR", "previous") |
| 60 | + |
| 61 | + Env.default.env_vars = {"TIDY3D_TEST_VAR": "applied"} |
| 62 | + assert os.environ["TIDY3D_TEST_VAR"] == "applied" |
| 63 | + |
| 64 | + Env.dev.env_vars = {} |
| 65 | + Env.dev.active() |
| 66 | + assert os.environ["TIDY3D_TEST_VAR"] == "previous" |
| 67 | + |
| 68 | + Env.default.active() |
| 69 | + assert os.environ["TIDY3D_TEST_VAR"] == "applied" |
| 70 | + finally: |
| 71 | + reload_config(profile="default") |
0 commit comments