11from __future__ import annotations
22
33import concurrent .futures
4+ import ctypes
45import os
56import sys
67import traceback
78import zipfile
9+ from errno import EACCES
810from stat import S_IEXEC , S_IREAD , S_IRGRP , S_IRWXU , S_IWUSR
911from typing import TYPE_CHECKING , Final
1012
1113import pytest
1214
15+ from virtualenv import __version__
1316from virtualenv .app_data import (
1417 AppDataDiskFolder ,
1518 TempAppData ,
2326from virtualenv .util .path import copy , safe_delete , symlink
2427from virtualenv .util .subprocess import run_cmd
2528
29+ if os .name == "nt" :
30+ from ctypes import wintypes
31+
32+ from virtualenv .util .path ._win import clear_readonly
33+
2634if TYPE_CHECKING :
2735 from collections .abc import Callable
2836 from pathlib import Path
@@ -157,6 +165,74 @@ def test_copy_replaces_contents(tmp_path: Path, directory: bool) -> None:
157165 assert (destination / "entry" if directory else destination ).read_bytes () == b"new"
158166
159167
168+ @pytest .mark .skipif (sys .platform != "linux" , reason = "minimal Linux builds can omit ctypes" )
169+ def test_command_line_import_without_ctypes () -> None :
170+ result : Final = run_cmd ([
171+ sys .executable ,
172+ "-c" ,
173+ (
174+ "import runpy, sys; sys.modules['ctypes'] = None; sys.argv = ['virtualenv', '--version']; "
175+ "runpy.run_module('virtualenv', run_name='__main__')"
176+ ),
177+ ])
178+ assert (result [0 ], __version__ in result [1 ], result [2 ]) == (0 , True , "" )
179+
180+
181+ @pytest .mark .skipif (os .name != "nt" , reason = "Windows file attributes" )
182+ @pytest .mark .parametrize (
183+ "operation" ,
184+ [
185+ pytest .param ("CreateFileW" , id = "open" ),
186+ pytest .param ("GetFileInformationByHandleEx" , id = "read" ),
187+ pytest .param ("SetFileInformationByHandle" , id = "write" ),
188+ ],
189+ )
190+ def test_safe_delete_reports_attribute_errors (tmp_path : Path , mocker : MockerFixture , operation : str ) -> None :
191+ folder : Final = tmp_path / "folder"
192+ folder .mkdir ()
193+ source : Final = folder / "read-only"
194+ source .touch ()
195+ source .chmod (S_IREAD )
196+ kernel : Final = ctypes .WinDLL ("kernel32" , use_last_error = True )
197+ mocker .patch .object (
198+ kernel , operation , autospec = True , return_value = wintypes .HANDLE (- 1 ).value if operation == "CreateFileW" else 0
199+ )
200+ mocker .patch ("ctypes.WinDLL" , autospec = True , return_value = kernel )
201+ mocker .patch ("ctypes.get_last_error" , autospec = True , return_value = 5 )
202+ with pytest .raises (PermissionError , match = "WinError 5" ):
203+ safe_delete (folder )
204+ assert source .stat ().st_mode & S_IWUSR == 0
205+ source .chmod (S_IREAD | S_IWUSR )
206+
207+
208+ @pytest .mark .skipif (os .name != "nt" or not fs_supports_symlink (), reason = "Windows file links" )
209+ def test_clear_readonly_refuses_reparse_points (tmp_path : Path ) -> None :
210+ source : Final = tmp_path / "source"
211+ source .touch ()
212+ link : Final = tmp_path / "link"
213+ link .symlink_to (source )
214+ with pytest .raises (OSError , match = "refusing to change attributes" ):
215+ clear_readonly (str (link ))
216+
217+
218+ @pytest .mark .skipif (not fs_supports_symlink (), reason = "symlinks are unavailable" )
219+ def test_safe_delete_does_not_chmod_link_target (tmp_path : Path , mocker : MockerFixture ) -> None :
220+ source : Final = tmp_path / "read-only"
221+ source .write_text ("unchanged" , encoding = "utf-8" )
222+ source .chmod (S_IREAD )
223+ mode : Final = source .stat ().st_mode
224+ folder : Final = tmp_path / "folder"
225+ folder .mkdir ()
226+ (folder / "link" ).symlink_to (source )
227+ unlink : Final = mocker .patch ("os.unlink" , autospec = True , side_effect = PermissionError (EACCES , "entry is busy" ))
228+ try :
229+ with pytest .raises (PermissionError , match = "entry is busy" ):
230+ safe_delete (folder )
231+ finally :
232+ mocker .stop (unlink )
233+ assert source .stat ().st_mode == mode
234+
235+
160236def test_run_fail (tmp_path ) -> None :
161237 code , out , err = run_cmd ([str (tmp_path )])
162238 assert err
@@ -211,7 +287,7 @@ def test_safe_delete_raises_the_original_error_not_a_retry_failure(tmp_path: Pat
211287
212288
213289@pytest .mark .skipif (sys .platform == "win32" , reason = "Windows deletion ignores directory permissions" )
214- def test_safe_delete_keeps_the_other_mode_bits_when_clearing_read_only (tmp_path : Path ) -> None :
290+ def test_safe_delete_preserves_file_mode_when_parent_denies_deletion (tmp_path : Path ) -> None :
215291 if hasattr (os , "geteuid" ) and os .geteuid () == 0 :
216292 pytest .skip ("root bypasses the permission bits that make the delete fail" )
217293 target = tmp_path / "target"
@@ -224,7 +300,7 @@ def test_safe_delete_keeps_the_other_mode_bits_when_clearing_read_only(tmp_path:
224300 try :
225301 with pytest .raises (PermissionError ):
226302 safe_delete (target )
227- assert blocked .stat ().st_mode & 0o777 == S_IREAD | S_IRGRP | S_IWUSR
303+ assert blocked .stat ().st_mode & 0o777 == S_IREAD | S_IRGRP
228304 finally :
229305 target .chmod (S_IRWXU )
230306
0 commit comments