Skip to content

Commit 49ec488

Browse files
committed
Add a simple test for taskgraph action
To be able to test without relying on actions that are always generated too much, I monkeypatch `registry._load` so it simply doesn't import the modules registering those actions. Adding that test found that parameters should be parsed with `strict=False` to match other CLI actions. The return value isn't strictly necessary but I added it to be consistent with other functions in the CLI.
1 parent 4e2067e commit 49ec488

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/taskgraph/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def actions(args):
738738
logging.root.setLevel(logging.DEBUG)
739739

740740
try:
741-
parameters = parameters_loader(args["parameters"])
741+
parameters = parameters_loader(args["parameters"], strict=False)
742742
tgg = TaskGraphGenerator(root_dir=args.get("root"), parameters=parameters)
743743

744744
actions = render_actions_json(tgg.parameters, tgg.graph_config, "DECISION-TASK")
@@ -747,6 +747,8 @@ def actions(args):
747747
traceback.print_exc()
748748
sys.exit(1)
749749

750+
return 0
751+
750752

751753
@command("action-callback", description="Run action callback used by action tasks")
752754
@argument(

test/test_main.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Any copyright is dedicated to the public domain.
22
# http://creativecommons.org/publicdomain/zero/1.0/
33

4+
import json
45
import os
56
import sys
67
from pathlib import Path
@@ -9,6 +10,7 @@
910
import pytest
1011

1112
import taskgraph
13+
from taskgraph.actions import registry
1214
from taskgraph.graph import Graph
1315
from taskgraph.main import get_filtered_taskgraph
1416
from taskgraph.main import main as taskgraph_main
@@ -350,3 +352,51 @@ def test_action_callback(mocker, run_taskgraph):
350352
)
351353
root = Path(trigger_mock.call_args.kwargs["root"])
352354
assert root.joinpath("config.yml").is_file()
355+
356+
357+
FAKE_ACTION = {
358+
"name": "test",
359+
"title": "Test action",
360+
"symbol": "ts",
361+
"description": "A test action",
362+
}
363+
364+
365+
@pytest.mark.parametrize(
366+
"with_generic_actions,new_action,expected",
367+
(
368+
pytest.param(True, {}, (9, None)),
369+
pytest.param(False, {}, (0, None)),
370+
pytest.param(False, FAKE_ACTION, (1, FAKE_ACTION)),
371+
),
372+
)
373+
def test_dump_actions_json(
374+
monkeypatch, run_taskgraph, capsys, with_generic_actions, new_action, expected
375+
):
376+
expected_actions_len, expected_fields = expected
377+
378+
actions = []
379+
callbacks = {}
380+
monkeypatch.setattr(registry, "actions", actions)
381+
monkeypatch.setattr(registry, "callbacks", callbacks)
382+
383+
def fake_actions_load(_graph_config):
384+
return callbacks, actions
385+
386+
if not with_generic_actions:
387+
monkeypatch.setattr(registry, "_load", fake_actions_load)
388+
389+
if new_action:
390+
registry.register_callback_action(**new_action)(lambda *args: None)
391+
392+
res = run_taskgraph(["actions"], target_tasks=[])
393+
assert res == 0
394+
395+
output = json.loads(capsys.readouterr().out)
396+
397+
actions = output["actions"]
398+
assert len(actions) == expected_actions_len
399+
if expected_fields is not None:
400+
action = actions[0]
401+
for field_name in ("name", "title", "description"):
402+
assert action[field_name] == expected_fields[field_name]

0 commit comments

Comments
 (0)