Ok, isso é um teste. Não use maiusculas.
Show More
@@ -27,6 +27,19 b' from vcsserver.str_utils import safe_byt' | |||
|
27 | 27 | |
|
28 | 28 | log = logging.getLogger(__name__) |
|
29 | 29 | |
|
30 |
HOOKS_DIR_MODE = 0o755
Certo. Feito |
|
|
31 | HOOKS_FILE_MODE = 0o755 | |
|
32 | ||
|
33 | ||
|
34 | def set_permissions_if_needed(path_to_check, perms: oct): | |
|
35 | # Get current permissions | |
|
36 | current_permissions = os.stat(path_to_check).st_mode & 0o777 # Extract permission bits | |
|
37 | ||
|
38 | # Check if current permissions are lower than required | |
|
39 | if current_permissions < int(perms): | |
|
40 | # Change the permissions if they are lower than required | |
|
41 | os.chmod(path_to_check, perms) | |
|
42 | ||
|
30 | 43 | |
|
31 | 44 | def get_git_hooks_path(repo_path, bare): |
|
32 | 45 | hooks_path = os.path.join(repo_path, 'hooks') |
@@ -51,10 +64,9 b' def install_git_hooks(repo_path, bare, e' | |||
|
51 | 64 | # we always call it to ensure dir exists and it has a proper mode |
|
52 | 65 | if not os.path.exists(hooks_path): |
|
53 | 66 | # If it doesn't exist, create a new directory with the specified mode |
|
54 |
os.makedirs(hooks_path, mode= |
|
|
55 | else: | |
|
56 | # If it exists, change the directory's mode to the specified mode | |
|
57 | os.chmod(hooks_path, mode=0o777) | |
|
67 | os.makedirs(hooks_path, mode=HOOKS_DIR_MODE, exist_ok=True) | |
|
68 | # If it exists, change the directory's mode to the specified mode | |
|
69 | set_permissions_if_needed(hooks_path, perms=HOOKS_DIR_MODE) | |
|
58 | 70 | |
|
59 | 71 | tmpl_post = pkg_resources.resource_string( |
|
60 | 72 | 'vcsserver', '/'.join( |
@@ -80,7 +92,7 b' def install_git_hooks(repo_path, bare, e' | |||
|
80 | 92 | template = template.replace(b'_ENV_', safe_bytes(executable)) |
|
81 | 93 | template = template.replace(b'_PATH_', safe_bytes(path)) |
|
82 | 94 | f.write(template) |
|
83 | os.chmod(_hook_file, 0o755) | |
|
95 | set_permissions_if_needed(_hook_file, perms=HOOKS_FILE_MODE) | |
|
84 | 96 | except OSError: |
|
85 | 97 | log.exception('error writing hook file %s', _hook_file) |
|
86 | 98 | else: |
@@ -22,8 +22,9 b' import pytest' | |||
|
22 | 22 | import vcsserver |
|
23 | 23 | import tempfile |
|
24 | 24 | from vcsserver import hook_utils |
|
25 | from vcsserver.hook_utils import set_permissions_if_needed, HOOKS_DIR_MODE, HOOKS_FILE_MODE | |
|
25 | 26 | from vcsserver.tests.fixture import no_newline_id_generator |
|
26 |
from vcsserver.str_utils import safe_bytes |
|
|
27 | from vcsserver.str_utils import safe_bytes | |
|
27 | 28 | from vcsserver.utils import AttributeDict |
|
28 | 29 | |
|
29 | 30 | |
@@ -58,13 +59,22 b' class TestCheckRhodecodeHook:' | |||
|
58 | 59 | class BaseInstallHooks: |
|
59 | 60 | HOOK_FILES = () |
|
60 | 61 | |
|
62 | def _check_hook_file_dir_mode(self, file_path): | |
|
63 | dir_path = os.path.dirname(file_path) | |
|
64 | assert os.path.exists(dir_path), f'dir {file_path} missing' | |
|
65 | stat_info = os.stat(dir_path) | |
|
66 | ||
|
67 | file_mode = stat.S_IMODE(stat_info.st_mode) | |
|
68 | expected_mode = int(HOOKS_DIR_MODE) | |
|
69 | assert expected_mode == file_mode, f'expected mode: {oct(expected_mode)} got: {oct(file_mode)} for {dir_path}' | |
|
70 | ||
|
61 | 71 | def _check_hook_file_mode(self, file_path): |
|
62 | 72 | assert os.path.exists(file_path), f'path {file_path} missing' |
|
63 | 73 | stat_info = os.stat(file_path) |
|
64 | 74 | |
|
65 | 75 | file_mode = stat.S_IMODE(stat_info.st_mode) |
|
66 |
expected_mode = int( |
|
|
67 | assert expected_mode == file_mode | |
|
76 | expected_mode = int(HOOKS_FILE_MODE) | |
|
77 | assert expected_mode == file_mode, f'expected mode: {oct(expected_mode)} got: {oct(file_mode)} for {file_path}' | |
|
68 | 78 | |
|
69 | 79 | def _check_hook_file_content(self, file_path, executable): |
|
70 | 80 | executable = executable or sys.executable |
@@ -102,6 +112,8 b' class BaseInstallHooks:' | |||
|
102 | 112 | file_path = os.path.join(repo_path, 'hooks', file_name) |
|
103 | 113 | else: |
|
104 | 114 | file_path = os.path.join(repo_path, '.git', 'hooks', file_name) |
|
115 | ||
|
116 | self._check_hook_file_dir_mode(file_path) | |
|
105 | 117 | self._check_hook_file_mode(file_path) |
|
106 | 118 | self._check_hook_file_content(file_path, sys.executable) |
|
107 | 119 | |
@@ -204,3 +216,74 b' class TestInstallSvnHooks(BaseInstallHoo' | |||
|
204 | 216 | repo.path, force_create=True) |
|
205 | 217 | assert result |
|
206 | 218 | self.check_hooks(repo.path, ) |
|
219 | ||
|
220 | ||
|
221 | def create_test_file(filename): | |
|
222 | """Utility function to create a test file.""" | |
|
223 | with open(filename, 'w') as f: | |
|
224 | f.write("Test file") | |
|
225 | ||
|
226 | ||
|
227 | def remove_test_file(filename): | |
|
228 | """Utility function to remove a test file.""" | |
|
229 | if os.path.exists(filename): | |
|
230 | os.remove(filename) | |
|
231 | ||
|
232 | ||
|
233 | @pytest.fixture | |
|
234 | def test_file(): | |
|
235 | filename = 'test_file.txt' | |
|
236 | create_test_file(filename) | |
|
237 | yield filename | |
|
238 | remove_test_file(filename) | |
|
239 | ||
|
240 | ||
|
241 | def test_increase_permissions(test_file): | |
|
242 | # Set initial lower permissions | |
|
243 | initial_perms = 0o644 | |
|
244 | os.chmod(test_file, initial_perms) | |
|
245 | ||
|
246 | # Set higher permissions | |
|
247 | new_perms = 0o666 | |
|
248 | set_permissions_if_needed(test_file, new_perms) | |
|
249 | ||
|
250 | # Check if permissions were updated | |
|
251 | assert (os.stat(test_file).st_mode & 0o777) == new_perms | |
|
252 | ||
|
253 | ||
|
254 | def test_no_permission_change_needed(test_file): | |
|
255 | # Set initial permissions | |
|
256 | initial_perms = 0o666 | |
|
257 | os.chmod(test_file, initial_perms) | |
|
258 | ||
|
259 | # Attempt to set the same permissions | |
|
260 | set_permissions_if_needed(test_file, initial_perms) | |
|
261 | ||
|
262 | # Check if permissions were unchanged | |
|
263 | assert (os.stat(test_file).st_mode & 0o777) == initial_perms | |
|
264 | ||
|
265 | ||
|
266 | def test_no_permission_reduction(test_file): | |
|
267 | # Set initial higher permissions | |
|
268 | initial_perms = 0o666 | |
|
269 | os.chmod(test_file, initial_perms) | |
|
270 | ||
|
271 | # Attempt to set lower permissions | |
|
272 | lower_perms = 0o644 | |
|
273 | set_permissions_if_needed(test_file, lower_perms) | |
|
274 | ||
|
275 | # Check if permissions were not reduced | |
|
276 | assert (os.stat(test_file).st_mode & 0o777) == initial_perms | |
|
277 | ||
|
278 | ||
|
279 | def test_no_permission_reduction_when_on_777(test_file): | |
|
280 | # Set initial higher permissions | |
|
281 | initial_perms = 0o777 | |
|
282 | os.chmod(test_file, initial_perms) | |
|
283 | ||
|
284 | # Attempt to set lower permissions | |
|
285 | lower_perms = 0o755 | |
|
286 | set_permissions_if_needed(test_file, lower_perms) | |
|
287 | ||
|
288 | # Check if permissions were not reduced | |
|
289 | assert (os.stat(test_file).st_mode & 0o777) == initial_perms |
General Comments 0
You need to be logged in to leave comments.
Login now