Skip to content

Commit

Permalink
Test ensure_directories
Browse files Browse the repository at this point in the history
  • Loading branch information
asmacdo committed May 14, 2024
1 parent c9c572b commit 50c96f0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
from unittest import mock
import pytest
from duct import ensure_directories


def ensure_directoiries(path: str) -> None:
if path.endswith(os.sep): # If it ends in "/" (for linux) treat as a dir
os.makedirs(path, exist_ok=True)
else:
# Path does not end with a separator, treat the last part as a filename
directory = os.path.dirname(path)
if directory: # If there's a directory part, create it
os.makedirs(directory, exist_ok=True)


@pytest.mark.parametrize(
"path",
[
"directory/",
"nested/directory/",
"/abs/path/",
],
)
@mock.patch("duct.os.makedirs")
def test_ensure_directories_with_dirs(mock_mkdir, path):
ensure_directories(path)
mock_mkdir.assert_called_once_with(path, exist_ok=True)


@mock.patch("duct.os.makedirs")
def test_ensure_directories_with_file(mock_mkdir):
ensure_directories("just_a_file_name")
mock_mkdir.assert_not_called()


@mock.patch("duct.os.makedirs")
def test_ensure_directories_with_filepart_and_directory_part(mock_mkdir):
ensure_directories("nested/dir/file_name")
mock_mkdir.assert_called_once_with("nested/dir", exist_ok=True)

0 comments on commit 50c96f0

Please sign in to comment.