diff --git a/testing/path/test_local.py b/testing/path/test_local.py index ae009362..20797006 100644 --- a/testing/path/test_local.py +++ b/testing/path/test_local.py @@ -1064,3 +1064,67 @@ def test_default_encoding(self, tmpdir): s = x.read_text("ascii") assert s == part assert type(s) == type(part) + + +def test_behavior_with_bytes(): # type: () -> None + """Test py.path.local's behavior with regard to bytes instead of text.""" + bpath = local(b"/bytesname") + brelpath = local(b"bytesname") + + if sys.version_info < (3,): + if sys.platform == "win32": + assert str(bpath) == r"C:\bytesname" + else: + assert str(bpath) == "/bytesname" + assert str(brelpath) == local("bytesname") + local() / brelpath + local(b"/") / brelpath + else: + with pytest.raises( + TypeError, match=r"__str__ returned non-string \(type bytes\)" + ): + str(bpath) + with pytest.raises( + TypeError, match="a bytes-like object is required, not 'str'" + ): + local() / brelpath + with pytest.raises( + TypeError, + match="endswith first arg must be bytes or a tuple of bytes, not str", + ): + local(b"/") / brelpath + + p1 = local(b"/") + p1.sep = b"/" # type: ignore[assignment,misc] # noqa: F821 + if sys.version_info < (3,): + p1 / brelpath + else: + with pytest.raises(TypeError, match="can't concat str to bytes"): + p1 / brelpath + + p1 = local(b"") + p1.sep = b"/" # type: ignore[assignment,misc] # noqa: F821 + if sys.version_info > (3,) and sys.platform == "win32": + with pytest.raises( + TypeError, match="a bytes-like object is required, not 'str'" + ): + p1 / brelpath + else: + assert p1 / brelpath == p1 / brelpath + + if sys.version_info < (3,): + assert p1 / brelpath == local("") / local("bytesname") + assert str(p1 / brelpath) == str(local("") / local("bytesname")) + else: + if sys.platform == "win32": + with pytest.raises(TypeError, "a bytes-like object is required, not 'str'"): + p1 / brelpath + else: + assert p1 / brelpath != local("") / local("bytesname") + with pytest.raises( + TypeError, match=r"__str__ returned non-string \(type bytes\)" + ): + str(p1 / brelpath) + + strpath = (p1 / brelpath).strpath.decode() + assert strpath == str(local("") / local("bytesname"))