diff --git a/tests/unit/test_remote.py b/tests/unit/test_remote.py index 8d24fc0bb3a..5982fb746f1 100644 --- a/tests/unit/test_remote.py +++ b/tests/unit/test_remote.py @@ -15,8 +15,14 @@ # along with this program. If not, see . """Test the cylc.flow.remote module.""" -from cylc.flow.remote import run_cmd, construct_rsync_over_ssh_cmd, construct_ssh_cmd +import os from unittest import mock + +import pytest + +from cylc.flow.remote import ( + run_cmd, construct_rsync_over_ssh_cmd, construct_ssh_cmd +) import cylc.flow @@ -90,10 +96,13 @@ def test_construct_rsync_over_ssh_cmd(): ] -def test_construct_ssh_cmd_forward_env(): +def test_construct_ssh_cmd_forward_env(monkeypatch: pytest.MonkeyPatch): """ Test for 'ssh forward environment variables' """ - import os + # Clear CYLC_* env vars as these will show up in the command + for env_var in os.environ: + if env_var.startswith('CYLC'): + monkeypatch.delenv(env_var) host = 'example.com' config = { @@ -101,7 +110,7 @@ def test_construct_ssh_cmd_forward_env(): 'use login shell': None, 'cylc path': None, 'ssh forward environment variables': ['FOO', 'BAZ'], - } + } # Variable isn't set, no change to command expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'cylc', 'play'] @@ -109,7 +118,7 @@ def test_construct_ssh_cmd_forward_env(): assert cmd == expect # Variable is set, appears in `env` list - with mock.patch.dict(os.environ, {'FOO': 'BAR'}): - expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'FOO=BAR', 'cylc', 'play'] - cmd = construct_ssh_cmd(['play'], config, host) - assert cmd == expect + monkeypatch.setenv('FOO', 'BAR') + expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'FOO=BAR', 'cylc', 'play'] + cmd = construct_ssh_cmd(['play'], config, host) + assert cmd == expect