Skip to content

Commit

Permalink
Clear env before test cases requiring empty env (#12561)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
tkonopka and akihironitta authored Apr 7, 2022
1 parent 52e568f commit 3bd48b8
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tests/callbacks/test_gpu_stats_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/loggers/test_tensorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/environments/test_kubeflow_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/environments/test_lightning_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/environments/test_lsf_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/environments/test_slurm_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"}):
Expand Down
6 changes: 3 additions & 3 deletions tests/plugins/environments/test_torchelastic_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 3bd48b8

Please sign in to comment.