diff --git a/SPEC.md b/SPEC.md index 5dea3a5..bbaf191 100644 --- a/SPEC.md +++ b/SPEC.md @@ -1,9 +1,9 @@ --- name: Fiona-Assistant Specification -version: 1.18.1 +version: 1.19.0 title: Project Spec Sheet created: 2026-09-23 -updated: 2026-10-19 +updated: 2026-10-21 type: summary --- @@ -4329,6 +4329,16 @@ is the pip-argument source both scripts call. `--local` is used without `--no-env` and the script is not dot-sourced. The message does not call `--local` an unknown option. +- *7.21.7.* The bash script refuses to run when it is executed + instead of sourced. Executed, an activated environment is lost + when the child shell exits, so the script prints an error that + names the source command and exits non-zero before parsing + options or touching the system. A sourced run is unaffected. + - *acceptance*: `bash bootstrap` exits non-zero and prints + "must be sourced". `source bootstrap --not-a-flag` still + reports the unknown option and does not print the + source-guard message. `sh bootstrap` exits non-zero and + reports that bash is required. ## Appendix A. Non-Goals diff --git a/bootstrap b/bootstrap index 497fe32..79c6ec6 100755 --- a/bootstrap +++ b/bootstrap @@ -31,6 +31,24 @@ # ex. source bootstrap --pyenv -c --env my_env --python 3.10.6 # ex. source bootstrap --local --no-env +# ============================================= +# SOURCED GUARD +# ============================================= +# Activation only persists when this file is sourced; executing it +# loses the environment when the child shell exits. `return` works +# at the top level only inside a sourced script. +if [ -z "${BASH_VERSION:-}" ]; then + echo "ERROR: bootstrap is a bash script. Run it with bash:" >&2 + echo "Run: source bootstrap [--pyenv] [-c|--clean] [--env name] [--python 3.x.x] [--local] [--no-env]" >&2 + exit 1 +fi +if ! (return 0 2>/dev/null); then + echo "ERROR: bootstrap must be sourced, not executed." >&2 + echo "The environment would be lost when the child shell exits." >&2 + echo "Run: source bootstrap [--pyenv] [-c|--clean] [--env name] [--python 3.x.x] [--local] [--no-env]" >&2 + exit 1 +fi + # ============================================= # VARIABLES # ============================================= diff --git a/src/fiona/__main__.py b/src/fiona/__main__.py index 72bda2b..570301e 100644 --- a/src/fiona/__main__.py +++ b/src/fiona/__main__.py @@ -10,6 +10,7 @@ import sys from fiona.customlinter.fmlint import main as fmlint_main from fiona.customlinter.jsonlint import main as jsonlint_main +from fiona.gittool import diff_report from fiona.gittool.git_tool import main as git_tool_main from fiona.librarian.librarian import main as librarian_main from fiona.librarian.toc_generator import main as toc_generator_main @@ -406,8 +407,6 @@ def main(): sys.argv = ["git_tool", "status"] git_tool_main() elif args.git_subcommand == "diff": - from fiona.gittool import diff_report - diff_argv: list[str] = [] if args.base: diff_argv.extend(["--base", args.base]) diff --git a/src/fiona/gittool/diff_report.py b/src/fiona/gittool/diff_report.py index 9adf11a..e2b9382 100644 --- a/src/fiona/gittool/diff_report.py +++ b/src/fiona/gittool/diff_report.py @@ -235,7 +235,7 @@ def classify_diff_line(line: str) -> str: Returns: A class name used by the HTML template. """ - if line.startswith("+++") or line.startswith("---"): + if line.startswith(("+++", "---")): return "diff-meta" if line.startswith("@@"): return "diff-hunk" @@ -490,7 +490,7 @@ def commit_meta(repo: pathlib.Path, head: str) -> tuple[str, str, str]: Returns: Full SHA, subject line, and body (may be empty). """ - text = run_git(repo, "log", "-1", f"--format=%H%n%s%n%b", head).strip() + text = run_git(repo, "log", "-1", "--format=%H%n%s%n%b", head).strip() lines = text.split("\n", 2) commit_hash = lines[0] subject = lines[1] if len(lines) > 1 else "" @@ -553,8 +553,10 @@ def generate_report( header_lines = [ f"
{html.escape(subject)}
", - f"Commit {html.escape(commit_hash[:12])} "
- f"vs {html.escape(base_ref[:12] if len(base_ref) > 12 else base_ref)}
Commit {html.escape(commit_hash[:12])} "
+ f"vs {html.escape(base_ref[:12] if len(base_ref) > 12 else base_ref)}
{html.escape(body.strip())}
") @@ -649,7 +651,7 @@ def main(argv: list[str] | None = None) -> int: ) args = parser.parse_args(argv) - repo = pathlib.Path(".").resolve() + repo = pathlib.Path.cwd() if not (repo / ".git").exists(): print(f"Not a git repo: {repo}", file=sys.stderr) return 1 @@ -657,7 +659,7 @@ def main(argv: list[str] | None = None) -> int: wiki_root = wiki_root_for_repo(repo) head_ref = args.head try: - base_ref, head_ref = resolve_base_head(repo, args.base, args.head) + _base_ref, head_ref = resolve_base_head(repo, args.base, args.head) except RuntimeError as exc: print(str(exc), file=sys.stderr) return 1 diff --git a/src/fiona/gittool/git_tool.py b/src/fiona/gittool/git_tool.py index b25ff85..489a9e9 100644 --- a/src/fiona/gittool/git_tool.py +++ b/src/fiona/gittool/git_tool.py @@ -12,6 +12,7 @@ from datetime import UTC, datetime import git from git.repo.base import Repo +from fiona.gittool import diff_report from fiona.logging_config import setup_logging from fiona.wiki.ignore import ensure_wiki_gitignore @@ -403,8 +404,6 @@ def main(): # pragma: no cover status = git_tool.status() print(status) elif args.subcommand == "diff": - from fiona.gittool import diff_report - diff_argv: list[str] = [] if args.base: diff_argv.extend(["--base", args.base]) diff --git a/tests/test_bootstrap_install.py b/tests/test_bootstrap_install.py index d633473..1389db7 100644 --- a/tests/test_bootstrap_install.py +++ b/tests/test_bootstrap_install.py @@ -161,3 +161,62 @@ class TestBootstrapScripts: result = _powershell("--not-a-flag") combined = result.stdout + result.stderr assert "Unknown option" in combined + + +class TestBashSourcedGuard: + """SPEC 7.21.7: executed bootstrap refuses to run.""" + + def _sandbox_env(self, tmp_path: pathlib.Path) -> dict[str, str]: + """An environment with no python, pyenv, sudo, or curl.""" + empty_bin = tmp_path / "bin" + home = tmp_path / "home" + empty_bin.mkdir() + home.mkdir() + return {"PATH": str(empty_bin), "HOME": str(home)} + + def test_executed_bootstrap_exits_with_source_hint(self, tmp_path): + """Running 'bash bootstrap' exits non-zero and says to source it.""" + result = subprocess.run( + ["/bin/bash", str(ROOT / "bootstrap")], + capture_output=True, + text=True, + timeout=60, + check=False, + env=self._sandbox_env(tmp_path), + ) + combined = result.stdout + result.stderr + assert result.returncode != 0 + assert "must be sourced" in combined + assert "source bootstrap" in combined + + def test_sourced_bootstrap_is_not_blocked(self, tmp_path): + """Sourcing still reaches argument parsing; no false positive.""" + result = subprocess.run( + [ + "/bin/bash", + "-c", + f'source "{ROOT / "bootstrap"}" --not-a-flag', + ], + capture_output=True, + text=True, + timeout=60, + check=False, + env=self._sandbox_env(tmp_path), + ) + combined = result.stdout + result.stderr + assert "Unknown option: --not-a-flag" in combined + assert "must be sourced" not in combined + + def test_sh_run_reports_bash_requirement(self, tmp_path): + """Running with sh/dash exits non-zero with a bash hint.""" + result = subprocess.run( + ["/bin/sh", str(ROOT / "bootstrap")], + capture_output=True, + text=True, + timeout=60, + check=False, + env=self._sandbox_env(tmp_path), + ) + combined = result.stdout + result.stderr + assert result.returncode != 0 + assert "bash" in combined