From f82612bc3df6365f93476f2989c6237110787098 Mon Sep 17 00:00:00 2001 From: Dan Nicholson Date: Mon, 15 Apr 2024 09:38:26 -0600 Subject: [PATCH] tests: Check git commit data Test that the git commit was made with the correct values in the metadata. Primarily this is useful to make sure that commit authorship is being done correctly. --- tests/test_main.py | 48 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index f45a61fb..b74b64b3 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -48,15 +48,59 @@ def _clear_environment(self): for var in unwanted_vars: os.environ.pop(var, None) - def _run_cmd(self, cmd): - return subprocess.run(cmd, cwd=self.test_dir.name, check=True) + def _run_cmd(self, cmd, **kwargs): + return subprocess.run(cmd, cwd=self.test_dir.name, check=True, **kwargs) + + def _get_commit_data(self, rev="HEAD"): + data = {} + for name, fmt in [ + ("commit", "%H"), + ("subject", "%s"), + ("body", "%b"), + ("author_name", "%an"), + ("author_email", "%ae"), + ("committer_name", "%cn"), + ("committer_email", "%ce"), + ]: + cmd = ["git", "show", "--no-patch", f"--pretty=format:{fmt}", rev] + proc = self._run_cmd(cmd, stdout=subprocess.PIPE) + output = proc.stdout.decode("utf-8") + data[name] = output + + return data async def test_full_run(self): args1 = main.parse_cli_args(["--update", "--commit-only", self.manifest_path]) self.assertEqual(await main.run_with_args(args1), (2, 0, True)) + + commit_data = self._get_commit_data() + self.assertEqual(commit_data["subject"], "Update 2 modules") + self.assertEqual(commit_data["author_name"], "Test Runner") + self.assertEqual(commit_data["author_email"], "test@localhost") + self.assertEqual(commit_data["committer_name"], "Test Runner") + self.assertEqual(commit_data["committer_email"], "test@localhost") + + body_lines = commit_data["body"].splitlines() + self.assertEqual(len(body_lines), 2) + self.assertRegex(body_lines[0], r"^Update libXaw-1.0.12.tar.bz2 to ") + self.assertRegex(body_lines[1], r"^Update xterm-snapshots.git to ") + args2 = main.parse_cli_args([self.manifest_path]) self.assertEqual(await main.run_with_args(args2), (0, 0, False)) + async def test_git_envvars(self): + os.environ["GIT_AUTHOR_NAME"] = "Some Guy" + os.environ["GIT_AUTHOR_EMAIL"] = "someguy@localhost" + args1 = main.parse_cli_args(["--update", "--commit-only", self.manifest_path]) + self.assertEqual(await main.run_with_args(args1), (2, 0, True)) + + commit_data = self._get_commit_data() + self.assertEqual(commit_data["subject"], "Update 2 modules") + self.assertEqual(commit_data["author_name"], "Some Guy") + self.assertEqual(commit_data["author_email"], "someguy@localhost") + self.assertEqual(commit_data["committer_name"], "Test Runner") + self.assertEqual(commit_data["committer_email"], "test@localhost") + class TestForceForkTristate(unittest.TestCase): def test_neither_fork_arg(self):