@@ -48,18 +48,23 @@ def __enter__(self):
4848 def __exit__ (self , * exc_info ):
4949 pass
5050
51- def check (self , cmd , tag , name , expect , windowed = 0 ):
51+ def check (self , cmd , tag , name , expect , windowed = 0 , script_code = None ):
5252 AU .create_alias (
5353 cmd ,
5454 {"tag" : tag },
5555 {"name" : name , "windowed" : windowed },
5656 self ._expect_target ,
57+ script_code = script_code ,
5758 )
5859 print (* cmd .global_dir .glob ("*" ), sep = "\n " )
5960 assert (cmd .global_dir / f"{ name } .exe" ).is_file ()
6061 assert (cmd .global_dir / f"{ name } .exe.__target__" ).is_file ()
6162 assert (cmd .global_dir / f"{ name } .exe" ).read_text () == expect
6263 assert (cmd .global_dir / f"{ name } .exe.__target__" ).read_text () == self ._expect_target
64+ if script_code :
65+ assert (cmd .global_dir / f"{ name } .exe.__script__.py" ).is_file ()
66+ assert (cmd .global_dir / f"{ name } .exe.__script__.py" ).read_text () == script_code
67+ assert name .casefold () in cmd .scratch ["aliasutils.create_alias.alias_written" ]
6368
6469 def check_32 (self , cmd , tag , name ):
6570 self .check (cmd , tag , name , self ._expect ["-32" ])
@@ -79,6 +84,10 @@ def check_arm64(self, cmd, tag, name):
7984 def check_warm64 (self , cmd , tag , name ):
8085 self .check (cmd , tag , name , self ._expect ["w-arm64" ], windowed = 1 )
8186
87+ def check_script (self , cmd , tag , name , windowed = 0 ):
88+ self .check (cmd , tag , name , self ._expect ["w-32" if windowed else "-32" ],
89+ windowed = windowed , script_code = secrets .token_hex (128 ))
90+
8291
8392def test_write_alias_tag_with_platform (alias_checker ):
8493 alias_checker .check_32 (alias_checker .Cmd (), "1.0-32" , "testA" )
@@ -103,6 +112,12 @@ def test_write_alias_fallback_platform(alias_checker):
103112 alias_checker .check_w64 (alias_checker .Cmd ("-spam" ), "1.0" , "testB" )
104113
105114
115+ def test_write_script_alias (alias_checker ):
116+ alias_checker .check_script (alias_checker .Cmd (), "1.0-32" , "testA" , windowed = 0 )
117+ alias_checker .check_script (alias_checker .Cmd (), "1.0-32" , "testB" , windowed = 1 )
118+ alias_checker .check_script (alias_checker .Cmd (), "1.0-32" , "testA" , windowed = 0 )
119+
120+
106121def test_write_alias_launcher_missing (fake_config , assert_log , tmp_path ):
107122 fake_config .launcher_exe = tmp_path / "non-existent.exe"
108123 fake_config .default_platform = '-32'
@@ -232,6 +247,38 @@ def test_parse_entrypoint_line():
232247 assert expect == AU ._parse_entrypoint_line (line )
233248
234249
250+ def test_scan_create_entrypoints (fake_config , tmp_path ):
251+ root = tmp_path / "test_install"
252+ site = root / "site-packages"
253+ A = site / "A.dist-info"
254+ A .mkdir (parents = True , exist_ok = True )
255+ (A / "entry_points.txt" ).write_text ("""[console_scripts]
256+ a = a:main
257+
258+ [gui_scripts]
259+ aw = a:main
260+ """ )
261+
262+ install = dict (prefix = root , id = "test" , alias = [dict (target = "target.exe" )])
263+
264+ created = []
265+ AU .scan_and_create_entrypoints (
266+ fake_config ,
267+ install ,
268+ dict (dirs = ["site-packages" ]),
269+ _create_alias = lambda * a , ** kw : created .append ((a , kw )),
270+ )
271+ assert 2 == len (created )
272+ for name , windowed , c in zip ("a aw" .split (), [0 , 1 ], created ):
273+ expect = dict (zip ("cmd install alias target" .split (), c [0 ])) | c [1 ]
274+ assert expect ["cmd" ] is fake_config
275+ assert expect ["install" ] is install
276+ assert expect ["alias" ]["name" ] == name
277+ assert expect ["alias" ]["windowed" ] == windowed
278+ assert expect ["target" ].match ("target.exe" )
279+ assert "from a import main" in expect ["script_code" ]
280+
281+
235282def test_scan_entrypoints (tmp_path ):
236283 site = tmp_path / "site"
237284 A = site / "a.dist-info"
@@ -243,8 +290,18 @@ def test_scan_entrypoints(tmp_path):
243290a_cmd = a:main
244291a2_cmd = a:main2 [spam]
245292
293+ [other] # shouldn't be included
294+ a3_cmd = a:main3
295+
246296[gui_scripts]
247297aw_cmd = a:main
298+ """ )
299+ (B / "entry_points.txt" ).write_bytes (b"""# Invalid file
300+
301+ \x80 \x81 \x82 \x83 \x84 \x85 \x86 \x87 \x88 \x89
302+
303+ [console_scripts]
304+ b_cmd = b:main
248305""" )
249306 actual = list (AU ._scan_one (site ))
250307 assert [a [0 ]["name" ] for a in actual ] == [
@@ -255,3 +312,24 @@ def test_scan_entrypoints(tmp_path):
255312 "(main())" , "(main2())" , "(main())"
256313 ]
257314
315+
316+ def test_cleanup_aliases (fake_config ):
317+ root = fake_config .global_dir
318+ root .mkdir (parents = True , exist_ok = True )
319+ (root / "alias1.exe" ).write_bytes (b"" )
320+ (root / "alias1.exe.__target__" ).write_bytes (b"" )
321+ (root / "alias1.exe.__script__.py" ).write_bytes (b"" )
322+ (root / "alias2.exe" ).write_bytes (b"" )
323+ (root / "alias2.exe.__target__" ).write_bytes (b"" )
324+ (root / "alias2.exe.__script__.py" ).write_bytes (b"" )
325+ (root / "alias3.exe" ).write_bytes (b"" )
326+ (root / "alias3.exe.__target__" ).write_bytes (b"" )
327+ fake_config .scratch ["aliasutils.create_alias.alias_written" ] = set ([
328+ "alias1" .casefold (),
329+ "alias3" .casefold (),
330+ ])
331+ AU .cleanup_alias (fake_config )
332+ assert set (f .name for f in root .glob ("*" )) == set ([
333+ "alias1.exe" , "alias1.exe.__target__" , "alias1.exe.__script__.py" ,
334+ "alias3.exe" , "alias3.exe.__target__" ,
335+ ])
0 commit comments