Skip to content

Commit

Permalink
Fixed Fakedirectory.path if cwd is not root
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Mar 14, 2018
1 parent 47ffc3a commit 5c2a381
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,18 @@ def path(self):
names.insert(0, obj.name)
obj = obj.parent_dir
sep = self.filesystem._path_separator(self.name)
return self.filesystem.absnormpath(sep.join(names[1:]))
if names[0] == sep:
names.pop(0)
dir_path = sep.join(names)
# Windows paths with drive have a root separator entry
# which should be removed
is_drive = names and len(names[0]) == 2 and names[0][1] == ':'
if not is_drive:
dir_path = sep + dir_path
else:
dir_path = sep.join(names)
dir_path = self.filesystem.absnormpath(dir_path)
return dir_path

@Deprecator('property path')
def GetPath(self):
Expand Down
24 changes: 24 additions & 0 deletions tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setUp(self):
self.orig_time = time.time
time.time = DummyTime(10, 1)
self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/')
self.os = fake_filesystem.FakeOsModule(self.filesystem)
self.fake_file = fake_filesystem.FakeFile(
'foobar', contents='dummy_file', filesystem=self.filesystem)
self.fake_dir = fake_filesystem.FakeDirectory(
Expand All @@ -58,6 +59,29 @@ def test_path(self):
self.filesystem.root.add_entry(self.fake_dir)
self.fake_dir.add_entry(self.fake_file)
self.assertEqual('/somedir/foobar', self.fake_file.path)
self.assertEqual('/somedir', self.fake_dir.path)

def test_path_with_drive(self):
self.filesystem.is_windows_fs = True
dir_path = 'C:/foo/bar/baz'
self.filesystem.create_dir(dir_path)
dir_object = self.filesystem.get_object(dir_path)
self.assertEqual(dir_path, dir_object.path)

def test_path_after_chdir(self):
dir_path = '/foo/bar/baz'
self.filesystem.create_dir(dir_path)
self.os.chdir(dir_path)
dir_object = self.filesystem.get_object(dir_path)
self.assertEqual(dir_path, dir_object.path)

def test_path_after_chdir_with_drive(self):
self.filesystem.is_windows_fs = True
dir_path = 'C:/foo/bar/baz'
self.filesystem.create_dir(dir_path)
self.os.chdir(dir_path)
dir_object = self.filesystem.get_object(dir_path)
self.assertEqual(dir_path, dir_object.path)

def test_remove_entry(self):
self.fake_dir.add_entry(self.fake_file)
Expand Down

0 comments on commit 5c2a381

Please sign in to comment.