From 727cdd610162b72e90fb9c9043fced9c76490af9 Mon Sep 17 00:00:00 2001 From: Andrew Halberstadt Date: Thu, 17 Oct 2024 13:24:01 -0400 Subject: [PATCH] fix(transforms.run): be more specific about implicit files changed for 'when' optimization --- src/taskgraph/transforms/run/__init__.py | 4 ++- test/test_transforms_run.py | 39 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/taskgraph/transforms/run/__init__.py b/src/taskgraph/transforms/run/__init__.py index c2ad5cd1..520cdf0a 100644 --- a/src/taskgraph/transforms/run/__init__.py +++ b/src/taskgraph/transforms/run/__init__.py @@ -121,7 +121,9 @@ def rewrite_when_to_optimization(config, tasks): files_changed = when.get("files-changed") # implicitly add task config directory. - files_changed.append(f"{config.path}/**") + files_changed.append(f"{config.path}/kind.yml") + if task.get("task-from") and task["task-from"] != "kind.yml": + files_changed.append(f"{config.path}/{task['task-from']}") # "only when files changed" implies "skip if files have not changed" task["optimization"] = {"skip-unless-changed": files_changed} diff --git a/test/test_transforms_run.py b/test/test_transforms_run.py index a0e102fd..820f0ed2 100644 --- a/test/test_transforms_run.py +++ b/test/test_transforms_run.py @@ -86,6 +86,45 @@ def test_worker_caches(task, transform): validate_schema(partial_schema, taskdesc["worker"][key], "validation error") +def test_rewrite_when_to_optimization(run_transform, make_transform_config): + config = make_transform_config() + + task = {"foo": "bar"} + result = run_transform(run.rewrite_when_to_optimization, task) + assert list(result) == [{"foo": "bar"}] + + task = {"foo": "bar", "when": {"files-changed": ["README.md"]}} + result = run_transform(run.rewrite_when_to_optimization, task) + assert list(result) == [ + { + "foo": "bar", + "optimization": { + "skip-unless-changed": ["README.md", f"{config.path}/kind.yml"] + }, + } + ] + + task = { + "foo": "bar", + "when": {"files-changed": ["README.md"]}, + "task-from": "foo.yml", + } + result = run_transform(run.rewrite_when_to_optimization, task) + assert list(result) == [ + { + "foo": "bar", + "optimization": { + "skip-unless-changed": [ + "README.md", + f"{config.path}/kind.yml", + f"{config.path}/foo.yml", + ] + }, + "task-from": "foo.yml", + } + ] + + def assert_use_fetches_toolchain_env(task): assert task["worker"]["env"]["FOO"] == "1"