Skip to content

Commit

Permalink
Code review: 284880043: Improvements to Windows path resolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Dec 27, 2015
1 parent ade84d2 commit cb770d0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/dpkg/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ python-dfvfs (20151227-1) unstable; urgency=low

* Auto-generated

-- Log2Timeline <[email protected]> Sun, 27 Dec 2015 12:25:31 +0100
-- Log2Timeline <[email protected]> Sun, 27 Dec 2015 12:53:15 +0100
15 changes: 13 additions & 2 deletions dfvfs/helpers/windows_path_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def _ResolvePath(self, path, expand_variables=True):

number_of_expanded_path_segments = 0

for path_segment in path.split(self._PATH_SEPARATOR):
search_path_segments = path.split(self._PATH_SEPARATOR)
while search_path_segments:
path_segment = search_path_segments.pop(0)
if file_entry is None:
return None, None

Expand All @@ -151,7 +153,7 @@ def _ResolvePath(self, path, expand_variables=True):
if path_segment == u'..':
# Only allow to traverse back up to the mount point.
if number_of_expanded_path_segments > 0:
_ = expanded_path_segments.pop()
_ = expanded_path_segments.pop(0)
number_of_expanded_path_segments -= 1
file_entry = file_entry.GetParentFileEntry()
continue
Expand All @@ -161,6 +163,15 @@ def _ResolvePath(self, path, expand_variables=True):
path_segment = self._environment_variables.get(
path_segment[1:-1].upper(), path_segment)

if self._PATH_SEPARATOR in path_segment:
# The expanded path segment itself can consist of multiple
# path segments, hence we need to split it and prepend it to
# the search path segments list.
path_segments = path_segment.split(self._PATH_SEPARATOR)
path_segments.extend(search_path_segments)
search_path_segments = path_segments
path_segment = search_path_segments.pop(0)

sub_file_entry = file_entry.GetSubFileEntryByName(
path_segment, case_sensitive=False)
if sub_file_entry is None:
Expand Down
1 change: 1 addition & 0 deletions test_data/testdir_os/subdir1/file6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file6
14 changes: 14 additions & 0 deletions tests/helpers/windows_path_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ def testResolvePathWithEnvironmentVariable(self):
path_spec = path_resolver.ResolvePath(windows_path)
self.assertIsNone(path_spec)

# Test resolving multi path segment environment variables.
path_resolver = windows_path_resolver.WindowsPathResolver(
self._os_file_system, self._os_path_spec)

expected_path = os.path.join(
os.getcwd(), u'test_data', u'testdir_os', u'subdir1', u'file6.txt')

path_resolver.SetEnvironmentVariable(u'Test', u'C:\\testdir_os\\subdir1')

windows_path = u'%Test%\\file6.txt'
path_spec = path_resolver.ResolvePath(windows_path)
self.assertIsNotNone(path_spec)
self.assertEqual(path_spec.location, expected_path)


if __name__ == '__main__':
unittest.main()
5 changes: 3 additions & 2 deletions tests/vfs/os_file_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ def testSubFileEntries(self):
file_entry = self._file_system.GetFileEntryByPathSpec(self._os_path_spec)
self.assertIsNotNone(file_entry)

self.assertEqual(file_entry.number_of_sub_file_entries, 5)
self.assertEqual(file_entry.number_of_sub_file_entries, 6)

expected_sub_file_entry_names = [
u'file1.txt', u'file2.txt', u'file3.txt', u'file4.txt', u'file5.txt']
u'file1.txt', u'file2.txt', u'file3.txt', u'file4.txt', u'file5.txt',
u'subdir1']

sub_file_entry_names = []
for sub_file_entry in file_entry.sub_file_entries:
Expand Down

0 comments on commit cb770d0

Please sign in to comment.