From d523a21e498fd40a158c5cf4c2de74a0f2c316f7 Mon Sep 17 00:00:00 2001 From: Zapple Date: Sat, 21 Aug 2021 23:25:30 -0500 Subject: [PATCH 1/2] process windows paths for pathlib fixes #111 --- oschmod/__init__.py | 25 ++++++++++-- requirements/test.txt | 1 + tests/test_oschmod_pathlib.py | 73 +++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 tests/test_oschmod_pathlib.py diff --git a/oschmod/__init__.py b/oschmod/__init__.py index 1e75830..1f1e035 100644 --- a/oschmod/__init__.py +++ b/oschmod/__init__.py @@ -31,7 +31,9 @@ import re import stat import string +import sys +PATHLIB_ERROR = "Pathlib not supported for py26""" + + mock_sys.version = '2.6.0' + + with pytest.raises(RuntimeError, match='Pathlib not supported for Date: Sat, 21 Aug 2021 23:48:31 -0500 Subject: [PATCH 2/2] add tests and logic for if value is already a string --- oschmod/__init__.py | 12 ++++++++++-- tests/test_oschmod_pathlib.py | 29 ++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/oschmod/__init__.py b/oschmod/__init__.py index 1f1e035..74455b7 100644 --- a/oschmod/__init__.py +++ b/oschmod/__init__.py @@ -361,7 +361,11 @@ def win_get_permissions(path): """Get the file or dir permissions.""" if not os.path.exists(path): raise FileNotFoundError('Path %s could not be found.' % path) - str_path = _win_transform_pathlib_to_str(path) + + if isinstance(path, str): + str_path = path + else: + str_path = _win_transform_pathlib_to_str(path) return _win_get_permissions(str_path, get_object_type(path)) @@ -401,7 +405,11 @@ def win_set_permissions(path, mode): """Set the file or dir permissions.""" if not os.path.exists(path): raise FileNotFoundError('Path %s could not be found.' % path) - str_path = _win_transform_pathlib_to_str(path) + + if isinstance(path, str): + str_path = path + else: + str_path = _win_transform_pathlib_to_str(path) _win_set_permissions(str_path, mode, get_object_type(path)) diff --git a/tests/test_oschmod_pathlib.py b/tests/test_oschmod_pathlib.py index a4a33d9..eb8697f 100644 --- a/tests/test_oschmod_pathlib.py +++ b/tests/test_oschmod_pathlib.py @@ -7,18 +7,37 @@ @mock.patch('oschmod._win_get_permissions') @mock.patch('oschmod._win_transform_pathlib_to_str') -def test_win_get_permissions(transform_mock, get_permissions_mock): +def test_win_get_permissions_string_path(transform_mock, get_permissions_mock): oschmod.win_get_permissions('.') - transform_mock.assert_called_once_with('.') + transform_mock.assert_not_called() @mock.patch('oschmod._win_set_permissions') @mock.patch('oschmod._win_transform_pathlib_to_str') -def test_win_set_permissions(transform_mock, set_permissions_mock): - oschmod.win_set_permissions('.', "mock_mode") +def test_win_set_permissions_string_path(transform_mock, set_permissions_mock): + oschmod.win_set_permissions('.', 'mock_mode') - transform_mock.assert_called_once_with('.') + transform_mock.assert_not_called() + +@mock.patch('oschmod._win_get_permissions') +@mock.patch('oschmod._win_transform_pathlib_to_str') +def test_win_get_permissions_path(transform_mock, get_permissions_mock): + mock_path = mock.MagicMock() + + oschmod.win_get_permissions(mock_path) + + transform_mock.assert_called_once_with(mock_path) + + +@mock.patch('oschmod._win_set_permissions') +@mock.patch('oschmod._win_transform_pathlib_to_str') +def test_win_set_permissions_path(transform_mock, set_permissions_mock): + mock_path = mock.MagicMock() + + oschmod.win_set_permissions(mock_path, 'mock_mode') + + transform_mock.assert_called_once_with(mock_path) @mock.patch('oschmod.sys')