40
40
41
41
42
42
class FakeOsModuleTestBase (RealFsTestCase ):
43
+ def setUp (self ):
44
+ super ().setUp ()
45
+ self .umask = self .os .umask (0o022 )
46
+
47
+ def tearDown (self ):
48
+ self .os .umask (self .umask )
49
+
43
50
def createTestFile (self , path ):
44
51
self .create_file (path )
45
52
self .assertTrue (self .os .path .exists (path ))
46
53
st = self .os .stat (path )
47
- self .assertEqual (0o666 , stat .S_IMODE (st .st_mode ))
54
+ # under Windows, the umask has no effect
55
+ mode = 0o666 if self .is_windows_fs else 0o0644
56
+ self .assertEqual (mode , stat .S_IMODE (st .st_mode ))
48
57
self .assertTrue (st .st_mode & stat .S_IFREG )
49
58
self .assertFalse (st .st_mode & stat .S_IFDIR )
50
59
51
60
def createTestDirectory (self , path ):
52
61
self .create_dir (path )
53
62
self .assertTrue (self .os .path .exists (path ))
54
63
st = self .os .stat (path )
55
- self .assertEqual (0o777 , stat .S_IMODE (st .st_mode ))
64
+ mode = 0o777 if self .is_windows_fs else 0o755
65
+ self .assertEqual (mode , stat .S_IMODE (st .st_mode ))
56
66
self .assertFalse (st .st_mode & stat .S_IFREG )
57
67
self .assertTrue (st .st_mode & stat .S_IFDIR )
58
68
59
69
60
70
class FakeOsModuleTest (FakeOsModuleTestBase ):
61
71
def setUp (self ):
62
- super (FakeOsModuleTest , self ).setUp ()
72
+ super ().setUp ()
63
73
self .rwx = self .os .R_OK | self .os .W_OK | self .os .X_OK
64
74
self .rw = self .os .R_OK | self .os .W_OK
65
75
@@ -2082,7 +2092,8 @@ def test_chmod_no_follow_symlink(self):
2082
2092
else :
2083
2093
self .os .chmod (link_path , 0o6543 , follow_symlinks = False )
2084
2094
st = self .os .stat (link_path )
2085
- self .assert_mode_equal (0o666 , st .st_mode )
2095
+ mode = 0o644 if self .is_macos else 0o666
2096
+ self .assert_mode_equal (mode , st .st_mode )
2086
2097
st = self .os .stat (link_path , follow_symlinks = False )
2087
2098
self .assert_mode_equal (0o6543 , st .st_mode )
2088
2099
@@ -2097,7 +2108,7 @@ def test_lchmod(self):
2097
2108
self .os .lchmod (link_path , 0o6543 )
2098
2109
2099
2110
st = self .os .stat (link_path )
2100
- self .assert_mode_equal (0o666 , st .st_mode )
2111
+ self .assert_mode_equal (0o644 , st .st_mode )
2101
2112
st = self .os .lstat (link_path )
2102
2113
self .assert_mode_equal (0o6543 , st .st_mode )
2103
2114
@@ -2807,9 +2818,9 @@ def test_nlink_for_directories(self):
2807
2818
2808
2819
def test_umask (self ):
2809
2820
self .check_posix_only ()
2810
- umask = os .umask (0o22 )
2811
- os .umask (umask )
2812
- self .assertEqual ( umask , self . os .umask (0o22 ) )
2821
+ umask = self . os .umask (0o22 )
2822
+ self . assertEqual ( umask , self . os .umask (0o12 ) )
2823
+ self .os .umask (umask )
2813
2824
2814
2825
def test_mkdir_umask_applied (self ):
2815
2826
"""mkdir creates a directory with umask applied."""
@@ -2826,7 +2837,7 @@ def test_mkdir_umask_applied(self):
2826
2837
def test_makedirs_umask_applied (self ):
2827
2838
"""makedirs creates a directories with umask applied."""
2828
2839
self .check_posix_only ()
2829
- self .os .umask (0o22 )
2840
+ umask = self .os .umask (0o22 )
2830
2841
self .os .makedirs (self .make_path ("p1" , "dir1" ))
2831
2842
self .assert_mode_equal (0o755 , self .os .stat (self .make_path ("p1" )).st_mode )
2832
2843
self .assert_mode_equal (
@@ -2838,6 +2849,7 @@ def test_makedirs_umask_applied(self):
2838
2849
self .assert_mode_equal (
2839
2850
0o710 , self .os .stat (self .make_path ("p2" , "dir2" )).st_mode
2840
2851
)
2852
+ self .os .umask (umask )
2841
2853
2842
2854
def test_mknod_umask_applied (self ):
2843
2855
"""mkdir creates a device with umask applied."""
@@ -2998,7 +3010,7 @@ def use_real_fs(self):
2998
3010
2999
3011
class FakeOsModuleTestCaseInsensitiveFS (FakeOsModuleTestBase ):
3000
3012
def setUp (self ):
3001
- super (FakeOsModuleTestCaseInsensitiveFS , self ).setUp ()
3013
+ super ().setUp ()
3002
3014
self .check_case_insensitive_fs ()
3003
3015
self .rwx = self .os .R_OK | self .os .W_OK | self .os .X_OK
3004
3016
self .rw = self .os .R_OK | self .os .W_OK
@@ -3072,7 +3084,7 @@ def test_listdir_impossible_without_read_permission(self):
3072
3084
with self .assertRaises (PermissionError ):
3073
3085
self .os .scandir (directory )
3074
3086
# we can access the file if we know the file name
3075
- assert self .os .stat (file_path ).st_mode & 0o777 == 0o777
3087
+ assert self .os .stat (file_path ).st_mode & 0o777 == 0o755
3076
3088
with self .open (file_path ) as f :
3077
3089
assert f .read () == "hey"
3078
3090
@@ -4109,10 +4121,6 @@ def test_utime_uses_open_fd_as_path(self):
4109
4121
class FakeOsModuleLowLevelFileOpTest (FakeOsModuleTestBase ):
4110
4122
"""Test low level functions `os.open()`, `os.read()` and `os.write()`."""
4111
4123
4112
- def setUp (self ):
4113
- os .umask (0o022 )
4114
- super (FakeOsModuleLowLevelFileOpTest , self ).setUp ()
4115
-
4116
4124
def test_open_read_only (self ):
4117
4125
file_path = self .make_path ("file1" )
4118
4126
self .create_file (file_path , contents = b"contents" )
@@ -4830,7 +4838,7 @@ def use_real_fs(self):
4830
4838
4831
4839
class FakeOsModuleDirFdTest (FakeOsModuleTestBase ):
4832
4840
def setUp (self ):
4833
- super (FakeOsModuleDirFdTest , self ).setUp ()
4841
+ super ().setUp ()
4834
4842
self .check_posix_only ()
4835
4843
if not self .use_real_fs ():
4836
4844
# in the real OS, we test the option as is, in the fake OS
@@ -4948,11 +4956,11 @@ def os_stat():
4948
4956
os_stat ()
4949
4957
self .add_supported_function (self .os .stat )
4950
4958
if self .os .stat in self .os .supports_dir_fd :
4951
- self .assertEqual (os_stat ().st_mode , 0o100666 )
4959
+ self .assertEqual (os_stat ().st_mode , 0o100644 )
4952
4960
4953
4961
def test_lstat (self ):
4954
4962
st = self .os .lstat (self .fname , dir_fd = self .dir_fd )
4955
- self .assertEqual (st .st_mode , 0o100666 )
4963
+ self .assertEqual (st .st_mode , 0o100644 )
4956
4964
4957
4965
def test_mkdir (self ):
4958
4966
def os_mkdir ():
@@ -5185,7 +5193,7 @@ class FakeScandirTest(FakeOsModuleTestBase):
5185
5193
LINKED_FILE_SIZE = 10
5186
5194
5187
5195
def setUp (self ):
5188
- super (FakeScandirTest , self ).setUp ()
5196
+ super ().setUp ()
5189
5197
self .supports_symlinks = not self .is_windows or not self .use_real_fs ()
5190
5198
5191
5199
if use_scandir_package :
@@ -5484,7 +5492,7 @@ def use_real_fs(self):
5484
5492
class FakeScandirFdTest (FakeScandirTest ):
5485
5493
def tearDown (self ):
5486
5494
self .os .close (self .dir_fd )
5487
- super (FakeScandirFdTest , self ).tearDown ()
5495
+ super ().tearDown ()
5488
5496
5489
5497
def scandir_path (self ):
5490
5498
# When scandir is called with a filedescriptor, only the name of the
@@ -5514,7 +5522,7 @@ def use_real_fs(self):
5514
5522
5515
5523
class FakeExtendedAttributeTest (FakeOsModuleTestBase ):
5516
5524
def setUp (self ):
5517
- super (FakeExtendedAttributeTest , self ).setUp ()
5525
+ super ().setUp ()
5518
5526
self .check_linux_only ()
5519
5527
self .dir_path = self .make_path ("foo" )
5520
5528
self .file_path = self .os .path .join (self .dir_path , "bar" )
@@ -5570,7 +5578,7 @@ def setUp(self):
5570
5578
# and cannot be created in the real OS using file system
5571
5579
# functions only
5572
5580
self .check_posix_only ()
5573
- super (FakeOsUnreadableDirTest , self ).setUp ()
5581
+ super ().setUp ()
5574
5582
self .dir_path = self .make_path ("some_dir" )
5575
5583
self .file_path = self .os .path .join (self .dir_path , "some_file" )
5576
5584
self .create_file (self .file_path )
@@ -5618,7 +5626,7 @@ def test_listdir_user_readable_dir_from_other_user(self):
5618
5626
self .check_posix_only ()
5619
5627
user_id = get_uid ()
5620
5628
set_uid (user_id + 1 )
5621
- dir_path = self . make_path ( " dir1")
5629
+ dir_path = "/ dir1"
5622
5630
self .create_dir (dir_path , perm = 0o600 )
5623
5631
self .assertTrue (self .os .path .exists (dir_path ))
5624
5632
reset_ids ()
@@ -5630,8 +5638,9 @@ def test_listdir_user_readable_dir_from_other_user(self):
5630
5638
5631
5639
def test_listdir_group_readable_dir_from_other_user (self ):
5632
5640
self .skip_real_fs () # won't change user in real fs
5641
+ self .check_posix_only ()
5633
5642
set_uid (get_uid () + 1 )
5634
- dir_path = self . make_path ( " dir1")
5643
+ dir_path = "/ dir1"
5635
5644
self .create_dir (dir_path , perm = 0o660 )
5636
5645
self .assertTrue (self .os .path .exists (dir_path ))
5637
5646
reset_ids ()
@@ -5642,7 +5651,7 @@ def test_listdir_group_readable_dir_from_other_group(self):
5642
5651
self .check_posix_only ()
5643
5652
group_id = self .os .getgid ()
5644
5653
set_gid (group_id + 1 )
5645
- dir_path = self . make_path ( " dir1")
5654
+ dir_path = "/ dir1"
5646
5655
self .create_dir (dir_path , perm = 0o060 )
5647
5656
self .assertTrue (self .os .path .exists (dir_path ))
5648
5657
set_gid (group_id )
@@ -5687,9 +5696,10 @@ def test_remove_unreadable_dir(self):
5687
5696
self .assertFalse (self .os .path .exists (dir_path ))
5688
5697
5689
5698
def test_remove_unreadable_dir_from_other_user (self ):
5699
+ self .check_posix_only ()
5690
5700
self .skip_real_fs () # won't change user in real fs
5691
5701
set_uid (get_uid () + 1 )
5692
- dir_path = self . make_path ( " dir1")
5702
+ dir_path = "/ dir1"
5693
5703
self .create_dir (dir_path , perm = 0o000 )
5694
5704
self .assertTrue (self .os .path .exists (dir_path ))
5695
5705
reset_ids ()
0 commit comments