Skip to content

Commit 2a8b33c

Browse files
committed
fix: make hash_paths work when base_path is set
Since taskcluster#664 we started returning paths relative to the repository root from `_find_matching_files`. This meant that when we join `base_path` with `path` in `hash_paths`, we end up with `base_path` in there twice. Fixing this without breaking `mozpath.match` means we need to join together the repository root and matched files after `mozpatch.match`. This, in turn, requires that some tests are able to call `get_repository`, which requires faking a repository being present.
1 parent b640f14 commit 2a8b33c

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/taskgraph/util/hash.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ def hash_paths(base_path, patterns):
4040
raise Exception(f"{pattern} did not match anything")
4141
for path in sorted(found):
4242
h.update(
43-
f"{hash_path(mozpath.abspath(mozpath.join(base_path, path)))} {mozpath.normsep(path)}\n".encode()
43+
f"{hash_path(path)} {mozpath.normsep(path)}\n".encode()
4444
)
4545
return h.hexdigest()
4646

4747

4848
@functools.lru_cache(maxsize=None)
4949
def _find_matching_files(base_path, pattern):
50-
files = _get_all_files(base_path)
51-
return [path for path in files if mozpath.match(path, pattern)]
50+
repo = get_repository(os.getcwd())
51+
files = _get_all_files(base_path, repo)
52+
return [mozpath.join(repo.path, path) for path in files if mozpath.match(path, pattern)]
5253

5354

5455
@functools.lru_cache(maxsize=None)
55-
def _get_all_files(base_path):
56-
repo = get_repository(os.getcwd())
56+
def _get_all_files(base_path, repo):
5757
return repo.get_tracked_files(base_path)

test/test_transforms_run_toolchain.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
Tests for the 'toolchain' transforms.
33
"""
44

5+
import os.path
56
from pprint import pprint
67

78
import pytest
89

910
from taskgraph.transforms.run import make_task_description
1011
from taskgraph.util.templates import merge
12+
from taskgraph.util.vcs import GitRepository
1113

1214
TASK_DEFAULTS = {
1315
"description": "fake description",
@@ -33,14 +35,23 @@ def run_task_using(mocker, run_transform):
3335
"taskcluster/scripts/toolchain/run.sh",
3436
"taskcluster/scripts/toolchain/run.ps1",
3537
]
38+
# fake a repository during the test, which is needed for `_get_all_files` to
39+
# work correctly
40+
mocker.patch("taskgraph.util.hash.get_repository", new=lambda path: GitRepository(path))
3641

3742
def inner(task):
3843
task = merge(TASK_DEFAULTS, task)
3944
m = mocker.patch(
4045
"taskgraph.transforms.run.toolchain.configure_taskdesc_for_run"
4146
)
42-
run_transform(make_task_description, task)
43-
return m.call_args[0]
47+
repo_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data"))
48+
cwd = os.getcwd()
49+
try:
50+
os.chdir(repo_dir)
51+
run_transform(make_task_description, task)
52+
return m.call_args[0]
53+
finally:
54+
os.chdir(cwd)
4455

4556
return inner
4657

@@ -84,7 +95,7 @@ def assert_docker_worker(task, taskdesc):
8495
},
8596
"cache": {
8697
"digest-data": [
87-
"a81c34908c02a5b6a14607465397c0f82d5c406b3e2e73f2c29644016d7bb031",
98+
"5683ebaf299188e06491f881b036427ab3cc49c38839e0b5d1a2b8d68b57773b",
8899
"public/build/artifact.zip",
89100
"toolchain-build",
90101
],
@@ -146,7 +157,7 @@ def assert_generic_worker(task, taskdesc):
146157
"attributes": {"toolchain-artifact": "public/build/artifact.zip"},
147158
"cache": {
148159
"digest-data": [
149-
"a81c34908c02a5b6a14607465397c0f82d5c406b3e2e73f2c29644016d7bb031",
160+
"5683ebaf299188e06491f881b036427ab3cc49c38839e0b5d1a2b8d68b57773b",
150161
"public/build/artifact.zip",
151162
"--foo",
152163
"bar",

0 commit comments

Comments
 (0)