diff --git a/tests/test_windows_launcher.py b/tests/test_windows_launcher.py new file mode 100644 index 0000000..c68800e --- /dev/null +++ b/tests/test_windows_launcher.py @@ -0,0 +1,65 @@ +"""`~/.tokeneconomizer/bin/te` on Windows (#167). Written in text mode there, the launcher's lines +ended in CRLF and `sh` handed every command a stray `\\r` argument, and the check that skips an +unchanged launcher read it back in text mode, so it was never repaired. On Windows the launcher +also ran `scripts/te` by a backslash path, which the shell omp runs commands in cannot `exec`.""" +import os +import shlex +import subprocess +import sys + +import pytest + +from tokeneconomizer.plugin import paths +from tokeneconomizer.plugin.paths import ensure_launcher +from tests.conftest import git_sh +from tests.test_live_hooks import repo # noqa: F401 + +PLUGIN = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "plugin") +CORE = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(paths.__file__)))) +HEAD = ("#!/bin/sh\n# Written by the TokenEconomizer plugin at session start: its `te` " + "command line.\n") + + +def _env(tmp_path): + return {"TOKENECONOMIZER_HOME": str(tmp_path / "home"), "TE_PLUGIN_ROOT": PLUGIN} + + +def test_a_launcher_left_with_crlf_endings_is_rewritten_with_lf(tmp_path): + env = _env(tmp_path) + path = ensure_launcher(env) + with open(path, "rb") as fh: + lf = fh.read() + assert b"\r" not in lf + with open(path, "wb") as fh: + fh.write(lf.replace(b"\n", b"\r\n")) + ensure_launcher(env) + with open(path, "rb") as fh: + assert fh.read() == lf + + +@pytest.mark.parametrize("windows", [False, True]) +def test_the_launcher_execs_scripts_te_on_posix_and_the_hooks_python_on_windows( + tmp_path, monkeypatch, windows): + monkeypatch.setattr(paths, "_WINDOWS", windows) + monkeypatch.setattr(sys, "executable", r"C:\Program Files\Python313\python.exe") + if windows: + core, bootstrap = (shlex.quote(p.replace("\\", "/")) for p in ( + CORE, os.path.join(PLUGIN, "scripts", "te_bootstrap.py"))) + expected = HEAD + ('[ -n "${TE_CORE_PATH:-}" ] || TE_CORE_PATH=%s\n' + "export TE_CORE_PATH PYTHONUTF8=1\n" + "exec 'C:/Program Files/Python313/python.exe' %s cli \"$@\"\n" + % (core, bootstrap)) + else: + expected = HEAD + 'exec %s "$@"\n' % shlex.quote(os.path.join(PLUGIN, "scripts", "te")) + with open(ensure_launcher(_env(tmp_path)), encoding="utf-8", newline="") as fh: + assert fh.read() == expected + + +def test_the_windows_launcher_runs_the_command_line(repo, tmp_path, monkeypatch): # noqa: F811 + monkeypatch.setattr(paths, "_WINDOWS", True) + env = _env(tmp_path) + run_env = dict(os.environ, TOKENECONOMIZER_HOME=env["TOKENECONOMIZER_HOME"], TE_CORE_PATH="", + PYTHONPATH="") + p = subprocess.run([git_sh() or "sh", ensure_launcher(env), "--cwd", repo, "config", "set", + "spiral=off"], env=run_env, capture_output=True, text=True) + assert p.returncode == 0, p.stderr diff --git a/tokeneconomizer/plugin/paths.py b/tokeneconomizer/plugin/paths.py index 5b0b8e4..7a541e5 100644 --- a/tokeneconomizer/plugin/paths.py +++ b/tokeneconomizer/plugin/paths.py @@ -23,6 +23,8 @@ HOME_ENV = "TOKENECONOMIZER_HOME" # Names the repository of a session whose directory holds no git metadata, by its remote URL. REPO_ENV = "TOKENECONOMIZER_REPO" +# A flag rather than `os.name` at the call, so the tests can write the Windows launcher anywhere. +_WINDOWS = os.name == "nt" def state_root(env=None): @@ -64,32 +66,45 @@ def ensure_launcher(env=None): location comes from the bootstrap (`TE_PLUGIN_ROOT`); each file is rewritten only when it changes, as an installed copy's path does across updates.""" import shlex + import sys env = os.environ if env is None else env plugin = env.get("TE_PLUGIN_ROOT") target = os.path.join(plugin, "scripts", "te") if plugin else "" if not target or not os.access(target, os.X_OK): return None path = launcher_path(env) - body = ("#!/bin/sh\n# Written by the TokenEconomizer plugin at session start: its `te` " - "command line.\nexec %s \"$@\"\n" % shlex.quote(target)) + head = ("#!/bin/sh\n# Written by the TokenEconomizer plugin at session start: its `te` " + "command line.\n") + body = head + "exec %s \"$@\"\n" % shlex.quote(target) launchers = {path: body} - if os.name == "nt": + if _WINDOWS: import json - import sys - launchers[path + ".json"] = json.dumps({ - "python": sys.executable, - "bootstrap": os.path.join(plugin, "scripts", "te_bootstrap.py"), - }) + "\n" + bootstrap = os.path.join(plugin, "scripts", "te_bootstrap.py") + launchers[path + ".json"] = json.dumps({"python": sys.executable, + "bootstrap": bootstrap}) + "\n" + if sys.executable: + # The shell omp runs commands in cannot `exec` a backslash path, and refuses the + # `cd` inside `$(...)` that `scripts/te` finds its own directory with (#167). So + # `te` runs what te.json names, with forward slashes: the Python the hook runs under + # (not one chosen again by TE_PYTHON at each run) and the package it imported. + core = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + launchers[path] = head + ('[ -n "${TE_CORE_PATH:-}" ] || TE_CORE_PATH=%s\n' + "export TE_CORE_PATH PYTHONUTF8=1\n" + 'exec %s %s cli "$@"\n') % tuple( + shlex.quote(p.replace("\\", "/")) for p in (core, sys.executable, bootstrap)) for launcher, body in launchers.items(): try: - with open(launcher, encoding="utf-8") as fh: + # newline="": a launcher an earlier version wrote with CRLF does not compare equal. + with open(launcher, encoding="utf-8", newline="") as fh: if fh.read() == body: continue except OSError: pass os.makedirs(os.path.dirname(launcher), exist_ok=True) tmp = "%s.%d.tmp" % (launcher, os.getpid()) - with open(tmp, "w", encoding="utf-8") as fh: + # newline="\n": text mode on Windows would end each line with CRLF, and `sh` would pass + # the trailing "\r" on as an argument. + with open(tmp, "w", encoding="utf-8", newline="\n") as fh: fh.write(body) os.chmod(tmp, 0o700) os.replace(tmp, launcher)