From 3bd48b85356e36b7a56b5d8f669403f0bfb9d717 Mon Sep 17 00:00:00 2001 From: Tomasz Konopka Date: Thu, 7 Apr 2022 16:44:28 +0100 Subject: [PATCH] Clear env before test cases requiring empty env (#12561) * clearing env vars in a test to allow compatibility with "make test" * added clear=True to more mock environments in testcases Co-authored-by: Akihiro Nitta --- tests/callbacks/test_gpu_stats_monitor.py | 2 +- tests/loggers/test_tensorboard.py | 2 +- tests/plugins/environments/test_kubeflow_environment.py | 2 +- tests/plugins/environments/test_lightning_environment.py | 4 ++-- tests/plugins/environments/test_lsf_environment.py | 2 +- tests/plugins/environments/test_slurm_environment.py | 4 ++-- tests/plugins/environments/test_torchelastic_environment.py | 6 +++--- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/callbacks/test_gpu_stats_monitor.py b/tests/callbacks/test_gpu_stats_monitor.py index 1e3a9953bf137..1cc4d51d90eba 100644 --- a/tests/callbacks/test_gpu_stats_monitor.py +++ b/tests/callbacks/test_gpu_stats_monitor.py @@ -142,7 +142,7 @@ def test_gpu_stats_monitor_parse_gpu_stats(): assert logs == expected -@mock.patch.dict(os.environ, {}) +@mock.patch.dict(os.environ, {}, clear=True) @mock.patch("torch.cuda.is_available", return_value=True) @mock.patch("torch.cuda.device_count", return_value=2) def test_gpu_stats_monitor_get_gpu_ids_cuda_visible_devices_unset(device_count_mock, is_available_mock): diff --git a/tests/loggers/test_tensorboard.py b/tests/loggers/test_tensorboard.py index f4ad9400097e8..1c3af727a745f 100644 --- a/tests/loggers/test_tensorboard.py +++ b/tests/loggers/test_tensorboard.py @@ -123,7 +123,7 @@ def test_tensorboard_no_name(tmpdir, name): assert os.listdir(tmpdir / "version_0") -@mock.patch.dict(os.environ, {}) +@mock.patch.dict(os.environ, {}, clear=True) def test_tensorboard_log_sub_dir(tmpdir): class TestLogger(TensorBoardLogger): # for reproducibility diff --git a/tests/plugins/environments/test_kubeflow_environment.py b/tests/plugins/environments/test_kubeflow_environment.py index a384ca29de4a6..d1abe1c112eeb 100644 --- a/tests/plugins/environments/test_kubeflow_environment.py +++ b/tests/plugins/environments/test_kubeflow_environment.py @@ -20,7 +20,7 @@ from pytorch_lightning.plugins.environments import KubeflowEnvironment -@mock.patch.dict(os.environ, {}) +@mock.patch.dict(os.environ, {}, clear=True) def test_default_attributes(): """Test the default attributes when no environment variables are set.""" env = KubeflowEnvironment() diff --git a/tests/plugins/environments/test_lightning_environment.py b/tests/plugins/environments/test_lightning_environment.py index 9dec96e768b3d..9197e5158317e 100644 --- a/tests/plugins/environments/test_lightning_environment.py +++ b/tests/plugins/environments/test_lightning_environment.py @@ -19,7 +19,7 @@ from pytorch_lightning.plugins.environments import LightningEnvironment -@mock.patch.dict(os.environ, {}) +@mock.patch.dict(os.environ, {}, clear=True) def test_default_attributes(): """Test the default attributes when no environment variables are set.""" env = LightningEnvironment() @@ -65,7 +65,7 @@ def test_node_rank_from_group_rank(): assert env.node_rank() == 1 -@mock.patch.dict(os.environ, {}) +@mock.patch.dict(os.environ, {}, clear=True) def test_random_master_port(): """Test randomly chosen main port when no main port was given by user.""" env = LightningEnvironment() diff --git a/tests/plugins/environments/test_lsf_environment.py b/tests/plugins/environments/test_lsf_environment.py index cdd36ba04bcaf..e4b213da47c8d 100644 --- a/tests/plugins/environments/test_lsf_environment.py +++ b/tests/plugins/environments/test_lsf_environment.py @@ -101,7 +101,7 @@ def test_node_rank(tmp_path): def test_detect(): """Test the detection of a LSF environment configuration.""" - with mock.patch.dict(os.environ, {}): + with mock.patch.dict(os.environ, {}, clear=True): assert not LSFEnvironment.detect() with mock.patch.dict( diff --git a/tests/plugins/environments/test_slurm_environment.py b/tests/plugins/environments/test_slurm_environment.py index aa8db284a1c64..61ef404dd127a 100644 --- a/tests/plugins/environments/test_slurm_environment.py +++ b/tests/plugins/environments/test_slurm_environment.py @@ -20,7 +20,7 @@ from pytorch_lightning.plugins.environments import SLURMEnvironment -@mock.patch.dict(os.environ, {}) +@mock.patch.dict(os.environ, {}, clear=True) def test_default_attributes(): """Test the default attributes when no environment variables are set.""" env = SLURMEnvironment() @@ -92,7 +92,7 @@ def test_master_address_from_slurm_node_list(slurm_node_list, expected): def test_detect(): """Test the detection of a SLURM environment configuration.""" - with mock.patch.dict(os.environ, {}): + with mock.patch.dict(os.environ, {}, clear=True): assert not SLURMEnvironment.detect() with mock.patch.dict(os.environ, {"SLURM_NTASKS": "2"}): diff --git a/tests/plugins/environments/test_torchelastic_environment.py b/tests/plugins/environments/test_torchelastic_environment.py index 8cf04e19a40a4..9b9d83a4d84e5 100644 --- a/tests/plugins/environments/test_torchelastic_environment.py +++ b/tests/plugins/environments/test_torchelastic_environment.py @@ -21,7 +21,7 @@ from tests.helpers.runif import RunIf -@mock.patch.dict(os.environ, {}) +@mock.patch.dict(os.environ, {}, clear=True) def test_default_attributes(): """Test the default attributes when no environment variables are set.""" env = TorchElasticEnvironment() @@ -74,7 +74,7 @@ def test_attributes_from_environment_variables(caplog): @RunIf(max_torch="1.9.0") def test_detect_before_1_9_1(): """Test the detection of a torchelastic environment configuration before 1.9.1.""" - with mock.patch.dict(os.environ, {}): + with mock.patch.dict(os.environ, {}, clear=True): assert not TorchElasticEnvironment.detect() with mock.patch.dict( @@ -92,7 +92,7 @@ def test_detect_before_1_9_1(): @RunIf(min_torch="1.9.1") def test_detect_after_1_9_1(): """Test the detection of a torchelastic environment configuration after 1.9.1.""" - with mock.patch.dict(os.environ, {}): + with mock.patch.dict(os.environ, {}, clear=True): assert not TorchElasticEnvironment.detect() with mock.patch.dict(