Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 3457213

Browse files
taylormadorebrunoapimentel
authored andcommitted
fix resolve_gomod function typing
The input parameters to the resolve_gomod function for the application directory and the overall source directory are both Paths or subclasses of Path Signed-off-by: Taylor Madore <tmadore@redhat.com>
1 parent f3a9691 commit 3457213

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

cachito/workers/pkg_managers/gomod.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,12 @@ def match_parent_module(package_name: str, module_names: Iterable[str]) -> Optio
280280

281281

282282
@tracer.start_as_current_span("resolve_gomod")
283-
def resolve_gomod(app_source_path, request, dep_replacements=None, git_dir_path=None):
283+
def resolve_gomod(
284+
app_source_path: Path,
285+
request: dict[str, Any],
286+
dep_replacements: Optional[list[dict[str, str]]] = None,
287+
git_dir_path: Optional[Path] = None,
288+
) -> dict[str, Any]:
284289
"""
285290
Resolve and fetch gomod dependencies for given app source archive.
286291
@@ -455,10 +460,10 @@ def go_list_deps(pattern: Literal["./...", "all"]) -> Iterator[GoPackage]:
455460
main_packages.append({"pkg": main_pkg, "pkg_deps": pkg_deps})
456461

457462
_vet_local_deps(main_module_deps, main_module_name, app_source_path, git_dir_path)
458-
for pkg in main_packages:
463+
for package in main_packages:
459464
# Local dependencies are always relative to the main module, even for subpackages
460-
_vet_local_deps(pkg["pkg_deps"], main_module_name, app_source_path, git_dir_path)
461-
_set_full_local_dep_relpaths(pkg["pkg_deps"], main_module_deps)
465+
_vet_local_deps(package["pkg_deps"], main_module_name, app_source_path, git_dir_path)
466+
_set_full_local_dep_relpaths(package["pkg_deps"], main_module_deps)
462467

463468
return {
464469
"module": main_module,
@@ -503,7 +508,7 @@ def _get_name_and_version(module: GoModule) -> tuple[str, str]:
503508
return name, version
504509

505510

506-
def _should_vendor_deps(flags: List[str], app_dir: str, strict: bool) -> Tuple[bool, bool]:
511+
def _should_vendor_deps(flags: List[str], app_dir: Path, strict: bool) -> Tuple[bool, bool]:
507512
"""
508513
Determine if Cachito should vendor dependencies and if it is allowed to make changes.
509514
@@ -517,7 +522,7 @@ def _should_vendor_deps(flags: List[str], app_dir: str, strict: bool) -> Tuple[b
517522
:return: (should vendor: bool, allowed to make changes in the vendor directory: bool)
518523
:raise ValidationError: if the vendor dir is present, the flags are not used and we are strict
519524
"""
520-
vendor = Path(app_dir) / "vendor"
525+
vendor = app_dir / "vendor"
521526

522527
if "gomod-vendor-check" in flags:
523528
return True, not vendor.exists()
@@ -534,7 +539,7 @@ def _should_vendor_deps(flags: List[str], app_dir: str, strict: bool) -> Tuple[b
534539

535540

536541
@tracer.start_as_current_span("_vendor_deps")
537-
def _vendor_deps(go: Go, run_params: dict, can_make_changes: bool, git_dir: str) -> list[GoModule]:
542+
def _vendor_deps(go: Go, run_params: dict, can_make_changes: bool, git_dir: Path) -> list[GoModule]:
538543
"""
539544
Vendor golang dependencies.
540545
@@ -609,7 +614,7 @@ def parse_module_line(line: str) -> GoModule:
609614

610615

611616
@tracer.start_as_current_span("_vendor_changed")
612-
def _vendor_changed(git_dir: str, app_dir: str) -> bool:
617+
def _vendor_changed(git_dir: Path, app_dir: str) -> bool:
613618
"""Check for changes in the vendor directory."""
614619
vendor = Path(app_dir).relative_to(git_dir).joinpath("vendor")
615620
modules_txt = vendor / "modules.txt"
@@ -640,7 +645,7 @@ def _vet_local_deps(
640645
dependencies: List[dict],
641646
module_name: str,
642647
app_source_path: Path,
643-
git_dir_path: str,
648+
git_dir_path: Path,
644649
) -> None:
645650
"""Fail if any local file dependency path is either absolute or outside the repository."""
646651
for dep in dependencies:
@@ -666,19 +671,19 @@ def _vet_local_deps(
666671

667672

668673
def _validate_local_dependency_path(
669-
app_source_path: Path, git_dir_path: str, dep_path: str
674+
app_source_path: Path, git_dir_path: Path, dep_path: str
670675
) -> None:
671676
"""
672677
Validate that the local dependency path is not outside the repository.
673678
674679
:param Path app_source_path: the full path to the application source code
675-
:param str git_dir_path: the full path to the git repository
680+
:param Path git_dir_path: the full path to the git repository
676681
:param str dep_path: the relative path for local replacements (the dep version)
677682
:raise ValidationError: if the local dependency path is invalid
678683
"""
679684
try:
680685
resolved_dep_path = Path(app_source_path, dep_path).resolve()
681-
resolved_dep_path.relative_to(Path(git_dir_path).resolve())
686+
resolved_dep_path.relative_to(git_dir_path.resolve())
682687
except ValueError:
683688
raise ValidationError(f"The local dependency path {dep_path} is outside the repository")
684689

tests/test_workers/test_pkg_managers/test_gomod.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_resolve_gomod(
122122
mock_go_release.return_value = "go0.1.0"
123123
mock_get_gomod_version.return_value = ("0.1.1", "0.1.2")
124124

125-
module_dir = str(tmp_path / "path/to/module")
125+
module_dir = tmp_path / "path/to/module"
126126
request = {"id": 3, "ref": "c50b93a32df1c9d700e3e80996845bc2e13be848", "flags": []}
127127
if cgo_disable:
128128
request["flags"].append("cgo-disable")
@@ -255,7 +255,7 @@ def test_resolve_gomod_vendor_dependencies(
255255
if force_gomod_tidy:
256256
request["flags"].append("force-gomod-tidy")
257257

258-
gomod_ = gomod.resolve_gomod(str(module_dir), request)
258+
gomod_ = gomod.resolve_gomod(module_dir, request)
259259

260260
assert mock_run.call_args_list[0][0][0] == ["go", "mod", "vendor"]
261261
# when vendoring, go list should be called without -mod readonly
@@ -312,7 +312,7 @@ def test_resolve_gomod_strict_mode_raise_error(
312312
mock.Mock(returncode=0, stdout=""), # go list -deps -json ./...
313313
]
314314

315-
module_dir = str(tmp_path)
315+
module_dir = tmp_path
316316
tmp_path.joinpath("vendor").mkdir()
317317

318318
request = {"id": 3, "ref": "c50b93a32df1c9d700e3e80996845bc2e13be848"}
@@ -380,7 +380,7 @@ def test_resolve_gomod_no_deps(
380380
mock_go_release.return_value = "go1.21.0"
381381
mock_get_gomod_version.return_value = ("0.1.1", "0.1.2")
382382

383-
module_dir = str(tmp_path / "/path/to/module")
383+
module_dir = tmp_path / "/path/to/module"
384384

385385
request = {"id": 3, "ref": "c50b93a32df1c9d700e3e80996845bc2e13be848"}
386386
if force_gomod_tidy:
@@ -430,7 +430,7 @@ def test_go_list_cmd_failure(
430430
go_mod_rc: int,
431431
go_list_rc: int,
432432
) -> None:
433-
module_dir = "/path/to/module"
433+
module_dir = Path("/path/to/module")
434434
request = {"id": 3, "ref": "c50b93a32df1c9d700e3e80996845bc2e13be848"}
435435

436436
# Mock the tempfile.TemporaryDirectory context manager
@@ -695,8 +695,8 @@ def test_vet_local_deps(mock_validate_dep_path):
695695
{"name": "baz", "version": "./local/baz"},
696696
]
697697
module_name = "some-module"
698-
app_dir = "/repo/some-module"
699-
git_dir = "/repo"
698+
app_dir = Path("/repo/some-module")
699+
git_dir = Path("/repo")
700700
mock_validate_dep_path.return_value = None
701701

702702
gomod._vet_local_deps(dependencies, module_name, app_dir, git_dir)
@@ -719,7 +719,7 @@ def test_vet_local_deps(mock_validate_dep_path):
719719
)
720720
def test_vet_local_deps_abspath(platform_specific_path):
721721
dependencies = [{"name": "foo", "version": platform_specific_path}]
722-
app_dir = "/some/path"
722+
app_dir = Path("/some/path")
723723

724724
expect_error = re.escape(
725725
f"Absolute paths to gomod dependencies are not supported: {platform_specific_path}"
@@ -902,7 +902,7 @@ def test_should_vendor_deps(flags, vendor_exists, expect_result, tmp_path):
902902
if vendor_exists:
903903
tmp_path.joinpath("vendor").mkdir()
904904

905-
assert gomod._should_vendor_deps(flags, str(tmp_path), False) == expect_result
905+
assert gomod._should_vendor_deps(flags, tmp_path, False) == expect_result
906906

907907

908908
@pytest.mark.parametrize(
@@ -921,9 +921,9 @@ def test_should_vendor_deps_strict(flags, vendor_exists, expect_error, tmp_path)
921921
if expect_error:
922922
msg = 'The "gomod-vendor" or "gomod-vendor-check" flag must be set'
923923
with pytest.raises(ValidationError, match=msg):
924-
gomod._should_vendor_deps(flags, str(tmp_path), True)
924+
gomod._should_vendor_deps(flags, tmp_path, True)
925925
else:
926-
gomod._should_vendor_deps(flags, str(tmp_path), True)
926+
gomod._should_vendor_deps(flags, tmp_path, True)
927927

928928

929929
@pytest.mark.parametrize("can_make_changes", [True, False])

0 commit comments

Comments
 (0)