Skip to content

Commit

Permalink
Merge pull request #198 from facelessuser/bugfix/windows-path
Browse files Browse the repository at this point in the history
Fix Windows drive normalization and pattern indexing with implied drives
  • Loading branch information
facelessuser authored Sep 19, 2022
2 parents 1b1bbe9 + 2e8b747 commit d8f2835
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/src/markdown/about/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 8.4.1

- **FIX**: Windows drive path separators should normalize like other path separators.
- **FIX**: Fix a Windows pattern parsing issue that caused absolute paths with ambiguous drives to not parse correctly.

## 8.4

- **NEW**: Drop support for Python 3.6.
Expand Down
22 changes: 19 additions & 3 deletions tests/test_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,13 +1591,29 @@ def test_selflink(self):
class TestGlobPaths(unittest.TestCase):
"""Test `glob` paths."""

def test_root(self):
@unittest.skipUnless(not sys.platform.startswith('win'), "Linux/Unix specific test")
def test_root_unix(self):
"""Test that `glob` translates the root properly."""

# On Windows, this should translate to the current drive.
# On Linux/Unix, this should translate to the root.
# Basically, we should not return an empty set.
self.assertTrue(len(glob.glob('/*')) > 0)
results = glob.glob('/*')
self.assertTrue(len(results) > 0)
self.assertTrue('/' not in results)

@unittest.skipUnless(sys.platform.startswith('win'), "Windows specific test")
def test_root_win(self):
"""Test that `glob` translates the root properly."""

# On Windows, this should translate to the current drive.
# Basically, we should not return an empty set.
results = glob.glob('/*')
self.assertTrue(len(results) > 0)
self.assertTrue('\\' not in results)

results = glob.glob(r'\\*')
self.assertTrue(len(results) > 0)
self.assertTrue('\\' not in results)

def test_start(self):
"""Test that starting directory/files are handled properly."""
Expand Down
2 changes: 1 addition & 1 deletion wcmatch/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,5 @@ def parse_version(ver: str) -> Version:
return Version(major, minor, micro, release, pre, post, dev)


__version_info__ = Version(8, 4, 0, "final")
__version_info__ = Version(8, 4, 1, "final")
__version__ = __version_info__._get_canonical()
4 changes: 2 additions & 2 deletions wcmatch/_wcparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ def _get_win_drive(
end = m.end(0)
if m.group(3) and RE_WIN_DRIVE_LETTER.match(m.group(0)):
if regex:
drive = escape_drive(RE_WIN_DRIVE_UNESCAPE.sub(r'\1', m.group(3)), case_sensitive)
drive = escape_drive(RE_WIN_DRIVE_UNESCAPE.sub(r'\1', m.group(3)).replace('/', '\\'), case_sensitive)
else:
drive = RE_WIN_DRIVE_UNESCAPE.sub(r'\1', m.group(0))
drive = RE_WIN_DRIVE_UNESCAPE.sub(r'\1', m.group(0)).replace('/', '\\')
slash = bool(m.group(4))
root_specified = True
elif m.group(2):
Expand Down
8 changes: 6 additions & 2 deletions wcmatch/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,12 @@ def split(self) -> List[_GlobPart]:
i.advance(start)
elif drive is None and root_specified:
parts.append(_GlobPart(b'\\' if is_bytes else '\\', False, False, True, True))
start = 1
i.advance(2)
if pattern.startswith('/'):
start = 0
i.advance(1)
else:
start = 1
i.advance(2)
elif not self.win_drive_detect and pattern.startswith('/'):
parts.append(_GlobPart(b'/' if is_bytes else '/', False, False, True, True))
start = 0
Expand Down

0 comments on commit d8f2835

Please sign in to comment.