Skip to content

Commit c6a293d

Browse files
committed
Add tests
1 parent c1d68f8 commit c6a293d

File tree

8 files changed

+357
-141
lines changed

8 files changed

+357
-141
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ jobs:
5858

5959
- name: Check python types
6060
run: uv run mypy .
61+
62+
- name: Install
63+
run: pip install --user --break-system-packages .
64+
65+
- name: Run tests
66+
run: bash run_tests.sh

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
default_install_hook_types:
22
- pre-commit
3+
- pre-push
34
default_stages:
45
- pre-commit
56
fail_fast: true
@@ -44,3 +45,9 @@ repos:
4445
language: system
4546
pass_filenames: false
4647
files: \.py$
48+
- id: py-test
49+
name: pytest
50+
stages: [pre-push]
51+
entry: bash run_tests.sh
52+
language: system
53+
pass_filenames: false

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ options:
7272
uv run ruff format
7373
uv run ruff check --fix --exit-non-zero-on-fix
7474
uv run mypy .
75+
uv run pytest -vvvs
7576
```
7677

7778
### Performance

git_evtag_py.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ def sign_tree_checksum(
136136
footer = f"\n\nGit-EVTag-v0-SHA512: {in_csum}\n"
137137
final_msg = cleaned_msg + footer
138138

139-
subprocess.run(
140-
["git", "tag", "-a", "-s", tag, commit, "-m", final_msg],
141-
check=True,
142-
cwd=repo,
143-
)
139+
tag_args = ["git", "tag", "-a"]
140+
if environ.get("EVTAG_NO_GPG_SIGN") != "true":
141+
tag_args.append("-s")
142+
143+
tag_args.extend([tag, commit, "-m", final_msg])
144+
subprocess.run(tag_args, check=True, cwd=repo)
144145

145146

146147
def is_tag_signature_valid(repo: Path, tag: str) -> bool:

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dev = [
2626
"pre-commit<4.0.0,>=3.8.0",
2727
"mypy<2.0.0,>=1.11.2",
2828
"ruff<1.0.0,>=0.6.7",
29+
"pytest<9.0.0,>=8.3.3",
2930
]
3031

3132
[tool.mypy]
@@ -92,3 +93,8 @@ ignore = [
9293
[tool.ruff.format]
9394
line-ending = "lf"
9495
quote-style = "double"
96+
97+
[tool.pytest.ini_options]
98+
testpaths = [
99+
"tests.py",
100+
]

run_tests.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
export KEY_URL="https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xd26d753395009db2b3b260940c3251a24745e484"
5+
export EVTAG_NO_GPG_SIGN="true"
6+
7+
if [[ -n "${TEST_CMD:-}" ]]; then
8+
eval "$TEST_CMD"
9+
elif command -v uv >/dev/null 2>&1; then
10+
uv run --frozen -q pytest --ff --exitfirst
11+
else
12+
pytest --ff --exitfirst
13+
fi
14+
15+
unset KEY_URL EVTAG_NO_GPG_SIGN

tests.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)