3131from virtualenv .create .pyenv_cfg import PyEnvCfg
3232from virtualenv .create .via_global_ref import api
3333from virtualenv .create .via_global_ref .builtin .cpython .common import is_mac_os_framework , is_macos_brew
34- from virtualenv .info import IS_PYPY , IS_WIN , fs_is_case_sensitive , fs_supports_symlink
34+ from virtualenv .info import IS_GRAALPY , IS_PYPY , IS_WIN , fs_is_case_sensitive , fs_supports_symlink
3535from virtualenv .run import cli_run , session_via_cli
3636from virtualenv .run .plugin .creators import CreatorSelector
3737from virtualenv .util .error import ProcessCallFailedError
@@ -118,6 +118,94 @@ def test_clear_direct_destination(tmp_path: Path, parent_link: bool) -> None:
118118 assert (session .creator .dest , marker .exists (), (target / "pyvenv.cfg" ).is_file ()) == (target .resolve (), False , True )
119119
120120
121+ @pytest .mark .parametrize ("creator_name" , [pytest .param ("builtin" , id = "builtin" ), pytest .param ("venv" , id = "venv" )])
122+ @pytest .mark .parametrize ("existing" , [pytest .param (False , id = "dangling" ), pytest .param (True , id = "existing" )])
123+ @pytest .mark .parametrize (
124+ "generated" ,
125+ [
126+ pytest .param (name , id = name )
127+ for name in ("pyvenv.cfg" , "activate" , "pip" , "CACHEDIR.TAG" , ".gitignore" , "_virtualenv.py" , "_virtualenv.pth" )
128+ ],
129+ )
130+ def test_creator_replaces_generated_file_links (
131+ tmp_path : Path ,
132+ creator_name : str ,
133+ existing : bool ,
134+ generated : str ,
135+ ) -> None :
136+ command : Final = [
137+ str (tmp_path / "env" ),
138+ "--creator" ,
139+ creator_name ,
140+ "--pip" ,
141+ "embed" ,
142+ "--no-periodic-update" ,
143+ ]
144+ if generated .startswith ("_virtualenv" ):
145+ if (python39 := shutil .which ("python3.9" )) is None :
146+ pytest .skip ("Python 3.9 is unavailable" )
147+ command .extend (["--python" , python39 ])
148+ session : Final = session_via_cli (command )
149+ session .creator .bin_dir .mkdir (parents = True )
150+ target : Final = session .creator .dest / "original"
151+ if existing :
152+ target .write_bytes (b"original" )
153+ path = (
154+ session .creator .bin_dir / ("pip.exe" if generated == "pip" and os .name == "nt" else generated )
155+ if generated in {"activate" , "pip" }
156+ else session .creator .dest / generated
157+ )
158+ if generated .startswith ("_virtualenv" ):
159+ path = session .creator .purelib / generated
160+ path .parent .mkdir (parents = True , exist_ok = True )
161+ path .symlink_to (target )
162+ with session :
163+ session .run ()
164+ if existing and (generated == "CACHEDIR.TAG" or (generated == ".gitignore" and creator_name == "builtin" )):
165+ assert path .is_symlink ()
166+ else :
167+ assert not path .is_symlink ()
168+ assert (target .read_bytes () if existing else target .exists ()) == (b"original" if existing else False )
169+
170+
171+ @pytest .mark .parametrize (
172+ "kind" ,
173+ [
174+ pytest .param ("file" , id = "file" ),
175+ pytest .param ("symlink" , id = "symlink" ),
176+ pytest .param ("dangling" , id = "dangling" ),
177+ pytest .param ("junction" , id = "junction" ),
178+ ],
179+ )
180+ def test_venv_prepares_real_executable_directory (tmp_path : Path , kind : str ) -> None :
181+ session : Final = session_via_cli ([str (tmp_path / "env" ), "--creator" , "venv" , "--no-seed" , "--activators=" ])
182+ session .creator .dest .mkdir ()
183+ if kind == "file" :
184+ session .creator .bin_dir .write_bytes (b"file" )
185+ else :
186+ target : Final = session .creator .dest / "empty"
187+ if kind != "dangling" :
188+ target .mkdir ()
189+ if kind == "junction" :
190+ if not IS_WIN :
191+ pytest .skip ("requires Windows junctions" )
192+ subprocess .run (
193+ ["cmd" , "/c" , "mklink" , "/J" , str (session .creator .bin_dir ), str (target )],
194+ capture_output = True ,
195+ check = True ,
196+ )
197+ else :
198+ if not fs_supports_symlink ():
199+ pytest .skip ("requires directory symlinks" )
200+ session .creator .bin_dir .symlink_to (target , target_is_directory = True )
201+ if kind == "file" :
202+ with pytest .raises (NotADirectoryError , match = "environment directory is not a real directory" ):
203+ session .run ()
204+ else :
205+ session .run ()
206+ assert session .creator .bin_dir .resolve () == session .creator .bin_dir
207+
208+
121209def test_os_path_sep_not_allowed (tmp_path , capsys ) -> None :
122210 target = str (tmp_path / f"a{ os .pathsep } b" )
123211 err = _non_success_exit_code (capsys , target )
@@ -462,6 +550,21 @@ def test_venv_configuration_uses_selected_options(tmp_path: Path, global_package
462550 assert {key : session .creator .pyenv_cfg .content .get (key ) for key in expected } == expected
463551
464552
553+ @pytest .mark .skipif (not CURRENT .has_venv or not fs_supports_symlink (), reason = "requires venv and symlinks" )
554+ @pytest .mark .skipif (
555+ is_mac_os_framework (CURRENT ) or is_macos_brew (CURRENT ) or (IS_GRAALPY and not IS_WIN ),
556+ reason = "stdlib creator requires symlinks on this runtime" ,
557+ )
558+ def test_venv_recreate_with_copied_interpreter (tmp_path : Path ) -> None :
559+ args : Final = [str (tmp_path / "env" ), "--creator" , "venv" , "--no-seed" ]
560+ cli_run ([* args , "--symlinks" ])
561+ session : Final = cli_run ([* args , "--copies" ])
562+ assert (session .creator .exe .is_file (), session .creator .exe .resolve ().parent ) == (
563+ True ,
564+ session .creator .bin_dir .resolve (),
565+ )
566+
567+
465568@pytest .mark .skipif (not CURRENT .has_venv , reason = "requires interpreter with venv" )
466569def test_venv_subprocess_skips_local_venv_module (tmp_path : Path , monkeypatch : pytest .MonkeyPatch ) -> None :
467570 (tmp_path / "venv" ).mkdir ()
@@ -479,6 +582,28 @@ def test_venv_subprocess_skips_local_venv_module(tmp_path: Path, monkeypatch: py
479582 assert (session .creator .dest / "pyvenv.cfg" ).is_file ()
480583
481584
585+ @pytest .mark .skipif (not CURRENT .has_venv , reason = "requires interpreter with venv" )
586+ @pytest .mark .parametrize ("mode" , [pytest .param ("--copies" , id = "copies" ), pytest .param ("--symlinks" , id = "symlinks" )])
587+ def test_venv_replaces_hard_linked_files (tmp_path : Path , mode : str ) -> None :
588+ if mode == "--symlinks" and not fs_supports_symlink ():
589+ pytest .skip ("requires symlinks" )
590+ if mode == "--copies" and (is_mac_os_framework (CURRENT ) or is_macos_brew (CURRENT ) or (IS_GRAALPY and not IS_WIN )):
591+ pytest .skip ("stdlib creator requires symlinks on this runtime" )
592+ session : Final = session_via_cli ([str (tmp_path / "env" ), "--creator" , "venv" , "--no-seed" , "--activators=" , mode ])
593+ session .creator .bin_dir .mkdir (parents = True )
594+ names : Final = (
595+ ("python.exe" , "pythonw.exe" ) if IS_WIN else ("python" , "python3" , f"python3.{ CURRENT .version_info .minor } " )
596+ )
597+ planted : Final = [session .creator .dest / "pyvenv.cfg" , * (session .creator .bin_dir / name for name in names )]
598+ for path in planted :
599+ (tmp_path / f"{ path .name } .original" ).write_bytes (b"original" )
600+ os .link (tmp_path / f"{ path .name } .original" , path )
601+ session .run ()
602+ assert [((tmp_path / f"{ path .name } .original" ).read_bytes (), path .lstat ().st_nlink ) for path in planted ] == [
603+ (b"original" , 1 )
604+ ] * len (planted )
605+
606+
482607@pytest .mark .skipif (not CURRENT .has_venv , reason = "requires interpreter with venv" )
483608def test_venv_fails_not_inline (tmp_path , capsys , mocker ) -> None :
484609 if hasattr (os , "geteuid" ) and os .geteuid () == 0 :
0 commit comments