Skip to content

Commit

Permalink
long path fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tor Colvin authored and Tor Colvin committed Feb 17, 2020
1 parent d18c75b commit 6c35f3b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -265,6 +265,7 @@ Tom Dalton
Tom Viner
Tomáš Gavenčiak
Tomer Keren
Tor Colvin
Trevor Bekolay
Tyler Goodlet
Tzu-ping Chung
Expand Down
1 change: 1 addition & 0 deletions changelog/6755.bugfix.rst
@@ -0,0 +1 @@
Support deleting paths longer than 260 characters on windows created inside tmpdir.
12 changes: 12 additions & 0 deletions src/_pytest/pathlib.py
Expand Up @@ -99,11 +99,23 @@ def chmod_rw(p: str) -> None:
func(path)
return True

def create_long_path(path: Path) -> Path:
"""Construct a path which will work on Windows if greater
than 260 characters. Resolves into a absolute path which
bypasses the typical MAX_PATH limitation:
https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation"""
if sys.platform.startswith("win32"):
path = path.resolve()
# for the API
if not str(path).startswith(r"\\?\"):
path = Path(r"\\?\" + str(path))
return path

def rm_rf(path: Path) -> None:
"""Remove the path contents recursively, even if some elements
are read-only.
"""
path = create_long_path(path)
onerror = partial(on_rm_rf_error, start_path=path)
shutil.rmtree(str(path), onerror=onerror)

Expand Down
12 changes: 12 additions & 0 deletions testing/test_pathlib.py
Expand Up @@ -89,3 +89,15 @@ def renamed_failed(*args):
lock_path = get_lock_path(path)
maybe_delete_a_numbered_dir(path)
assert not lock_path.is_file()

def test_long_path_during_cleanup(tmp_path):
"""Ensure that deleting long path works (particularly on Windows (#6775))."""
path = tmp_path / ("a" * 200)
if sys.platform == "win32":
dirname = path.resolve()
dirname = r"\\?\" + str(path)
os.mkdir(dirname)

lock_path = get_lock_path(path)
maybe_delete_a_numbered_dir(path)
assert not lock_path.is_file()

0 comments on commit 6c35f3b

Please sign in to comment.