test_launcher.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. import contextlib
  2. import itertools
  3. import os
  4. import re
  5. import shutil
  6. import subprocess
  7. import sys
  8. import sysconfig
  9. import tempfile
  10. import textwrap
  11. import unittest
  12. from pathlib import Path
  13. from test import support
  14. if sys.platform != "win32":
  15. raise unittest.SkipTest("test only applies to Windows")
  16. # Get winreg after the platform check
  17. import winreg
  18. PY_EXE = "py.exe"
  19. if sys.executable.casefold().endswith("_d.exe".casefold()):
  20. PY_EXE = "py_d.exe"
  21. # Registry data to create. On removal, everything beneath top-level names will
  22. # be deleted.
  23. TEST_DATA = {
  24. "PythonTestSuite": {
  25. "DisplayName": "Python Test Suite",
  26. "SupportUrl": "https://www.python.org/",
  27. "3.100": {
  28. "DisplayName": "X.Y version",
  29. "InstallPath": {
  30. None: sys.prefix,
  31. "ExecutablePath": "X.Y.exe",
  32. }
  33. },
  34. "3.100-32": {
  35. "DisplayName": "X.Y-32 version",
  36. "InstallPath": {
  37. None: sys.prefix,
  38. "ExecutablePath": "X.Y-32.exe",
  39. }
  40. },
  41. "3.100-arm64": {
  42. "DisplayName": "X.Y-arm64 version",
  43. "InstallPath": {
  44. None: sys.prefix,
  45. "ExecutablePath": "X.Y-arm64.exe",
  46. "ExecutableArguments": "-X fake_arg_for_test",
  47. }
  48. },
  49. "ignored": {
  50. "DisplayName": "Ignored because no ExecutablePath",
  51. "InstallPath": {
  52. None: sys.prefix,
  53. }
  54. },
  55. },
  56. "PythonTestSuite1": {
  57. "DisplayName": "Python Test Suite Single",
  58. "3.100": {
  59. "DisplayName": "Single Interpreter",
  60. "InstallPath": {
  61. None: sys.prefix,
  62. "ExecutablePath": sys.executable,
  63. }
  64. }
  65. },
  66. }
  67. TEST_PY_ENV = dict(
  68. PY_PYTHON="PythonTestSuite/3.100",
  69. PY_PYTHON2="PythonTestSuite/3.100-32",
  70. PY_PYTHON3="PythonTestSuite/3.100-arm64",
  71. )
  72. TEST_PY_DEFAULTS = "\n".join([
  73. "[defaults]",
  74. *[f"{k[3:].lower()}={v}" for k, v in TEST_PY_ENV.items()],
  75. ])
  76. TEST_PY_COMMANDS = "\n".join([
  77. "[commands]",
  78. "test-command=TEST_EXE.exe",
  79. ])
  80. def create_registry_data(root, data):
  81. def _create_registry_data(root, key, value):
  82. if isinstance(value, dict):
  83. # For a dict, we recursively create keys
  84. with winreg.CreateKeyEx(root, key) as hkey:
  85. for k, v in value.items():
  86. _create_registry_data(hkey, k, v)
  87. elif isinstance(value, str):
  88. # For strings, we set values. 'key' may be None in this case
  89. winreg.SetValueEx(root, key, None, winreg.REG_SZ, value)
  90. else:
  91. raise TypeError("don't know how to create data for '{}'".format(value))
  92. for k, v in data.items():
  93. _create_registry_data(root, k, v)
  94. def enum_keys(root):
  95. for i in itertools.count():
  96. try:
  97. yield winreg.EnumKey(root, i)
  98. except OSError as ex:
  99. if ex.winerror == 259:
  100. break
  101. raise
  102. def delete_registry_data(root, keys):
  103. ACCESS = winreg.KEY_WRITE | winreg.KEY_ENUMERATE_SUB_KEYS
  104. for key in list(keys):
  105. with winreg.OpenKey(root, key, access=ACCESS) as hkey:
  106. delete_registry_data(hkey, enum_keys(hkey))
  107. winreg.DeleteKey(root, key)
  108. def is_installed(tag):
  109. key = rf"Software\Python\PythonCore\{tag}\InstallPath"
  110. for root, flag in [
  111. (winreg.HKEY_CURRENT_USER, 0),
  112. (winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_64KEY),
  113. (winreg.HKEY_LOCAL_MACHINE, winreg.KEY_WOW64_32KEY),
  114. ]:
  115. try:
  116. winreg.CloseKey(winreg.OpenKey(root, key, access=winreg.KEY_READ | flag))
  117. return True
  118. except OSError:
  119. pass
  120. return False
  121. class PreservePyIni:
  122. def __init__(self, path, content):
  123. self.path = Path(path)
  124. self.content = content
  125. self._preserved = None
  126. def __enter__(self):
  127. try:
  128. self._preserved = self.path.read_bytes()
  129. except FileNotFoundError:
  130. self._preserved = None
  131. self.path.write_text(self.content, encoding="utf-16")
  132. def __exit__(self, *exc_info):
  133. if self._preserved is None:
  134. self.path.unlink()
  135. else:
  136. self.path.write_bytes(self._preserved)
  137. class RunPyMixin:
  138. py_exe = None
  139. @classmethod
  140. def find_py(cls):
  141. py_exe = None
  142. if sysconfig.is_python_build():
  143. py_exe = Path(sys.executable).parent / PY_EXE
  144. else:
  145. for p in os.getenv("PATH").split(";"):
  146. if p:
  147. py_exe = Path(p) / PY_EXE
  148. if py_exe.is_file():
  149. break
  150. else:
  151. py_exe = None
  152. # Test launch and check version, to exclude installs of older
  153. # releases when running outside of a source tree
  154. if py_exe:
  155. try:
  156. with subprocess.Popen(
  157. [py_exe, "-h"],
  158. stdin=subprocess.PIPE,
  159. stdout=subprocess.PIPE,
  160. stderr=subprocess.PIPE,
  161. encoding="ascii",
  162. errors="ignore",
  163. ) as p:
  164. p.stdin.close()
  165. version = next(p.stdout, "\n").splitlines()[0].rpartition(" ")[2]
  166. p.stdout.read()
  167. p.wait(10)
  168. if not sys.version.startswith(version):
  169. py_exe = None
  170. except OSError:
  171. py_exe = None
  172. if not py_exe:
  173. raise unittest.SkipTest(
  174. "cannot locate '{}' for test".format(PY_EXE)
  175. )
  176. return py_exe
  177. def get_py_exe(self):
  178. if not self.py_exe:
  179. self.py_exe = self.find_py()
  180. return self.py_exe
  181. def run_py(self, args, env=None, allow_fail=False, expect_returncode=0, argv=None):
  182. if not self.py_exe:
  183. self.py_exe = self.find_py()
  184. ignore = {"VIRTUAL_ENV", "PY_PYTHON", "PY_PYTHON2", "PY_PYTHON3"}
  185. env = {
  186. **{k.upper(): v for k, v in os.environ.items() if k.upper() not in ignore},
  187. "PYLAUNCHER_DEBUG": "1",
  188. "PYLAUNCHER_DRYRUN": "1",
  189. "PYLAUNCHER_LIMIT_TO_COMPANY": "",
  190. **{k.upper(): v for k, v in (env or {}).items()},
  191. }
  192. if not argv:
  193. argv = [self.py_exe, *args]
  194. with subprocess.Popen(
  195. argv,
  196. env=env,
  197. executable=self.py_exe,
  198. stdin=subprocess.PIPE,
  199. stdout=subprocess.PIPE,
  200. stderr=subprocess.PIPE,
  201. ) as p:
  202. p.stdin.close()
  203. p.wait(10)
  204. out = p.stdout.read().decode("utf-8", "replace")
  205. err = p.stderr.read().decode("ascii", "replace")
  206. if p.returncode != expect_returncode and support.verbose and not allow_fail:
  207. print("++ COMMAND ++")
  208. print([self.py_exe, *args])
  209. print("++ STDOUT ++")
  210. print(out)
  211. print("++ STDERR ++")
  212. print(err)
  213. if allow_fail and p.returncode != expect_returncode:
  214. raise subprocess.CalledProcessError(p.returncode, [self.py_exe, *args], out, err)
  215. else:
  216. self.assertEqual(expect_returncode, p.returncode)
  217. data = {
  218. s.partition(":")[0]: s.partition(":")[2].lstrip()
  219. for s in err.splitlines()
  220. if not s.startswith("#") and ":" in s
  221. }
  222. data["stdout"] = out
  223. data["stderr"] = err
  224. return data
  225. def py_ini(self, content):
  226. local_appdata = os.environ.get("LOCALAPPDATA")
  227. if not local_appdata:
  228. raise unittest.SkipTest("LOCALAPPDATA environment variable is "
  229. "missing or empty")
  230. return PreservePyIni(Path(local_appdata) / "py.ini", content)
  231. @contextlib.contextmanager
  232. def script(self, content, encoding="utf-8"):
  233. file = Path(tempfile.mktemp(dir=os.getcwd()) + ".py")
  234. file.write_text(content, encoding=encoding)
  235. try:
  236. yield file
  237. finally:
  238. file.unlink()
  239. @contextlib.contextmanager
  240. def fake_venv(self):
  241. venv = Path.cwd() / "Scripts"
  242. venv.mkdir(exist_ok=True, parents=True)
  243. venv_exe = (venv / Path(sys.executable).name)
  244. venv_exe.touch()
  245. try:
  246. yield venv_exe, {"VIRTUAL_ENV": str(venv.parent)}
  247. finally:
  248. shutil.rmtree(venv)
  249. class TestLauncher(unittest.TestCase, RunPyMixin):
  250. @classmethod
  251. def setUpClass(cls):
  252. with winreg.CreateKey(winreg.HKEY_CURRENT_USER, rf"Software\Python") as key:
  253. create_registry_data(key, TEST_DATA)
  254. if support.verbose:
  255. p = subprocess.check_output("reg query HKCU\\Software\\Python /s")
  256. #print(p.decode('mbcs'))
  257. @classmethod
  258. def tearDownClass(cls):
  259. with winreg.OpenKey(winreg.HKEY_CURRENT_USER, rf"Software\Python", access=winreg.KEY_WRITE | winreg.KEY_ENUMERATE_SUB_KEYS) as key:
  260. delete_registry_data(key, TEST_DATA)
  261. def test_version(self):
  262. data = self.run_py(["-0"])
  263. self.assertEqual(self.py_exe, Path(data["argv0"]))
  264. self.assertEqual(sys.version.partition(" ")[0], data["version"])
  265. def test_help_option(self):
  266. data = self.run_py(["-h"])
  267. self.assertEqual("True", data["SearchInfo.help"])
  268. def test_list_option(self):
  269. for opt, v1, v2 in [
  270. ("-0", "True", "False"),
  271. ("-0p", "False", "True"),
  272. ("--list", "True", "False"),
  273. ("--list-paths", "False", "True"),
  274. ]:
  275. with self.subTest(opt):
  276. data = self.run_py([opt])
  277. self.assertEqual(v1, data["SearchInfo.list"])
  278. self.assertEqual(v2, data["SearchInfo.listPaths"])
  279. def test_list(self):
  280. data = self.run_py(["--list"])
  281. found = {}
  282. expect = {}
  283. for line in data["stdout"].splitlines():
  284. m = re.match(r"\s*(.+?)\s+?(\*\s+)?(.+)$", line)
  285. if m:
  286. found[m.group(1)] = m.group(3)
  287. for company in TEST_DATA:
  288. company_data = TEST_DATA[company]
  289. tags = [t for t in company_data if isinstance(company_data[t], dict)]
  290. for tag in tags:
  291. arg = f"-V:{company}/{tag}"
  292. expect[arg] = company_data[tag]["DisplayName"]
  293. expect.pop(f"-V:{company}/ignored", None)
  294. actual = {k: v for k, v in found.items() if k in expect}
  295. try:
  296. self.assertDictEqual(expect, actual)
  297. except:
  298. if support.verbose:
  299. print("*** STDOUT ***")
  300. print(data["stdout"])
  301. raise
  302. def test_list_paths(self):
  303. data = self.run_py(["--list-paths"])
  304. found = {}
  305. expect = {}
  306. for line in data["stdout"].splitlines():
  307. m = re.match(r"\s*(.+?)\s+?(\*\s+)?(.+)$", line)
  308. if m:
  309. found[m.group(1)] = m.group(3)
  310. for company in TEST_DATA:
  311. company_data = TEST_DATA[company]
  312. tags = [t for t in company_data if isinstance(company_data[t], dict)]
  313. for tag in tags:
  314. arg = f"-V:{company}/{tag}"
  315. install = company_data[tag]["InstallPath"]
  316. try:
  317. expect[arg] = install["ExecutablePath"]
  318. try:
  319. expect[arg] += " " + install["ExecutableArguments"]
  320. except KeyError:
  321. pass
  322. except KeyError:
  323. expect[arg] = str(Path(install[None]) / Path(sys.executable).name)
  324. expect.pop(f"-V:{company}/ignored", None)
  325. actual = {k: v for k, v in found.items() if k in expect}
  326. try:
  327. self.assertDictEqual(expect, actual)
  328. except:
  329. if support.verbose:
  330. print("*** STDOUT ***")
  331. print(data["stdout"])
  332. raise
  333. def test_filter_to_company(self):
  334. company = "PythonTestSuite"
  335. data = self.run_py([f"-V:{company}/"])
  336. self.assertEqual("X.Y.exe", data["LaunchCommand"])
  337. self.assertEqual(company, data["env.company"])
  338. self.assertEqual("3.100", data["env.tag"])
  339. def test_filter_to_company_with_default(self):
  340. company = "PythonTestSuite"
  341. data = self.run_py([f"-V:{company}/"], env=dict(PY_PYTHON="3.0"))
  342. self.assertEqual("X.Y.exe", data["LaunchCommand"])
  343. self.assertEqual(company, data["env.company"])
  344. self.assertEqual("3.100", data["env.tag"])
  345. def test_filter_to_tag(self):
  346. company = "PythonTestSuite"
  347. data = self.run_py([f"-V:3.100"])
  348. self.assertEqual("X.Y.exe", data["LaunchCommand"])
  349. self.assertEqual(company, data["env.company"])
  350. self.assertEqual("3.100", data["env.tag"])
  351. data = self.run_py([f"-V:3.100-32"])
  352. self.assertEqual("X.Y-32.exe", data["LaunchCommand"])
  353. self.assertEqual(company, data["env.company"])
  354. self.assertEqual("3.100-32", data["env.tag"])
  355. data = self.run_py([f"-V:3.100-arm64"])
  356. self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test", data["LaunchCommand"])
  357. self.assertEqual(company, data["env.company"])
  358. self.assertEqual("3.100-arm64", data["env.tag"])
  359. def test_filter_to_company_and_tag(self):
  360. company = "PythonTestSuite"
  361. data = self.run_py([f"-V:{company}/3.1"], expect_returncode=103)
  362. data = self.run_py([f"-V:{company}/3.100"])
  363. self.assertEqual("X.Y.exe", data["LaunchCommand"])
  364. self.assertEqual(company, data["env.company"])
  365. self.assertEqual("3.100", data["env.tag"])
  366. def test_filter_with_single_install(self):
  367. company = "PythonTestSuite1"
  368. data = self.run_py(
  369. [f"-V:Nonexistent"],
  370. env={"PYLAUNCHER_LIMIT_TO_COMPANY": company},
  371. expect_returncode=103,
  372. )
  373. def test_search_major_3(self):
  374. try:
  375. data = self.run_py(["-3"], allow_fail=True)
  376. except subprocess.CalledProcessError:
  377. raise unittest.SkipTest("requires at least one Python 3.x install")
  378. self.assertEqual("PythonCore", data["env.company"])
  379. self.assertTrue(data["env.tag"].startswith("3."), data["env.tag"])
  380. def test_search_major_3_32(self):
  381. try:
  382. data = self.run_py(["-3-32"], allow_fail=True)
  383. except subprocess.CalledProcessError:
  384. if not any(is_installed(f"3.{i}-32") for i in range(5, 11)):
  385. raise unittest.SkipTest("requires at least one 32-bit Python 3.x install")
  386. raise
  387. self.assertEqual("PythonCore", data["env.company"])
  388. self.assertTrue(data["env.tag"].startswith("3."), data["env.tag"])
  389. self.assertTrue(data["env.tag"].endswith("-32"), data["env.tag"])
  390. def test_search_major_2(self):
  391. try:
  392. data = self.run_py(["-2"], allow_fail=True)
  393. except subprocess.CalledProcessError:
  394. if not is_installed("2.7"):
  395. raise unittest.SkipTest("requires at least one Python 2.x install")
  396. self.assertEqual("PythonCore", data["env.company"])
  397. self.assertTrue(data["env.tag"].startswith("2."), data["env.tag"])
  398. def test_py_default(self):
  399. with self.py_ini(TEST_PY_DEFAULTS):
  400. data = self.run_py(["-arg"])
  401. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  402. self.assertEqual("3.100", data["SearchInfo.tag"])
  403. self.assertEqual("X.Y.exe -arg", data["stdout"].strip())
  404. def test_py2_default(self):
  405. with self.py_ini(TEST_PY_DEFAULTS):
  406. data = self.run_py(["-2", "-arg"])
  407. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  408. self.assertEqual("3.100-32", data["SearchInfo.tag"])
  409. self.assertEqual("X.Y-32.exe -arg", data["stdout"].strip())
  410. def test_py3_default(self):
  411. with self.py_ini(TEST_PY_DEFAULTS):
  412. data = self.run_py(["-3", "-arg"])
  413. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  414. self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
  415. self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test -arg", data["stdout"].strip())
  416. def test_py_default_env(self):
  417. data = self.run_py(["-arg"], env=TEST_PY_ENV)
  418. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  419. self.assertEqual("3.100", data["SearchInfo.tag"])
  420. self.assertEqual("X.Y.exe -arg", data["stdout"].strip())
  421. def test_py2_default_env(self):
  422. data = self.run_py(["-2", "-arg"], env=TEST_PY_ENV)
  423. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  424. self.assertEqual("3.100-32", data["SearchInfo.tag"])
  425. self.assertEqual("X.Y-32.exe -arg", data["stdout"].strip())
  426. def test_py3_default_env(self):
  427. data = self.run_py(["-3", "-arg"], env=TEST_PY_ENV)
  428. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  429. self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
  430. self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test -arg", data["stdout"].strip())
  431. def test_py_default_short_argv0(self):
  432. with self.py_ini(TEST_PY_DEFAULTS):
  433. for argv0 in ['"py.exe"', 'py.exe', '"py"', 'py']:
  434. with self.subTest(argv0):
  435. data = self.run_py(["--version"], argv=f'{argv0} --version')
  436. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  437. self.assertEqual("3.100", data["SearchInfo.tag"])
  438. self.assertEqual(f'X.Y.exe --version', data["stdout"].strip())
  439. def test_py_default_in_list(self):
  440. data = self.run_py(["-0"], env=TEST_PY_ENV)
  441. default = None
  442. for line in data["stdout"].splitlines():
  443. m = re.match(r"\s*-V:(.+?)\s+?\*\s+(.+)$", line)
  444. if m:
  445. default = m.group(1)
  446. break
  447. self.assertEqual("PythonTestSuite/3.100", default)
  448. def test_virtualenv_in_list(self):
  449. with self.fake_venv() as (venv_exe, env):
  450. data = self.run_py(["-0p"], env=env)
  451. for line in data["stdout"].splitlines():
  452. m = re.match(r"\s*\*\s+(.+)$", line)
  453. if m:
  454. self.assertEqual(str(venv_exe), m.group(1))
  455. break
  456. else:
  457. self.fail("did not find active venv path")
  458. data = self.run_py(["-0"], env=env)
  459. for line in data["stdout"].splitlines():
  460. m = re.match(r"\s*\*\s+(.+)$", line)
  461. if m:
  462. self.assertEqual("Active venv", m.group(1))
  463. break
  464. else:
  465. self.fail("did not find active venv entry")
  466. def test_virtualenv_with_env(self):
  467. with self.fake_venv() as (venv_exe, env):
  468. data1 = self.run_py([], env={**env, "PY_PYTHON": "PythonTestSuite/3"})
  469. data2 = self.run_py(["-V:PythonTestSuite/3"], env={**env, "PY_PYTHON": "PythonTestSuite/3"})
  470. # Compare stdout, because stderr goes via ascii
  471. self.assertEqual(data1["stdout"].strip(), str(venv_exe))
  472. self.assertEqual(data1["SearchInfo.lowPriorityTag"], "True")
  473. # Ensure passing the argument doesn't trigger the same behaviour
  474. self.assertNotEqual(data2["stdout"].strip(), str(venv_exe))
  475. self.assertNotEqual(data2["SearchInfo.lowPriorityTag"], "True")
  476. def test_py_shebang(self):
  477. with self.py_ini(TEST_PY_DEFAULTS):
  478. with self.script("#! /usr/bin/python -prearg") as script:
  479. data = self.run_py([script, "-postarg"])
  480. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  481. self.assertEqual("3.100", data["SearchInfo.tag"])
  482. self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
  483. def test_python_shebang(self):
  484. with self.py_ini(TEST_PY_DEFAULTS):
  485. with self.script("#! python -prearg") as script:
  486. data = self.run_py([script, "-postarg"])
  487. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  488. self.assertEqual("3.100", data["SearchInfo.tag"])
  489. self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
  490. def test_py2_shebang(self):
  491. with self.py_ini(TEST_PY_DEFAULTS):
  492. with self.script("#! /usr/bin/python2 -prearg") as script:
  493. data = self.run_py([script, "-postarg"])
  494. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  495. self.assertEqual("3.100-32", data["SearchInfo.tag"])
  496. self.assertEqual(f"X.Y-32.exe -prearg {script} -postarg", data["stdout"].strip())
  497. def test_py3_shebang(self):
  498. with self.py_ini(TEST_PY_DEFAULTS):
  499. with self.script("#! /usr/bin/python3 -prearg") as script:
  500. data = self.run_py([script, "-postarg"])
  501. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  502. self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
  503. self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
  504. def test_py_shebang_nl(self):
  505. with self.py_ini(TEST_PY_DEFAULTS):
  506. with self.script("#! /usr/bin/python -prearg\n") as script:
  507. data = self.run_py([script, "-postarg"])
  508. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  509. self.assertEqual("3.100", data["SearchInfo.tag"])
  510. self.assertEqual(f"X.Y.exe -prearg {script} -postarg", data["stdout"].strip())
  511. def test_py2_shebang_nl(self):
  512. with self.py_ini(TEST_PY_DEFAULTS):
  513. with self.script("#! /usr/bin/python2 -prearg\n") as script:
  514. data = self.run_py([script, "-postarg"])
  515. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  516. self.assertEqual("3.100-32", data["SearchInfo.tag"])
  517. self.assertEqual(f"X.Y-32.exe -prearg {script} -postarg", data["stdout"].strip())
  518. def test_py3_shebang_nl(self):
  519. with self.py_ini(TEST_PY_DEFAULTS):
  520. with self.script("#! /usr/bin/python3 -prearg\n") as script:
  521. data = self.run_py([script, "-postarg"])
  522. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  523. self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
  524. self.assertEqual(f"X.Y-arm64.exe -X fake_arg_for_test -prearg {script} -postarg", data["stdout"].strip())
  525. def test_py_shebang_short_argv0(self):
  526. with self.py_ini(TEST_PY_DEFAULTS):
  527. with self.script("#! /usr/bin/python -prearg") as script:
  528. # Override argv to only pass "py.exe" as the command
  529. data = self.run_py([script, "-postarg"], argv=f'"py.exe" "{script}" -postarg')
  530. self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
  531. self.assertEqual("3.100", data["SearchInfo.tag"])
  532. self.assertEqual(f'X.Y.exe -prearg "{script}" -postarg', data["stdout"].strip())
  533. def test_py_handle_64_in_ini(self):
  534. with self.py_ini("\n".join(["[defaults]", "python=3.999-64"])):
  535. # Expect this to fail, but should get oldStyleTag flipped on
  536. data = self.run_py([], allow_fail=True, expect_returncode=103)
  537. self.assertEqual("3.999-64", data["SearchInfo.tag"])
  538. self.assertEqual("True", data["SearchInfo.oldStyleTag"])
  539. def test_search_path(self):
  540. stem = Path(sys.executable).stem
  541. with self.py_ini(TEST_PY_DEFAULTS):
  542. with self.script(f"#! /usr/bin/env {stem} -prearg") as script:
  543. data = self.run_py(
  544. [script, "-postarg"],
  545. env={"PATH": f"{Path(sys.executable).parent};{os.getenv('PATH')}"},
  546. )
  547. self.assertEqual(f"{sys.executable} -prearg {script} -postarg", data["stdout"].strip())
  548. def test_search_path_exe(self):
  549. # Leave the .exe on the name to ensure we don't add it a second time
  550. name = Path(sys.executable).name
  551. with self.py_ini(TEST_PY_DEFAULTS):
  552. with self.script(f"#! /usr/bin/env {name} -prearg") as script:
  553. data = self.run_py(
  554. [script, "-postarg"],
  555. env={"PATH": f"{Path(sys.executable).parent};{os.getenv('PATH')}"},
  556. )
  557. self.assertEqual(f"{sys.executable} -prearg {script} -postarg", data["stdout"].strip())
  558. def test_recursive_search_path(self):
  559. stem = self.get_py_exe().stem
  560. with self.py_ini(TEST_PY_DEFAULTS):
  561. with self.script(f"#! /usr/bin/env {stem}") as script:
  562. data = self.run_py(
  563. [script],
  564. env={"PATH": f"{self.get_py_exe().parent};{os.getenv('PATH')}"},
  565. )
  566. # The recursive search is ignored and we get normal "py" behavior
  567. self.assertEqual(f"X.Y.exe {script}", data["stdout"].strip())
  568. def test_install(self):
  569. data = self.run_py(["-V:3.10"], env={"PYLAUNCHER_ALWAYS_INSTALL": "1"}, expect_returncode=111)
  570. cmd = data["stdout"].strip()
  571. # If winget is runnable, we should find it. Otherwise, we'll be trying
  572. # to open the Store.
  573. try:
  574. subprocess.check_call(["winget.exe", "--version"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  575. except FileNotFoundError:
  576. self.assertIn("ms-windows-store://", cmd)
  577. else:
  578. self.assertIn("winget.exe", cmd)
  579. # Both command lines include the store ID
  580. self.assertIn("9PJPW5LDXLZ5", cmd)
  581. def test_literal_shebang_absolute(self):
  582. with self.script(f"#! C:/some_random_app -witharg") as script:
  583. data = self.run_py([script])
  584. self.assertEqual(
  585. f"C:\\some_random_app -witharg {script}",
  586. data["stdout"].strip(),
  587. )
  588. def test_literal_shebang_relative(self):
  589. with self.script(f"#! ..\\some_random_app -witharg") as script:
  590. data = self.run_py([script])
  591. self.assertEqual(
  592. f"{script.parent.parent}\\some_random_app -witharg {script}",
  593. data["stdout"].strip(),
  594. )
  595. def test_literal_shebang_quoted(self):
  596. with self.script(f'#! "some random app" -witharg') as script:
  597. data = self.run_py([script])
  598. self.assertEqual(
  599. f'"{script.parent}\\some random app" -witharg {script}',
  600. data["stdout"].strip(),
  601. )
  602. with self.script(f'#! some" random "app -witharg') as script:
  603. data = self.run_py([script])
  604. self.assertEqual(
  605. f'"{script.parent}\\some random app" -witharg {script}',
  606. data["stdout"].strip(),
  607. )
  608. def test_literal_shebang_quoted_escape(self):
  609. with self.script(f'#! some\\" random "app -witharg') as script:
  610. data = self.run_py([script])
  611. self.assertEqual(
  612. f'"{script.parent}\\some\\ random app" -witharg {script}',
  613. data["stdout"].strip(),
  614. )
  615. def test_literal_shebang_command(self):
  616. with self.py_ini(TEST_PY_COMMANDS):
  617. with self.script('#! test-command arg1') as script:
  618. data = self.run_py([script])
  619. self.assertEqual(
  620. f"TEST_EXE.exe arg1 {script}",
  621. data["stdout"].strip(),
  622. )
  623. def test_literal_shebang_invalid_template(self):
  624. with self.script('#! /usr/bin/not-python arg1') as script:
  625. data = self.run_py([script])
  626. expect = script.parent / "/usr/bin/not-python"
  627. self.assertEqual(
  628. f"{expect} arg1 {script}",
  629. data["stdout"].strip(),
  630. )