Skip to content

Commit

Permalink
Add logging and tests for chdir
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaasgoyal committed Mar 6, 2024
1 parent 9482458 commit 5c9c7f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions benchcab/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
def chdir(newdir: Path):
"""Context manager `cd`."""
prevdir = Path.cwd()
get_logger().debug(f"Changing current working directory from {prevdir} to {newdir}")
os.chdir(newdir.expanduser())
try:
yield
Expand Down
24 changes: 23 additions & 1 deletion tests/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
pytest autouse fixture.
"""

import logging
from pathlib import Path

import pytest

from benchcab.utils.fs import mkdir, next_path
from benchcab.utils.fs import chdir, mkdir, next_path


class TestNextPath:
Expand All @@ -30,6 +31,27 @@ def test_next_path_in_non_empty_cwd(self, pattern):
assert next_path(pattern) == Path("rev_number-2.log")


class TestChdir:
"""Tests for `chdir()`."""

@pytest.fixture(autouse=True)
def original_cwd_path(self) -> Path:
"""Get current working directory before running any tests."""
return Path.cwd()

def test_chdir(self, caplog, original_cwd_path: Path, tmp_path: Path):
"""Success case: Change current working directory to `tmp_path` within context."""
caplog.set_level(logging.DEBUG)
assert original_cwd_path != tmp_path # chdir should have different CWD
assert Path.cwd() == original_cwd_path
with chdir(tmp_path):
log_msg = f"Changing current working directory from {original_cwd_path} to {tmp_path}"
assert caplog.record_tuples == [("benchcab", logging.DEBUG, log_msg)]
assert Path.cwd() == tmp_path

assert Path.cwd() == original_cwd_path


class TestMkdir:
"""Tests for `mkdir()`."""

Expand Down

0 comments on commit 5c9c7f6

Please sign in to comment.