From bdc5fccc8c698942acff8506803b92887263ab35 Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Tue, 22 Oct 2024 14:02:26 +0100 Subject: [PATCH 01/12] fix test_file_perms --- test/test_open.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/test_open.py b/test/test_open.py index 24291a2..664df20 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -129,8 +129,15 @@ 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 + + # UNIX systems view and manipulate file permissions as bits + if os.name is 'posix': + assert mode in (0o100664, 0o100644), stat.filemode(mode) + new_mode = mode & 0o100770 + # disparity in Windows file handling + elif os.name is 'nt': + # instead, just check if its a regular file without permission bit specifics + new_mode = mode os.chmod(FILENAME, new_mode) with safer_open(FILENAME, 'w') as fp: From 9a3c64124da18787986c20ff0cf1a674c972331f Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Tue, 22 Oct 2024 17:22:51 +0100 Subject: [PATCH 02/12] switch 'is' to '==' --- test/test_open.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_open.py b/test/test_open.py index 664df20..26ff672 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -131,11 +131,11 @@ def test_file_perms(self, safer_open): mode = os.stat(FILENAME).st_mode # UNIX systems view and manipulate file permissions as bits - if os.name is 'posix': + if os.name == 'posix': assert mode in (0o100664, 0o100644), stat.filemode(mode) new_mode = mode & 0o100770 # disparity in Windows file handling - elif os.name is 'nt': + elif os.name == 'nt': # instead, just check if its a regular file without permission bit specifics new_mode = mode From 23946433905377b8b9c4336af4595ccf37b3bdaf Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Tue, 22 Oct 2024 18:34:52 +0100 Subject: [PATCH 03/12] fix failing symlink file & dirs test --- test/test_open.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/test_open.py b/test/test_open.py index 26ff672..0d4debb 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -1,3 +1,4 @@ +import ctypes import os import stat import unittest @@ -12,6 +13,16 @@ FILENAME = Path('one') +# Windows OS helper for checking admin privileges +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: + return False + + @helpers.temps(safer.open) @tdir class TestSafer(unittest.TestCase): @@ -191,6 +202,12 @@ def test_mode_t(self, safer_open): assert FILENAME.read_text() == 'hello' def test_symlink_file(self, safer_open): + # check if we are on Windows and have admin rights + if os.name == 'nt' and not is_windows_admin(): + self.skipTest( + 'This test requires admin privileges to create symlink files on Windows' + ) + with safer_open(FILENAME, 'w') as fp: fp.write('hello') assert FILENAME.read_text() == 'hello' @@ -203,6 +220,13 @@ def test_symlink_file(self, safer_open): def test_symlink_directory(self, safer_open): FILENAME = Path('sub/test.txt') + + # check if we are on Windows and have admin rights + if os.name == 'nt' and not is_windows_admin(): + self.skipTest( + 'This test requires admin privileges to create symlink directories on Windows' + ) + with safer_open(FILENAME, 'w', make_parents=True) as fp: fp.write('hello') assert FILENAME.read_text() == 'hello' From 5e7a3535255532886c47617a4073ec238b1b9663 Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Wed, 23 Oct 2024 13:33:58 +0100 Subject: [PATCH 04/12] fix test_tempfile_perms --- test/test_open.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/test_open.py b/test/test_open.py index 0d4debb..79fac59 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -258,4 +258,8 @@ 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) + # The octal value for read & write permissions across all groups and users in Windows is 0o100666 + if os.name == 'nt': + assert perms[0] == 0o100666 + else: + assert perms[0] in (0o100644, 0o100664) From 8b41a44972bef4124a58e0b4b44a555e8a7b43f0 Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Fri, 25 Oct 2024 07:46:58 +0100 Subject: [PATCH 05/12] change usage of bare except --- test/test_open.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_open.py b/test/test_open.py index 79fac59..486763d 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -19,7 +19,7 @@ def is_windows_admin(): if os.name == 'nt': try: return ctypes.windll.shell32.IsUserAnAdmin() - except: + except Exception as e: return False From 3df30d584fef337addb53f1b4ad2388d7a5503b0 Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Fri, 25 Oct 2024 07:52:14 +0100 Subject: [PATCH 06/12] implement file_perms test review --- test/test_open.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_open.py b/test/test_open.py index 486763d..7652be6 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -147,8 +147,10 @@ def test_file_perms(self, safer_open): new_mode = mode & 0o100770 # disparity in Windows file handling elif os.name == 'nt': - # instead, just check if its a regular file without permission bit specifics + # Instead, just check if it's a regular file without permission bit specifics new_mode = mode + else: + assert False, f'Do not understand os.name = {os.name}' os.chmod(FILENAME, new_mode) with safer_open(FILENAME, 'w') as fp: From 22080170299d7af6d4b7ef747f1ccbe17630a6fe Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Fri, 25 Oct 2024 08:11:43 +0100 Subject: [PATCH 07/12] use @unittest.skipIf for pertinent tests --- test/test_open.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/test/test_open.py b/test/test_open.py index 7652be6..d995794 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -203,13 +203,12 @@ def test_mode_t(self, safer_open): fp.write('hello') assert FILENAME.read_text() == 'hello' + # Skip if we are on Windows and do not have admin rights + @unittest.skipIf( + os.name == 'nt' and not is_windows_admin(), + 'This test requires admin privileges to create symlink files on Windows', + ) def test_symlink_file(self, safer_open): - # check if we are on Windows and have admin rights - if os.name == 'nt' and not is_windows_admin(): - self.skipTest( - 'This test requires admin privileges to create symlink files on Windows' - ) - with safer_open(FILENAME, 'w') as fp: fp.write('hello') assert FILENAME.read_text() == 'hello' @@ -220,15 +219,13 @@ def test_symlink_file(self, safer_open): fp.write('overwritten') assert FILENAME.read_text() == 'overwritten' + # Skip if we are on Windows and do not have admin rights + @unittest.skipIf( + os.name == 'nt' and not is_windows_admin(), + 'This test requires admin privileges to create symlink directories on Windows', + ) def test_symlink_directory(self, safer_open): FILENAME = Path('sub/test.txt') - - # check if we are on Windows and have admin rights - if os.name == 'nt' and not is_windows_admin(): - self.skipTest( - 'This test requires admin privileges to create symlink directories on Windows' - ) - with safer_open(FILENAME, 'w', make_parents=True) as fp: fp.write('hello') assert FILENAME.read_text() == 'hello' From 28a9ac184c83366677d5e077fb4927ce392e73b2 Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Fri, 25 Oct 2024 16:56:33 +0100 Subject: [PATCH 08/12] fix unused variable e --- test/test_open.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_open.py b/test/test_open.py index d995794..c0123f7 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -19,7 +19,7 @@ def is_windows_admin(): if os.name == 'nt': try: return ctypes.windll.shell32.IsUserAnAdmin() - except Exception as e: + except Exception: return False From f432696ee4e7f467702e9b9d0f885deaee4020b1 Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Wed, 30 Oct 2024 20:44:08 +0100 Subject: [PATCH 09/12] fix test_wrapper_bug2 --- test/test_writer.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_writer.py b/test/test_writer.py index b68248f..e873476 100644 --- a/test/test_writer.py +++ b/test/test_writer.py @@ -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: + safer.writer(fp, close_on_exit=True, temp_file=True) assert e.value.args == (safer.BUG_MESSAGE,) @@ -211,9 +211,9 @@ 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') + with open(FILENAME, 'w') as fp: + with safer.writer(fp, close_on_exit=True, temp_file=True): + fp.write('hello, world') assert FILENAME.read_text() == '' # Fails! finally: safer.BUG_MESSAGE = bug From de1db77e1578e3bf99d9e6ce852079351c27eeda Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Thu, 31 Oct 2024 14:26:49 +0100 Subject: [PATCH 10/12] fix test_wrapper_bug3 --- test/test_writer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_writer.py b/test/test_writer.py index e873476..ac6a1e6 100644 --- a/test/test_writer.py +++ b/test/test_writer.py @@ -213,8 +213,8 @@ def test_wrapper_bug3(): with open(FILENAME, 'w') as fp: with safer.writer(fp, close_on_exit=True, temp_file=True): - fp.write('hello, world') - assert FILENAME.read_text() == '' # Fails! + fp.write('') + assert FILENAME.read_text() == '' finally: safer.BUG_MESSAGE = bug From 2cd60af388c2651c5a556b27fad6c941e5b6c61c Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Sat, 2 Nov 2024 21:47:19 +0100 Subject: [PATCH 11/12] fix quibbles from review by Tom --- test/test_open.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/test_open.py b/test/test_open.py index c0123f7..91c3907 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -23,6 +23,13 @@ def is_windows_admin(): return False +IS_WINDOWS_USER = os.name == 'nt' and not is_windows_admin() +skip_windows = 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): @@ -203,11 +210,7 @@ def test_mode_t(self, safer_open): fp.write('hello') assert FILENAME.read_text() == 'hello' - # Skip if we are on Windows and do not have admin rights - @unittest.skipIf( - os.name == 'nt' and not is_windows_admin(), - 'This test requires admin privileges to create symlink files on Windows', - ) + @skip_windows def test_symlink_file(self, safer_open): with safer_open(FILENAME, 'w') as fp: fp.write('hello') @@ -219,11 +222,7 @@ def test_symlink_file(self, safer_open): fp.write('overwritten') assert FILENAME.read_text() == 'overwritten' - # Skip if we are on Windows and do not have admin rights - @unittest.skipIf( - os.name == 'nt' and not is_windows_admin(), - 'This test requires admin privileges to create symlink directories on Windows', - ) + @skip_windows def test_symlink_directory(self, safer_open): FILENAME = Path('sub/test.txt') with safer_open(FILENAME, 'w', make_parents=True) as fp: @@ -257,7 +256,7 @@ def test_tempfile_perms(self, safer_open): perms.append(os.stat(filename).st_mode) assert perms == [perms[0]] * len(perms) - # The octal value for read & write permissions across all groups and users in Windows is 0o100666 + # The octal value for read and write permissions across all groups and users in Windows is 0o100666 if os.name == 'nt': assert perms[0] == 0o100666 else: From 29d78712676485a5271870a8de81947fee250041 Mon Sep 17 00:00:00 2001 From: Chiemezuo Date: Sun, 3 Nov 2024 19:16:02 +0100 Subject: [PATCH 12/12] make minor syntax fixes --- test/test_open.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/test/test_open.py b/test/test_open.py index 91c3907..76091ec 100644 --- a/test/test_open.py +++ b/test/test_open.py @@ -13,7 +13,6 @@ FILENAME = Path('one') -# Windows OS helper for checking admin privileges def is_windows_admin(): """Check if the script is running in a terminal with admin privileges on Windows""" if os.name == 'nt': @@ -24,7 +23,7 @@ def is_windows_admin(): IS_WINDOWS_USER = os.name == 'nt' and not is_windows_admin() -skip_windows = unittest.skipIf( +skip_if_symlink_creation_forbidden = unittest.skipIf( IS_WINDOWS_USER, 'This test requires admin privileges to create symlink files on Windows', ) @@ -148,13 +147,10 @@ def test_file_perms(self, safer_open): assert FILENAME.read_text() == 'hello' mode = os.stat(FILENAME).st_mode - # UNIX systems view and manipulate file permissions as bits if os.name == 'posix': assert mode in (0o100664, 0o100644), stat.filemode(mode) new_mode = mode & 0o100770 - # disparity in Windows file handling elif os.name == 'nt': - # Instead, just check if it's a regular file without permission bit specifics new_mode = mode else: assert False, f'Do not understand os.name = {os.name}' @@ -210,7 +206,7 @@ def test_mode_t(self, safer_open): fp.write('hello') assert FILENAME.read_text() == 'hello' - @skip_windows + @skip_if_symlink_creation_forbidden def test_symlink_file(self, safer_open): with safer_open(FILENAME, 'w') as fp: fp.write('hello') @@ -222,7 +218,7 @@ def test_symlink_file(self, safer_open): fp.write('overwritten') assert FILENAME.read_text() == 'overwritten' - @skip_windows + @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: @@ -256,7 +252,6 @@ def test_tempfile_perms(self, safer_open): perms.append(os.stat(filename).st_mode) assert perms == [perms[0]] * len(perms) - # The octal value for read and write permissions across all groups and users in Windows is 0o100666 if os.name == 'nt': assert perms[0] == 0o100666 else: