Skip to content

Commit a6617e5

Browse files
committed
Add apply_umask argument to FakeFilesystem.create_dir
* allows to ignore the umask * closes #1038
1 parent 64ed360 commit a6617e5

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

CHANGES.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# pyfakefs Release Notes
22
The released versions correspond to PyPI releases.
33

4+
## Policy for Python version support
5+
* support for new versions is usually added preliminarily during the Python release beta phase,
6+
official support after the final release
7+
* support for EOL versions is removed as soon as the CI (GitHub actions) does no longer provide
8+
these versions (usually several months after the official EOL)
9+
410
## Planned changes for next major release (6.0.0)
5-
* remove support for patching legacy modules `scandir` and `pathlib2`
6-
* remove support for Python 3.7
11+
* support for patching legacy modules `scandir` and `pathlib2` will be removed
12+
* the default for `FakeFilesystem.shuffle_listdir_results` will change to `True` to reflect
13+
the real filesystem behavior
714

815
## Unreleased
916

1017
### Enhancements
1118
* added preliminary support for Python 3.13 (tested with beta2) (see [#1017](../../issues/1017))
19+
* added `apply_umask` argument to `FakeFilesystem.create_dir` to allow ignoring the umask (see [#1038](../../issues/1038))
1220

1321
### Fixes
1422
* use real open calls for remaining `pathlib` functions so that it works nice with skippedmodules (see [#1012](../../issues/1012))

pyfakefs/fake_filesystem.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,10 @@ def make_string_path(self, path: AnyPath) -> AnyStr: # type: ignore[type-var]
20902090
return path_str.replace(os_sep, fake_sep) # type: ignore[return-value]
20912091

20922092
def create_dir(
2093-
self, directory_path: AnyPath, perm_bits: int = helpers.PERM_DEF
2093+
self,
2094+
directory_path: AnyPath,
2095+
perm_bits: int = helpers.PERM_DEF,
2096+
apply_umask: bool = True,
20942097
) -> FakeDirectory:
20952098
"""Create `directory_path` and all the parent directories, and return
20962099
the created :py:class:`FakeDirectory<pyfakefs.fake_file.FakeDirectory>` object.
@@ -2100,6 +2103,8 @@ def create_dir(
21002103
Args:
21012104
directory_path: The full directory path to create.
21022105
perm_bits: The permission bits as set by ``chmod``.
2106+
apply_umask: If `True` (default), the current umask is applied
2107+
to `perm_bits`.
21032108
21042109
Returns:
21052110
The newly created
@@ -2138,7 +2143,9 @@ def create_dir(
21382143
# set the permission after creating the directories
21392144
# to allow directory creation inside a read-only directory
21402145
for new_dir in new_dirs:
2141-
new_dir.st_mode = S_IFDIR | (perm_bits & ~self.umask)
2146+
if apply_umask:
2147+
perm_bits &= ~self.umask
2148+
new_dir.st_mode = S_IFDIR | perm_bits
21422149

21432150
return current_dir
21442151

@@ -2169,8 +2176,8 @@ def create_file(
21692176
the file is considered to be in "large file mode" and trying
21702177
to read from or write to the file will result in an exception.
21712178
create_missing_dirs: If `True`, auto create missing directories.
2172-
apply_umask: `True` if the current umask must be applied
2173-
on `st_mode`.
2179+
apply_umask: If `True` (default), the current umask is applied
2180+
to `st_mode`.
21742181
encoding: If `contents` is of type `str`, the encoding used
21752182
for serialization.
21762183
errors: The error mode used for encoding/decoding errors.

pyfakefs/tests/fake_filesystem_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,20 @@ def test_create_directory(self):
515515
self.assertEqual(os.path.basename(path), new_dir.name)
516516
self.assertTrue(stat.S_IFDIR & new_dir.st_mode)
517517

518+
def test_create_dir_umask(self):
519+
old_umask = self.filesystem.umask
520+
self.filesystem.umask = 0o22
521+
path = "foo/bar/baz"
522+
self.filesystem.create_dir(path, perm_bits=0o777)
523+
new_dir = self.filesystem.get_object(path)
524+
self.assertEqual(stat.S_IFDIR | 0o755, new_dir.st_mode)
525+
526+
path = "foo/bar/boo"
527+
self.filesystem.create_dir(path, perm_bits=0o777, apply_umask=False)
528+
new_dir = self.filesystem.get_object(path)
529+
self.assertEqual(stat.S_IFDIR | 0o777, new_dir.st_mode)
530+
self.filesystem.umask = old_umask
531+
518532
def test_create_directory_already_exists_error(self):
519533
path = "foo/bar/baz"
520534
self.filesystem.create_dir(path)

0 commit comments

Comments
 (0)