Skip to content

Commit

Permalink
Merge pull request #92 from cmatsuoka/fix/fileset-relative-path-valid…
Browse files Browse the repository at this point in the history
…ation

parts: add validators for stage and prime filesets (CRAFT-366)
  • Loading branch information
cmatsuoka authored Jul 13, 2021
2 parents 2d90858 + 588c316 commit 793db26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion craft_parts/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Set

from pydantic import BaseModel, Field, ValidationError
from pydantic import BaseModel, Field, ValidationError, validator

from craft_parts import errors
from craft_parts.dirs import ProjectDirs
Expand Down Expand Up @@ -63,6 +63,18 @@ class Config:
allow_mutation = False
alias_generator = lambda s: s.replace("_", "-") # noqa: E731

# pylint: disable=no-self-argument,no-self-use
@validator("stage_files", "prime_files", each_item=True)
def validate_relative_path_list(cls, item):
"""Check if the list does not contain empty of absolute paths."""
assert item != "", "path cannot be empty"
assert (
item[0] != "/"
), f"{item!r} must be a relative path (cannot start with '/')"
return item

# pylint: enable=no-self-argument,no-self-use

@classmethod
def unmarshal(cls, data: Dict[str, Any]) -> "PartSpec":
"""Create and populate a new ``PartSpec`` object from dictionary data.
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ def test_part_unmarshal_type_error(self):
assert raised.value.part_name == "foo"
assert raised.value.message == "'plugin': str type expected"

@pytest.mark.parametrize("fileset", ["stage", "prime"])
def test_relative_path_validation(self, fileset):
with pytest.raises(errors.PartSpecificationError) as raised:
Part("foo", {fileset: ["bar", "/baz", ""]})
assert raised.value.part_name == "foo"
assert raised.value.message == (
f"{fileset!r},1: '/baz' must be a relative path (cannot start with '/')\n"
f"{fileset!r},2: path cannot be empty"
)


class TestPartHelpers:
"""Test part-related helper functions."""
Expand Down

0 comments on commit 793db26

Please sign in to comment.