Skip to content

Commit

Permalink
Do not fail when receiving pathlib.Path objects as filepaths anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ntninja committed Dec 18, 2020
1 parent c658f55 commit 2ef0925
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/unasync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ def isidentifier(s):

StringIO = io.StringIO

if hasattr(os, "fspath"): # PY3
fspath = os.fspath
else: # PY2
fspath = str


class Rule:
"""A single set of rules for 'unasync'ing file(s)"""

def __init__(self, fromdir, todir, additional_replacements=None):
self.fromdir = fromdir.replace("/", os.sep)
self.todir = todir.replace("/", os.sep)
self.fromdir = fspath(fromdir).replace("/", os.sep)
self.todir = fspath(todir).replace("/", os.sep)

# Add any additional user-defined token replacements to our list.
self.token_replacements = _ASYNC_TO_SYNC.copy()
Expand All @@ -69,6 +74,8 @@ def _match(self, filepath):
"""Determines if a Rule matches a given filepath and if so
returns a higher comparable value if the match is more specific.
"""
filepath = fspath(filepath)

file_segments = [x for x in filepath.split(os.sep) if x]
from_segments = [x for x in self.fromdir.split(os.sep) if x]
len_from_segments = len(from_segments)
Expand All @@ -83,6 +90,7 @@ def _match(self, filepath):
return False

def _unasync_file(self, filepath):
filepath = fspath(filepath)
with open(filepath, "rb") as f:
write_kwargs = {}
if sys.version_info[0] >= 3: # PY3 # pragma: no branch
Expand Down
3 changes: 2 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest>=4.3.0
pytest-cov
pytest-cov
pathlib2 ; python_version < '3.5'
11 changes: 11 additions & 0 deletions tests/test_unasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import errno
import io
import os

try:
import pathlib
except ImportError:
import pathlib2 as pathlib
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -43,6 +48,12 @@ def test_rule_on_short_path():
assert rule._match("/ahip/") is False


def test_rule_with_pathlib_path():
path_async_base = pathlib.Path("/ahip")
path_sync_base = pathlib.Path("/hip")
unasync.Rule(path_async_base / "tests", path_sync_base / "tests")


@pytest.mark.parametrize("source_file", TEST_FILES)
def test_unasync(tmpdir, source_file):

Expand Down

0 comments on commit 2ef0925

Please sign in to comment.