Skip to content

Commit 5b946b7

Browse files
committed
fix: make hash_paths work when base_path is set
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 5b946b7

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/taskgraph/util/hash.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ 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):
41+
for (abs_path, 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(abs_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), 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: 13 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,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

0 commit comments

Comments
 (0)