Skip to content

Commit f33a40a

Browse files
committed
cleanup; tests for github cli
1 parent f3a3b37 commit f33a40a

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

tests/github/test_create_github_release_notes.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,17 @@ def test_github_release_page(monkeypatch, downloads_dir, release_s3_bucket):
7070
class SubmoduleAsRepo(Repo):
7171
"""This will fake a git submodule as a git repository object."""
7272
def __new__(cls, *args, **kwargs):
73-
print('In SubmoduleAsRepo.__new__')
7473
r = super().__new__(Repo)
7574
r.__init__(*args, **kwargs)
76-
print(f'{r=}')
7775

7876
maybe_gl_submodule = [submodule for submodule in r.submodules if submodule.name.endswith("/gardenlinux")]
7977
if not maybe_gl_submodule:
8078
return r
8179
else:
8280
gl = maybe_gl_submodule[0]
83-
print(f'{gl=}')
8481

8582
sr = gl.module()
86-
print(f'{sr=}')
8783
sr.remotes.origin.pull("main")
88-
print('git pull done')
8984
return sr
9085

9186
monkeypatch.setattr("gardenlinux.github.release_notes.helpers.Repo", SubmoduleAsRepo)

tests/github/test_github_script.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import sys
2+
3+
import pytest
4+
5+
import gardenlinux.github.__main__ as gh
6+
from gardenlinux.constants import GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME
7+
8+
from ..constants import TEST_GARDENLINUX_COMMIT, TEST_GARDENLINUX_RELEASE
9+
10+
11+
def test_script_parse_args_wrong_command(monkeypatch, capfd):
12+
monkeypatch.setattr(sys, "argv", ["gh", "rejoice"])
13+
14+
with pytest.raises(SystemExit):
15+
gh.main()
16+
captured = capfd.readouterr()
17+
18+
assert "argument command: invalid choice: 'rejoice'" in captured.err, "Expected help message printed"
19+
20+
21+
def test_script_parse_args_create_command_required_args(monkeypatch, capfd):
22+
monkeypatch.setattr(sys, "argv", ["gh", "create", "--owner", "gardenlinux", "--repo", "gardenlinux"])
23+
24+
with pytest.raises(SystemExit):
25+
gh.main()
26+
captured = capfd.readouterr()
27+
28+
assert "the following arguments are required: --tag, --commit" in captured.err, \
29+
"Expected help message on missing arguments for 'create' command"
30+
31+
32+
def test_script_parse_args_upload_command_required_args(monkeypatch, capfd):
33+
monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", "gardenlinux"])
34+
35+
with pytest.raises(SystemExit):
36+
gh.main()
37+
captured = capfd.readouterr()
38+
39+
assert "the following arguments are required: --release_id, --file_path" in captured.err, \
40+
"Expected help message on missing arguments for 'upload' command"
41+
42+
43+
def test_script_create_dry_run(monkeypatch, capfd):
44+
45+
monkeypatch.setattr(sys, "argv", ["gh", "create", "--owner", "gardenlinux", "--repo",
46+
"gardenlinux", "--tag", TEST_GARDENLINUX_RELEASE, "--commit", TEST_GARDENLINUX_COMMIT, "--dry-run"])
47+
monkeypatch.setattr("gardenlinux.github.__main__.create_github_release_notes",
48+
lambda tag, commit, bucket: f"{tag} {commit} {bucket}")
49+
50+
gh.main()
51+
captured = capfd.readouterr()
52+
53+
assert captured.out == f"Dry Run ...\nThis release would be created:\n{TEST_GARDENLINUX_RELEASE} {TEST_GARDENLINUX_COMMIT} {GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME}\n", \
54+
"Expected dry-run create to return generated release notes text"
55+
56+
57+
def test_script_create(monkeypatch, caplog):
58+
monkeypatch.setattr(sys, "argv", ["gh", "create", "--owner", "gardenlinux", "--repo",
59+
"gardenlinux", "--tag", TEST_GARDENLINUX_RELEASE, "--commit", TEST_GARDENLINUX_COMMIT])
60+
monkeypatch.setattr("gardenlinux.github.__main__.create_github_release_notes",
61+
lambda tag, commit, bucket: f"{tag} {commit} {bucket}")
62+
monkeypatch.setattr("gardenlinux.github.__main__.create_github_release",
63+
lambda a1, a2, a3, a4, a5, a6: TEST_GARDENLINUX_RELEASE)
64+
65+
gh.main()
66+
67+
assert any(f"Release created with ID: {TEST_GARDENLINUX_RELEASE}" in record.message for record in caplog.records), \
68+
"Expected a release creation confirmation log entry"
69+
70+
71+
def test_script_upload_dry_run(monkeypatch, capfd):
72+
monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo",
73+
"gardenlinux", "--release_id", TEST_GARDENLINUX_RELEASE, "--file_path", "foo", "--dry-run"])
74+
monkeypatch.setattr("gardenlinux.github.__main__.upload_to_github_release_page",
75+
lambda a1, a2, a3, a4, dry_run: print(f"dry-run: {dry_run}"))
76+
77+
gh.main()
78+
captured = capfd.readouterr()
79+
80+
assert captured.out == "dry-run: True\n"

0 commit comments

Comments
 (0)