Debian Codegraph @

tests/push/test_trafficjam.py

raw symbols SCIP
1 import pytest
2 import operator
3 from gftools.push.trafficjam import (
4 PushItem,
5 PushItems,
6 PushCategory,
7 PushStatus,
8 PushList,
9 )
10 from pathlib import Path
11 import os
12
13
14 CWD = os.path.dirname(__file__)
15 TEST_DIR = os.path.join(CWD, "..", "..", "data", "test", "gf_fonts")
16 TEST_FAMILY_DIR = Path(os.path.join(TEST_DIR, "ofl", "mavenpro"))
17
18
19 @pytest.mark.parametrize(
20 "item1,item2,expected",
21 [
22 (
23 PushItem(
24 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_DEV, "45"
25 ),
26 PushItem(
27 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_DEV, "45"
28 ),
29 True,
30 ),
31 (
32 PushItem(
33 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_DEV, "45"
34 ),
35 PushItem(
36 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_DEV, "46"
37 ),
38 False,
39 ),
40 (
41 PushItem(
42 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_DEV, "45"
43 ),
44 PushItem(
45 Path("ofl/mavenpro2"), PushCategory.UPGRADE, PushStatus.IN_DEV, "45"
46 ),
47 False,
48 ),
49 (
50 PushItem(
51 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_DEV, "45"
52 ),
53 PushItem(
54 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_SANDBOX, "45"
55 ),
56 True,
57 ),
58 ],
59 )
60 def test_push_item_eq(item1, item2, expected):
61 assert (item1 == item2) == expected
62
63
64 @pytest.mark.parametrize(
65 "items, expected_size",
66 [
67 (
68 [
69 PushItem("1", "1", "1", "1"),
70 PushItem("1", "1", "1", "1"),
71 PushItem("1", "1", "1", "1"),
72 ],
73 1,
74 ),
75 (
76 [
77 PushItem("1", "1", "1", "1"),
78 PushItem("1", "1", "1", "1"),
79 PushItem("2", "1", "1", "1"),
80 ],
81 2,
82 ),
83 ],
84 )
85 def test_push_item_set(items, expected_size):
86 new_items = PushItems(set(items))
87 assert len(new_items) == expected_size
88
89
90 @pytest.mark.parametrize(
91 """operator,item1,item2,expected""",
92 [
93 # add items together
94 (
95 operator.add,
96 PushItems(
97 [
98 PushItem(
99 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
100 ),
101 ]
102 ),
103 PushItems(
104 [
105 PushItem(
106 Path("ofl/mavenpro"),
107 PushCategory.NEW,
108 PushStatus.IN_SANDBOX,
109 "1",
110 ),
111 PushItem(
112 Path("ofl/amatic"), PushCategory.NEW, PushStatus.IN_DEV, "1"
113 ),
114 ]
115 ),
116 PushItems(
117 [
118 PushItem(
119 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
120 ),
121 PushItem(
122 Path("ofl/mavenpro"),
123 PushCategory.NEW,
124 PushStatus.IN_SANDBOX,
125 "1",
126 ),
127 PushItem(
128 Path("ofl/amatic"), PushCategory.NEW, PushStatus.IN_DEV, "1"
129 ),
130 ]
131 ),
132 ),
133 # sub items
134 (
135 operator.sub,
136 PushItems(
137 [
138 PushItem(
139 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
140 ),
141 PushItem(
142 Path("ofl/amatic"), PushCategory.NEW, PushStatus.IN_DEV, "1"
143 ),
144 ]
145 ),
146 PushItems(
147 [
148 PushItem(
149 Path("ofl/mavenpro"),
150 PushCategory.NEW,
151 PushStatus.IN_SANDBOX,
152 "1",
153 ),
154 ]
155 ),
156 PushItems(
157 [
158 PushItem(
159 Path("ofl/amatic"), PushCategory.NEW, PushStatus.IN_DEV, "1"
160 ),
161 ]
162 ),
163 ),
164 ],
165 )
166 def test_push_items_operators(operator, item1, item2, expected):
167 assert operator(item1, item2) == expected
168
169
170 @pytest.mark.parametrize(
171 "items, expected",
172 [
173 # fonts filenames get removed
174 (
175 [
176 PushItem(
177 Path("ofl/mavenpro/MavenPro[wght].ttf"),
178 "update",
179 PushStatus.IN_DEV,
180 "1",
181 )
182 ],
183 PushItems(
184 [PushItem(Path("ofl/mavenpro"), "update", PushStatus.IN_DEV, "1")]
185 ),
186 ),
187 # font family
188 (
189 [
190 PushItem(
191 Path("ofl/mavenpro/MavenPro[wght].ttf"),
192 "update",
193 PushStatus.IN_DEV,
194 "1",
195 ),
196 PushItem(
197 Path("ofl/mavenpro/MavenPro-Italic[wght].ttf"),
198 "update",
199 PushStatus.IN_DEV,
200 "1",
201 ),
202 ],
203 PushItems(
204 [PushItem(Path("ofl/mavenpro"), "update", PushStatus.IN_DEV, "1")]
205 ),
206 ),
207 # axisregistry
208 (
209 [
210 PushItem(
211 Path("axisregistry/Lib/axisregistry/data/bounce.textproto"),
212 PushCategory.NEW,
213 PushStatus.IN_DEV,
214 "1",
215 ),
216 PushItem(
217 Path("axisregistry/Lib/axisregistry/data/morph.textproto"),
218 PushCategory.NEW,
219 PushStatus.IN_DEV,
220 "1",
221 ),
222 ],
223 PushItems(
224 [
225 PushItem(
226 Path("axisregistry/bounce.textproto"),
227 PushCategory.NEW,
228 PushStatus.IN_DEV,
229 "1",
230 ),
231 PushItem(
232 Path("axisregistry/morph.textproto"),
233 PushCategory.NEW,
234 PushStatus.IN_DEV,
235 "1",
236 ),
237 ]
238 ),
239 ),
240 # lang
241 (
242 [
243 PushItem(
244 Path("lang/Lib/gflanguages/data/languages/aa_Latn.textproto"),
245 PushCategory.NEW,
246 PushStatus.IN_DEV,
247 "1",
248 ),
249 ],
250 PushItems(
251 [
252 PushItem(
253 Path("lang/languages/aa_Latn.textproto"),
254 PushCategory.NEW,
255 PushStatus.IN_DEV,
256 "1",
257 ),
258 ]
259 ),
260 ),
261 # child
262 (
263 [
264 PushItem(
265 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
266 ),
267 PushItem(Path("ofl"), PushCategory.NEW, PushStatus.IN_DEV, "1"),
268 ],
269 PushItems(
270 [
271 PushItem(
272 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
273 ),
274 ]
275 ),
276 ),
277 # parent
278 (
279 [
280 PushItem(Path("ofl"), PushCategory.NEW, PushStatus.IN_DEV, "1"),
281 PushItem(
282 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
283 ),
284 ],
285 PushItems(
286 [
287 PushItem(
288 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
289 ),
290 ]
291 ),
292 ),
293 # parent dir
294 (
295 [
296 PushItem(Path("ofl"), PushCategory.NEW, PushStatus.IN_DEV, "1"),
297 PushItem(Path("apache"), PushCategory.NEW, PushStatus.IN_DEV, "1"),
298 PushItem(
299 Path("lang/authors.txt"), PushCategory.NEW, PushStatus.IN_DEV, "1"
300 ),
301 ],
302 PushItems(),
303 ),
304 # noto article
305 (
306 [
307 PushItem(
308 Path("ofl/notosans/article/index.html"),
309 PushCategory.NEW,
310 PushStatus.IN_DEV,
311 "1",
312 ),
313 ],
314 PushItems(
315 [
316 PushItem(
317 Path("ofl/notosans"), PushCategory.NEW, PushStatus.IN_DEV, "1"
318 ),
319 ]
320 ),
321 ),
322 # noto full
323 (
324 [
325 PushItem(
326 Path("ofl/notosans/article"),
327 PushCategory.NEW,
328 PushStatus.IN_DEV,
329 "1",
330 ),
331 PushItem(
332 Path("ofl/notosans/NotoSans[wght].ttf"),
333 PushCategory.NEW,
334 PushStatus.IN_DEV,
335 "1",
336 ),
337 PushItem(
338 Path("ofl/notosans/DESCRIPTION.en_us.html"),
339 PushCategory.NEW,
340 PushStatus.IN_DEV,
341 "1",
342 ),
343 PushItem(
344 Path("ofl/notosans/upstream.yaml"),
345 PushCategory.NEW,
346 PushStatus.IN_DEV,
347 "1",
348 ),
349 PushItem(
350 Path("ofl/notosans/OFL.txt"),
351 PushCategory.NEW,
352 PushStatus.IN_DEV,
353 "1",
354 ),
355 PushItem(
356 Path("ofl/notosans/DESCRIPTION.en_us.html"),
357 PushCategory.NEW,
358 PushStatus.IN_DEV,
359 "1",
360 ),
361 ],
362 PushItems(
363 [
364 PushItem(
365 Path("ofl/notosans"), PushCategory.NEW, PushStatus.IN_DEV, "1"
366 ),
367 ]
368 ),
369 ),
370 # multi notosans
371 (
372 [
373 PushItem(
374 Path("ofl/notosanspsalterpahlavi/METADATA.pb"),
375 PushCategory.NEW,
376 PushStatus.IN_DEV,
377 "1",
378 ),
379 PushItem(
380 Path("ofl/notosans/METADATA.pb"),
381 PushCategory.NEW,
382 PushStatus.IN_DEV,
383 "1",
384 ),
385 ],
386 PushItems(
387 [
388 PushItem(
389 Path("ofl/notosanspsalterpahlavi"),
390 PushCategory.NEW,
391 PushStatus.IN_DEV,
392 "1",
393 ),
394 PushItem(
395 Path("ofl/notosans"), PushCategory.NEW, PushStatus.IN_DEV, "1"
396 ),
397 ]
398 ),
399 ),
400 # multi notosans 2
401 (
402 [
403 PushItem(
404 Path("ofl/notosans/METADATA.pb"),
405 PushCategory.NEW,
406 PushStatus.IN_DEV,
407 "1",
408 ),
409 PushItem(
410 Path("ofl/notosanspsalterpahlavi/METADATA.pb"),
411 PushCategory.NEW,
412 PushStatus.IN_DEV,
413 "1",
414 ),
415 ],
416 PushItems(
417 [
418 PushItem(
419 Path("ofl/notosans"), PushCategory.NEW, PushStatus.IN_DEV, "1"
420 ),
421 PushItem(
422 Path("ofl/notosanspsalterpahlavi"),
423 PushCategory.NEW,
424 PushStatus.IN_DEV,
425 "1",
426 ),
427 ]
428 ),
429 ),
430 # designer
431 (
432 [
433 PushItem(
434 Path("catalog/designers/colophonfoundry/bio.html"),
435 PushCategory.NEW,
436 PushStatus.IN_DEV,
437 "1",
438 ),
439 PushItem(
440 Path("catalog/designers/colophonfoundry/colophonfoundry.png"),
441 PushCategory.NEW,
442 PushStatus.IN_DEV,
443 "1",
444 ),
445 PushItem(
446 Path("catalog/designers/colophonfoundry/info.pb"),
447 PushCategory.NEW,
448 PushStatus.IN_DEV,
449 "1",
450 ),
451 ],
452 PushItems(
453 [
454 PushItem(
455 Path("catalog/designers/colophonfoundry"),
456 PushCategory.NEW,
457 PushStatus.IN_DEV,
458 "1",
459 ),
460 ]
461 ),
462 ),
463 # ensure both items are added
464 (
465 [
466 PushItem(
467 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
468 ),
469 PushItem(
470 Path("ofl/mavenpro"), PushCategory.UPGRADE, PushStatus.IN_DEV, "2"
471 ),
472 ],
473 PushItems(
474 [
475 PushItem(
476 Path("ofl/mavenpro"), PushCategory.NEW, PushStatus.IN_DEV, "1"
477 ),
478 PushItem(
479 Path("ofl/mavenpro"),
480 PushCategory.UPGRADE,
481 PushStatus.IN_DEV,
482 "2",
483 ),
484 ]
485 ),
486 ),
487 # Ensure Knowledge articles are unique
488 (
489 [
490 PushItem(
491 Path("cc-by-sa/knowledge/glossary/terms/xopq_axis/content.md"),
492 PushCategory.NEW,
493 PushStatus.IN_DEV,
494 "1",
495 ),
496 PushItem(
497 Path(
498 "cc-by-sa/knowledge/modules/using_type/lessons/the_complications_of_typographic_size/content.md"
499 ),
500 PushCategory.NEW,
501 PushStatus.IN_DEV,
502 "1",
503 ),
504 ],
505 PushItems(
506 [
507 PushItem(
508 Path("cc-by-sa/knowledge/glossary/terms/xopq_axis/content.md"),
509 PushCategory.NEW,
510 PushStatus.IN_DEV,
511 "1",
512 ),
513 PushItem(
514 Path(
515 "cc-by-sa/knowledge/modules/using_type/lessons/the_complications_of_typographic_size/content.md"
516 ),
517 PushCategory.NEW,
518 PushStatus.IN_DEV,
519 "1",
520 ),
521 ]
522 ),
523 ),
524 ],
525 )
526 def test_push_items_add(items, expected):
527 res = PushItems()
528 for item in items:
529 res.add(item)
530 for got, want in zip(res, expected):
531 assert got.path == want.path
532 assert got.status == want.status
533 assert got.category == want.category
534 assert got.url == want.url
535
536
537 # TODO reactivate this. Doesn't work on GHA
538 # def test_push_items_from_traffic_jam():
539 # items = PushItems.from_traffic_jam()
540 # # traffic board shouldn't be empty
541 # assert (
542 # len(items) != 0
543 # ), "board is empty! check https://github.com/orgs/google/projects/74"
544
545
546 @pytest.mark.parametrize(
547 "string, expected_size",
548 [
549 ("ofl/noto # 2", 1),
550 ("# New\nofl/noto # 2\nofl/foobar # 3\n\n# Upgrade\nofl/mavenPro # 4", 3),
551 ("# New\nofl/noto\n# Deleted: lang/languages/wsg_Gong.textproto # 5", 2),
552 ],
553 )
554 def test_push_items_from_server_file(string, expected_size):
555 from io import StringIO
556
557 data = StringIO()
558 data.write(string)
559 data.seek(0)
560 items = PushItems.from_server_file(data, PushStatus.IN_DEV, PushList.TO_SANDBOX)
561 assert len(items) == expected_size
562
563
564 @pytest.mark.parametrize(
565 "items,create_dirs,expected",
566 [
567 # standard items
568 (
569 PushItems(
570 [
571 PushItem(
572 Path("ofl/mavenpro"),
573 PushCategory.UPGRADE,
574 PushStatus.IN_DEV,
575 "45",
576 ),
577 PushItem(
578 Path("ofl/amatic"), PushCategory.NEW, PushStatus.IN_DEV, "46"
579 ),
580 ]
581 ),
582 (True, True),
583 "# New\nofl/amatic # 46\n\n# Upgrade\nofl/mavenpro # 45\n",
584 ),
585 # deleted item
586 (
587 PushItems(
588 [
589 PushItem(
590 Path("ofl/mavenpro"),
591 PushCategory.UPGRADE,
592 PushStatus.IN_DEV,
593 "45",
594 ),
595 PushItem(
596 Path("ofl/amatic"), PushCategory.NEW, PushStatus.IN_DEV, "46"
597 ),
598 PushItem(
599 Path("ofl/opensans"),
600 PushCategory.UPGRADE,
601 PushStatus.IN_DEV,
602 "47",
603 ),
604 ]
605 ),
606 (True, False, True),
607 "# New\n# Deleted: ofl/amatic # 46\n\n# Upgrade\nofl/mavenpro # 45\nofl/opensans # 47\n",
608 ),
609 # duplicate items
610 (
611 PushItems(
612 [
613 PushItem(
614 Path("ofl/mavenpro"),
615 PushCategory.UPGRADE,
616 PushStatus.IN_DEV,
617 "45",
618 ),
619 PushItem(
620 Path("ofl/mavenpro"),
621 PushCategory.UPGRADE,
622 PushStatus.IN_DEV,
623 "45",
624 ),
625 PushItem(
626 Path("ofl/mavenpro"),
627 PushCategory.UPGRADE,
628 PushStatus.IN_DEV,
629 "45",
630 ),
631 ]
632 ),
633 (True, True),
634 f"# Upgrade\nofl/mavenpro # 45\n",
635 ),
636 ],
637 )
638 def test_push_items_to_server_file(items, create_dirs, expected):
639 from io import StringIO
640 import tempfile
641
642 # We need to mock the item paths because if they don't exist, the server_file
643 # should mention that they're deleted
644 with tempfile.TemporaryDirectory() as tmp_dir:
645 cwd = os.getcwd()
646 os.chdir(tmp_dir)
647 for item, create_dir in zip(items, create_dirs):
648 new_path = tmp_dir / item.path
649 if create_dir:
650 os.makedirs(new_path, exist_ok=True)
651
652 out = StringIO()
653 items.to_server_file(out)
654 out.seek(0)
655 os.chdir(cwd)
656 assert out.read() == expected
657
658
659 @pytest.mark.parametrize(
660 "path, expected",
661 [
662 (TEST_FAMILY_DIR, []),
663 (Path("/foo/bar"), [Path("/foo/bar")]),
664 ],
665 )
666 def test_push_items_missing_paths(path, expected):
667 items = PushItems([PushItem(path, "a", PushStatus.IN_DEV, "a")])
668 assert items.missing_paths() == expected
669
670
671 @pytest.mark.parametrize(
672 """item,expected""",
673 [
674 (
675 PushItem(
676 Path("ofl/mavenpro"),
677 PushCategory.UPGRADE,
678 PushStatus.IN_DEV,
679 "45",
680 ),
681 {
682 "path": "ofl/mavenpro",
683 "category": "Upgrade",
684 "status": "In Dev / PR Merged",
685 "url": "45",
686 },
687 ),
688 (
689 PushItem(
690 Path("ofl/mavenpro"),
691 None,
692 None,
693 "45",
694 ),
695 {"path": "ofl/mavenpro", "category": None, "status": None, "url": "45"},
696 ),
697 (
698 PushItem(
699 Path("ofl/mavenpro"),
700 None,
701 None,
702 None,
703 ),
704 {"path": "ofl/mavenpro", "category": None, "status": None, "url": None},
705 ),
706 ],
707 )
708 def test_push_items_to_json(item, expected):
709 assert item.to_json() == expected