feat: refuse to run bootstrap when not sourced
Bash bootstrap now exits non-zero when executed instead of sourced (and when run under a non-bash shell), pointing the user at the source command, per SPEC 7.21.7. Also clears every ruff and pycustomlint violation so run_sanity.py passes: five ruff findings in diff_report.py and the two inline diff_report imports moved to module top in __main__.py and git_tool.py.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
# =============================================
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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"<h1>{html.escape(page_title)}</h1>",
|
||||
f"<p><strong>{html.escape(subject)}</strong></p>",
|
||||
f"<p>Commit <code>{html.escape(commit_hash[:12])}</code> "
|
||||
f"vs <code>{html.escape(base_ref[:12] if len(base_ref) > 12 else base_ref)}</code></p>",
|
||||
(
|
||||
f"<p>Commit <code>{html.escape(commit_hash[:12])}</code> "
|
||||
f"vs <code>{html.escape(base_ref[:12] if len(base_ref) > 12 else base_ref)}</code></p>"
|
||||
),
|
||||
]
|
||||
if body.strip():
|
||||
header_lines.append(f"<p>{html.escape(body.strip())}</p>")
|
||||
@@ -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
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user