Skip to content

Commit c529536

Browse files
authored
fix: make hash_paths work when base_path is set (#723)
Since #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 c529536

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/taskgraph/util/hash.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,22 @@ def hash_paths(base_path, patterns):
3838
found.update(matches)
3939
else:
4040
raise Exception(f"{pattern} did not match anything")
41-
for path in sorted(found):
42-
h.update(
43-
f"{hash_path(mozpath.abspath(mozpath.join(base_path, path)))} {mozpath.normsep(path)}\n".encode()
44-
)
41+
for abs_path, path in sorted(found):
42+
h.update(f"{hash_path(abs_path)} {mozpath.normsep(path)}\n".encode())
4543
return h.hexdigest()
4644

4745

4846
@functools.lru_cache(maxsize=None)
4947
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)]
48+
repo = get_repository(os.getcwd())
49+
files = _get_all_files(base_path, repo)
50+
return [
51+
(mozpath.join(repo.path, path), path)
52+
for path in files
53+
if mozpath.match(path, pattern)
54+
]
5255

5356

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

test/test_transforms_run_toolchain.py

Lines changed: 15 additions & 2 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,25 @@ 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(
41+
"taskgraph.util.hash.get_repository", new=lambda path: GitRepository(path)
42+
)
3643

3744
def inner(task):
3845
task = merge(TASK_DEFAULTS, task)
3946
m = mocker.patch(
4047
"taskgraph.transforms.run.toolchain.configure_taskdesc_for_run"
4148
)
42-
run_transform(make_task_description, task)
43-
return m.call_args[0]
49+
repo_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data"))
50+
cwd = os.getcwd()
51+
try:
52+
os.chdir(repo_dir)
53+
run_transform(make_task_description, task)
54+
return m.call_args[0]
55+
finally:
56+
os.chdir(cwd)
4457

4558
return inner
4659

0 commit comments

Comments
 (0)