Skip to content

Commit 98d0f66

Browse files
Chiemezuorec
authored andcommitted
Fix tests to work on Windows
1 parent 8e15b99 commit 98d0f66

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

test/test_open.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ctypes
12
import os
23
import stat
34
import unittest
@@ -12,6 +13,22 @@
1213
FILENAME = Path('one')
1314

1415

16+
def is_windows_admin():
17+
"""Check if the script is running in a terminal with admin privileges on Windows"""
18+
if os.name == 'nt':
19+
try:
20+
return ctypes.windll.shell32.IsUserAnAdmin()
21+
except Exception:
22+
return False
23+
24+
25+
IS_WINDOWS_USER = os.name == 'nt' and not is_windows_admin()
26+
skip_if_symlink_creation_forbidden = unittest.skipIf(
27+
IS_WINDOWS_USER,
28+
'This test requires admin privileges to create symlink files on Windows',
29+
)
30+
31+
1532
@helpers.temps(safer.open)
1633
@tdir
1734
class TestSafer(unittest.TestCase):
@@ -129,8 +146,14 @@ def test_file_perms(self, safer_open):
129146
fp.write('hello')
130147
assert FILENAME.read_text() == 'hello'
131148
mode = os.stat(FILENAME).st_mode
132-
assert mode in (0o100664, 0o100644), stat.filemode(mode)
133-
new_mode = mode & 0o100770
149+
150+
if os.name == 'posix':
151+
assert mode in (0o100664, 0o100644), stat.filemode(mode)
152+
new_mode = mode & 0o100770
153+
elif os.name == 'nt':
154+
new_mode = mode
155+
else:
156+
assert False, f'Do not understand os.name = {os.name}'
134157

135158
os.chmod(FILENAME, new_mode)
136159
with safer_open(FILENAME, 'w') as fp:
@@ -183,6 +206,7 @@ def test_mode_t(self, safer_open):
183206
fp.write('hello')
184207
assert FILENAME.read_text() == 'hello'
185208

209+
@skip_if_symlink_creation_forbidden
186210
def test_symlink_file(self, safer_open):
187211
with safer_open(FILENAME, 'w') as fp:
188212
fp.write('hello')
@@ -194,6 +218,7 @@ def test_symlink_file(self, safer_open):
194218
fp.write('overwritten')
195219
assert FILENAME.read_text() == 'overwritten'
196220

221+
@skip_if_symlink_creation_forbidden
197222
def test_symlink_directory(self, safer_open):
198223
FILENAME = Path('sub/test.txt')
199224
with safer_open(FILENAME, 'w', make_parents=True) as fp:
@@ -227,4 +252,7 @@ def test_tempfile_perms(self, safer_open):
227252
perms.append(os.stat(filename).st_mode)
228253

229254
assert perms == [perms[0]] * len(perms)
230-
assert perms[0] in (0o100644, 0o100664)
255+
if os.name == 'nt':
256+
assert perms[0] == 0o100666
257+
else:
258+
assert perms[0] in (0o100644, 0o100664)

test/test_writer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ def test_wrapper_bug():
198198
@tdir
199199
def test_wrapper_bug2():
200200
with pytest.raises(NotImplementedError) as e:
201-
fp = open(FILENAME, 'w')
202-
safer.writer(fp, close_on_exit=True, temp_file=True)
201+
with open(FILENAME, 'w') as fp:
202+
safer.writer(fp, close_on_exit=True, temp_file=True)
203203
assert e.value.args == (safer.BUG_MESSAGE,)
204204

205205

@@ -211,10 +211,10 @@ def test_wrapper_bug3():
211211
fp.write('hello, world')
212212
assert FILENAME.read_text() == 'hello, world' # OK!
213213

214-
fp = open(FILENAME, 'w')
215-
with safer.writer(fp, close_on_exit=True, temp_file=True):
216-
fp.write('hello, world')
217-
assert FILENAME.read_text() == '' # Fails!
214+
with open(FILENAME, 'w') as fp:
215+
with safer.writer(fp, close_on_exit=True, temp_file=True):
216+
fp.write('')
217+
assert FILENAME.read_text() == ''
218218
finally:
219219
safer.BUG_MESSAGE = bug
220220

0 commit comments

Comments
 (0)