|
| 1 | +# ruff: noqa: S101 |
| 2 | + |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | +from os import environ |
| 7 | +from pathlib import Path |
| 8 | +from urllib.request import urlopen |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +def run_git(repo: Path, *args: str) -> str: |
| 14 | + result = subprocess.run( |
| 15 | + ["git", *list(args)], |
| 16 | + cwd=repo, |
| 17 | + check=True, |
| 18 | + capture_output=True, |
| 19 | + text=True, |
| 20 | + ) |
| 21 | + return result.stdout.strip() |
| 22 | + |
| 23 | + |
| 24 | +def clone_repo( |
| 25 | + url: str = "https://github.com/bbhtt/git-evtag-py.git", |
| 26 | +) -> tuple[Path, str]: |
| 27 | + tmpdir = tempfile.mkdtemp() |
| 28 | + repo = Path(tmpdir) / "test_repo" |
| 29 | + run_git(Path(tmpdir), "clone", url, str(repo)) |
| 30 | + return repo, tmpdir |
| 31 | + |
| 32 | + |
| 33 | +def import_gpg_key() -> None: |
| 34 | + key_url = environ.get("KEY_URL") |
| 35 | + |
| 36 | + if key_url and key_url.startswith("https://"): |
| 37 | + with urlopen(key_url) as response: # noqa: S310 |
| 38 | + key_data = response.read().decode("utf-8") |
| 39 | + else: |
| 40 | + key_data = Path("pub_key.asc").read_text() |
| 41 | + |
| 42 | + subprocess.run( |
| 43 | + ["gpg", "--import"], |
| 44 | + input=key_data, |
| 45 | + text=True, |
| 46 | + check=True, |
| 47 | + capture_output=True, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def test_flow() -> None: |
| 52 | + import_gpg_key() |
| 53 | + repo, tmpdir = clone_repo() |
| 54 | + try: |
| 55 | + assert (repo / ".git").is_dir() |
| 56 | + |
| 57 | + run_git(repo, "config", "user.name", "bbhtt") |
| 58 | + run_git( repo, "config", "user.email", "[email protected]") |
| 59 | + |
| 60 | + run_git(repo, "checkout", "v1.0.1") |
| 61 | + sha = run_git(repo, "rev-parse", "HEAD") |
| 62 | + assert sha == "295a273a2af003e57edd369f7f7e83155c36a074" |
| 63 | + |
| 64 | + expected_evtag = ( |
| 65 | + "Git-EVTag-v0-SHA512: " |
| 66 | + "3973d3ad28248971e6c870936012f44c512df1700cde0cf80713aced9a134fff" |
| 67 | + "0f71eb99d87d45fd8b6c7a6fa32ec095b3bc529d09e7774ca07cefd53b1ee802" |
| 68 | + ) |
| 69 | + |
| 70 | + assert run_git(repo, "evtag") == expected_evtag |
| 71 | + |
| 72 | + run_git(repo, "checkout", "main") |
| 73 | + for ref in ("v1.0.1", sha): |
| 74 | + assert run_git(repo, "evtag", "--rev", ref) == expected_evtag |
| 75 | + |
| 76 | + run_git(repo, "evtag", "--verify", "v1.0.8") |
| 77 | + with pytest.raises(subprocess.CalledProcessError): |
| 78 | + run_git(repo, "evtag", "--verify", "v1.0.1") |
| 79 | + |
| 80 | + run_git(repo, "checkout", "v1.0.1") |
| 81 | + run_git(repo, "evtag", "--sign", "v-test", "-m", "Testing evtag in automation") |
| 82 | + tag_msg = run_git(repo, "show", "v-test") |
| 83 | + lines: set[str] = {line.strip() for line in tag_msg.splitlines()} |
| 84 | + assert "Testing evtag in automation" in lines |
| 85 | + assert expected_evtag in lines |
| 86 | + |
| 87 | + expected_evtag = ( |
| 88 | + "Git-EVTag-v0-SHA512: " |
| 89 | + "b078bd1616243c65346ff47370ddf1f2e99dda350c9093b079f24647d122d38" |
| 90 | + "5d8a5080a1645716dcec7530690f5537fc33f9c44e06ed3d265ff74fddf0cca74" |
| 91 | + ) |
| 92 | + run_git(repo, "checkout", "v1.0.8") |
| 93 | + msg_file = repo / "tagmsg.txt" |
| 94 | + exp_msg = "Testing evtag tagging using tag message file" |
| 95 | + msg_file.write_text(exp_msg) |
| 96 | + run_git(repo, "evtag", "--sign", "v-test-2", "-F", str(msg_file)) |
| 97 | + lines_t: set[str] = { |
| 98 | + line.strip() for line in run_git(repo, "show", "v-test-2").splitlines() |
| 99 | + } |
| 100 | + assert exp_msg in lines_t |
| 101 | + assert expected_evtag in lines_t |
| 102 | + finally: |
| 103 | + shutil.rmtree(tmpdir) |
0 commit comments