modify_environ

lsst.ts.salobj.modify_environ(**kwargs: Any) Generator[None, None, None]

Context manager to temporarily patch os.environ.

This calls unittest.mock.patch and is only intended for unit tests.

Parameters
kwargsdict [str, str or None]

Environment variables to set or clear. Each key is the name of an environment variable (with correct case); it need not already exist. Each value must be one of:

  • A string value to set the env variable.

  • None to delete the env variable, if present.

Raises
RuntimeError

If any value in kwargs is not of type str or None.

Notes

Example of use:

from lsst.ts import salobj
...
def test_foo(self):
    set_value = "Value for $ENV_TO_SET"
    with salobj.modify_environ(
        HOME=None,  # Delete this env var
        ENV_TO_SET=set_value,  # Set this env var
    ):
        self.assertNotIn("HOME", os.environ)
        self.assert(os.environ["ENV_TO_SET"], set_value)