Skip to content

Commit

Permalink
Remove custom error solutions for PathOutsideRoot exceptions
Browse files Browse the repository at this point in the history
Simplify error handling across package managers by removing redundant
custom error solutions. Leverage PathOutsideRoot exception, which
provides a default solution. Replace custom exception raising with
direct calls to join_within_root, ensuring consistent error handling and
avoiding unnecessary exception type modifications.

Note: This change does not apply to pydantic validators that validate
paths inside models. Those have to raise ValueError.

Signed-off-by: Michal Šoltis <[email protected]>
  • Loading branch information
slimreaper35 committed Dec 3, 2024
1 parent a01e3f5 commit 7e5f9dc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 35 deletions.
22 changes: 4 additions & 18 deletions cachi2/core/package_managers/gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from cachi2.core.models.output import EnvironmentVariable, RequestOutput
from cachi2.core.models.property_semantics import PropertySet
from cachi2.core.models.sbom import Component
from cachi2.core.rooted_path import PathOutsideRoot, RootedPath
from cachi2.core.rooted_path import RootedPath
from cachi2.core.scm import get_repo_id
from cachi2.core.utils import get_cache_dir, load_json_stream, run_cmd

Expand Down Expand Up @@ -727,14 +727,7 @@ def _protect_against_symlinks(app_dir: RootedPath) -> None:
"""

def check_potential_symlink(relative_path: Union[str, Path]) -> None:
try:
app_dir.join_within_root(relative_path)
except PathOutsideRoot as e:
e.solution = (
"Found a potentially harmful symlink, which would make the go command read "
"a file outside of your source repository. Refusing to proceed."
)
raise
app_dir.join_within_root(relative_path)

check_potential_symlink("go.mod")
check_potential_symlink("go.sum")
Expand Down Expand Up @@ -1456,15 +1449,8 @@ def _validate_local_replacements(modules: Iterable[ParsedModule], app_path: Root
if module.replace and module.replace.path.startswith(".")
]

for name, path in replaced_paths:
try:
app_path.join_within_root(path)
except PathOutsideRoot as e:
e.solution = (
f"The module '{name}' is being replaced by the local path '{path}', "
"which falls outside of the repository root. Refusing to proceed."
)
raise
for _, path in replaced_paths:
app_path.join_within_root(path)


def _parse_vendor(module_dir: RootedPath) -> Iterable[ParsedModule]:
Expand Down
13 changes: 2 additions & 11 deletions cachi2/core/package_managers/yarn_classic/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pydantic

from cachi2.core.errors import PackageRejected
from cachi2.core.rooted_path import PathOutsideRoot, RootedPath
from cachi2.core.rooted_path import RootedPath


class Workspace(pydantic.BaseModel):
Expand All @@ -32,16 +32,7 @@ def ensure_no_path_leads_out(
Does nothing when path does not exist in the file system.
"""
for path in paths:
try:
source_dir.join_within_root(path)
except PathOutsideRoot:
raise PackageRejected(
f"Found a workspace path which is not relative to package: {path}",
solution=(
"Avoid using packages which try to access your filesystem "
"outside of package directory."
),
)
source_dir.join_within_root(path)


def _ensure_workspaces_are_well_formed(
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/package_managers/test_gomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,9 @@ def test_resolve_gomod_suspicious_symlinks(symlinked_file: str, gomod_request: R
app_dir = gomod_request.source_dir

expect_err_msg = re.escape(f"Joining path '{symlinked_file}' to '{app_dir}'")
with pytest.raises(PathOutsideRoot, match=expect_err_msg) as exc_info:
with pytest.raises(PathOutsideRoot, match=expect_err_msg):
_resolve_gomod(app_dir, gomod_request, tmp_path, version_resolver)

e = exc_info.value
assert "Found a potentially harmful symlink" in e.friendly_msg()


@pytest.mark.parametrize(
"go_sum_content, expect_modules",
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/package_managers/yarn_classic/test_workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from cachi2.core.errors import PackageRejected
from cachi2.core.errors import PathOutsideRoot
from cachi2.core.package_managers.yarn_classic.workspaces import (
Workspace,
_extract_workspaces_globs,
Expand All @@ -23,7 +23,7 @@ def test_packages_with_workspaces_outside_source_dir_are_rejected(
mock_get_ws_paths.return_value = [Path("/tmp/foo/bar"), Path("/usr")]
package_path = RootedPath("/tmp/foo")

with pytest.raises(PackageRejected):
with pytest.raises(PathOutsideRoot):
extract_workspace_metadata(package_path)


Expand Down

0 comments on commit 7e5f9dc

Please sign in to comment.