-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_env.py
More file actions
434 lines (405 loc) · 13.5 KB
/
test_env.py
File metadata and controls
434 lines (405 loc) · 13.5 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
"""Test the get_env_vars function"""
import os
import unittest
from unittest.mock import patch
from env import EnvVars, get_env_vars, get_int_env_var
class TestEnv(unittest.TestCase):
"""Test the get_env_vars function"""
def setUp(self):
env_keys = [
"DRY_RUN",
"ENABLE_GITHUB_ACTIONS_STEP_SUMMARY",
"EXEMPT_PRS",
"EXEMPT_REPOS",
"GH_APP_ID",
"GH_APP_INSTALLATION_ID",
"GH_APP_PRIVATE_KEY",
"GH_ENTERPRISE_URL",
"GH_TOKEN",
"GITHUB_APP_ENTERPRISE_ONLY",
"INCLUDE_DRAFTS",
"ORGANIZATION",
"OUTPUT_FILE",
"REPORT_TITLE",
"REPOSITORY",
"SLACK_CHANNEL",
"SLACK_WEBHOOK_URL",
"VERIFY_CONFLICTS",
"FILTER_AUTHORS",
"FILTER_TEAMS",
]
for key in env_keys:
if key in os.environ:
del os.environ[key]
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"EXEMPT_REPOS": "repo4,repo5",
"EXEMPT_PRS": "10,20,30",
"INCLUDE_DRAFTS": "false",
"VERIFY_CONFLICTS": "true",
"DRY_RUN": "true",
"REPORT_TITLE": "Custom Report",
"OUTPUT_FILE": "custom_report.md",
"SLACK_WEBHOOK_URL": "https://hooks.slack.com/test",
"SLACK_CHANNEL": "#alerts",
"ENABLE_GITHUB_ACTIONS_STEP_SUMMARY": "false",
},
clear=True,
)
def test_get_env_vars_with_org(self):
"""Test that all environment variables are set correctly using an organization"""
expected_result = EnvVars(
gh_app_id=None,
gh_app_installation_id=None,
gh_app_private_key_bytes=b"",
gh_app_enterprise_only=False,
token="my_token",
ghe="",
organization="my_organization",
repository_list=[],
include_drafts=False,
verify_conflicts=True,
exempt_repos=["repo4", "repo5"],
exempt_prs=[10, 20, 30],
dry_run=True,
report_title="Custom Report",
output_file="custom_report.md",
slack_webhook_url="https://hooks.slack.com/test",
slack_channel="#alerts",
enable_github_actions_step_summary=False,
enable_pr_comments=False,
enable_report_issues=True,
filter_authors=[],
filter_teams=[],
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(
os.environ,
{
"REPOSITORY": "org/repo1,org2/repo2",
"GH_TOKEN": "my_token",
},
clear=True,
)
def test_get_env_vars_with_repos(self):
"""Test that all environment variables are set correctly using a list of repositories"""
expected_result = EnvVars(
gh_app_id=None,
gh_app_installation_id=None,
gh_app_private_key_bytes=b"",
gh_app_enterprise_only=False,
token="my_token",
ghe="",
organization=None,
repository_list=["org/repo1", "org2/repo2"],
include_drafts=True,
verify_conflicts=False,
exempt_repos=[],
exempt_prs=[],
dry_run=False,
report_title="PR Conflict Report",
output_file="pr_conflict_report.md",
slack_webhook_url="",
slack_channel="",
enable_github_actions_step_summary=True,
filter_authors=[],
filter_teams=[],
enable_pr_comments=False,
enable_report_issues=True,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
},
clear=True,
)
def test_get_env_vars_optional_values(self):
"""Test that optional values are set to their default values if not provided"""
expected_result = EnvVars(
gh_app_id=None,
gh_app_installation_id=None,
gh_app_private_key_bytes=b"",
gh_app_enterprise_only=False,
token="my_token",
ghe="",
organization="my_organization",
repository_list=[],
include_drafts=True,
verify_conflicts=False,
exempt_repos=[],
exempt_prs=[],
dry_run=False,
report_title="PR Conflict Report",
output_file="pr_conflict_report.md",
slack_webhook_url="",
slack_channel="",
enable_github_actions_step_summary=True,
filter_authors=[],
filter_teams=[],
enable_pr_comments=False,
enable_report_issues=True,
)
result = get_env_vars(True)
self.assertEqual(result, expected_result)
@patch.dict(
os.environ,
{
"GH_TOKEN": "my_token",
},
clear=True,
)
def test_get_env_vars_missing_org_and_repo(self):
"""Test that an error is raised if neither ORGANIZATION nor REPOSITORY is set"""
with self.assertRaises(ValueError) as cm:
get_env_vars(True)
self.assertEqual(
str(cm.exception),
"ORGANIZATION and REPOSITORY environment variables were not set. "
"Please set one",
)
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
},
clear=True,
)
def test_get_env_vars_missing_token(self):
"""Test that an error is raised if GH_TOKEN is not set and no app credentials"""
with self.assertRaises(ValueError) as cm:
get_env_vars(True)
self.assertEqual(
str(cm.exception),
"GH_TOKEN environment variable not set",
)
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_APP_ID": "12345",
"GH_APP_PRIVATE_KEY": "my_private_key",
"GH_APP_INSTALLATION_ID": "67890",
},
clear=True,
)
def test_get_env_vars_with_gh_app(self):
"""Test that GitHub App credentials are parsed correctly"""
result = get_env_vars(True)
self.assertEqual(result.gh_app_id, 12345)
self.assertEqual(result.gh_app_installation_id, 67890)
self.assertEqual(result.gh_app_private_key_bytes, b"my_private_key")
self.assertEqual(result.token, "")
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_APP_ID": "12345",
},
clear=True,
)
def test_get_env_vars_gh_app_missing_installation_id(self):
"""Test error when GH_APP_ID is set but installation ID or private key is missing"""
with self.assertRaises(ValueError) as cm:
get_env_vars(True)
self.assertEqual(
str(cm.exception),
"GH_APP_ID set and GH_APP_INSTALLATION_ID or GH_APP_PRIVATE_KEY "
"variable not set",
)
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"GH_ENTERPRISE_URL": " https://ghe.example.com ",
},
clear=True,
)
def test_get_env_vars_ghe_url_stripped(self):
"""Test that GH_ENTERPRISE_URL is stripped of whitespace"""
result = get_env_vars(True)
self.assertEqual(result.ghe, "https://ghe.example.com")
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"GITHUB_APP_ENTERPRISE_ONLY": "true",
},
clear=True,
)
def test_get_env_vars_enterprise_only(self):
"""Test that GITHUB_APP_ENTERPRISE_ONLY is parsed correctly"""
result = get_env_vars(True)
self.assertTrue(result.gh_app_enterprise_only)
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"EXEMPT_PRS": "1,abc,3",
},
clear=True,
)
def test_get_env_vars_exempt_prs_invalid(self):
"""Test that an error is raised if EXEMPT_PRS contains non-integer values"""
with self.assertRaises(ValueError) as cm:
get_env_vars(True)
self.assertIn("non-integer value", str(cm.exception))
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"EXEMPT_REPOS": " repo1 , repo2 , repo3 ",
},
clear=True,
)
def test_get_env_vars_exempt_repos_whitespace(self):
"""Test that EXEMPT_REPOS handles whitespace correctly"""
result = get_env_vars(True)
self.assertEqual(result.exempt_repos, ["repo1", "repo2", "repo3"])
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"EXEMPT_PRS": " 10 , 20 , 30 ",
},
clear=True,
)
def test_get_env_vars_exempt_prs_whitespace(self):
"""Test that EXEMPT_PRS handles whitespace correctly"""
result = get_env_vars(True)
self.assertEqual(result.exempt_prs, [10, 20, 30])
@patch.dict(
os.environ,
{
"REPOSITORY": " org/repo1 , org/repo2 ",
"GH_TOKEN": "my_token",
},
clear=True,
)
def test_get_env_vars_repository_whitespace(self):
"""Test that REPOSITORY handles whitespace correctly"""
result = get_env_vars(True)
self.assertEqual(result.repository_list, ["org/repo1", "org/repo2"])
def test_get_int_env_var_invalid_value(self):
"""Test get_int_env_var with a non-integer value."""
with patch.dict(os.environ, {"TEST_VAR": "not_a_number"}):
result = get_int_env_var("TEST_VAR")
self.assertIsNone(result)
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"FILTER_AUTHORS": "alice, @bob , charlie",
},
clear=True,
)
def test_get_env_vars_filter_authors(self):
"""Test that FILTER_AUTHORS parses correctly with @ prefix stripping"""
result = get_env_vars(True)
self.assertEqual(result.filter_authors, ["alice", "bob", "charlie"])
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"FILTER_AUTHORS": "",
},
clear=True,
)
def test_get_env_vars_filter_authors_empty(self):
"""Test that empty FILTER_AUTHORS results in empty list"""
result = get_env_vars(True)
self.assertEqual(result.filter_authors, [])
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
},
clear=True,
)
def test_get_env_vars_filter_authors_not_set(self):
"""Test that unset FILTER_AUTHORS defaults to empty list"""
result = get_env_vars(True)
self.assertEqual(result.filter_authors, [])
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"FILTER_TEAMS": "my-org/team-a, my-org/team-b",
},
clear=True,
)
def test_get_env_vars_filter_teams(self):
"""Test that FILTER_TEAMS parses correctly with whitespace handling"""
result = get_env_vars(True)
self.assertEqual(result.filter_teams, ["my-org/team-a", "my-org/team-b"])
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"FILTER_TEAMS": "",
},
clear=True,
)
def test_get_env_vars_filter_teams_empty(self):
"""Test that empty FILTER_TEAMS results in empty list"""
result = get_env_vars(True)
self.assertEqual(result.filter_teams, [])
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
},
clear=True,
)
def test_get_env_vars_filter_teams_not_set(self):
"""Test that unset FILTER_TEAMS defaults to empty list"""
result = get_env_vars(True)
self.assertEqual(result.filter_teams, [])
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"FILTER_TEAMS": "invalid-no-slash",
},
clear=True,
)
def test_get_env_vars_filter_teams_invalid_format(self):
"""Test that malformed FILTER_TEAMS entries raise ValueError"""
with self.assertRaises(ValueError) as ctx:
get_env_vars(True)
self.assertIn("invalid-no-slash", str(ctx.exception))
self.assertIn("org/team-slug", str(ctx.exception))
@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"FILTER_TEAMS": "valid-org/team-a, /missing-org",
},
clear=True,
)
def test_get_env_vars_filter_teams_empty_org_raises(self):
"""Test that team entries with empty org part raise ValueError"""
with self.assertRaises(ValueError):
get_env_vars(True)
if __name__ == "__main__":
unittest.main()