From b732e7bb864a25795ecde7cc00529370800a0f40 Mon Sep 17 00:00:00 2001 From: Sergio Date: Tue, 10 Mar 2026 15:02:30 -0700 Subject: [PATCH 1/5] test: add coverage for switch_working_directory Fixes python-poetry/poetry#9161 --- tests/test_helpers.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 42c56dfef32..de722762c76 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -2,8 +2,11 @@ import os +import pytest + from tests.helpers import flatten_dict from tests.helpers import isolated_environment +from tests.helpers import switch_working_directory def test_flatten_dict() -> None: @@ -44,3 +47,25 @@ def test_isolated_environment_updates_environ() -> None: with isolated_environment(environ={"NEW_VAR": "new_value"}): assert os.environ["NEW_VAR"] == "new_value" assert "NEW_VAR" not in os.environ + + +def test_switch_working_directory_restores_original_cwd_on_error(tmp_path) -> None: + original_cwd = os.getcwd() + + with pytest.raises(RuntimeError): + with switch_working_directory(tmp_path): + assert os.getcwd() == str(tmp_path) + raise RuntimeError("boom") + + assert os.getcwd() == original_cwd + + +def test_switch_working_directory_remove_deletes_directory(tmp_path) -> None: + temp_dir = tmp_path / "temp-working-dir" + temp_dir.mkdir() + + with switch_working_directory(temp_dir, remove=True): + assert os.getcwd() == str(temp_dir) + assert temp_dir.exists() + + assert not temp_dir.exists() From 9d9016c2bb336934516a4172e499d90e41ffb73e Mon Sep 17 00:00:00 2001 From: sergiochan Date: Tue, 10 Mar 2026 17:53:52 -0700 Subject: [PATCH 2/5] test: annotate tmp_path fixtures in switch_working_directory tests --- tests/test_helpers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index de722762c76..88352f2939d 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +from pathlib import Path import pytest @@ -49,7 +50,7 @@ def test_isolated_environment_updates_environ() -> None: assert "NEW_VAR" not in os.environ -def test_switch_working_directory_restores_original_cwd_on_error(tmp_path) -> None: +def test_switch_working_directory_restores_original_cwd_on_error(tmp_path: Path) -> None: original_cwd = os.getcwd() with pytest.raises(RuntimeError): @@ -60,7 +61,7 @@ def test_switch_working_directory_restores_original_cwd_on_error(tmp_path) -> No assert os.getcwd() == original_cwd -def test_switch_working_directory_remove_deletes_directory(tmp_path) -> None: +def test_switch_working_directory_remove_deletes_directory(tmp_path: Path) -> None: temp_dir = tmp_path / "temp-working-dir" temp_dir.mkdir() From 40fb627473fe90da8fd4a7c65b8b2b98be98d19f Mon Sep 17 00:00:00 2001 From: sergiochan Date: Tue, 10 Mar 2026 17:58:26 -0700 Subject: [PATCH 3/5] test: apply ruff fixes for switch_working_directory tests --- tests/test_helpers.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 88352f2939d..815d56b54c0 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,7 +1,8 @@ from __future__ import annotations import os -from pathlib import Path + +from typing import TYPE_CHECKING import pytest @@ -10,6 +11,10 @@ from tests.helpers import switch_working_directory +if TYPE_CHECKING: + from pathlib import Path + + def test_flatten_dict() -> None: orig_dict = { "a": 1, @@ -50,13 +55,14 @@ def test_isolated_environment_updates_environ() -> None: assert "NEW_VAR" not in os.environ -def test_switch_working_directory_restores_original_cwd_on_error(tmp_path: Path) -> None: +def test_switch_working_directory_restores_original_cwd_on_error( + tmp_path: Path, +) -> None: original_cwd = os.getcwd() - with pytest.raises(RuntimeError): - with switch_working_directory(tmp_path): - assert os.getcwd() == str(tmp_path) - raise RuntimeError("boom") + with pytest.raises(RuntimeError), switch_working_directory(tmp_path): + assert os.getcwd() == str(tmp_path) + raise RuntimeError("boom") assert os.getcwd() == original_cwd From 351190d09e9c890ee05c859801d48e5ac5ab5a67 Mon Sep 17 00:00:00 2001 From: sergiochan Date: Thu, 19 Mar 2026 09:10:03 -0700 Subject: [PATCH 4/5] test: parametrize switch_working_directory behavior coverage --- tests/test_helpers.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 815d56b54c0..73d4058973c 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -55,24 +55,22 @@ def test_isolated_environment_updates_environ() -> None: assert "NEW_VAR" not in os.environ -def test_switch_working_directory_restores_original_cwd_on_error( - tmp_path: Path, +@pytest.mark.parametrize(("remove", "raise_error"), [(False, False), (False, True), (True, False), (True, True)]) +def test_switch_working_directory_changes_restores_and_removes( + tmp_path: Path, remove: bool, raise_error: bool ) -> None: original_cwd = os.getcwd() - - with pytest.raises(RuntimeError), switch_working_directory(tmp_path): - assert os.getcwd() == str(tmp_path) - raise RuntimeError("boom") - - assert os.getcwd() == original_cwd - - -def test_switch_working_directory_remove_deletes_directory(tmp_path: Path) -> None: - temp_dir = tmp_path / "temp-working-dir" + temp_dir = tmp_path / f"temp-working-dir-{remove}-{raise_error}" temp_dir.mkdir() - with switch_working_directory(temp_dir, remove=True): - assert os.getcwd() == str(temp_dir) - assert temp_dir.exists() + if raise_error: + with pytest.raises(RuntimeError): + with switch_working_directory(temp_dir, remove=remove): + assert os.getcwd() == str(temp_dir) + raise RuntimeError("boom") + else: + with switch_working_directory(temp_dir, remove=remove): + assert os.getcwd() == str(temp_dir) - assert not temp_dir.exists() + assert os.getcwd() == original_cwd + assert temp_dir.exists() is (not remove) From 5623aae0f057e6eadb17fa4d4e9e1fa5e7f6492a Mon Sep 17 00:00:00 2001 From: sergiochan Date: Thu, 19 Mar 2026 09:53:02 -0700 Subject: [PATCH 5/5] tests: satisfy pre-commit lint for switch_working_directory test --- tests/test_helpers.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 73d4058973c..bc4c5851c7f 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -55,7 +55,10 @@ def test_isolated_environment_updates_environ() -> None: assert "NEW_VAR" not in os.environ -@pytest.mark.parametrize(("remove", "raise_error"), [(False, False), (False, True), (True, False), (True, True)]) +@pytest.mark.parametrize( + ("remove", "raise_error"), + [(False, False), (False, True), (True, False), (True, True)], +) def test_switch_working_directory_changes_restores_and_removes( tmp_path: Path, remove: bool, raise_error: bool ) -> None: @@ -64,10 +67,12 @@ def test_switch_working_directory_changes_restores_and_removes( temp_dir.mkdir() if raise_error: - with pytest.raises(RuntimeError): - with switch_working_directory(temp_dir, remove=remove): - assert os.getcwd() == str(temp_dir) - raise RuntimeError("boom") + with ( + pytest.raises(RuntimeError), + switch_working_directory(temp_dir, remove=remove), + ): + assert os.getcwd() == str(temp_dir) + raise RuntimeError("boom") else: with switch_working_directory(temp_dir, remove=remove): assert os.getcwd() == str(temp_dir)