Skip to content

Commit

Permalink
Merge pull request #21 from d-chris/develop
Browse files Browse the repository at this point in the history
Fix UrlPath.exists and improve test coverage
  • Loading branch information
d-chris authored Nov 2, 2024
2 parents 819de53 + 46bea60 commit 858b7e7
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- id: trailing-whitespace
- id: check-toml
- repo: https://github.com/tox-dev/pyproject-fmt
rev: v2.4.3
rev: v2.5.0
hooks:
- id: pyproject-fmt
- repo: https://github.com/tox-dev/tox-ini-fmt
Expand Down Expand Up @@ -38,7 +38,7 @@ repos:
- id: poetry-lock
args: ["--no-update"]
- repo: https://github.com/google/yamlfmt
rev: v0.13.0
rev: v0.14.0
hooks:
- id: yamlfmt
- repo: https://github.com/PyCQA/flake8
Expand Down
9 changes: 8 additions & 1 deletion pathlibutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
from pathlibutil.path import Path, Register7zFormat
from pathlibutil.types import ByteInt, StatResult, TimeInt, byteint

__all__ = ["Path", "Register7zFormat", "ByteInt", "byteint", "TimeInt", "StatResult"]
__all__ = [
"Path",
"Register7zFormat",
"ByteInt",
"byteint",
"TimeInt",
"StatResult",
]
10 changes: 9 additions & 1 deletion pathlibutil/urlpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dataclasses import asdict, dataclass, field
from functools import cached_property, wraps
from typing import Any, Dict, Optional, Tuple, TypeVar, Union
from urllib.error import HTTPError


@dataclass
Expand Down Expand Up @@ -612,7 +613,14 @@ def exists(self, errors: bool = False, **kwargs) -> bool:

try:
with urllib.request.urlopen(url, **kwargs) as response:
return response.status == 200
if response.status == 200:
return True

raise HTTPError(
url=url,
code=response.status,
message=response.reason,
)
except Exception as e:
if errors is not False:
raise FileNotFoundError(url) from e
Expand Down
34 changes: 17 additions & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ documentation = "https://d-chris.github.io/pathlibutil"

[tool.poetry.dependencies]
python = "^3.8.1"
py7zr = { version = "^0.20.2", optional = true }
py7zr = { version = ">=0.20.2", optional = true }

[tool.poetry.extras]
7z = [ "py7zr" ]
Expand Down Expand Up @@ -76,3 +76,10 @@ addopts = [
exclude_lines = [
"^def normalize_url",
]

[tool.coverage.run]
omit = [
"*/tests/*",
"*/docs/*",
"*/examples/*",
]
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def cls() -> Generator[Path, Any, Any]:
yield Path
Path.default_hash = hash

try:
del Path._netuse
except AttributeError:
pass


@pytest.fixture
def file(cls) -> Path:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def obj(file):
return [file, {1: file}, "string"]


def test_dump_raises():
class Base:
pass

with pytest.raises(TypeError):
_ = dumps(Base())


def test_dumps(obj):

result = dumps(obj)
Expand Down
12 changes: 10 additions & 2 deletions tests/test_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def mock_run(mocker):


@pytest.fixture
def path():
return Path("file.txt")
def path(cls):

yield cls("file.txt")


def test_resolve_default(path, mock_resolve, mock_run):
Expand All @@ -43,6 +44,13 @@ def test_resolve_unctrue(path, mock_resolve, mock_run):
assert path.resolve(unc=True) == Path("//server/temp/file.txt")


def test_resolve_uncfalse_none(path, mock_run, mocker):

mocker.patch("re.finditer", side_effect=Exception)
mocker.patch("pathlib.Path.resolve", return_value=path)
assert path.resolve(unc=False).as_posix() == path.as_posix()


@pytest.mark.skipif(os.name != "nt", reason="Windows only")
def test_resolve_uncfalse(path, mock_resolve, mock_run):
assert path.resolve(unc=False) == Path("T:/file.txt")
57 changes: 45 additions & 12 deletions tests/test_urlpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ def test_urlnetloc_normalize(netlocs):
assert str(netloc) == result


def test_urlnetloc_normalize_patch(mocker):

url = "www.ExamplE.com:443"

mocker.patch("re.search", return_value=None)
netloc = UrlNetloc.from_netloc(url, normalize=False)

assert url != str(netloc)


def test_urlnetloc_str():
hostname = "www.example.com"

Expand Down Expand Up @@ -267,22 +277,45 @@ def test_url_from():
assert str(url) == "https://www.server.com/path/readme.pdf"


def test_anchor():
url = UrlPath("//server/root/path/readme.pdf")
def test_url_from_exist(mocker, mock_openurl):

assert url.anchor == "//server/root"
file = "//server/root/path/readme.pdf"

mocker.patch(
"pathlib.Path.resolve",
return_value=pathlib.Path(file),
)
if mock_openurl == 404:
with pytest.raises(FileNotFoundError):
_ = url_from(file, "https://www.server.com", strict=True)
else:
url = url_from(file, "https://www.server.com", strict=True)
assert str(url) == "https://www.server.com/path/readme.pdf"

def test_with_anchor():
url = UrlPath("//server/root/path/readme.pdf")

assert (
url.with_anchor("//fubar", root=True).__str__()
== "//fubar/root/path/readme.pdf"
)
@pytest.mark.parametrize(
"url,result",
[
("//server/root/path/readme.pdf", "//server/root"),
("//server/root/", "//server/root"),
("//server/", "//server/"),
],
)
def test_anchor(url, result):

assert UrlPath(url).anchor == result


def test_fubar_anchor():
url = UrlPath("//server/root/path/readme.pdf")
@pytest.mark.parametrize(
"anchor,root,result",
[
("//fubar", False, "//fubar/path/readme.pdf"),
("//fubar", True, "//fubar/root/path/readme.pdf"),
("//fubar/share", False, "//fubar/share/path/readme.pdf"),
("//fubar/share", True, "//fubar/share/root/path/readme.pdf"),
],
)
def test_with_anchor(anchor, root, result):
url = UrlPath("//server/root/path/readme.pdf").with_anchor(anchor, root)

assert url.with_anchor("//fubar").__str__() == "//fubar/path/readme.pdf"
assert str(url) == result

0 comments on commit 858b7e7

Please sign in to comment.