refactor(shell): extract helpers from run() to reduce complexity (S3776) #82
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "Claude/backup-script:fix/issue-34-S3776-shell-complexity"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #34
engine/shell.run()had cognitive complexity 27 (SonarQube ceiling: 15).Refactor
_build_env()— isolate the env-merge tri-state:None(let subprocess inherit unchanged) vsdict(os.environ)+ overlay vs hermetic{}+ overlay._execute()— isolate thesubprocess.runcall plus the three exception branches (TimeoutExpired,FileNotFoundError,PermissionError), returning a flat(returncode, stdout, stderr, timed_out)tuple.run()no longer holds four uninitialized locals across a try-block.run()itself is now a straight-line driver: build env, log start, dry-run short-circuit, execute, truncate, buildShellResult, log end, optionally raise.Behavior
Byte-for-byte identical. All subprocess semantics preserved:
-1timeout,127ENOENT,126EACCES).capture_output=True,check=False,input=stdininvocation — no change to stdout/stderr capture, no change to signal handling (stillsubprocess.run, notPopen— same SIGTERM behavior on parent exit).timed_outflag semantics: only true whenTimeoutExpiredwas raised.subprocess.startthensubprocess.endwithdry_run=True, returns a syntheticShellResultwithduration_s=0.0).subprocess.start/subprocess.end) with identical keyword payloads._truncate→ShellResult→_format_for_logonly for the log line.check=True⇒ShellErrorraise condition (not result.ok).Verification
pytest -q tests/unit/— passes (207/207, includingtest_recover.pyandtest_restic_engine_audit_fixes.pywhich callshell.runvia mocks at the call-site, exercising the patched signature).mypy --strict src/gardien/core src/gardien/config src/gardien/engine src/gardien/steps— passes (Success: no issues found in 26 source files).analyze_code_snippeton the refactored file —issueCount: 0.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.