Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

import os

from typing import TYPE_CHECKING

import pytest

from tests.helpers import flatten_dict
from tests.helpers import isolated_environment
from tests.helpers import switch_working_directory


if TYPE_CHECKING:
from pathlib import Path


def test_flatten_dict() -> None:
Expand Down Expand Up @@ -44,3 +53,26 @@ 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: Path,
) -> 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.mkdir()

with switch_working_directory(temp_dir, remove=True):
assert os.getcwd() == str(temp_dir)
assert temp_dir.exists()

assert not temp_dir.exists()