Skip to content

Commit f23ccf4

Browse files
mnriemCopilot
andcommitted
fix: select Python reliably for Unicode Bash feature names
Assisted-by: GitHub Copilot (model: GPT-6 Sol, autonomous) Co-authored-by: Copilot App <[email protected]>
1 parent f2c0f93 commit f23ccf4

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎scripts/bash/create-new-feature.sh‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,21 @@ unicode_words() {
171171
local name="$1"
172172
local separator="$2"
173173
if printf '%s' "$name" | LC_ALL=C grep -q '[^ -~]'; then
174-
local python_spec
175-
if ! python_spec=$(_python3_command); then
174+
local -a python_cmd=()
175+
local override="${SPECKIT_PYTHON_EXECUTABLE:-${SPECKIT_PYTHON:-}}"
176+
if [ -n "$override" ] && command -v "$override" >/dev/null 2>&1 &&
177+
"$override" -c 'import sys; raise SystemExit(sys.version_info.major != 3)' >/dev/null 2>&1; then
178+
python_cmd=("$override")
179+
else
180+
local python_line
181+
while IFS= read -r python_line; do
182+
python_cmd+=("$python_line")
183+
done < <(_python3_command)
184+
fi
185+
if [ "${#python_cmd[@]}" -eq 0 ]; then
176186
echo "Error: Python 3 is required to create a Unicode feature name" >&2
177187
return 1
178188
fi
179-
local -a python_cmd
180-
read -r -a python_cmd <<< "$python_spec"
181189
printf '%s' "$name" | "${python_cmd[@]}" -c '
182190
import sys
183191
value = sys.stdin.buffer.read().decode("utf-8")

‎tests/test_create_new_feature_python_parity.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
from __future__ import annotations
44

5+
import os
56
import re
67
import shlex
78
import shutil
89
import subprocess
10+
import sys
911
from pathlib import Path
1012

1113
import pytest
@@ -15,6 +17,7 @@
1517
from tests.conftest import requires_bash
1618
from tests.parity_helpers import (
1719
HAS_POWERSHELL,
20+
_bash_posix_path,
1821
bash_cmd,
1922
break_wrap_layer,
2023
clean_env,
@@ -23,6 +26,7 @@
2326
install_scripts,
2427
json_stdout,
2528
make_repo,
29+
make_yaml_less_venv,
2630
normalize_repo_paths,
2731
normalize_script_names,
2832
ps_cmd,
@@ -242,6 +246,56 @@ def test_bash_requires_python_only_for_unicode_names(
242246
assert "Error: Python 3 is required to create a Unicode feature name" in unicode_result.stderr
243247

244248

249+
@requires_bash
250+
def test_bash_unicode_uses_configured_python_without_pyyaml_in_spaced_path(
251+
repo: Path, tmp_path: Path
252+
) -> None:
253+
no_yaml_exe = make_yaml_less_venv(tmp_path / "tool env")
254+
shim_dir = tmp_path / "bin"
255+
shim_dir.mkdir()
256+
for name in ("python3", "python", "py"):
257+
shim = shim_dir / name
258+
shim.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8", newline="\n")
259+
shim.chmod(0o755)
260+
env = clean_env()
261+
env["SPECKIT_PYTHON_EXECUTABLE"] = _bash_posix_path(no_yaml_exe)
262+
env["PATH"] = f"{shim_dir}{os.pathsep}{env['PATH']}"
263+
264+
result = run(bash_cmd(repo, SCRIPT, "--json", "--dry-run", "添加用户"), repo, env)
265+
266+
assert result.returncode == 0, result.stderr
267+
assert json_stdout(result)["BRANCH_NAME"] == "001-添加用户"
268+
269+
270+
@requires_bash
271+
def test_bash_unicode_uses_py_launcher_with_separate_version_arg(
272+
repo: Path, tmp_path: Path
273+
) -> None:
274+
shim_dir = tmp_path / "bin"
275+
shim_dir.mkdir()
276+
for name in ("python3", "python"):
277+
shim = shim_dir / name
278+
shim.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8", newline="\n")
279+
shim.chmod(0o755)
280+
launcher = shim_dir / "py"
281+
launcher.write_text(
282+
"#!/bin/sh\n"
283+
'[ "$1" = "-3" ] || exit 1\n'
284+
"shift\n"
285+
f'exec {shlex.quote(_bash_posix_path(Path(sys.executable)))} "$@"\n',
286+
encoding="utf-8",
287+
newline="\n",
288+
)
289+
launcher.chmod(0o755)
290+
env = clean_env()
291+
env["PATH"] = f"{shim_dir}{os.pathsep}{env['PATH']}"
292+
293+
result = run(bash_cmd(repo, SCRIPT, "--json", "--dry-run", "添加用户"), repo, env)
294+
295+
assert result.returncode == 0, result.stderr
296+
assert json_stdout(result)["BRANCH_NAME"] == "001-添加用户"
297+
298+
245299
def _run_all_variants_allow_existing(
246300
repo: Path, *, number: str, short_name: str
247301
):

0 commit comments

Comments
 (0)