aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrbean-bremen <hansemrbean@googlemail.com>2021-09-23 14:37:14 +0200
committermrbean-bremen <mrbean-bremen@users.noreply.github.com>2021-09-23 16:56:47 +0200
commit83f53443531ebc2e265dbe4b0c5b44f914ae1112 (patch)
treeff62286d07d2aae50cf2b11ae285e8deabeb9578
parentdfda25a196c6032dfddbbdc9ad713d0b09d0319f (diff)
downloadpyfakefs-83f53443531ebc2e265dbe4b0c5b44f914ae1112.tar.gz
Randomize list order for FakeFilesystem.listdir
- makes the output order for os.listdir, `os.scandir` and `pathlib.Path.listdir` random - see #638
-rw-r--r--CHANGES.md6
-rw-r--r--pyfakefs/fake_filesystem.py9
2 files changed, 11 insertions, 4 deletions
diff --git a/CHANGES.md b/CHANGES.md
index 83ce724..5bb036d 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -3,8 +3,12 @@ The released versions correspond to PyPi releases.
## Version 4.6.0 (as yet unreleased)
+### Changes
+* `os.listdir`, `os.scandir` and `pathlib.Path.listdir` now return the
+ directory list in a random order (see [#638](../../issues/638))
+
### Fixes
-* fixed handling of alternative path separator in `os.path.split`,
+* fixed handling of alternative path separator in `os.path.split`,
`os.path.splitdrive` and `glob.glob`
(see [#632](../../issues/632))
diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py
index f80de28..4c07280 100644
--- a/pyfakefs/fake_filesystem.py
+++ b/pyfakefs/fake_filesystem.py
@@ -98,6 +98,7 @@ import heapq
import io
import locale
import os
+import random
import sys
import traceback
import uuid
@@ -3213,15 +3214,17 @@ class FakeFilesystem:
Returns:
A list of file names within the target directory in arbitrary
- order.
+ order. Note that the order is intentionally not the same in
+ subsequent calls to avoid tests relying on any ordering.
Raises:
OSError: if the target is not a directory.
"""
target_directory = self.resolve_path(target_directory, allow_fd=True)
directory = self.confirmdir(target_directory)
- directory_contents = directory.entries
- return list(directory_contents.keys()) # type: ignore[arg-type]
+ directory_contents = list(directory.entries.keys())
+ random.shuffle(directory_contents)
+ return directory_contents # type: ignore[return-value]
def __str__(self) -> str:
return str(self.root)