From 7b0a114c02488488123f9c5ca57ffa0846dacf0d Mon Sep 17 00:00:00 2001 From: Doug Latornell Date: Mon, 11 Nov 2024 17:31:28 -0800 Subject: [PATCH] Refactor tests to use monkeypatch for setting SYSTEM Replaced `with patch` context managers with `monkeypatch.setattr` in all TestModules test cases to improve code readability and consistency. Added new test for "salish" system to ensure future-proofing and full coverage of available systems. --- tests/test_run.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/tests/test_run.py b/tests/test_run.py index 3feaa85..070350e 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -3121,10 +3121,19 @@ def test_unknown_system(self): modules = salishsea_cmd.run._modules() assert modules == "" + def test_salish(self, monkeypatch): + monkeypatch.setattr(salishsea_cmd.run, "SYSTEM", "salish") + + modules = salishsea_cmd.run._modules() + + assert modules == "" + @pytest.mark.parametrize("system", ["beluga", "cedar", "graham"]) - def test_beluga_cedar_graham(self, system): - with patch("salishsea_cmd.run.SYSTEM", system): - modules = salishsea_cmd.run._modules() + def test_beluga_cedar_graham(self, system, monkeypatch): + monkeypatch.setattr(salishsea_cmd.run, "SYSTEM", system) + + modules = salishsea_cmd.run._modules() + expected = textwrap.dedent( """\ module load StdEnv/2020 @@ -3134,9 +3143,11 @@ def test_beluga_cedar_graham(self, system): assert modules == expected @pytest.mark.parametrize("system", ("orcinus", "seawolf1", "seawolf2", "seawolf3")) - def test_orcinus(self, system): - with patch("salishsea_cmd.run.SYSTEM", system): - modules = salishsea_cmd.run._modules() + def test_orcinus(self, system, monkeypatch): + monkeypatch.setattr(salishsea_cmd.run, "SYSTEM", system) + + modules = salishsea_cmd.run._modules() + expected = textwrap.dedent( """\ module load intel @@ -3151,9 +3162,11 @@ def test_orcinus(self, system): assert modules == expected @pytest.mark.parametrize("system", ["delta", "sigma", "omega"]) - def test_optimum(self, system): - with patch("salishsea_cmd.run.SYSTEM", system): - modules = salishsea_cmd.run._modules() + def test_optimum(self, system, monkeypatch): + monkeypatch.setattr(salishsea_cmd.run, "SYSTEM", system) + + modules = salishsea_cmd.run._modules() + expected = textwrap.dedent( """\ module load OpenMPI/2.1.6/GCC/SYSTEM @@ -3161,9 +3174,11 @@ def test_optimum(self, system): ) assert modules == expected - def test_sockeye(self): - with patch("salishsea_cmd.run.SYSTEM", "sockeye"): - modules = salishsea_cmd.run._modules() + def test_sockeye(self, monkeypatch): + monkeypatch.setattr(salishsea_cmd.run, "SYSTEM", "sockeye") + + modules = salishsea_cmd.run._modules() + expected = textwrap.dedent( """\ module load gcc/5.5.0