Skip to content

Commit 7a4654f

Browse files
committed
tests: kdoc: fix parse failure logging
Commit 0a1066f ("tests: kdoc: fix handling file removal") added extra logging to the test intending to help debug parsing failures. Unfortunately, the newly added "<parse fail>" log line is added to every warning which doesn't end in ':', regardless of whether or not the KdocWarning class handles parsing of the line. This appears to have happened due to the confusion of the way skip and extra work. What we really want to do is convert the text into a warning object, then check if it cleanly parsed. We could check for the 'Unknown' kind. However, in the interest of being precise should new kinds ever be added, expose a new 'parsed' field of the warning object. Its set to true if we got a clean regex parse, and false otherwise. Additionally refactor the conditional checks and add comments for clarity. In particular, set extra to None unconditionally first, then perform the merging check. This should improve the readability and intended flow of this logic. Signed-off-by: Jacob Keller <[email protected]>
1 parent c6748a0 commit 7a4654f

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

tests/patch/kdoc/test.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class KdocWarning:
3131
line : Optional[int] = dataclasses.field(repr=True, compare=False)
3232
# The content of the warning (excluding kind, file, line)
3333
content : str = dataclasses.field(repr=True, compare=True)
34+
parsed : bool = dataclasses.field(repr=True, compare=True)
3435

3536
@classmethod
3637
def from_text(self, line, extra=None):
@@ -60,14 +61,12 @@ def from_text(self, line, extra=None):
6061
content = m['content']
6162
if extra:
6263
content += '\n' + extra
63-
else:
64-
kind = 'Unknown'
65-
file = None
66-
line = None
67-
content = message
6864

69-
return KdocWarning(message, kind=kind, file=file, line=line,
70-
content=content)
65+
return KdocWarning(message, kind=kind, file=file, line=line,
66+
content=content, parsed=True)
67+
68+
return KdocWarning(message, kind='Unknown', file=None, line=None,
69+
content=line, parsed=False)
7170

7271
def __str__(self):
7372
return self.message
@@ -80,20 +79,28 @@ def parse_warnings(lines, logs) -> List[KdocWarning]:
8079

8180
# Walk through lines and convert to warning objects
8281
for i, line in enumerate(lines):
82+
# Skip lines already merged into previous warning
8383
if skip:
8484
skip = False
8585
continue
8686

87+
# Ignore blank lines
88+
if not line.strip():
89+
continue
90+
91+
# Check if we should merge the next line with this warning
92+
extra = None
8793
if line.endswith(':') and i + 1 < length:
8894
extra = lines[i + 1]
8995
skip = True
90-
elif not line.strip():
91-
continue
92-
else:
93-
logs += ["<parse fail>: " + line.strip()]
94-
extra = None
9596

96-
warnings.append(KdocWarning.from_text(line, extra))
97+
# Parse line into warning object
98+
warning = KdocWarning.from_text(line, extra);
99+
100+
if not warning.parsed:
101+
logs += [f'<parse fail>: {line.strip()}']
102+
103+
warnings.append(warning)
97104

98105
return warnings
99106

0 commit comments

Comments
 (0)