Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change RelatedFile's file_path to path & improve validation error message #4605

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions demisto_sdk/commands/content_graph/parsers/related_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, main_file_path: Path, git_sha: Optional[str] = None) -> None:
self.main_file_path: Path = main_file_path
self.git_sha = git_sha
self.exist: bool = False
self.file_path: Path = self.find_the_right_path(self.get_optional_paths())
self.path: Path = self.find_the_right_path(self.get_optional_paths())

@property
def git_status(self) -> Union[GitStatuses, None]:
Expand All @@ -56,7 +56,7 @@ def git_status(self) -> Union[GitStatuses, None]:
remote, branch = git_util.handle_prev_ver(
self.git_sha # type: ignore[arg-type]
)
status = git_util._check_file_status(str(self.file_path), remote, branch)
status = git_util._check_file_status(str(self.path), remote, branch)
return None if not status else GitStatuses(status)

def find_the_right_path(self, file_paths: List[Path]) -> Path:
Expand Down Expand Up @@ -94,12 +94,12 @@ def file_content(self) -> str:
try:
if self.git_sha:
self.file_content_str = TextFile.read_from_git_path(
path=self.file_path,
path=self.path,
tag=self.git_sha,
)
else:
self.file_content_str = TextFile.read_from_local_path(
path=self.file_path
path=self.path
)
except Exception as e:
logger.debug(f"Failed to get related text file, error: {e}")
Expand Down Expand Up @@ -236,11 +236,11 @@ def load_image(self):

class PNGFiles(ImageFiles):
def get_file_size(self):
return self.file_path.stat()
return self.path.stat()

def load_image(self) -> Union[str, bytearray, memoryview]:
encoded_image = ""
with open(self.file_path, "rb") as image:
with open(self.path, "rb") as image:
image_data = image.read()
encoded_image = base64.b64encode(image_data) # type: ignore
if isinstance(encoded_image, bytes):
Expand All @@ -251,7 +251,7 @@ def load_image(self) -> Union[str, bytearray, memoryview]:
class SVGFiles(ImageFiles):
def load_image(self) -> bytes:
encoded_image = b""
with open(self.file_path, "rb") as image_file:
with open(self.path, "rb") as image_file:
encoded_image = image_file.read() # type: ignore
return encoded_image

Expand Down
6 changes: 3 additions & 3 deletions demisto_sdk/commands/validate/tests/PA_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,18 +1663,18 @@ def test_PackFilesValidator_fix(file_attribute: str):
pack = create_pack_object()
meta_file: RelatedFile = getattr(pack, file_attribute)

meta_file.file_path.unlink()
meta_file.path.unlink()
meta_file.exist = False

assert not meta_file.exist # sanity check
assert not meta_file.file_path.exists() # sanity check
assert not meta_file.path.exists() # sanity check

assert PackFilesValidator().obtain_invalid_content_items(
[pack]
) # invalid once deleted
PackFilesValidator().fix(pack)

assert meta_file.file_path.exists()
assert meta_file.path.exists()
assert meta_file.exist # changed in the fix


Expand Down
4 changes: 2 additions & 2 deletions demisto_sdk/commands/validate/tests/RN_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ def test_IsBCRNExistValidator_obtain_invalid_content_items():
content_items=content_items
)
expected_msgs = [
f"The release notes contain information about breaking changes but missing a breaking change file, make sure to add one as {str(content_items[1].release_note.file_path).replace('.md', '.json')} and that the file contains the 'breakingChanges' entry.",
f"The release notes contain information about breaking changes but missing a breaking change file, make sure to add one as {str(content_items[2].release_note.file_path).replace('.md', '.json')} and that the file contains the 'breakingChanges' entry.",
f"The release notes contain information about breaking changes but missing a breaking change file, make sure to add one as {str(content_items[1].release_note.path).replace('.md', '.json')} and that the file contains the 'breakingChanges' entry.",
f"The release notes contain information about breaking changes but missing a breaking change file, make sure to add one as {str(content_items[2].release_note.path).replace('.md', '.json')} and that the file contains the 'breakingChanges' entry.",
]

assert len(results) == 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def obtain_invalid_content_items(
ValidationResult(
validator=self,
message=self.error_message.format(
content_item.description_file.file_path.name
content_item.description_file.path.name
),
content_object=content_item,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def obtain_invalid_content_items(
ValidationResult(
validator=self,
message=self.error_message.format(
content_item.description_file.file_path.name
content_item.description_file.path.name
),
content_object=content_item,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def obtain_invalid_content_items(
]

def is_image_valid(self, content_item: ContentTypes):
file_type = content_item.image.file_path.suffix
file_type = content_item.image.path.suffix
if file_type == ".png":
return content_item.image.get_file_size().st_size > IMAGE_MAX_SIZE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@


def is_image_dimensions_valid(content_item: ContentTypes) -> bool:
if content_item.image.file_path.suffix == "png":
return (IMAGE_WIDTH, IMAGE_HEIGHT) == imagesize.get(
content_item.image.file_path
)
if content_item.image.path.suffix == "png":
return (IMAGE_WIDTH, IMAGE_HEIGHT) == imagesize.get(content_item.image.path)
return True


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def obtain_invalid_content_items(
def fix(self, content_item: ContentTypes) -> FixResult:
for meta_file in find_related_files(content_item):
if not meta_file.exist:
logger.debug(f"creating {type(meta_file)} {meta_file.file_path!s}")
meta_file.file_path.touch()
logger.debug(f"creating {type(meta_file)} {meta_file.path!s}")
meta_file.path.touch()
meta_file.exist = True

return FixResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def obtain_invalid_content_items(
ValidationResult(
validator=self,
message=self.error_message.format(", ".join(lines_contain_demsito)),
content_object=content_item,
content_object=content_item.readme,
)
for content_item in content_items
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def obtain_invalid_content_items(
for content_item in content_items:
if "breaking change" not in content_item.release_note.file_content:
continue
json_path = str(content_item.release_note.file_path).replace(".md", ".json")
json_path = str(content_item.release_note.path).replace(".md", ".json")
if Path(json_path).exists():
if (
json_file_content := get_dict_from_file(path=json_path)[0]
Expand Down
6 changes: 3 additions & 3 deletions demisto_sdk/commands/validate/validators/base_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
BaseContentMetaclass,
)
from demisto_sdk.commands.content_graph.objects.content_item import ContentItem
from demisto_sdk.commands.content_graph.parsers.related_files import RelatedFileType
from demisto_sdk.commands.content_graph.parsers.related_files import RelatedFile, RelatedFileType

ContentTypes = TypeVar("ContentTypes", bound=BaseContent)

Expand Down Expand Up @@ -220,10 +220,10 @@ def get_all_validators_specific_validation(
class BaseResult(BaseModel):
validator: BaseValidator
message: str
content_object: BaseContent
content_object: BaseContent | RelatedFile

@property
def format_readable_message(self):
def format_readable_message(self) -> str:
path: Path = self.content_object.path
if path.is_absolute():
path = path.relative_to(CONTENT_PATH)
Expand Down
Loading