Skip to content

Commit 75209e3

Browse files
committed
Added FakeDirectory.ordered_dirs
- see #151
1 parent 85d37b6 commit 75209e3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

fake_filesystem_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ def testDirectoryInode(self):
149149
dir_obj.SetIno(43)
150150
self.assertEqual(43, fake_os.stat(dirpath)[stat.ST_INO])
151151

152+
def testOrderedDirs(self):
153+
filesystem = fake_filesystem.FakeFilesystem(path_separator='/')
154+
filesystem.CreateDirectory('/foo')
155+
filesystem.CreateFile('/foo/2')
156+
filesystem.CreateFile('/foo/4')
157+
filesystem.CreateFile('/foo/1')
158+
filesystem.CreateFile('/foo/3')
159+
fake_dir = filesystem.GetObject('/foo')
160+
self.assertEqual(['2', '4', '1', '3'], fake_dir.ordered_dirs)
161+
152162

153163
class SetLargeFileSizeTest(FakeDirectoryUnitTest):
154164
def testShouldThrowIfSizeIsNotInteger(self):

pyfakefs/fake_filesystem.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ def contents(self):
427427
"""Return the list of contained directory entries."""
428428
return self.byte_contents
429429

430+
@property
431+
def ordered_dirs(self):
432+
"""Return the list of contained directory entry names ordered by creation order."""
433+
return [item[0] for item in sorted(self.byte_contents.items(), key=lambda entry: entry[1].st_ino)]
434+
430435
def AddEntry(self, path_object):
431436
"""Adds a child FakeFile to this directory.
432437

0 commit comments

Comments
 (0)