mirror of
https://github.com/yt-dlp/yt-dlp
synced 2024-12-25 21:59:04 +01:00
[test] Add test_locked_file
This commit is contained in:
parent
b63837bce0
commit
b506289fe2
2 changed files with 34 additions and 2 deletions
|
@ -56,6 +56,7 @@ from yt_dlp.utils import (
|
||||||
is_html,
|
is_html,
|
||||||
js_to_json,
|
js_to_json,
|
||||||
limit_length,
|
limit_length,
|
||||||
|
locked_file,
|
||||||
merge_dicts,
|
merge_dicts,
|
||||||
mimetype2ext,
|
mimetype2ext,
|
||||||
month_by_name,
|
month_by_name,
|
||||||
|
@ -1795,6 +1796,36 @@ Line 1
|
||||||
self.assertEqual(Config.hide_login_info(['--username=foo']),
|
self.assertEqual(Config.hide_login_info(['--username=foo']),
|
||||||
['--username=PRIVATE'])
|
['--username=PRIVATE'])
|
||||||
|
|
||||||
|
def test_locked_file(self):
|
||||||
|
TEXT = 'test_locked_file\n'
|
||||||
|
FILE = 'test_locked_file.ytdl'
|
||||||
|
MODES = 'war' # Order is important
|
||||||
|
|
||||||
|
try:
|
||||||
|
for lock_mode in MODES:
|
||||||
|
with locked_file(FILE, lock_mode, False) as f:
|
||||||
|
if lock_mode == 'r':
|
||||||
|
self.assertEqual(f.read(), TEXT * 2, 'Wrong file content')
|
||||||
|
else:
|
||||||
|
f.write(TEXT)
|
||||||
|
for test_mode in MODES:
|
||||||
|
testing_write = test_mode != 'r'
|
||||||
|
try:
|
||||||
|
with locked_file(FILE, test_mode, False):
|
||||||
|
pass
|
||||||
|
except (BlockingIOError, PermissionError):
|
||||||
|
if not testing_write: # FIXME
|
||||||
|
print(f'Known issue: Exclusive lock ({lock_mode}) blocks read access ({test_mode})')
|
||||||
|
continue
|
||||||
|
self.assertTrue(testing_write, f'{test_mode} is blocked by {lock_mode}')
|
||||||
|
else:
|
||||||
|
self.assertFalse(testing_write, f'{test_mode} is not blocked by {lock_mode}')
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
os.remove(FILE)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -684,8 +684,9 @@ def sanitize_open(filename, open_mode):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
# FIXME: Windows only has mandatory locking which also locks the file from being read.
|
# FIXME: An exclusive lock also locks the file from being read.
|
||||||
# So for now, don't lock the file on windows. Ref: https://github.com/yt-dlp/yt-dlp/issues/3124
|
# Since windows locks are mandatory, don't lock the file on windows (for now).
|
||||||
|
# Ref: https://github.com/yt-dlp/yt-dlp/issues/3124
|
||||||
raise LockingUnsupportedError()
|
raise LockingUnsupportedError()
|
||||||
stream = locked_file(filename, open_mode, block=False).__enter__()
|
stream = locked_file(filename, open_mode, block=False).__enter__()
|
||||||
except LockingUnsupportedError:
|
except LockingUnsupportedError:
|
||||||
|
|
Loading…
Reference in a new issue