-
Notifications
You must be signed in to change notification settings - Fork 11
Fix Failing Tests on Windows (WIP) #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bdc5fcc
9a3c641
2394643
5e7a353
8b41a44
3df30d5
2208017
28a9ac1
f432696
de1db77
2cd60af
29d7871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import ctypes | ||
| import os | ||
| import stat | ||
| import unittest | ||
|
|
@@ -12,6 +13,22 @@ | |
| FILENAME = Path('one') | ||
|
|
||
|
|
||
| def is_windows_admin(): | ||
| """Check if the script is running in a terminal with admin privileges on Windows""" | ||
| if os.name == 'nt': | ||
| try: | ||
| return ctypes.windll.shell32.IsUserAnAdmin() | ||
| except Exception: | ||
| return False | ||
|
|
||
|
|
||
| IS_WINDOWS_USER = os.name == 'nt' and not is_windows_admin() | ||
| skip_if_symlink_creation_forbidden = unittest.skipIf( | ||
| IS_WINDOWS_USER, | ||
| 'This test requires admin privileges to create symlink files on Windows', | ||
| ) | ||
|
|
||
|
|
||
| @helpers.temps(safer.open) | ||
| @tdir | ||
| class TestSafer(unittest.TestCase): | ||
|
|
@@ -129,8 +146,14 @@ def test_file_perms(self, safer_open): | |
| fp.write('hello') | ||
| assert FILENAME.read_text() == 'hello' | ||
| mode = os.stat(FILENAME).st_mode | ||
| assert mode in (0o100664, 0o100644), stat.filemode(mode) | ||
| new_mode = mode & 0o100770 | ||
|
|
||
| if os.name == 'posix': | ||
| assert mode in (0o100664, 0o100644), stat.filemode(mode) | ||
| new_mode = mode & 0o100770 | ||
| elif os.name == 'nt': | ||
| new_mode = mode | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, for the Windows-y mode, I'll have to go through the code in more detail and check docs, because a chunk of it was new to me. I'll give feedback in a couple of hours, though. |
||
| else: | ||
| assert False, f'Do not understand os.name = {os.name}' | ||
|
|
||
| os.chmod(FILENAME, new_mode) | ||
| with safer_open(FILENAME, 'w') as fp: | ||
|
|
@@ -183,6 +206,7 @@ def test_mode_t(self, safer_open): | |
| fp.write('hello') | ||
| assert FILENAME.read_text() == 'hello' | ||
|
|
||
| @skip_if_symlink_creation_forbidden | ||
| def test_symlink_file(self, safer_open): | ||
| with safer_open(FILENAME, 'w') as fp: | ||
| fp.write('hello') | ||
|
|
@@ -194,6 +218,7 @@ def test_symlink_file(self, safer_open): | |
| fp.write('overwritten') | ||
| assert FILENAME.read_text() == 'overwritten' | ||
|
|
||
| @skip_if_symlink_creation_forbidden | ||
| def test_symlink_directory(self, safer_open): | ||
| FILENAME = Path('sub/test.txt') | ||
| with safer_open(FILENAME, 'w', make_parents=True) as fp: | ||
|
|
@@ -227,4 +252,7 @@ def test_tempfile_perms(self, safer_open): | |
| perms.append(os.stat(filename).st_mode) | ||
|
|
||
| assert perms == [perms[0]] * len(perms) | ||
| assert perms[0] in (0o100644, 0o100664) | ||
| if os.name == 'nt': | ||
| assert perms[0] == 0o100666 | ||
| else: | ||
| assert perms[0] in (0o100644, 0o100664) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -198,8 +198,8 @@ def test_wrapper_bug(): | |
| @tdir | ||
| def test_wrapper_bug2(): | ||
| with pytest.raises(NotImplementedError) as e: | ||
| fp = open(FILENAME, 'w') | ||
| safer.writer(fp, close_on_exit=True, temp_file=True) | ||
| with open(FILENAME, 'w') as fp: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha, I'm pretty sure you've found the issue! So this change needs two non-obvious tweaks. I didn't accidentally forget to In this first test, I think you have actually revealed an issue:
So I need to make this change, or verify the bug doesn't exist, and then the first piece of code needs to change to: Probably the second code block should stay the same... I'll make that change and let you know!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Our bug hunt pays off. :) |
||
| safer.writer(fp, close_on_exit=True, temp_file=True) | ||
| assert e.value.args == (safer.BUG_MESSAGE,) | ||
|
|
||
|
|
||
|
|
@@ -211,10 +211,10 @@ def test_wrapper_bug3(): | |
| fp.write('hello, world') | ||
| assert FILENAME.read_text() == 'hello, world' # OK! | ||
|
|
||
| fp = open(FILENAME, 'w') | ||
| with safer.writer(fp, close_on_exit=True, temp_file=True): | ||
| fp.write('hello, world') | ||
| assert FILENAME.read_text() == '' # Fails! | ||
| with open(FILENAME, 'w') as fp: | ||
| with safer.writer(fp, close_on_exit=True, temp_file=True): | ||
| fp.write('') | ||
| assert FILENAME.read_text() == '' | ||
| finally: | ||
| safer.BUG_MESSAGE = bug | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.