From 68cdeb8f3d719cd777b27b6cf9f9a7fbf533543f Mon Sep 17 00:00:00 2001 From: Andrey Shpak Date: Thu, 9 Jun 2022 03:42:16 +0300 Subject: [PATCH] Move OSError raising out of replay.dump (raise in make_sure_path_exists) --- cookiecutter/replay.py | 5 ++--- tests/replay/test_dump.py | 6 ++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cookiecutter/replay.py b/cookiecutter/replay.py index 9730e84da..d3c989879 100644 --- a/cookiecutter/replay.py +++ b/cookiecutter/replay.py @@ -16,10 +16,9 @@ def get_file_name(replay_dir, template_name): return os.path.join(replay_dir, file_name) -def dump(replay_dir, template_name, context): +def dump(replay_dir: "os.PathLike[str]", template_name: str, context: dict): """Write json data to file.""" - if not make_sure_path_exists(replay_dir): - raise OSError(f'Unable to create replay dir at {replay_dir}') + make_sure_path_exists(replay_dir) if not isinstance(template_name, str): raise TypeError('Template name is required to be of type str') diff --git a/tests/replay/test_dump.py b/tests/replay/test_dump.py index c757321b1..57ad8ee74 100644 --- a/tests/replay/test_dump.py +++ b/tests/replay/test_dump.py @@ -57,7 +57,9 @@ def mock_ensure_failure(mocker): Used to mock internal function and limit test scope. Always return expected value: False """ - return mocker.patch('cookiecutter.replay.make_sure_path_exists', return_value=False) + return mocker.patch( + 'cookiecutter.replay.make_sure_path_exists', side_effect=OSError + ) @pytest.fixture @@ -72,7 +74,7 @@ def mock_ensure_success(mocker): def test_ioerror_if_replay_dir_creation_fails(mock_ensure_failure, replay_test_dir): """Test that replay.dump raises when the replay_dir cannot be created.""" - with pytest.raises(IOError): + with pytest.raises(OSError): replay.dump(replay_test_dir, 'foo', {'cookiecutter': {'hello': 'world'}}) mock_ensure_failure.assert_called_once_with(replay_test_dir)