Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for drive-specific current directory on non-current drive.
Co-authored-by: Eryk Sun <eryksun@gmail.com>
  • Loading branch information
barneygale and eryksun committed Jan 7, 2023
commit ad9db67b41ee4fb146954e053b2e07d9df053bc9
35 changes: 35 additions & 0 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import re
import stat
import string
import sys
import time
import unittest
Expand Down Expand Up @@ -716,3 +717,37 @@ def __exit__(self, *ignore_exc):
else:
self._environ[k] = v
os.environ = self._environ


try:
import ctypes
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
except (ImportError, AttributeError):
def subst_drive(path):
raise NotImplementedError('ctypes or kernel32 is not available')
Comment thread
barneygale marked this conversation as resolved.
Outdated
else:
ERROR_FILE_NOT_FOUND = 2
DDD_REMOVE_DEFINITION = 2
DDD_EXACT_MATCH_ON_REMOVE = 4
DDD_NO_BROADCAST_SYSTEM = 8
Comment thread
barneygale marked this conversation as resolved.

@contextlib.contextmanager
def subst_drive(path):
"""Temporarily yield a substitute drive for a given path."""
for c in reversed(string.ascii_uppercase):
drive = f'{c}:'
if (not kernel32.QueryDosDeviceW(drive, None, 0) and
ctypes.get_last_error() == ERROR_FILE_NOT_FOUND):
break
else:
raise FileExistsError('no available logical drive')
Comment thread
barneygale marked this conversation as resolved.
Outdated
if not kernel32.DefineDosDeviceW(
DDD_NO_BROADCAST_SYSTEM, drive, path):
raise ctypes.WinError(ctypes.get_last_error())
try:
yield drive
finally:
if not kernel32.DefineDosDeviceW(
DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE,
Comment thread
barneygale marked this conversation as resolved.
drive, path):
raise ctypes.WinError(ctypes.get_last_error())
17 changes: 13 additions & 4 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3004,14 +3004,23 @@ def test_absolute(self):

drive = os.path.splitdrive(BASE)[0]
with os_helper.change_cwd(BASE):
# Relative path with drive
self.assertEqual(str(P(drive).absolute()), BASE)
self.assertEqual(str(P(drive + 'foo').absolute()), os.path.join(BASE, 'foo'))

# Relative path with root
self.assertEqual(str(P('\\').absolute()), drive + '\\')
self.assertEqual(str(P('\\foo').absolute()), drive + '\\foo')

# Relative path on current drive
self.assertEqual(str(P(drive).absolute()), BASE)
self.assertEqual(str(P(drive + 'foo').absolute()), os.path.join(BASE, 'foo'))

with os_helper.subst_drive(BASE) as other_drive:
other_cwd = f'{other_drive}\\dirA'
with os_helper.change_cwd(other_cwd):
Comment thread
barneygale marked this conversation as resolved.
Outdated
pass
Comment thread
barneygale marked this conversation as resolved.
Outdated

# Relative path on another drive
self.assertEqual(str(P(other_drive).absolute()), other_cwd)
self.assertEqual(str(P(other_drive + 'foo').absolute()), other_cwd + '\\foo')


def test_glob(self):
P = self.cls
Expand Down