import os, pathlib, subprocess, tempfile script = str(pathlib.Path(__file__).resolve().with_name('pruneLocalBranches')) root = pathlib.Path(tempfile.mkdtemp(prefix='git-tools-test-')) env = dict(os.environ, GIT_AUTHOR_NAME='Test', GIT_AUTHOR_EMAIL='test@example.com', GIT_COMMITTER_NAME='Test', GIT_COMMITTER_EMAIL='test@example.com', GIT_CONFIG_NOSYSTEM='1') def cmd(*args, cwd=None, expected=0, data=None): p = subprocess.run(args, cwd=cwd or root, env=env, input=data, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) assert p.returncode == expected, (args, p.returncode, p.stdout) return p.stdout cmd('git','init','--bare','origin.git') cmd('git','init','-b','main','repo') repo=root/'repo' def git(*args): return cmd('git',*args,cwd=repo) def run(*args,expected=0,data=None): return cmd(script,*args,cwd=repo,expected=expected,data=data) git('commit','--allow-empty','-m','initial') git('remote','add','origin',str(root/'origin.git')) git('push','-u','origin','main') git('branch','merged') git('push','origin','merged') run('--check') git('push','origin','--delete','merged') git('branch','S6-deployment') git('branch','--set-upstream-to=origin/main','S6-deployment') git('branch','protected') git('push','origin','protected') git('checkout','-b','unique') git('commit','--allow-empty','-m','unpublished') git('checkout','main') git('branch','in-worktree') git('worktree','add',str(root/'linked'),'in-worktree') out=run('--check') for value in ['ELIGIBLE merged','ELIGIBLE S6-deployment','KEEP protected: same-name','KEEP main: same-name','KEEP unique: commits absent','unpublished','KEEP in-worktree: checked out']: assert value in out, (value,out) assert 'Cancelled' in run('-d','merged',data='no\n') git('show-ref','--verify','refs/heads/merged') assert 'Cancelled' in run('-d','merged',data='') run('-d','protected',expected=1) run('-d','unique',expected=1) run('-d','missing',expected=1) run('-d','merged',data='DELETE\n') cmd('git','show-ref','--verify','--quiet','refs/heads/merged',cwd=repo,expected=1) run('-d','*',data='DELETE\n') cmd('git','show-ref','--verify','--quiet','refs/heads/S6-deployment',cwd=repo,expected=1) assert 'unique' in git('branch','--list') assert 'protected' in git('branch','--list') assert 'in-worktree' in git('branch','--list') remote=git('ls-remote','--heads','origin') assert 'refs/heads/main' in remote and 'refs/heads/protected' in remote run('--bad',expected=2) git('remote','set-url','origin',str(root/'does-not-exist')) run('--check',expected=1) print('PASS: fetch/prune, same-name protection, upstream-name independence, unique commits, worktree protection, cancellation/EOF, single/bulk deletion, remote preservation, invalid arguments, fetch failure') print('Temporary fixtures:',root)