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

Handle trailing whitespace in tag values #393

Merged
merged 2 commits into from
Jul 3, 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
4 changes: 2 additions & 2 deletions specfile/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class PatchMacro(PrepMacro):
@property
def number(self) -> int:
"""Number of the %patch macro."""
tokens = re.split(r"(\d+)", self.name, maxsplit=1)
tokens = re.split(r"(\d+)$", self.name, maxsplit=1)
if len(tokens) > 1:
return int(tokens[1])
if self.options.P is not None:
Expand All @@ -123,7 +123,7 @@ def number(self) -> int:

@number.setter
def number(self, value: int) -> None:
tokens = re.split(r"(\d+)", self.name, maxsplit=1)
tokens = re.split(r"(\d+)$", self.name, maxsplit=1)
if len(tokens) > 1:
self.name = f"{tokens[0]}{value}"
elif self.options.P is not None:
Expand Down
2 changes: 1 addition & 1 deletion specfile/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _extract_number(self) -> Optional[str]:
Returns:
Extracted number or `None` if there isn't one.
"""
tokens = re.split(r"(\d+)", self._tag.name, maxsplit=1)
tokens = re.split(r"(\d+)$", self._tag.name, maxsplit=1)
if len(tokens) > 1:
return tokens[1]
return None
Expand Down
8 changes: 6 additions & 2 deletions specfile/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ def regex_pattern(tag):
data = []
buffer: List[str] = []
for line, valid in lines:
ws = ""
tokens = re.split(r"([^\S\n]+)$", line, maxsplit=1)
if len(tokens) > 1:
line, ws, _ = tokens
nforro marked this conversation as resolved.
Show resolved Hide resolved
line, prefix, suffix = split_conditional_macro_expansion(line)
# find out if there is a match for one of the tag regexes
m = next((m for m in (r.match(line) for r in tag_regexes) if m), None)
Expand All @@ -511,13 +515,13 @@ def regex_pattern(tag):
Comments.parse(buffer),
valid,
prefix,
suffix,
suffix + ws,
context,
)
)
buffer = []
else:
buffer.append(prefix + line + suffix)
buffer.append(prefix + line + suffix + ws)
return cls(data, buffer)

def get_raw_section_data(self) -> List[str]:
Expand Down
8 changes: 4 additions & 4 deletions specfile/value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MacroSubstitution(Node):
"""Node representing macro substitution, e.g. _%version_."""

def __init__(self, body: str) -> None:
tokens = re.split(r"([?!]*)", body, maxsplit=1)
tokens = re.split(r"^([?!]*)", body, maxsplit=1)
if len(tokens) == 1:
self.prefix, self.name = "", tokens[0]
else:
Expand All @@ -97,7 +97,7 @@ class EnclosedMacroSubstitution(Node):
"""Node representing macro substitution enclosed in brackets, e.g. _%{?dist}_."""

def __init__(self, body: str) -> None:
tokens = re.split(r"([?!]*)", body, maxsplit=1)
tokens = re.split(r"^([?!]*)", body, maxsplit=1)
if len(tokens) == 1:
self.prefix, rest = "", tokens[0]
else:
Expand Down Expand Up @@ -129,7 +129,7 @@ class ConditionalMacroExpansion(Node):
"""Node representing conditional macro expansion, e.g. _%{?prerel:0.}_."""

def __init__(self, condition: str, body: List[Node]) -> None:
tokens = re.split(r"([?!]*)", condition, maxsplit=1)
tokens = re.split(r"^([?!]*)", condition, maxsplit=1)
if len(tokens) == 1:
self.prefix, self.name = "", tokens[0]
else:
Expand Down Expand Up @@ -271,7 +271,7 @@ def find_macro_end(index):
elif value[start + 1] == "{":
if ":" in value[start:end]:
condition, body = value[start + 2 : end - 1].split(":", maxsplit=1)
tokens = re.split(r"([?!]*)", condition, maxsplit=1)
tokens = re.split(r"^([?!]*)", condition, maxsplit=1)
prefix = tokens[0 if len(tokens) == 1 else 1]
if "?" in prefix:
result.append(
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_parse():
"Epoch: 1",
"%endif",
"",
"Requires: make",
"Requires: make ",
"Requires(post): bash",
"",
"Provides: testX = %{version}-%{release}",
Expand Down Expand Up @@ -102,7 +102,15 @@ def test_get_raw_section_data():
Comments([Comment("this is a valid comment", " # ")]),
),
Tag("Epoch", "1", ": ", Comments([], ["", "%if 0"])),
Tag("Requires", "make", ": ", Comments([], ["%endif", ""])),
Tag(
"Requires",
"make",
": ",
Comments([], ["%endif", ""]),
True,
"",
" ",
),
Tag("Requires(post)", "bash", ": ", Comments()),
Tag(
"Suggests",
Expand Down Expand Up @@ -133,7 +141,7 @@ def test_get_raw_section_data():
"Epoch: 1",
"%endif",
"",
"Requires: make",
"Requires: make ",
"Requires(post): bash",
"",
"%{?fedora:Suggests: diffutils}",
Expand Down
Loading