155 lines
7.5 KiB
Python
155 lines
7.5 KiB
Python
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
from review_git import git_env, refusal
|
|
|
|
WRAPPER = pathlib.Path(__file__).resolve().parent / "review_git.py"
|
|
# Paths the fake filesystem resolves to an existing file outside the clone.
|
|
OUTSIDE = {"..", "link-out"}
|
|
|
|
|
|
def escapes(path: str) -> bool:
|
|
return path in OUTSIDE
|
|
|
|
|
|
class Refusal(unittest.TestCase):
|
|
def test_refuses_an_option_outside_the_subcommands_allowlist(self):
|
|
for argv in (
|
|
["commit", "-aF/abs/secret"], ["commit", "-t/abs/template", "-m", "x"], ["diff", "-RO/abs/out"],
|
|
["commit", "--fil=/abs/secret"], ["commit", "--templ=/abs/template", "-m", "x"],
|
|
["add", "--pathspec-from=/abs/list"], ["add", "--pathspec-f=list"], ["ls-files", "--exclude-f=/abs/x"],
|
|
["diff", "--ext"], ["ls-files", "-oX/abs/x"], ["diff", "--output=.git/config"], ["diff", "--no-index", "a", "b"],
|
|
["commit", "-S", "-m", "x"], ["commit", "--gpg-sign", "-m", "x"], ["commit", "-e", "-m", "x"],
|
|
["commit", "--edit", "-m", "x"], ["commit", "-c", "HEAD"], ["add", "-e"], ["add", "-p"], ["add", "-i"],
|
|
["add", "--patch"], ["log", "--help"], ["log", "--format=%h %G?"], ["show", "--pretty=%GS"],
|
|
):
|
|
with self.subTest(argv):
|
|
self.assertIn(argv[1], refusal(argv, escapes) or "")
|
|
|
|
def test_reads_an_option_after_a_message_glued_to_m(self):
|
|
for argv in (["commit", "-mm", "--pathspec-from-file=/secret"], ["commit", "-mfixm", "--pathspec-from-file=/secret"]):
|
|
with self.subTest(argv):
|
|
self.assertIn("--pathspec-from-file=/secret", refusal(argv, escapes) or "")
|
|
|
|
def test_refuses_a_commit_without_a_message(self):
|
|
for argv in (["commit"], ["commit", "-a"]):
|
|
with self.subTest(argv):
|
|
self.assertIsNotNone(refusal(argv, escapes))
|
|
|
|
def test_refuses_a_subcommand_outside_the_review(self):
|
|
for argv in ([], ["config", "core.pager", "sh"], ["-c", "alias.x=!sh", "x"], ["push"]):
|
|
with self.subTest(argv):
|
|
self.assertIsNotNone(refusal(argv, escapes))
|
|
|
|
def test_refuses_an_argument_naming_a_path_outside_the_clone(self):
|
|
for argv in (
|
|
["add", "/etc/passwd"], ["diff", "~/.ssh/id_rsa"], ["diff", "--", "../other/f"], ["add", ".."],
|
|
["show", "link-out"], ["diff", "HEAD..link-out"], ["diff", "a/../../b"], ["diff", "HEAD..~/x"],
|
|
["log", ":(top)"], ["log", ":/"], ["add", "--", "/etc/passwd"], ["commit", "-m", "x", "/etc/passwd"],
|
|
):
|
|
with self.subTest(argv):
|
|
self.assertIn(argv[-1], refusal(argv, escapes) or "")
|
|
|
|
def test_allows_the_reviews_own_commands(self):
|
|
for argv in (
|
|
["log", "--oneline", "abc123..chunk"], ["log", "-5"], ["log", "--max-count=3", "--oneline"],
|
|
["log", "--stat", "HEAD~1..HEAD"], ["log", "--oneline", "--", "src/a.py"], ["diff", "base...chunk", "--stat"],
|
|
["diff", "--cached"], ["diff", "--name-only", "HEAD~1"], ["diff", "--", "src"], ["show", "HEAD"],
|
|
["show", "--stat", "abc123"], ["show", "HEAD:README.md"], ["status"], ["status", "--short"], ["ls-files"],
|
|
["ls-files", "--others", "--exclude-standard"], ["add", "-A"], ["add", "--", "src/a.py"],
|
|
["commit", "-m", "case - review: fix"], ["commit", "-am", "fix"], ["commit", "-a", "-m", "fix"],
|
|
["commit", "-m", "/a message, never a path"], ["commit", "-mfix"], ["commit", "--message=fix"],
|
|
["commit", "--message", "fix"],
|
|
):
|
|
with self.subTest(argv):
|
|
self.assertIsNone(refusal(argv, escapes))
|
|
|
|
|
|
class GitEnv(unittest.TestCase):
|
|
def test_pins_git_to_the_clone_and_drops_every_program_the_environment_names(self):
|
|
inherited = {"PATH": "/bin", "GIT_DIR": "/other/.git", "GIT_EDITOR": "sh", "GIT_SEQUENCE_EDITOR": "sh",
|
|
"EDITOR": "sh", "VISUAL": "sh", "PAGER": "sh", "GIT_ASKPASS": "sh", "SSH_ASKPASS": "sh", "GIT_EXTERNAL_DIFF": "sh"}
|
|
env = git_env(inherited, "/clone")
|
|
self.assertEqual({name: env.get(name) for name in (*inherited, "GIT_WORK_TREE")}, {
|
|
"PATH": "/bin", "GIT_DIR": "/clone/.git", "GIT_EDITOR": "true", "GIT_SEQUENCE_EDITOR": None, "EDITOR": None,
|
|
"VISUAL": None, "PAGER": None, "GIT_ASKPASS": None, "SSH_ASKPASS": None, "GIT_EXTERNAL_DIFF": None,
|
|
"GIT_WORK_TREE": "/clone"})
|
|
|
|
def test_names_the_committer_whatever_the_inherited_environment_holds(self):
|
|
env = git_env({"GIT_AUTHOR_NAME": "someone", "EMAIL": "x@y"}, "/clone")
|
|
self.assertEqual([env[name] for name in ("GIT_AUTHOR_NAME", "GIT_COMMITTER_NAME")], ["bench", "bench"])
|
|
|
|
|
|
class Wrapper(unittest.TestCase):
|
|
def setUp(self):
|
|
scratch = tempfile.TemporaryDirectory()
|
|
self.addCleanup(scratch.cleanup)
|
|
self.root = pathlib.Path(scratch.name)
|
|
self.clone = self.root / "clone"
|
|
self.other = self.root / "other"
|
|
self.env = {name: value for name, value in os.environ.items() if not name.startswith(("GIT_", "REVIEW_GIT_"))}
|
|
self.env.update(EMAIL="bench@example.com", GIT_CONFIG_GLOBAL="/dev/null", GIT_CONFIG_NOSYSTEM="1")
|
|
for repo in (self.clone, self.other):
|
|
subprocess.run(["git", "init", "-q", "-b", "chunk", str(repo)], check=True, env=self.env)
|
|
(repo / "a.txt").write_text("one\n")
|
|
subprocess.run(["git", "-C", str(repo), "add", "a.txt"], check=True, env=self.env)
|
|
subprocess.run(["git", "-C", str(repo), "commit", "-qm", "start"], check=True, env=self.env)
|
|
|
|
def wrapper(self, *args, cwd, clone_env=True):
|
|
env = {**self.env, "REVIEW_GIT_CLONE": str(self.clone)} if clone_env else self.env
|
|
return subprocess.run([sys.executable, str(WRAPPER), *args], cwd=cwd, env=env, capture_output=True, text=True)
|
|
|
|
def subjects(self, repo):
|
|
return subprocess.run(["git", "-C", str(repo), "log", "--format=%s"], check=True, env=self.env,
|
|
capture_output=True, text=True).stdout.split("\n")[:-1]
|
|
|
|
def test_an_edit_is_added_and_committed_on_the_clone_whatever_the_working_directory(self):
|
|
(self.clone / "a.txt").write_text("two\n")
|
|
(self.other / "a.txt").write_text("two\n")
|
|
self.wrapper("add", "-A", cwd=self.other)
|
|
self.wrapper("commit", "-m", "case - review: fix", cwd=self.other)
|
|
self.assertEqual((self.subjects(self.clone), self.subjects(self.other)), (["case - review: fix", "start"], ["start"]))
|
|
|
|
def test_a_refused_output_option_leaves_git_config_unchanged(self):
|
|
config = (self.clone / ".git" / "config").read_bytes()
|
|
ran = self.wrapper("diff", "--output=.git/config", cwd=self.clone)
|
|
self.assertEqual((ran.returncode != 0, (self.clone / ".git" / "config").read_bytes()), (True, config))
|
|
|
|
def test_runs_no_program_that_a_config_or_hook_names(self):
|
|
marker = self.root / "ran"
|
|
program = self.root / "program.sh"
|
|
program.write_text(f"#!/bin/sh\ntouch {marker}\n")
|
|
program.chmod(0o755)
|
|
home = self.root / "home"
|
|
home.mkdir()
|
|
(home / ".gitconfig").write_text(f"[diff]\n\texternal = {program}\n")
|
|
(self.clone / ".git" / "hooks" / "pre-commit").write_text(f"#!/bin/sh\ntouch {marker}\n")
|
|
(self.clone / ".git" / "hooks" / "pre-commit").chmod(0o755)
|
|
with (self.clone / ".git" / "config").open("a") as config:
|
|
config.write(f"[core]\n\tfsmonitor = {program}\n")
|
|
(self.clone / "a.txt").write_text("two\n")
|
|
self.env["HOME"] = str(home)
|
|
self.wrapper("diff", cwd=self.clone)
|
|
self.wrapper("status", cwd=self.clone)
|
|
self.wrapper("commit", "-a", "-m", "x", cwd=self.clone)
|
|
self.assertFalse(marker.exists())
|
|
|
|
def test_refuses_to_run_without_the_clone_named(self):
|
|
(self.clone / "a.txt").write_text("two\n")
|
|
ran = self.wrapper("diff", cwd=self.clone, clone_env=False)
|
|
self.assertEqual((ran.returncode != 0, ran.stdout), (True, ""))
|
|
|
|
def test_refuses_a_link_in_the_clone_to_a_file_outside_it(self):
|
|
(self.root / "secret").write_text("token\n")
|
|
(self.clone / "link-out").symlink_to(self.root / "secret")
|
|
ran = self.wrapper("diff", "--no-color", "link-out", cwd=self.clone)
|
|
self.assertEqual((ran.returncode != 0, "token" in ran.stdout), (True, False))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|