-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_github_script.py
More file actions
177 lines (148 loc) · 4.79 KB
/
Copy pathtest_github_script.py
File metadata and controls
177 lines (148 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import sys
import pytest
import requests_mock
import gardenlinux.github.release.__main__ as gh
from gardenlinux.constants import GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME
from ..constants import TEST_GARDENLINUX_COMMIT, TEST_GARDENLINUX_RELEASE
from .constants import REPO_JSON
def test_script_parse_args_wrong_command(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(sys, "argv", ["gh", "rejoice"])
with pytest.raises(SystemExit):
gh.main()
captured = capfd.readouterr()
assert "argument command: invalid choice: 'rejoice'" in captured.err, (
"Expected help message printed"
)
def test_script_parse_args_create_command_required_args(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(
sys,
"argv",
[
"gh",
"create-with-gl-release-notes",
"--owner",
"gardenlinux",
"--repo",
"gardenlinux",
],
)
with pytest.raises(SystemExit):
gh.main()
captured = capfd.readouterr()
assert "the following arguments are required: --tag, --commit" in captured.err, (
"Expected help message on missing arguments for 'create' command"
)
def test_script_parse_args_upload_command_required_args(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(
sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", "gardenlinux"]
)
with pytest.raises(SystemExit):
gh.main()
captured = capfd.readouterr()
assert (
"the following arguments are required: --release_id, --file_path"
in captured.err
), "Expected help message on missing arguments for 'upload' command"
def test_script_create_dry_run(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(
sys,
"argv",
[
"gh",
"create-with-gl-release-notes",
"--owner",
"gardenlinux",
"--repo",
"gardenlinux",
"--tag",
TEST_GARDENLINUX_RELEASE,
"--commit",
TEST_GARDENLINUX_COMMIT,
"--dry-run",
],
)
monkeypatch.setattr(
"gardenlinux.github.release.__main__.create_github_release_notes",
lambda tag, commit, bucket: f"{tag} {commit} {bucket}",
)
gh.main()
captured = capfd.readouterr()
assert (
captured.out
== f"Dry Run ...\nThis release would be created:\n{TEST_GARDENLINUX_RELEASE} {TEST_GARDENLINUX_COMMIT} {GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME}\n"
), "Expected dry-run create to return generated release notes text"
def test_script_create(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
with requests_mock.Mocker() as m:
monkeypatch.setattr(
sys,
"argv",
[
"gh",
"create-with-gl-release-notes",
"--owner",
"gardenlinux",
"--repo",
"gardenlinux",
"--tag",
TEST_GARDENLINUX_RELEASE,
"--commit",
TEST_GARDENLINUX_COMMIT,
],
)
monkeypatch.setattr(
"gardenlinux.github.release.__main__.create_github_release_notes",
lambda tag, commit, bucket: f"{tag} {commit} {bucket}",
)
monkeypatch.setenv("GITHUB_TOKEN", "invalid")
m.get(
"//api.github.com:443/repos/gardenlinux/gardenlinux",
json=REPO_JSON,
status_code=200,
)
m.post(
"//api.github.com:443/repos/gardenlinux/gardenlinux/releases",
json={"id": 101},
status_code=201,
)
gh.main()
assert any(
"Release created with ID: 101" in record.message
for record in caplog.records
), "Expected a release creation confirmation log entry"
def test_script_upload_dry_run(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(
sys,
"argv",
[
"gh",
"upload",
"--owner",
"gardenlinux",
"--repo",
"gardenlinux",
"--release_id",
TEST_GARDENLINUX_RELEASE,
"--file_path",
"foo",
"--dry-run",
],
)
monkeypatch.setattr(
"gardenlinux.github.release.__main__.upload_to_github_release_page",
lambda a1, a2, a3, a4, dry_run: print(f"dry-run: {dry_run}"),
)
gh.main()
captured = capfd.readouterr()
assert captured.out == "dry-run: True\n"