33"""
44
55from pprint import pprint
6+ from typing import Dict
67
78import pytest
89from pytest_taskgraph import make_task
910
11+ from taskgraph .task import Task
1012from taskgraph .transforms import from_deps
1113
1214
15+ def get_task_label_by_attributes (
16+ tasks : Dict [str , Task ], attributes : Dict [str , str ]
17+ ) -> str :
18+ for task in tasks .values ():
19+ if all ([task .attributes [k ] == v for k , v in attributes .items ()]):
20+ return task .label
21+
22+ return ""
23+
24+
1325def handle_exception (obj , exc = None ):
1426 if exc :
1527 assert isinstance (obj , exc )
1628 elif isinstance (obj , Exception ):
1729 raise obj
1830
1931
20- def assert_no_kind_dependencies (e ):
32+ def assert_no_kind_dependencies (_ , e ):
2133 handle_exception (e , exc = Exception )
2234
2335
24- def assert_invalid_only_kinds (e ):
36+ def assert_invalid_only_kinds (_ , e ):
2537 handle_exception (e , exc = Exception )
2638
2739
28- def assert_defaults (tasks ):
40+ def assert_defaults (deps , tasks ):
2941 handle_exception (tasks )
3042 assert len (tasks ) == 2
31- assert tasks [0 ]["attributes" ] == {"primary-kind-dependency" : "foo" }
43+ assert tasks [0 ]["attributes" ] == {
44+ "primary-kind-dependency" : "foo" ,
45+ "primary-dependency-label" : get_task_label_by_attributes (deps , {"kind" : "foo" }),
46+ }
3247 assert tasks [0 ]["dependencies" ] == {"foo" : "a" }
3348 assert tasks [0 ]["name" ] == "a"
34- assert tasks [1 ]["attributes" ] == {"primary-kind-dependency" : "bar" }
49+ assert tasks [1 ]["attributes" ] == {
50+ "primary-kind-dependency" : "bar" ,
51+ "primary-dependency-label" : get_task_label_by_attributes (deps , {"kind" : "bar" }),
52+ }
3553 assert tasks [1 ]["dependencies" ] == {"bar" : "bar-b" }
3654 assert tasks [1 ]["name" ] == "b"
3755
3856
3957assert_group_by_single = assert_defaults
4058
4159
42- def assert_group_by_attribute (tasks ):
60+ def assert_group_by_attribute (deps , tasks ):
4361 handle_exception (tasks )
4462 assert len (tasks ) == 2
4563 assert tasks [0 ]["dependencies" ] == {"foo" : "a" }
46- assert tasks [0 ]["attributes" ] == {"primary-kind-dependency" : "foo" }
64+ assert tasks [0 ]["attributes" ] == {
65+ "primary-kind-dependency" : "foo" ,
66+ "primary-dependency-label" : get_task_label_by_attributes (
67+ deps , {"kind" : "foo" , "build-type" : "linux" }
68+ ),
69+ }
4770 assert tasks [1 ]["dependencies" ] == {"foo" : "b" , "bar" : "c" }
48- assert tasks [1 ]["attributes" ] == {"primary-kind-dependency" : "foo" }
71+ assert tasks [1 ]["attributes" ] == {
72+ "primary-kind-dependency" : "foo" ,
73+ "primary-dependency-label" : get_task_label_by_attributes (
74+ deps , {"kind" : "foo" , "build-type" : "win" }
75+ ),
76+ }
4977
5078
51- def assert_group_by_attribute_dupe (e ):
79+ def assert_group_by_attribute_dupe (_ , e ):
5280 handle_exception (e , exc = Exception )
5381
5482
55- def assert_group_by_attribute_dupe_allowed (tasks ):
83+ def assert_group_by_attribute_dupe_allowed (deps , tasks ):
5684 handle_exception (tasks )
5785 assert len (tasks ) == 2
5886 assert tasks [0 ]["dependencies" ] == {"a" : "a" }
59- assert tasks [0 ]["attributes" ] == {"primary-kind-dependency" : "foo" }
87+ assert tasks [0 ]["attributes" ] == {
88+ "primary-kind-dependency" : "foo" ,
89+ "primary-dependency-label" : get_task_label_by_attributes (
90+ deps , {"kind" : "foo" , "build-type" : "linux" }
91+ ),
92+ }
6093 assert tasks [1 ]["dependencies" ] == {"b" : "b" , "c" : "c" }
61- assert tasks [1 ]["attributes" ] == {"primary-kind-dependency" : "foo" }
94+ assert tasks [1 ]["attributes" ] == {
95+ "primary-kind-dependency" : "foo" ,
96+ "primary-dependency-label" : get_task_label_by_attributes (
97+ deps , {"kind" : "foo" , "build-type" : "win" }
98+ ),
99+ }
62100
63101
64- def assert_copy_attributes (tasks ):
102+ def assert_copy_attributes (deps , tasks ):
65103 handle_exception (tasks )
66104 assert len (tasks ) == 1
67105
@@ -70,48 +108,49 @@ def assert_copy_attributes(tasks):
70108 "build-type" : "win" ,
71109 "kind" : "foo" ,
72110 "primary-kind-dependency" : "foo" ,
111+ "primary-dependency-label" : get_task_label_by_attributes (deps , {"kind" : "foo" }),
73112 }
74113
75114
76- def assert_group_by_all (tasks ):
115+ def assert_group_by_all (deps , tasks ):
77116 handle_exception (tasks )
78117 assert len (tasks ) == 1
79118 assert tasks [0 ]["dependencies" ] == {"foo" : "a" , "bar" : "bar-b" }
80119
81120
82- def assert_group_by_all_dupe_allowed (tasks ):
121+ def assert_group_by_all_dupe_allowed (deps , tasks ):
83122 handle_exception (tasks )
84123 assert len (tasks ) == 1
85124 assert tasks [0 ]["dependencies" ] == {"a" : "a" , "b" : "b" , "c" : "c" }
86125
87126
88- def assert_dont_set_name (tasks ):
127+ def assert_dont_set_name (deps , tasks ):
89128 handle_exception (tasks )
90129 assert len (tasks ) == 1
91130 assert tasks [0 ]["name" ] == "a-special-name"
92131
93132
94- def assert_dont_set_name_false (tasks ):
133+ def assert_dont_set_name_false (deps , tasks ):
95134 handle_exception (tasks )
96135 assert len (tasks ) == 1
97136 assert tasks [0 ]["name" ] == "a-special-name"
98137
99138
100- def assert_set_name_strip_kind (tasks ):
139+ def assert_set_name_strip_kind (deps , tasks ):
101140 handle_exception (tasks )
102141 assert len (tasks ) == 2
103142 assert tasks [0 ]["name" ] == "a"
104143 assert tasks [1 ]["name" ] == "b"
105144
106145
107- def assert_set_name_retain_kind (tasks ):
146+ def assert_set_name_retain_kind (deps , tasks ):
108147 handle_exception (tasks )
109148 assert len (tasks ) == 2
110149 assert tasks [0 ]["name" ] == "a"
111150 assert tasks [1 ]["name" ] == "bar-b"
112151
113152
114- def assert_group_by_all_with_fetch (tasks ):
153+ def assert_group_by_all_with_fetch (deps , tasks ):
115154 handle_exception (tasks )
116155 assert len (tasks ) == 1
117156 assert tasks [0 ]["dependencies" ] == {
@@ -412,4 +451,4 @@ def test_transforms(
412451
413452 param_id = request .node .callspec .id
414453 assert_func = globals ()[f"assert_{ param_id } " ]
415- assert_func (result )
454+ assert_func (deps , result )
0 commit comments