You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2026/10/16 19:57:23 UTC
(juneau) branch juneau-release-manager updated: Move push.py onto the shared juneau_scripts toolkit (R0035)
This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch juneau-release-manager
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/juneau-release-manager by this push:
new 5c4b36e6c4 Move push.py onto the shared juneau_scripts toolkit (R0035)
5c4b36e6c4 is described below
commit 5c4b36e6c4359665381d2f320008912faca73c58
Author: James Bognar <ja...@apache.org>
AuthorDate: Fri Oct 16 15:57:17 2026 -0400
Move push.py onto the shared juneau_scripts toolkit (R0035)
Co-Authored-By: Claude Opus 5.5 (1M context) <no...@anthropic.com>
---
push.py | 452 +++++++++++++++++++++------------------------
scripts/tests/README.md | 2 +-
scripts/tests/test_push.py | 150 +++++++++------
3 files changed, 303 insertions(+), 301 deletions(-)
diff --git a/push.py b/push.py
index b953800296..88f1fcfc20 100755
--- a/push.py
+++ b/push.py
@@ -25,11 +25,12 @@ Usage:
./push.py "commit message"
./push.py # prompts for a commit message
./push.py --dry-run "message" # show what would happen, change nothing
- ./push.py --console condensed "message" # accepted for parity with the Juneau scripts; no effect here
+ ./push.py --console condensed "message" # the steps as console rows; git output goes to the full log
- --console full|condensed|none sets JUNEAU_RUN_CONSOLE (the flag wins over a value already in the
- environment), exactly as scripts/test.py does in apache/juneau. This script has no Maven step and loads
- no juneau_run console view, so every choice behaves the same here (git output is always printed in full).
+ --console full|condensed|none sets JUNEAU_RUN_CONSOLE before juneau_run is loaded (the flag wins over a value
+ already in the environment), exactly as scripts/test.py does in apache/juneau. Only condensed (or RUN_MARKERS=1)
+ makes this script load juneau_run; full and none leave it unloaded, so both give the plain output. This script
+ has no Maven step, so the console view shows the identity, review, commit and push steps only.
When a run ends (success or failure) on a terminal, push.py rings the terminal bell.
Set JRM_PUSH_NO_BELL=1 to silence it (it is also skipped when SANDBOX_RUNTIME=1).
@@ -38,271 +39,238 @@ Safety:
- Never uses --force or --no-verify.
- Never runs `git config` (it only reads config; you fix a missing
identity yourself).
- - Never runs `git add` (see check_unreviewed_changes()'s docstring): a
- staged path is the operator's record that they've reviewed it and it's
- good, so this script commits the index exactly as it finds it and
- refuses outright -- commits and pushes nothing -- if the working tree
- has any unstaged modification or any untracked, non-gitignored file.
+ - Never runs `git add`: a staged path is the operator's record that
+ they've reviewed it and it's good, so this script commits the index
+ exactly as it finds it and refuses outright -- commits and pushes
+ nothing -- if the working tree has any unstaged modification or any
+ untracked, non-gitignored file.
- Exits non-zero with a clear message on any precondition failure
(no git identity configured, unreviewed changes present, push
rejected, etc).
+
+Built on Apache Juneau's juneau_scripts toolkit, loaded by the vendored scripts/bootstrap.py from the juneau-run
+artifact at the pom's juneau.version. The policy above is this file's code; the toolkit supplies the git, run and
+bell helpers.
"""
import argparse
import os
-import shlex
-import subprocess
import sys
from pathlib import Path
REPO_DIR = Path(__file__).resolve().parent
+sys.path.insert(0, str(REPO_DIR / "scripts")) # push.py lives at the repo root; bootstrap.py is vendored under scripts/
-def run(cmd, cwd=REPO_DIR, check=True, capture=False, input_text=None):
- """Run a command (list form, no shell) and return the CompletedProcess."""
- return subprocess.run(
- cmd,
- cwd=cwd,
- check=check,
- capture_output=capture,
- text=True,
- input=input_text,
- )
-
+import bootstrap # noqa: E402
+from bootstrap import run_requested # noqa: E402,F401 (push.run_requested is the environment rule tests check)
-def git_identity_configured(repo_dir):
- """True if `git config user.email` resolves to a non-empty value."""
- result = run(["git", "config", "--get", "user.email"], cwd=repo_dir, check=False, capture=True)
- return bool(result.stdout.strip())
+# The toolkit modules, bound by toolkit() on first use so that importing this file has no side effects.
+git = run = notify = None
-def get_staged_paths(repo_dir):
- """
- List paths currently staged in the index.
-
- This is the sole definition of "what push.py is about to commit": a staged path is the
- operator's record that they reviewed it and it's good (see check_unreviewed_changes()'s
- docstring), so this script commits the index exactly as it finds it rather than deciding
- what belongs in the commit itself.
+class _NoMarkers:
+ """Stands in for juneau_run when it isn't loaded: the calls this script makes directly are no-ops or False."""
- Returns:
- list[str]: staged paths (possibly empty).
+ def console_active(self):
+ return False
- Raises:
- subprocess.CalledProcessError, OSError: if the check itself can't run. Deliberately not
- caught here -- an error running `git diff --cached` almost always means something is
- fundamentally wrong with the repo, and this check exists specifically to protect an
- invariant, so the caller should fail closed (abort) rather than silently proceed.
- """
- result = subprocess.run(
- ["git", "diff", "--cached", "--name-only"],
- cwd=repo_dir,
- capture_output=True,
- text=True,
- check=True,
- )
- return [line for line in result.stdout.splitlines() if line.strip()]
+ def note(self, *args, **kwargs):
+ # Intentionally empty: markers are off, so there is nothing to record.
+ pass
-def check_unreviewed_changes(repo_dir):
- """
- List paths that have NOT been staged: tracked files with unstaged modifications/deletions,
- plus untracked files that are not gitignored.
+# The run-protocol module in use: juneau_run when the environment asks for it and it loads, otherwise the stand-in.
+run_markers = _NoMarkers()
- Staging is how this script knows something has been reviewed (see get_staged_paths()'s
- docstring); this is the other half of that invariant. Its presence alongside staged content
- means the operator's review pass isn't finished, so nothing should be committed or pushed
- yet -- not even the already-staged part -- until the tree is fully resolved one way or
- another (staged, committed separately, stashed, or discarded).
- Gitignored untracked files are deliberately excluded (via `git ls-files --exclude-standard`):
- build output and similar noise was never going to be staged or reviewed, and flagging it
- would make this refusal fire on essentially every real run.
+def toolkit():
+ """Load juneau_scripts (once per process) and bind git, run and notify. Returns the package."""
+ global git, run, notify
+ # cache_dir: the first load fixes it for the process, so a fetched copy lands in <repo>/target as before.
+ _, js = bootstrap.load(REPO_DIR / "pom.xml", run=False, cache_dir=REPO_DIR / "target" / "juneau-run")
+ from juneau_scripts import git, notify, run # noqa: F811
+ return js
- Returns:
- list[str]: unreviewed paths (possibly empty).
- Raises:
- subprocess.CalledProcessError, OSError: see get_staged_paths() -- same fail-closed
- reasoning applies here.
+def say(text="", level=None):
"""
- unstaged = subprocess.run(
- ["git", "diff", "--name-only"],
- cwd=repo_dir,
- capture_output=True,
- text=True,
- check=True,
- ).stdout.splitlines()
- untracked = subprocess.run(
- ["git", "ls-files", "--others", "--exclude-standard"],
- cwd=repo_dir,
- capture_output=True,
- text=True,
- check=True,
- ).stdout.splitlines()
- return [line for line in (*unstaged, *untracked) if line.strip()]
-
-
-def current_branch(repo_dir):
- result = run(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=repo_dir, capture=True)
- return result.stdout.strip()
-
-
-def short_sha(repo_dir):
- result = run(["git", "rev-parse", "--short", "HEAD"], cwd=repo_dir, capture=True)
- return result.stdout.strip()
-
-
-def remote_url(repo_dir, remote="origin"):
- result = run(["git", "remote", "get-url", remote], cwd=repo_dir, check=False, capture=True)
- return result.stdout.strip() if result.returncode == 0 else "(no remote)"
+ run.say(), with the level (error/warn/info) read from the leading mark when not given. Identical to print() with
+ a plain run; a filtered note under a console view.
+ """
+ if level is None:
+ level = "info"
+ if "❌" in text:
+ level = "error"
+ elif "⚠" in text:
+ level = "warn"
+ run.say(text, level)
-def resolve_commit_message(cli_message):
- """Use the CLI-supplied message, or prompt for one; abort if still empty."""
+def resolve_commit_message(s, cli_message):
+ """Use the CLI-supplied message, or prompt for one; an aborted prompt ends the session (s.fail says why)."""
if cli_message:
return cli_message
-
try:
message = input("Commit message: ").strip()
except (EOFError, KeyboardInterrupt):
- print("\n❌ Aborted: no commit message provided.")
- sys.exit(1)
-
+ s.fail("\n❌ Aborted: no commit message provided.")
if not message:
- print("❌ Aborted: commit message cannot be empty.")
- sys.exit(1)
-
+ s.fail("❌ Aborted: commit message cannot be empty.")
return message
-def commit_and_push(repo_dir, cli_message, *, dry_run=False):
+def _review(s, repo_dir, dry_run):
"""
- Commit whatever is already staged in repo_dir and push, refusing to commit or push
- ANYTHING if the working tree has unreviewed content.
-
- Extracted out of main() (as a repo_dir-parameterized function, mirroring juneau's/ssc's
- own commit_and_push()) so it's testable against a real temporary git repository without
- needing to fake this module's REPO_DIR constant.
-
- A staged path is the operator's record that they've reviewed it and it's good -- staging
- as they review, not as a mechanical step right before committing. So this commits the
- index exactly as it finds it (no `git add`, anywhere, ever): it does not decide what
- belongs in the commit, because the index already says. It refuses outright -- commits and
- pushes NOTHING -- if the working tree has any unstaged modification to a tracked file, or
- any untracked, non-gitignored file (see check_unreviewed_changes()), since their presence
- means the review pass isn't finished. This is deliberately strict with no override flag --
- even a flag that names the exact paths being overridden is still a way to push something
- nobody reviewed, which is the one thing this refusal exists to prevent. For genuinely
- partial work (some files reviewed and staged, others still mid-edit), the sanctioned way
- out is `git stash push -- <paths>` before running push.py and `git stash pop` after; the
- refusal message itself prints that command with the actual offending paths already filled
- in.
-
- A fully CLEAN tree (nothing staged, nothing unreviewed) is a benign no-op, not a refusal:
- there is genuinely nothing to commit, and this repo has no push-only path (unlike juneau's
- ahead-of-upstream case), so there is nothing else to do. The dirty-but-unstaged case is
- still refused (dirty content is, by definition, unreviewed) -- that is the case this
- invariant exists to catch.
-
- Both gate queries (check_unreviewed_changes(), get_staged_paths()) are read-only, so they
- always run -- including under --dry-run -- rather than being skipped. That is what makes
- --dry-run report the REAL outcome instead of a guess: it can never claim it "would commit"
- a tree the real run would refuse.
-
- Args:
- repo_dir: Repository root to operate against.
- cli_message: Commit message supplied on the CLI, or None to prompt for one (the prompt
- is only reached once it's established that there is something to actually commit).
- dry_run: If True, report what would happen and change nothing (a refusal is reported
- as "would REFUSE" and exits 0, since --dry-run itself did not fail).
-
- Returns:
- int: 0 on success (committed+pushed, a genuine no-op on a clean tree, or a --dry-run
- report of either outcome). 1 if refused (unreviewed content present) or the gate
- checks themselves failed to run (fails closed -- see check_unreviewed_changes()'s
- docstring).
+ The review gate (step 2). Returns the staged paths, or None when a dry run would refuse.
+
+ A staged path is the operator's record that they've reviewed it and it's good -- staging as they review, not as
+ a mechanical step right before committing. So this commits the index exactly as it finds it (no `git add`,
+ anywhere, ever) and refuses outright -- commits and pushes NOTHING -- if the working tree has any unstaged
+ modification to a tracked file, or any untracked, non-gitignored file, since their presence means the review pass
+ isn't finished. Gitignored untracked files are not flagged: build output was never going to be staged or
+ reviewed. This is deliberately strict with no override flag -- even a flag that names the exact paths being
+ overridden is still a way to push something nobody reviewed, which is the one thing this refusal exists to
+ prevent. For genuinely partial work, the sanctioned way out is `git stash push -- <paths>` before running
+ push.py and `git stash pop` after; the refusal message prints that command with the offending paths filled in.
+
+ Both queries are read-only, so they run under --dry-run too: it reports the REAL outcome instead of a guess. They
+ also fail closed: if git can't run, the run is refused rather than proceeding unchecked.
"""
+ with run.step("review", "Review gate (staged index)", n=2):
+ try:
+ unreviewed = git.unstaged_paths(repo_dir) + git.untracked_paths(repo_dir)
+ staged = git.staged_paths(repo_dir)
+ except git.GitError as e:
+ s.fail(f"\n❌ ERROR: Could not check the working tree/index: {e}")
+ if not unreviewed:
+ return staged
+ header = "🔍 DRY RUN — would REFUSE" if dry_run else "❌ ERROR"
+ first = (f"\n{header}: the working tree has unreviewed changes — staging is how this "
+ "script knows something has been reviewed, and these paths are not staged:")
+ rest = [f" {path}" for path in unreviewed]
+ rest += [" Why: everything this pushes should be something you've read, not just",
+ " something that happened to be in the tree.",
+ "\n Nothing was committed or pushed. Stage each path above if you've reviewed",
+ " it, or set it aside for this run and restore it after:"]
+ rest += [f" {line}" for line in git.stash_hint(unreviewed).splitlines()]
+ if not dry_run:
+ s.fail(first, hint=rest)
+ for line in (first, *rest):
+ say(line)
+ run_markers.note("warn", "Dry run — would refuse: unreviewed changes", step="review")
+ return None
+
+
+def _dry_run_report(staged, message, branch):
+ """The --dry-run report: what the commit and push would do."""
+ plural = "" if len(staged) == 1 else "s"
+ say(f"\n🔍 DRY RUN — no changes will be made. Would commit the staged index "
+ f"({len(staged)} path{plural}) and push to origin/{branch}:")
+ say(f" git commit -F- <<< {message!r}")
+ say(f" git push origin {branch}")
+ run_markers.note("info", "Dry run — no changes made.")
+
+
+def _commit(s, repo_dir, message):
+ """Commit the index exactly as found (step 3). Returns the short sha; s.commit is set as soon as it exists."""
+ with run.step("commit", "Commit", n=3):
+ say("\n📝 Committing...")
+ # Message via stdin (git commit -F -) so multi-line messages and odd characters need no quoting.
+ r = git.commit_index(message, repo_dir, stdin=True)
+ if not r.ok:
+ s.fail(f"\n❌ ERROR: git commit failed (exit code {r.code}).")
+ sha = git.short_head(repo_dir)
+ s.commit = sha
+ run_markers.note("info", f"Committed {sha}", step="commit")
+ return sha
+
+
+def _push_incomplete(sha):
+ text = f"Committed {sha} locally; push did not complete"
+ say(f"\n⚠ {text}.")
+ run_markers.note("warn", text, step="push")
+
+
+def _push(s, repo_dir, branch, sha):
+ """Push the new commit to origin (step 4). A failed or interrupted push still reports the local commit."""
try:
- unreviewed = check_unreviewed_changes(repo_dir)
- staged = get_staged_paths(repo_dir)
- except (subprocess.CalledProcessError, OSError) as e:
- print(f"\n❌ ERROR: Could not check the working tree/index: {e}")
- return 1
+ with run.step("push", "Push", n=4) as p:
+ say(f"🚀 Pushing to origin/{branch}...")
+ r = git.push(repo_dir, branch=branch)
+ if r.ok:
+ run_markers.note("info", f"Pushed origin/{branch}", step="push")
+ else:
+ p.fail()
+ except KeyboardInterrupt:
+ _push_incomplete(sha)
+ raise
+ if not r.ok:
+ _push_incomplete(sha)
+ s.fail(f"\n❌ ERROR: git push failed (exit code {r.code}).")
+
+
+def commit_and_push(s, repo_dir, cli_message, *, dry_run=False):
+ """
+ Commit whatever is already staged in repo_dir and push, refusing to commit or push ANYTHING if the working tree
+ has unreviewed content (see _review()).
- if unreviewed:
- header = "🔍 DRY RUN — would REFUSE" if dry_run else "❌ ERROR"
- print(f"\n{header}: the working tree has unreviewed changes — staging is how this "
- "script knows something has been reviewed, and these paths are not staged:")
- for path in unreviewed:
- print(f" {path}")
- print(" Why: everything this pushes should be something you've read, not just")
- print(" something that happened to be in the tree.")
- print("\n Nothing was committed or pushed. Stage each path above if you've reviewed")
- print(" it, or set it aside for this run and restore it after:")
- quoted_paths = " ".join(shlex.quote(path) for path in unreviewed)
- print(f" git stash push -- {quoted_paths}")
- print(" git stash pop")
- return 0 if dry_run else 1
+ A fully CLEAN tree (nothing staged, nothing unreviewed) is a benign no-op, not a refusal: there is genuinely
+ nothing to commit, and this repo has no push-only path. --dry-run reports the real outcome and changes nothing;
+ a refusal under it is reported as "would REFUSE" and does not fail the run.
+ Returns normally on success (committed and pushed, a no-op on a clean tree, or a --dry-run report); every failure
+ ends the session through s.fail(). s.commit is set as soon as the commit exists, so the done marker reports it
+ even when the push then fails or is cancelled.
+ """
+ staged = _review(s, repo_dir, dry_run)
+ if staged is None:
+ return
if not staged:
- print("⚠ Nothing to commit — working tree is clean. Nothing to do.")
- return 0
+ say("⚠ Nothing to commit — working tree is clean. Nothing to do.")
+ run_markers.note("warn", "Nothing to commit — working tree is clean.")
+ return
- message = resolve_commit_message(cli_message)
- branch = current_branch(repo_dir)
+ message = resolve_commit_message(s, cli_message)
+ branch = git.current_branch(repo_dir) or "HEAD"
if dry_run:
- plural = "" if len(staged) == 1 else "s"
- print(f"\n🔍 DRY RUN — no changes will be made. Would commit the staged index "
- f"({len(staged)} path{plural}) and push to origin/{branch}:")
- print(f" git commit -F- <<< {message!r}")
- print(f" git push origin {branch}")
- return 0
-
- print("\n📝 Committing...")
- # Commit the index exactly as it was found -- see check_unreviewed_changes()'s docstring
- # for why there is no `git add` anywhere in this script. Pipe the message via stdin
- # (git commit -F -) instead of -m, so multi-line messages and odd characters round-trip
- # safely with no shell quoting at all.
- run(["git", "commit", "-F", "-"], cwd=repo_dir, input_text=message)
-
- print(f"🚀 Pushing to origin/{branch}...")
- run(["git", "push", "origin", branch], cwd=repo_dir)
-
- print("\n" + "=" * 70)
- print("✅ Push complete.")
- print(f" Branch: {branch}")
- print(f" Commit: {short_sha(repo_dir)}")
- print(f" Remote: {remote_url(repo_dir)}")
- print("=" * 70)
- return 0
+ _dry_run_report(staged, message, branch)
+ return
+ sha = _commit(s, repo_dir, message)
+ _push(s, repo_dir, branch, sha)
-def ring_bell():
- """
- Ring the terminal bell (BEL) so a finished run gets noticed; macOS Terminal also badges the
- Dock icon when the window is unfocused. Best-effort: only on a TTY (checked at write time),
- never in sandbox mode (SANDBOX_RUNTIME=1), and not when JRM_PUSH_NO_BELL=1. Never raises.
- """
- if os.environ.get("JRM_PUSH_NO_BELL") == "1" or os.environ.get("SANDBOX_RUNTIME") == "1":
- return
- try:
- if sys.stdout.isatty():
- sys.stdout.write("\a")
- sys.stdout.flush()
- except Exception: # NOSONAR - best-effort notification must never affect the exit status
- pass
+ say("\n" + "=" * 70)
+ say("✅ Push complete.")
+ say(f" Branch: {branch}")
+ say(f" Commit: {sha}")
+ say(f" Remote: {git.remote_url(repo_dir) or '(no remote)'}")
+ say("=" * 70)
-def main():
- try:
- _main()
- finally:
- ring_bell()
+def _identity(s, repo_dir):
+ """The identity gate (step 1) -- must hold before anything else."""
+ with run.step("identity", "Git identity", n=1):
+ say("🔐 Checking git identity...")
+ if git.user_email(repo_dir):
+ say("✅ Git identity configured.")
+ return
+ s.fail("❌ ERROR: no git identity configured (git config user.email is unset).",
+ hint=[" This has caused a failed push before on this repo. Fix it with:",
+ ' git config user.email "you@example.com"',
+ ' git config user.name "Your Name"',
+ " (this script will never set it for you)"])
+
+
+def ring_bell():
+ """Ring the terminal bell once (the session calls the same helper at the end of a run). Best-effort once the
+ toolkit loads. Skipped in a sandbox (SANDBOX_RUNTIME=1), when JRM_PUSH_NO_BELL=1, or when stdout is not a TTY."""
+ toolkit().notify.bell(silence_env="JRM_PUSH_NO_BELL")
-def _main():
+def main(argv=None, repo_dir=REPO_DIR):
+ global run_markers
parser = argparse.ArgumentParser(
description="Commit and push juneau-release-manager to origin.",
formatter_class=argparse.RawDescriptionHelpFormatter,
@@ -311,7 +279,7 @@ Examples:
./push.py "Fix credential validator NPE"
./push.py
./push.py --dry-run "Would-be commit message"
- ./push.py --console none "Quiet-console run (no effect here: this script has no console view)"
+ ./push.py --console condensed "Fix credential validator NPE"
""",
)
parser.add_argument(
@@ -328,33 +296,33 @@ Examples:
parser.add_argument(
"--console",
choices=("full", "condensed", "none"),
- help=(
- "Maven output on the console: full (default), condensed or none (also JUNEAU_RUN_CONSOLE). "
- "Accepted for parity with the Juneau scripts; this script runs no Maven and has no console view, "
- "so it has no effect here."
- ),
+ help="Console view of the steps: full (default), condensed or none (also JUNEAU_RUN_CONSOLE).",
)
- args = parser.parse_args()
+ args = parser.parse_args(argv)
if args.console:
- os.environ["JUNEAU_RUN_CONSOLE"] = args.console # an argument beats a value already in the environment
-
- print("=" * 70)
- print("📦 juneau-release-manager push")
- print("=" * 70)
+ # Before toolkit()/bootstrap.load() read the environment; an argument beats a value already set there.
+ os.environ["JUNEAU_RUN_CONSOLE"] = args.console
- # Identity gate — must hold before anything else.
- print("🔐 Checking git identity...")
- if not git_identity_configured(REPO_DIR):
- print("❌ ERROR: no git identity configured (git config user.email is unset).")
- print(" This has caused a failed push before on this repo. Fix it with:")
- print(' git config user.email "you@example.com"')
- print(' git config user.name "Your Name"')
- print(" (this script will never set it for you)")
- sys.exit(1)
- print("✅ Git identity configured.")
+ try:
+ toolkit()
+ jr, _ = bootstrap.load(REPO_DIR / "pom.xml")
+ except bootstrap.BootstrapError as e:
+ print(f"❌ ERROR: {e}", file=sys.stderr)
+ print(" (juneau-run 10.0.0-SNAPSHOT must be in ~/.m2: build Juneau first.)", file=sys.stderr)
+ return 1
+ run_markers = jr if jr is not None else _NoMarkers()
- sys.exit(commit_and_push(REPO_DIR, args.message, dry_run=args.dry_run))
+ with run.session("🚀 juneau-release-manager push", header=(git.current_branch(repo_dir) or "HEAD",), bell=True,
+ silence_env="JRM_PUSH_NO_BELL", mode="push", project="juneau-release-manager",
+ repo=repo_dir, exit=False) as s:
+ if not run_markers.console_active():
+ say("=" * 70)
+ say("📦 juneau-release-manager push")
+ say("=" * 70)
+ _identity(s, repo_dir)
+ commit_and_push(s, repo_dir, args.message, dry_run=args.dry_run)
+ return s.code
if __name__ == "__main__":
- main()
+ sys.exit(main())
diff --git a/scripts/tests/README.md b/scripts/tests/README.md
index 704b78a78a..b9b8c7ef40 100644
--- a/scripts/tests/README.md
+++ b/scripts/tests/README.md
@@ -9,7 +9,7 @@ release-manager`), with tests alongside at `~/Project Work/scripts/tests/`. See
This repo's own `push.py` (at the repo root, not under `scripts/`) is a bare commit/push
helper with one gate worth testing (WORK-R0006): it refuses to commit or push **anything**
when the working tree has any unstaged modification to a tracked file, or any untracked file
-that isn't gitignored (`check_unreviewed_changes()`/`get_staged_paths()`/`commit_and_push()`).
+that isn't gitignored (`_review()` / `commit_and_push()`, built on the `juneau_scripts` toolkit's `git` and `run` modules, loaded through the vendored `scripts/bootstrap.py`, which needs juneau-run 10.0.0-SNAPSHOT in `~/.m2` -- build Juneau first; the tests drive `push.main(argv, repo_dir)` and read its exit code).
A staged path is the operator's record of having reviewed it, so `push.py` commits the index
exactly as it finds it and never runs `git add`. `test_push.py` covers the three refusal
shapes (nothing-staged-while-dirty, staged+unstaged-tracked, staged+untracked-non-ignored)
diff --git a/scripts/tests/test_push.py b/scripts/tests/test_push.py
index bcab198471..d0138b6f8d 100644
--- a/scripts/tests/test_push.py
+++ b/scripts/tests/test_push.py
@@ -17,8 +17,9 @@ Tests for the root push.py: the end-of-run terminal bell (TestEndOfRunBell) and
refuse-on-unreviewed-changes gate, ported from the
already-reviewed gate in apache/juneau's scripts/push.py and sandbox-support-console's
scripts/push.py. This repo's push.py is a bare add/commit/push helper with no other gates, so
-the gate tests cover: check_unreviewed_changes(), get_staged_paths(), and commit_and_push()'s
-refuse / no-op / proceed decision (the three refusal shapes plus the allowed staged-only case).
+the gate tests cover: the toolkit's path readers as push.py uses them, and the refuse / no-op /
+proceed decision (the three refusal shapes plus the allowed staged-only case). push.py is built on
+the juneau_scripts toolkit; the tests drive it through main(argv, repo_dir) and read the exit code.
Real temp git repos, real git -- no mocked subprocess. A bare repo stands in for the remote and
a real clone is the working tree; every test asserts REAL effects (did the remote ref actually
@@ -37,6 +38,7 @@ from __future__ import annotations
import importlib.util
import os
import subprocess
+import sys
from pathlib import Path
import pytest
@@ -53,10 +55,37 @@ def _load_push_module():
return module
+@pytest.fixture(autouse=True)
+def clean_run_env(monkeypatch):
+ """Keep the developer's shell (e.g. JUNEAU_RUN_CONSOLE=condensed) out of every push test, and forget any
+ juneau_run a previous test loaded. JUNEAU_RUN_PY / JUNEAU_SCRIPTS_ZIP stay: they locate the toolkit."""
+ keep = {"JUNEAU_RUN_PY"}
+ for key in [k for k in os.environ if k.startswith("JUNEAU_RUN_") and k not in keep]:
+ monkeypatch.delenv(key)
+ monkeypatch.delenv("RUN_MARKERS", raising=False)
+ monkeypatch.delenv("RUN_ARTIFACTS", raising=False)
+ monkeypatch.delitem(sys.modules, "juneau_run", raising=False)
+ monkeypatch.setenv("JRM_PUSH_NO_BELL", "1") # keep the main()-calling tests quiet under `pytest -s`
+
+
+class _Push:
+ """The loaded push.py, plus commit_and_push(repo, message, dry_run=) as an exit code, via main()."""
+
+ def __init__(self, module):
+ self.module = module
+ module.toolkit()
+
+ def __getattr__(self, name):
+ return getattr(self.module, name)
+
+ def commit_and_push(self, repo, message, *, dry_run=False):
+ return self.module.main([*(["--dry-run"] if dry_run else []), message], repo_dir=repo)
+
+
@pytest.fixture
def push_module():
- """A fresh root push.py module object, loaded once per test."""
- return _load_push_module()
+ """A fresh root push.py module object (toolkit bound), loaded once per test."""
+ return _Push(_load_push_module())
# ---------------------------------------------------------------------------------------
@@ -119,7 +148,7 @@ def repo(tmp_path: Path, remote_repo: Path) -> Path:
@pytest.fixture
def git_spy(push_module, monkeypatch):
- """Record every argv passed to push_module.subprocess.run while still running real git."""
+ """Record every argv passed to subprocess.run (the toolkit's git helpers use it) while still running real git."""
calls: list[list[str]] = []
real_run = subprocess.run
@@ -127,7 +156,7 @@ def git_spy(push_module, monkeypatch):
calls.append(list(cmd) if isinstance(cmd, (list, tuple)) else [cmd])
return real_run(cmd, *args, **kwargs)
- monkeypatch.setattr(push_module.subprocess, "run", _spy)
+ monkeypatch.setattr(subprocess, "run", _spy)
return calls
@@ -139,17 +168,17 @@ class TestGetStagedPaths:
"""
def test_a_clean_index_reports_nothing(self, push_module, repo):
- assert push_module.get_staged_paths(repo) == []
+ assert push_module.git.staged_paths(repo) == []
def test_a_staged_file_is_reported(self, push_module, repo):
(repo / "reviewed.txt").write_text("staged by the operator\n", encoding="utf-8")
_run(repo, "git", "add", "reviewed.txt")
- assert push_module.get_staged_paths(repo) == ["reviewed.txt"]
+ assert push_module.git.staged_paths(repo) == ["reviewed.txt"]
def test_an_unstaged_modification_alone_is_not_reported(self, push_module, repo):
(repo / "README.md").write_text("unstaged edit\n", encoding="utf-8")
- assert push_module.get_staged_paths(repo) == []
+ assert push_module.git.staged_paths(repo) == []
class TestCheckUnreviewedChanges:
@@ -160,29 +189,29 @@ class TestCheckUnreviewedChanges:
"""
def test_a_clean_tree_reports_nothing(self, push_module, repo):
- assert push_module.check_unreviewed_changes(repo) == []
+ assert (push_module.git.unstaged_paths(repo) + push_module.git.untracked_paths(repo)) == []
def test_an_unstaged_modification_to_a_tracked_file_is_reported(self, push_module, repo):
(repo / "README.md").write_text("unstaged edit\n", encoding="utf-8")
- assert push_module.check_unreviewed_changes(repo) == ["README.md"]
+ assert (push_module.git.unstaged_paths(repo) + push_module.git.untracked_paths(repo)) == ["README.md"]
def test_an_untracked_non_ignored_file_is_reported(self, push_module, repo):
(repo / "scratch.txt").write_text("brand new, not staged\n", encoding="utf-8")
- assert push_module.check_unreviewed_changes(repo) == ["scratch.txt"]
+ assert (push_module.git.unstaged_paths(repo) + push_module.git.untracked_paths(repo)) == ["scratch.txt"]
def test_a_gitignored_untracked_file_is_not_reported(self, push_module, repo):
(repo / ".gitignore").write_text("ignored.log\n", encoding="utf-8")
_commit(repo, "add gitignore")
(repo / "ignored.log").write_text("noise\n", encoding="utf-8")
- assert push_module.check_unreviewed_changes(repo) == []
+ assert (push_module.git.unstaged_paths(repo) + push_module.git.untracked_paths(repo)) == []
def test_a_staged_file_alone_is_not_reported(self, push_module, repo):
"""Staged content is reviewed content -- it's the other function's (get_staged_paths()) job."""
(repo / "reviewed.txt").write_text("staged and reviewed\n", encoding="utf-8")
_run(repo, "git", "add", "reviewed.txt")
- assert push_module.check_unreviewed_changes(repo) == []
+ assert (push_module.git.unstaged_paths(repo) + push_module.git.untracked_paths(repo)) == []
class TestCommitAndPushGenuineNoOp:
@@ -425,11 +454,11 @@ class TestCommitAndPushDryRun:
class TestGitIdentityConfigured:
def test_configured_identity_is_true(self, push_module, repo):
- assert push_module.git_identity_configured(repo) is True
+ assert bool(push_module.git.user_email(repo)) is True
def test_unset_identity_is_false(self, push_module, repo):
_run(repo, "git", "config", "--unset", "user.email")
- assert push_module.git_identity_configured(repo) is False
+ assert bool(push_module.git.user_email(repo)) is False
class _FakeStdout:
@@ -451,21 +480,23 @@ class _FakeStdout:
def _run_main(push_module, monkeypatch, *, tty, identity=True, commit_result=0, commit_raises=None):
- """Run main() with every git-touching call stubbed; return (exit_code, fake_stdout)."""
+ """Run main() with the identity and commit steps stubbed; return (exit_code, fake_stdout)."""
fake = _FakeStdout(tty)
- monkeypatch.setattr(push_module.sys, "stdout", fake)
- monkeypatch.setattr(push_module.sys, "argv", ["push.py", "msg"])
- monkeypatch.setattr(push_module, "git_identity_configured", lambda _repo: identity)
+ monkeypatch.setattr(sys, "stdout", fake)
+
+ def fake_identity(s, _repo):
+ if not identity:
+ s.fail("❌ ERROR: no git identity configured")
- def fake_commit_and_push(*_args, **_kwargs):
+ def fake_commit_and_push(s, *_args, **_kwargs):
if commit_raises is not None:
raise commit_raises
- return commit_result
+ if commit_result:
+ s.fail("❌ refused")
- monkeypatch.setattr(push_module, "commit_and_push", fake_commit_and_push)
- with pytest.raises(SystemExit) as exc:
- push_module.main()
- return exc.value.code, fake
+ monkeypatch.setattr(push_module.module, "_identity", fake_identity)
+ monkeypatch.setattr(push_module.module, "commit_and_push", fake_commit_and_push)
+ return push_module.main(["msg"]), fake
class TestEndOfRunBell:
@@ -473,10 +504,12 @@ class TestEndOfRunBell:
def clean_env(self, monkeypatch):
monkeypatch.delenv("JRM_PUSH_NO_BELL", raising=False)
monkeypatch.delenv("SANDBOX_RUNTIME", raising=False)
+ monkeypatch.delenv("JUNEAU_SCRIPTS_QUIET", raising=False)
def test_rings_on_tty_after_success(self, push_module, monkeypatch):
code, fake = _run_main(push_module, monkeypatch, tty=True)
assert code == 0
+ assert fake.written.count("\a") == 1
assert fake.written.endswith("\a")
assert fake.flushed
@@ -490,9 +523,9 @@ class TestEndOfRunBell:
assert code == 1
assert fake.written.endswith("\a")
- def test_rings_on_tty_when_commit_message_aborts_with_system_exit(self, push_module, monkeypatch):
- code, fake = _run_main(push_module, monkeypatch, tty=True, commit_raises=SystemExit(1))
- assert code == 1
+ def test_rings_on_tty_when_cancelled(self, push_module, monkeypatch):
+ code, fake = _run_main(push_module, monkeypatch, tty=True, commit_raises=KeyboardInterrupt())
+ assert code == 130
assert fake.written.endswith("\a")
def test_silent_when_not_a_tty(self, push_module, monkeypatch):
@@ -505,18 +538,11 @@ class TestEndOfRunBell:
_, fake = _run_main(push_module, monkeypatch, tty=True)
assert fake.written.endswith("\a")
- def test_rings_and_propagates_when_push_step_raises(self, push_module, monkeypatch):
- fake = _FakeStdout(True)
- monkeypatch.setattr(push_module.sys, "stdout", fake)
- monkeypatch.setattr(push_module.sys, "argv", ["push.py", "msg"])
- monkeypatch.setattr(push_module, "git_identity_configured", lambda _repo: True)
-
- def boom(*_args, **_kwargs):
- raise subprocess.CalledProcessError(1, ["git", "push"])
-
- monkeypatch.setattr(push_module, "commit_and_push", boom)
- with pytest.raises(subprocess.CalledProcessError):
- push_module.main()
+ def test_rings_and_reports_when_the_push_step_raises(self, push_module, monkeypatch):
+ code, fake = _run_main(push_module, monkeypatch, tty=True,
+ commit_raises=subprocess.CalledProcessError(1, ["git", "push"]))
+ assert code == 1
+ assert "CalledProcessError" in fake.written
assert fake.written.endswith("\a")
def test_silent_when_opted_out(self, push_module, monkeypatch):
@@ -529,20 +555,19 @@ class TestEndOfRunBell:
_, fake = _run_main(push_module, monkeypatch, tty=True)
assert "\a" not in fake.written
- def test_never_raises_when_stdout_misbehaves(self, push_module, monkeypatch):
+ def test_ring_bell_never_raises_when_stdout_misbehaves(self, push_module, monkeypatch):
class Broken(_FakeStdout):
def write(self, text):
if text == "\a":
raise OSError("closed")
super().write(text)
- fake = Broken(True)
- monkeypatch.setattr(push_module.sys, "stdout", fake)
+ monkeypatch.setattr(sys, "stdout", Broken(True))
push_module.ring_bell() # must not raise
class TestConsoleFlag:
- """--console full|condensed|none is accepted for parity with the Juneau scripts and sets JUNEAU_RUN_CONSOLE."""
+ """--console full|condensed|none sets JUNEAU_RUN_CONSOLE before the toolkit is loaded."""
@pytest.fixture(autouse=True)
def clean_env(self, monkeypatch):
@@ -550,15 +575,12 @@ class TestConsoleFlag:
monkeypatch.delenv("JUNEAU_RUN_CONSOLE", raising=False)
def _main(self, push_module, monkeypatch, *flags):
- monkeypatch.setattr(push_module.sys, "stdout", _FakeStdout(False))
- monkeypatch.setattr(push_module.sys, "argv", ["push.py", *flags, "msg"])
- monkeypatch.setattr(push_module, "git_identity_configured", lambda _repo: True)
- monkeypatch.setattr(push_module, "commit_and_push", lambda *_a, **_k: 0)
- with pytest.raises(SystemExit) as exc:
- push_module.main()
- return exc.value.code
+ monkeypatch.setattr(sys, "stdout", _FakeStdout(False))
+ monkeypatch.setattr(push_module.module, "_identity", lambda *_a: None)
+ monkeypatch.setattr(push_module.module, "commit_and_push", lambda *_a, **_k: None)
+ return push_module.main([*flags, "msg"])
- @pytest.mark.parametrize("mode", ["full", "condensed", "none"])
+ @pytest.mark.parametrize("mode", ["full", "none"])
def test_flag_sets_the_env_var(self, push_module, monkeypatch, mode):
assert self._main(push_module, monkeypatch, "--console", mode) == 0
assert os.environ["JUNEAU_RUN_CONSOLE"] == mode
@@ -569,12 +591,24 @@ class TestConsoleFlag:
assert os.environ["JUNEAU_RUN_CONSOLE"] == "none"
def test_env_var_is_left_alone_without_the_flag(self, push_module, monkeypatch):
- monkeypatch.setenv("JUNEAU_RUN_CONSOLE", "condensed")
+ monkeypatch.setenv("JUNEAU_RUN_CONSOLE", "full")
assert self._main(push_module, monkeypatch) == 0
- assert os.environ["JUNEAU_RUN_CONSOLE"] == "condensed"
+ assert os.environ["JUNEAU_RUN_CONSOLE"] == "full"
- def test_unknown_mode_is_a_usage_error(self, push_module, monkeypatch):
- monkeypatch.setattr(push_module.sys, "argv", ["push.py", "--console", "loud", "msg"])
+ def test_unknown_mode_is_a_usage_error(self, push_module):
with pytest.raises(SystemExit) as exc:
- push_module.main()
+ push_module.main(["--console", "loud", "msg"])
assert exc.value.code == 2
+
+
+class TestCondensedConsole:
+ def test_a_push_under_condensed_succeeds_and_the_remote_moves(self, push_module, repo, remote_repo, capfd):
+ (repo / "README.md").write_text("new content\n", encoding="utf-8")
+ _run(repo, "git", "add", "README.md")
+
+ assert push_module.main(["--console", "condensed", "msg"], repo_dir=repo) == 0
+ assert _run(remote_repo, "git", "rev-parse", "master") == _run(repo, "git", "rev-parse", "HEAD")
+
+ def test_a_plain_run_loads_no_juneau_run(self, push_module, repo):
+ assert push_module.main(["--dry-run", "msg"], repo_dir=repo) == 0
+ assert "juneau_run" not in sys.modules