-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_user_yaml.py
567 lines (541 loc) · 21.1 KB
/
test_user_yaml.py
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import datetime
from copy import deepcopy
from freezegun import freeze_time
from shared.components import Component
from shared.config import (
LEGACY_DEFAULT_SITE_CONFIG,
PATCH_CENTRIC_DEFAULT_CONFIG,
PATCH_CENTRIC_DEFAULT_TIME_START,
)
from shared.yaml import UserYaml, merge_yamls
from shared.yaml.user_yaml import (
OwnerContext,
_fix_yaml_defaults_based_on_time,
_get_possible_additional_user_yaml,
)
class TestYamlMerge(object):
def test_merge_yamls(self):
d1 = {
"key_one": "value",
"key_two": {"sub": "p"},
"key_three": "super",
"key_four": "pro",
}
d2 = {"key_two": "textnow", "key_three": "super", "key_four": "mega"}
first_expected_result = {
"key_four": "mega",
"key_one": "value",
"key_three": "super",
"key_two": "textnow",
}
assert first_expected_result == merge_yamls(d1, d2)
second_expected_result = {
"key_four": "pro",
"key_one": "value",
"key_three": "super",
"key_two": {"sub": "p"},
}
assert second_expected_result == merge_yamls(d2, d1)
class TestUserYaml(object):
def test_init(self):
d = {"value": "sample"}
v = UserYaml(d)
assert v is not None
assert v["value"] == "sample"
assert str(v) == "UserYaml<{'value': 'sample'}>"
def test_from_dict_to_dict(self):
d = {"value": "sample"}
v = UserYaml.from_dict(d)
assert v is not None
assert v["value"] == "sample"
assert v.get("value") == "sample"
assert v.get("notthere") is None
assert v.get("hshshshsh", "p") == "p"
dicted = v.to_dict()
assert dicted == d
assert dicted is not d
dicted["l"] = "banana"
assert d == {"value": "sample"}
assert dicted == {"value": "sample", "l": "banana"}
def test_eq(self):
d1 = {
"key_one": "value",
"key_two": {"sub": "p"},
"key_three": "super",
"key_four": "pro",
}
d2 = {"key_two": "textnow", "key_three": "super", "key_four": "mega"}
assert UserYaml(d1) == UserYaml(d1)
assert UserYaml(d1) != UserYaml(d2)
assert UserYaml(d1) != d1
def test_read_yaml_field(self):
my_dict = {
"key_one": "value",
"key_two": {"sub": "p"},
"key_three": "super",
"key_four": "pro",
}
subject = UserYaml(my_dict)
assert subject.read_yaml_field("key_two", "sub") == "p"
assert subject.read_yaml_field("key_two", "kowabunga", _else=23) == 23
def test_has_any_carryforward(self):
assert UserYaml(
{"flags": {"banana": {"carryforward": True}}, "flag_management": {}}
).has_any_carryforward()
assert not UserYaml(
{"flags": {"banana": {"carryforward": False}}, "flag_management": {}}
).has_any_carryforward()
assert UserYaml(
{
"flags": {"banana": {"carryforward": False}},
"flag_management": {"default_rules": {"carryforward": True}},
}
).has_any_carryforward()
assert UserYaml(
{
"flags": {"banana": {"carryforward": False}},
"flag_management": {
"default_rules": {"carryforward": False},
"individual_flags": [{"name": "strawberry", "carryforward": True}],
},
}
).has_any_carryforward()
def test_flag_has_carryfoward(self):
assert UserYaml(
{"flags": {"banana": {"carryforward": True}}, "flag_management": {}}
).flag_has_carryfoward("banana")
assert not UserYaml(
{"flags": {"banana": {"carryforward": False}}, "flag_management": {}}
).flag_has_carryfoward("banana")
subject = UserYaml(
{
"flags": {"banana": {"carryforward": False}},
"flag_management": {"default_rules": {"carryforward": True}},
}
)
assert not subject.flag_has_carryfoward("banana")
assert subject.flag_has_carryfoward("strawberry")
subject_2 = UserYaml(
{
"flags": {"banana": {"carryforward": False}},
"flag_management": {
"default_rules": {"carryforward": False},
"individual_flags": [{"name": "strawberry", "carryforward": True}],
},
}
)
assert not subject_2.flag_has_carryfoward("banana")
assert subject_2.flag_has_carryfoward("strawberry")
assert not subject_2.flag_has_carryfoward("pineapple")
def test_get_flag_configuration(self):
old_style = UserYaml({"flags": {"banana": {"key_one": True}}})
assert old_style.get_flag_configuration("banana") == {"key_one": True}
assert old_style.get_flag_configuration("pineapple") is None
assert UserYaml(
{"flags": {"banana": {"key_one": True}}, "flag_management": {}}
).get_flag_configuration("banana") == {"key_one": True}
assert UserYaml(
{
"flag_management": {
"default_rules": {"key_one": False, "key_two": "something"}
}
}
).get_flag_configuration("banana") == {"key_one": False, "key_two": "something"}
subject = UserYaml(
{
"flags": {"banana": {"carryforward": False}},
"flag_management": {
"default_rules": {"key_one": False, "key_two": "something"}
},
}
)
assert subject.get_flag_configuration("banana") == {"carryforward": False}
assert subject.get_flag_configuration("strawberry") == {
"key_one": False,
"key_two": "something",
}
subject_2 = UserYaml(
{
"flags": {"banana": {"carryforward": False}},
"flag_management": {
"default_rules": {"key_one": False, "key_two": "something"},
"individual_flags": [
{
"name": "strawberry",
"key_one": True,
"key_three": ["array", "values"],
}
],
},
}
)
assert subject_2.get_flag_configuration("banana") == {"carryforward": False}
assert subject_2.get_flag_configuration("strawberry") == {
"key_one": True,
"key_three": ["array", "values"],
"key_two": "something",
"name": "strawberry",
}
assert subject_2.get_flag_configuration("pineapple") == {
"key_one": False,
"key_two": "something",
}
def test_get_final_yaml(self, mock_configuration):
mock_configuration._params["site"] = {"codecov": {"max_report_age": 86400}}
owner_yaml = {"key": {"value": "one", "a": "b"}}
repo_yaml = {"barber": "shop"}
commit_yaml = {"key": {"value": "two", "c": "d"}}
expected_result = {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
}
assert (
UserYaml.get_final_yaml(
owner_yaml=owner_yaml, repo_yaml=repo_yaml, commit_yaml=commit_yaml
).to_dict()
== expected_result
)
def test_get_final_yaml_with_additional_user_yaml(self, mock_configuration):
mock_configuration._params["site"] = {"codecov": {"max_report_age": 86400}}
mock_configuration._params["additional_user_yamls"] = [
{"percentage": 10, "name": "banana", "override": {"a": 2, "b": 3}},
{"percentage": 30, "name": "apple", "override": {"d": "klmnop", "b": 3}},
]
owner_yaml = {"key": {"value": "one", "a": "b"}}
repo_yaml = {"barber": "shop"}
commit_yaml = {"key": {"value": "two", "c": "d"}}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml, repo_yaml=repo_yaml, commit_yaml=commit_yaml
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml,
repo_yaml=repo_yaml,
commit_yaml=commit_yaml,
ownerid=100,
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
"a": 2,
"b": 3,
}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml,
repo_yaml=repo_yaml,
commit_yaml=commit_yaml,
ownerid=121,
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
"b": 3,
"d": "klmnop",
}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml,
repo_yaml=repo_yaml,
commit_yaml=commit_yaml,
ownerid=140,
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
}
def test_get_final_yaml_with_additional_user_yaml_via_ownercontext(
self, mock_configuration
):
mock_configuration._params["site"] = {"codecov": {"max_report_age": 86400}}
mock_configuration._params["additional_user_yamls"] = [
{"percentage": 10, "name": "banana", "override": {"a": 2, "b": 3}},
{"percentage": 30, "name": "apple", "override": {"d": "klmnop", "b": 3}},
]
owner_yaml = {"key": {"value": "one", "a": "b"}}
repo_yaml = {"barber": "shop"}
commit_yaml = {"key": {"value": "two", "c": "d"}}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml, repo_yaml=repo_yaml, commit_yaml=commit_yaml
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml,
repo_yaml=repo_yaml,
commit_yaml=commit_yaml,
owner_context=OwnerContext(ownerid=100),
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
"a": 2,
"b": 3,
}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml,
repo_yaml=repo_yaml,
commit_yaml=commit_yaml,
owner_context=OwnerContext(ownerid=121),
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
"b": 3,
"d": "klmnop",
}
assert UserYaml.get_final_yaml(
owner_yaml=owner_yaml,
repo_yaml=repo_yaml,
commit_yaml=commit_yaml,
owner_context=OwnerContext(ownerid=140),
).to_dict() == {
"codecov": {"max_report_age": 86400},
"key": {"a": "b", "c": "d", "value": "two"},
}
def test_get_final_yaml_no_commit_yaml(self, mock_configuration):
mock_configuration._params["site"] = {"codecov": {"max_report_age": 86400}}
owner_yaml = {"key": {"value": "one", "a": "b"}}
repo_yaml = {"barber": "shop"}
expected_result = {
"key": {"a": "b", "value": "one"},
"barber": "shop",
"codecov": {"max_report_age": 86400},
}
assert (
UserYaml.get_final_yaml(
owner_yaml=owner_yaml, repo_yaml=repo_yaml, commit_yaml=None
).to_dict()
== expected_result
)
def test_get_final_yaml_only_owner_yaml(self, mock_configuration):
mock_configuration._params["site"] = {"codecov": {"max_report_age": 86400}}
owner_yaml = {"key": {"value": "one", "a": "b"}}
expected_result = {
"key": {"a": "b", "value": "one"},
"codecov": {"max_report_age": 86400},
}
assert (
UserYaml.get_final_yaml(
owner_yaml=owner_yaml, repo_yaml=None, commit_yaml=None
).to_dict()
== expected_result
)
def test_get_final_yaml_nothing(self, mock_configuration):
mock_configuration._params["site"] = {"codecov": {"max_report_age": 86400}}
expected_result = {"codecov": {"max_report_age": 86400}}
assert (
UserYaml.get_final_yaml(
owner_yaml=None, repo_yaml=None, commit_yaml=None
).to_dict()
== expected_result
)
@freeze_time("2024-04-30 00:00:00.000")
def test_default_yaml_behavior_change(self):
current_yaml = LEGACY_DEFAULT_SITE_CONFIG
day_timedelta = datetime.timedelta(days=1)
year_timedelta = datetime.timedelta(days=365)
patch_centric_expected_onboarding_date = (
datetime.datetime.now(datetime.timezone.utc) + day_timedelta
)
no_change_expected_onboarding_date = (
datetime.datetime.now(datetime.timezone.utc) - day_timedelta
)
notify_error_expected_onboarding_date = (
datetime.datetime.now(datetime.timezone.utc) + year_timedelta
)
no_change = _fix_yaml_defaults_based_on_time(
current_yaml,
no_change_expected_onboarding_date,
no_change_expected_onboarding_date,
)
assert no_change == current_yaml
patch_centric = _fix_yaml_defaults_based_on_time(
current_yaml,
patch_centric_expected_onboarding_date,
no_change_expected_onboarding_date,
)
assert patch_centric != current_yaml
assert patch_centric == PATCH_CENTRIC_DEFAULT_CONFIG
patch_centric = _fix_yaml_defaults_based_on_time(
current_yaml,
patch_centric_expected_onboarding_date,
notify_error_expected_onboarding_date,
)
assert patch_centric != current_yaml
notify_error_config = deepcopy(PATCH_CENTRIC_DEFAULT_CONFIG)
notify_error_config["codecov"]["notify"]["notify_error"] = True
assert patch_centric == notify_error_config
def test_get_final_yaml_default_based_on_owner_context(self):
day_timedelta = datetime.timedelta(days=1)
patch_centric_expected_onboarding_date = (
PATCH_CENTRIC_DEFAULT_TIME_START + day_timedelta
)
no_change_expected_onboarding_date = (
PATCH_CENTRIC_DEFAULT_TIME_START - day_timedelta
)
legacy_default = UserYaml.get_final_yaml(
owner_yaml=None,
repo_yaml=None,
commit_yaml=None,
owner_context=OwnerContext(
owner_onboarding_date=no_change_expected_onboarding_date
),
)
assert legacy_default.to_dict() == {
"codecov": {"require_ci_to_pass": True, "notify": {"wait_for_ci": True}},
"coverage": {
"precision": 2,
"round": "down",
"range": [60.0, 80.0],
"status": {
"project": True,
"patch": True,
"changes": False,
"default_rules": {"flag_coverage_not_uploaded_behavior": "include"},
},
},
"comment": {
"layout": "reach,diff,flags,tree,reach",
"behavior": "default",
"show_carryforward_flags": False,
},
"slack_app": True,
"github_checks": {"annotations": True},
}
patch_centric_default = UserYaml.get_final_yaml(
owner_yaml=None,
repo_yaml=None,
commit_yaml=None,
owner_context=OwnerContext(
owner_onboarding_date=patch_centric_expected_onboarding_date
),
)
assert patch_centric_default.to_dict() == {
"codecov": {"require_ci_to_pass": True, "notify": {"wait_for_ci": True}},
"coverage": {
"precision": 2,
"round": "down",
"range": [60.0, 80.0],
"status": {
"project": False,
"patch": True,
"changes": False,
"default_rules": {"flag_coverage_not_uploaded_behavior": "include"},
},
},
"comment": {
"layout": "condensed_header, flags, tree, component",
"behavior": "default",
"show_carryforward_flags": False,
"hide_project_coverage": True,
},
"slack_app": True,
"github_checks": {"annotations": True},
}
def test_get_possible_additional_user_yaml_empty(self, mock_configuration):
assert _get_possible_additional_user_yaml(1) == {}
assert _get_possible_additional_user_yaml(101) == {}
def test_get_possible_additional_user_yaml(self, mock_configuration):
mock_configuration._params["additional_user_yamls"] = [
{"percentage": 10, "name": "banana", "override": {"a": 2, "b": 3}},
{"percentage": 30, "name": "apple", "override": {"d": "kllmnop", "b": 3}},
]
assert _get_possible_additional_user_yaml(0) == {"a": 2, "b": 3}
assert _get_possible_additional_user_yaml(1) == {"a": 2, "b": 3}
assert _get_possible_additional_user_yaml(9) == {"a": 2, "b": 3}
assert _get_possible_additional_user_yaml(10) == {"d": "kllmnop", "b": 3}
assert _get_possible_additional_user_yaml(11) == {"d": "kllmnop", "b": 3}
assert _get_possible_additional_user_yaml(39) == {"d": "kllmnop", "b": 3}
assert _get_possible_additional_user_yaml(40) == {}
assert _get_possible_additional_user_yaml(41) == {}
assert _get_possible_additional_user_yaml(100) == {"a": 2, "b": 3}
assert _get_possible_additional_user_yaml(101) == {"a": 2, "b": 3}
def test_get_components_no_default(self):
user_yaml = UserYaml(
{
"component_management": {
"individual_components": [
{"component_id": "py_files", "paths": [r".*\.py"]}
]
}
}
)
components = user_yaml.get_components()
assert len(components) == 1
assert components == [
Component(
component_id="py_files",
paths=[r".*\.py"],
name="",
flag_regexes=[],
statuses=[],
)
]
def test_get_components_default_no_components(self):
user_yaml = UserYaml({"component_management": {}})
components = user_yaml.get_components()
assert len(components) == 0
def test_get_components_default_only(self):
user_yaml = UserYaml(
{
"component_management": {
"default_rules": {"paths": [r".*\.py"], "flag_regexes": [r"flag.*"]}
}
}
)
components = user_yaml.get_components()
assert len(components) == 0
def test_get_components_all(self):
user_yaml = UserYaml(
{
"component_management": {
"default_rules": {
"paths": [r".*\.py"],
"flag_regexes": [r"flag.*"],
},
"individual_components": [
{"component_id": "go_files", "paths": [r".*\.go"]},
{"component_id": "rules_from_default"},
{
"component_id": "I have my flags",
"flag_regexes": [r"python-.*"],
},
{
"component_id": "required",
"name": "display",
"flag_regexes": [],
"paths": [r"src/.*"],
},
],
}
}
)
components = user_yaml.get_components()
assert len(components) == 4
assert components == [
Component(
component_id="go_files",
paths=[r".*\.go"],
name="",
flag_regexes=[r"flag.*"],
statuses=[],
),
Component(
component_id="rules_from_default",
paths=[r".*\.py"],
name="",
flag_regexes=[r"flag.*"],
statuses=[],
),
Component(
component_id="I have my flags",
paths=[r".*\.py"],
name="",
flag_regexes=[r"python-.*"],
statuses=[],
),
Component(
component_id="required",
name="display",
paths=[r"src/.*"],
flag_regexes=[],
statuses=[],
),
]