Skip to content

Commit d47b3e5

Browse files
marcelsafinCopilot
andcommitted
fix(extensions/git): platform-aware persist hint and stronger SPECIFY_INIT_DIR test
- Add a shared _persist_hint() helper in create_new_feature_branch.py and use it for both the JSON-mode stderr hint and the human-readable stdout hint, so there is a single place emitting the SPECIFY_FEATURE persistence guidance. On Windows (os.name == "nt") it prints PowerShell $env:VAR = "..." syntax; elsewhere it keeps the existing POSIX export VAR=... syntax (parity with the bash twin). - Rework test_specify_init_dir_resolves_target_project so SPECIFY_INIT_DIR is the only thing that can produce the observed result: the script now runs from a separate host_proj (no existing specs, so script/cwd-based discovery would yield 001) while SPECIFY_INIT_DIR points at a different target_proj that already has an existing spec (007-existing, so the override must yield 008). The old version pointed SPECIFY_INIT_DIR at the same project the script was installed in, so it passed even if the env var were ignored. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f861459 commit d47b3e5

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

extensions/git/scripts/python/create_new_feature_branch.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ def _err(message: str) -> None:
6868
print(message, file=sys.stderr)
6969

7070

71+
def _persist_hint(var_name: str, value: str) -> str:
72+
"""Shell-appropriate guidance for persisting an env var in the caller's shell."""
73+
if os.name == "nt":
74+
return f'$env:{var_name} = "{value}"'
75+
return f"export {var_name}={shlex.quote(value)}"
76+
77+
7178
@dataclass
7279
class Args:
7380
json_mode: bool = False
@@ -598,7 +605,7 @@ def build_branch_name(feature_num: str, branch_suffix: str) -> str:
598605
f"creation for {branch_name}"
599606
)
600607

601-
_err(f"# To persist: export SPECIFY_FEATURE={shlex.quote(branch_name)}")
608+
_err(f"# To persist: {_persist_hint('SPECIFY_FEATURE', branch_name)}")
602609

603610
if args.json_mode:
604611
payload: dict[str, object] = {
@@ -613,8 +620,8 @@ def build_branch_name(feature_num: str, branch_suffix: str) -> str:
613620
print(f"FEATURE_NUM: {feature_num}")
614621
if not args.dry_run:
615622
print(
616-
"# To persist in your shell: export "
617-
f"SPECIFY_FEATURE={shlex.quote(branch_name)}"
623+
"# To persist in your shell: "
624+
f"{_persist_hint('SPECIFY_FEATURE', branch_name)}"
618625
)
619626

620627
return 0

tests/extensions/git/test_git_extension_python_parity.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,18 +295,26 @@ def test_empty_description_errors(self, tmp_path: Path):
295295
assert "cannot be empty or contain only whitespace" in p.stderr
296296

297297
def test_specify_init_dir_resolves_target_project(self, tmp_path: Path):
298-
_, py_proj = _twin_projects(tmp_path)
298+
# The script is installed under host_proj, so script_file-based
299+
# discovery (and cwd-based discovery, since we run from elsewhere)
300+
# would resolve host_proj, not target_proj. host_proj has no specs
301+
# (next number 001); target_proj already has 007-existing (next
302+
# number 008). Only honoring SPECIFY_INIT_DIR produces 008, so this
303+
# proves the env var -- not script location or cwd -- controls
304+
# resolution.
305+
host_proj = _setup_py_project(tmp_path / "host")
306+
target_proj = _setup_py_project(tmp_path / "target")
307+
(target_proj / "specs" / "007-existing").mkdir(parents=True)
299308
elsewhere = tmp_path / "elsewhere"
300309
elsewhere.mkdir()
301-
# Run from outside the project so SPECIFY_INIT_DIR is what resolves it.
302310
p = _run_py(
303-
"create-new-feature-branch", py_proj,
311+
"create-new-feature-branch", host_proj,
304312
"--json", "--dry-run", "init dir feature",
305-
env_extra={"SPECIFY_INIT_DIR": str(py_proj)},
313+
env_extra={"SPECIFY_INIT_DIR": str(target_proj)},
306314
run_cwd=elsewhere,
307315
)
308316
assert p.returncode == 0
309-
assert json.loads(p.stdout)["FEATURE_NUM"] == "001"
317+
assert json.loads(p.stdout)["FEATURE_NUM"] == "008"
310318

311319
def test_specify_init_dir_without_core_errors(self, tmp_path: Path):
312320
_, py_proj = _twin_projects(tmp_path)

0 commit comments

Comments
 (0)