Skip to content

Commit

Permalink
test: add some additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahal committed Jan 23, 2025
1 parent ebe3432 commit 5c7f61d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from pathlib import Path

import pytest
Expand Down Expand Up @@ -193,20 +194,25 @@ def target_tasks_method(full_task_graph, parameters, graph_config):

@pytest.fixture
def make_transform_config(parameters, graph_config):
default_graph_config = graph_config

def inner(
kind_config=None,
kind_dependencies_tasks=None,
graph_config=None,
extra_params=None,
extra_graph_config=None,
):
kind_config = kind_config or {}
kind_dependencies_tasks = kind_dependencies_tasks or {}
graph_config = graph_config or deepcopy(default_graph_config)
if extra_params:
parameters.update(extra_params)
if extra_graph_config:
# We need this intermediate variable because `GraphConfig` is
# frozen and we can't set attributes on it.
new_graph_config = merge(graph_config._config, extra_graph_config)
graph_config._config.clear()
graph_config._config.update(new_graph_config)

return TransformConfig(
Expand Down
56 changes: 50 additions & 6 deletions test/test_transforms_run_run_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,18 @@ def inner(task, **kwargs):

caches = result["worker"][key]
caches = [c for c in caches if "cache-name" in c]
print("Dumping for copy/paste:")
print("Dumping caches for copy/paste:")
pprint(caches, indent=2)

env = result["worker"]["env"]
print("Dumping env for copy/paste:")
pprint(env, indent=2)

# Create a new schema object with just the part relevant to caches.
partial_schema = Schema(payload_builders[impl].schema.schema[key])
validate_schema(partial_schema, caches, "validation error")

return caches
return caches, env

return inner

Expand All @@ -254,12 +258,16 @@ def test_caches_enabled(run_caches):
"use-caches": True,
}
}
caches = run_caches(task)
caches, env = run_caches(task)
assert len(caches) == len(CACHES)
cache_names = {c["cache-name"] for c in caches}
for name, cfg in CACHES.items():
if "cache_name" in cfg:
continue

if "env" in cfg:
assert cfg["env"] in env
assert env[cfg["env"]] == f"{{task_workdir}}/.task-cache/{name}"
assert name in cache_names


Expand All @@ -269,7 +277,7 @@ def test_caches_disabled(run_caches):
"use-caches": False,
}
}
assert run_caches(task) == []
assert run_caches(task)[0] == []


def test_caches_explicit(run_caches):
Expand All @@ -278,7 +286,7 @@ def test_caches_explicit(run_caches):
"use-caches": ["cargo"],
}
}
assert run_caches(task) == [
assert run_caches(task)[0] == [
{"cache-name": "cargo", "directory": ".task-cache/cargo"}
]

Expand All @@ -287,5 +295,41 @@ def test_caches_project_explicit(run_caches):
caches = run_caches(
{},
extra_graph_config={"taskgraph": {"run": {"use-caches": ["cargo"]}}},
)
)[0]
assert caches == [{"cache-name": "cargo", "directory": ".task-cache/cargo"}]


def test_checkout_cache_name_multiple_repos(graph_config, run_caches):
graph_config._config["taskgraph"]["repositories"] = {
"foo": {
"name": "Foo",
"project-regex": "some-project",
"default-repository": True,
"default-ref": "foo",
"type": "git",
},
"bar": {
"name": "Bar",
"project-regex": "other-project",
"default-repository": False,
"default-ref": "bar",
"type": "git",
},
}
caches = run_caches(
{"run": {"checkout": {"foo": {}}, "use-caches": ["checkout"]}},
graph_config=graph_config,
)[0]
assert caches == [
{
"cache-name": "checkouts-repos-1d0c10d2cdc5a6f1acd71c0b363719b15805bcdd809ad7b459a43796203ff2c4",
"directory": "checkouts",
}
]


def test_checkout_cache_name_sparse(run_caches):
caches = run_caches({"run": {"sparse-profile": "foo", "use-caches": ["checkout"]}})[
0
]
assert caches == [{"cache-name": "checkouts-sparse", "directory": "checkouts"}]
4 changes: 3 additions & 1 deletion test/test_util_cached_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ def test_add_optimization(
cache_type = "cache-type"
cache_name = "cache-name"

config = make_transform_config(None, None, extra_params, extra_graph_config)
config = make_transform_config(
None, None, extra_params=extra_params, extra_graph_config=extra_graph_config
)

try:
add_optimization(
Expand Down

0 comments on commit 5c7f61d

Please sign in to comment.