Skip to content

Commit 8ecf249

Browse files
authored
feat: pass along write_artifacts to loaders (#779)
Taskgraph already passes this information along to transforms, and over in https://bugzilla.mozilla.org/show_bug.cgi?id=1989038 we have a use case to have loaders know about it too: we need to write out an artifact whose contents are only known after all tasks for a specific kind have been loaded and transformed. (Technically we could avoid passing this along, but it would mean we have no reasonable way to switch off the writing of this artifact outside of decision tasks...) Co-Authored-By: [email protected]
1 parent 993ab70 commit 8ecf249

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

packages/pytest-taskgraph/src/pytest_taskgraph/fixtures/gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
here = Path(__file__).parent
1818

1919

20-
def fake_loader(kind, path, config, parameters, loaded_tasks):
20+
def fake_loader(kind, path, config, parameters, loaded_tasks, write_artifacts):
2121
for i in range(3):
2222
dependencies = {}
2323
if i >= 1:

src/taskgraph/generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
import copy
6+
import inspect
67
import logging
78
import multiprocessing
89
import os
@@ -60,12 +61,17 @@ def load_tasks(self, parameters, kind_dependencies_tasks, write_artifacts):
6061
loader = self._get_loader()
6162
config = copy.deepcopy(self.config)
6263

64+
if "write_artifacts" in inspect.signature(loader).parameters:
65+
extra_args = (write_artifacts,)
66+
else:
67+
extra_args = ()
6368
inputs = loader(
6469
self.name,
6570
self.path,
6671
config,
6772
parameters,
6873
list(kind_dependencies_tasks.values()),
74+
*extra_args,
6975
)
7076

7177
transforms = TransformSequence()

src/taskgraph/loader/default.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
]
1717

1818

19-
def loader(kind, path, config, params, loaded_tasks):
19+
def loader(kind, path, config, params, loaded_tasks, write_artifacts):
2020
"""
2121
This default loader builds on the `transform` loader by providing sensible
2222
default transforms that the majority of simple tasks will need.
@@ -30,4 +30,4 @@ def loader(kind, path, config, params, loaded_tasks):
3030
f"Transform {t} is already present in the loader's default transforms; it must not be defined in the kind"
3131
)
3232
transform_refs.extend(DEFAULT_TRANSFORMS)
33-
return transform_loader(kind, path, config, params, loaded_tasks)
33+
return transform_loader(kind, path, config, params, loaded_tasks, write_artifacts)

src/taskgraph/loader/transform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14-
def loader(kind, path, config, params, loaded_tasks):
14+
def loader(kind, path, config, params, loaded_tasks, write_artifacts):
1515
"""
1616
Get the input elements that will be transformed into tasks in a generic
1717
way. The elements themselves are free-form, and become the input to the

test/test_generator.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ def test_load_tasks_for_kind(monkeypatch):
216216
)
217217

218218

219+
def test_loader_backwards_compat_interface(graph_config):
220+
"""Ensure loaders can be called even if they don't support a
221+
`write_artifacts` argument."""
222+
223+
class OldLoaderKind(Kind):
224+
def _get_loader(self):
225+
return lambda kind, path, config, params, tasks: []
226+
227+
kind = OldLoaderKind("", "", {"transforms": []}, graph_config)
228+
kind.load_tasks({}, {}, False)
229+
230+
219231
@pytest.mark.parametrize(
220232
"config,expected_transforms",
221233
(
@@ -243,7 +255,7 @@ def test_default_loader(config, expected_transforms):
243255
assert loader is default_loader, (
244256
"Default Kind loader should be taskgraph.loader.default.loader"
245257
)
246-
loader("", "", config, {}, [])
258+
loader("", "", config, {}, [], False)
247259

248260
assert config["transforms"] == expected_transforms
249261

@@ -273,7 +285,7 @@ def test_default_loader(config, expected_transforms):
273285
def test_default_loader_errors(config):
274286
loader = Kind("", "", config, {})._get_loader()
275287
try:
276-
loader("", "", config, {}, [])
288+
loader("", "", config, {}, [], False)
277289
except KeyError:
278290
return
279291

0 commit comments

Comments
 (0)