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

Fix document detection #267

Merged
merged 3 commits into from
Nov 17, 2024
Merged
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
21 changes: 10 additions & 11 deletions server/galaxyls/services/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

from galaxyls.services.xml.types import DocumentType

MAX_PEEK_CONTENT = 100
TAG_GROUP_NAME = "tag"
TAG_REGEX = rf"[\n\s]*?.*?[\n\s]*?<(?!\?)(?P<{TAG_GROUP_NAME}>[\w]*)"
MAX_PEEK_CONTENT = 1000
TAG_GROUP_NAME = "root_tag"
TAG_REGEX = r"[\n\s]*?.*?[\n\s]*?<(?!\?)(?!\!)(?P<root_tag>[\w]*)"
SUPPORTED_ROOT_TAGS = [e.name.lower() for e in DocumentType if e != DocumentType.UNKNOWN]


class DocumentValidator:
Expand All @@ -19,17 +20,15 @@ def has_valid_root(cls, document: Document) -> bool:
or is an empty document."""
if DocumentValidator.is_empty_document(document):
return True
root = DocumentValidator._get_document_root_tag(document)
if root is not None:
root_tag = root.upper()
supported = [e.name for e in DocumentType if e != DocumentType.UNKNOWN]
return root_tag == "" or root_tag in supported
root_tag = DocumentValidator.get_document_root_tag(document)
if root_tag is not None:
return root_tag == "" or root_tag in SUPPORTED_ROOT_TAGS
return False

@classmethod
def is_tool_document(cls, document: Document) -> bool:
"""Checks if the document's root element is <tool>."""
root = DocumentValidator._get_document_root_tag(document)
root = DocumentValidator.get_document_root_tag(document)
if root is not None:
root_tag = root.upper()
return root_tag == DocumentType.TOOL.name
Expand All @@ -41,11 +40,11 @@ def is_empty_document(cls, document: Document) -> bool:
return not document.source or document.source.isspace()

@classmethod
def _get_document_root_tag(cls, document: Document) -> Optional[str]:
def get_document_root_tag(cls, document: Document) -> Optional[str]:
"""Checks the first MAX_PEEK_CONTENT characters of the document for a root tag and
returns the name of the tag if found."""
content_peek = document.source[:MAX_PEEK_CONTENT]
match = re.match(TAG_REGEX, content_peek)
match = re.search(TAG_REGEX, content_peek)
if match:
group = match.group(TAG_GROUP_NAME)
return group
Expand Down
53 changes: 53 additions & 0 deletions server/galaxyls/tests/unit/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ class TestDocumentValidatorClass:
('<?xml version="1.0" encoding="UTF-8"?>\n<macros>', True),
("test", False),
("<test>", False),
("<visualization>", False),
(
'<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE visualization SYSTEM "../../visualization.dtd">\n<visualization>',
False,
),
(
'<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE test SYSTEM "../../test.dtd">\n',
False,
),
(
'<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE test SYSTEM "../../test.dtd">\n<tool',
True,
),
],
)
def test_has_valid_root_returns_expected(self, source: str, expected: bool) -> None:
Expand All @@ -45,3 +58,43 @@ def test_has_valid_root_returns_expected(self, source: str, expected: bool) -> N
actual = validator.has_valid_root(document)

assert actual == expected

@pytest.mark.parametrize(
"source, expected",
[
("", None),
(" ", None),
(" \n ", None),
("test", None),
("<", ""),
("<tes", "tes"),
("<test ", "test"),
("<tool>", "tool"),
(" <tool>", "tool"),
("\n<tool>", "tool"),
("\n <tool>", "tool"),
(" \n <tool>", "tool"),
("unexpected <tool>", "tool"),
(" unexpected <tool>", "tool"),
("\nunexpected\n <tool>", "tool"),
('<?xml version="1.0" encoding="UTF-8"?><tool>', "tool"),
('<?xml version="1.0" encoding="UTF-8"?>\n<tool>', "tool"),
("<macros>", "macros"),
('<?xml version="1.0" encoding="UTF-8"?>\n<macros>', "macros"),
("<test>", "test"),
('<test attribute="0">', "test"),
("<visualization>", "visualization"),
('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE test SYSTEM "../../test.dtd">\n', None),
(
'<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE visualization SYSTEM "../../visualization.dtd">\n<visualization>',
"visualization",
),
],
)
def test_get_document_root_tag_returns_expected(self, source: str, expected: bool) -> None:
document = TestUtils.to_document(source)
validator = DocumentValidator()

actual = validator.get_document_root_tag(document)

assert actual == expected