Skip to content

Commit 80b4e92

Browse files
authored
Allow default env to be set by env var (#267)
1 parent f581170 commit 80b4e92

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Unreleased
77
- Minimum supported python is now 3.11.
88
- Switch python packaging backend back to setuptools. (`#263 <https://github.com/DiamondLightSource/python-zocalo/pull/263>`_)
99
- ``zocalo.dlq_purge``: Show which queues DLQ messages came from, and accept queue names with prefix. (`#264 <https://github.com/DiamondLightSource/python-zocalo/pull/264>`_)
10+
- Allow default zocalo environment to be set by environment variable `ZOCALO_DEFAULT_ENV`. This takes priority over default in `ZOCALO_CONFIG` file (`#267 <https://github.com/DiamondLightSource/python-zocalo/pull/267>`_)
1011

1112
1.2.0 (2024-11-14)
1213
------------------

src/zocalo/configuration/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ def activate(
169169
envs = zocalo.configuration.argparse.get_specified_environments(
170170
arguments=self.environment_cmd_args
171171
)
172-
if default and not envs and "default" in self._environments:
173-
envs = ["default"]
172+
if default and not envs:
173+
if os.environ.get("ZOCALO_DEFAULT_ENV"):
174+
envs = [os.environ["ZOCALO_DEFAULT_ENV"]]
175+
elif "default" in self._environments:
176+
envs = ["default"]
174177
for environment in envs:
175178
self.activate_environment(environment)
176179
return tuple(envs)

tests/configuration/test_configuration.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,22 @@ def test_activate_additional_environments():
215215
assert "part-2" in str(zc)
216216

217217

218-
def test_activate_default_environment():
218+
def test_activate_default_environment_from_config_file():
219219
zc = zocalo.configuration.from_string(sample_configuration)
220220
e = zc.activate([])
221221
assert e == ("default",)
222222
assert zc.active_environments == ("default",)
223223
assert "default" in str(zc)
224224

225225

226+
def test_activate_default_environment_from_environment_variable():
227+
with mock.patch.dict(os.environ, {"ZOCALO_DEFAULT_ENV": "partial"}):
228+
zc = zocalo.configuration.from_string(sample_configuration)
229+
e = zc.activate([])
230+
assert e == ("partial",)
231+
assert zc.active_environments == ("partial",)
232+
233+
226234
def test_activate_call_honours_default_flag():
227235
zc = zocalo.configuration.from_string(sample_configuration)
228236
e = zc.activate([], default=False)

0 commit comments

Comments
 (0)