Skip to content

Commit

Permalink
Remove ansi escape chars in assertExpectedInline and add options to s…
Browse files Browse the repository at this point in the history
…kip comments and to skip empty lines (pytorch#134248)

I had a night mare rewriting tests in test_misc.py specifically :
1. graphs can have comments that refers to my files "/lsakka/.." we really dont care about comments add option to ignore comments.
2. empty lines added when EXPECTTEST_ACCEPT=1  are changed with linter causing tests to fail or linter fail!
add flag to ignore empty lines.
3. EXPECTTEST_ACCEPT fails when the text have some not readable characters. those should not effect comparing strings, also those causes weird diffs comments when tests fails. I removed ansi_escape chars pytorch#133045

this is used in

Pull Request resolved: pytorch#134248
Approved by: https://github.com/aorenste
ghstack dependencies: pytorch#133639, pytorch#134364
  • Loading branch information
laithsakka authored and pytorchmergebot committed Aug 26, 2024
1 parent 2ec149c commit 7b6b104
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion torch/testing/_internal/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2867,7 +2867,44 @@ def assertLeaksNoCudaTensors(self, name=None):
def enforceNonDefaultStream(self):
return CudaNonDefaultStream()

def assertExpectedInline(self, actual, expect, skip=0):
def _remove_ansi_escape(self, input):
# 7-bit C1 ANSI sequences
ansi_escape = re.compile(r'''
\x1B # ESC
(?: # 7-bit C1 Fe (except CSI)
[@-Z\\-_]
| # or [ for CSI, followed by a control sequence
\[
[0-?]* # Parameter bytes
[ -/]* # Intermediate bytes
[@-~] # Final byte
)
''', re.VERBOSE)
return ansi_escape.sub('', input)

def remove_comment_lines(self, input_string):
lines = input_string.split('\n')
filtered_lines = [line for line in lines if not line.strip().startswith('#')]
return '\n'.join(filtered_lines)

def remove_empty_lines(self, input_string):
lines = input_string.split('\n')
filtered_lines = [line for line in lines if not line.strip() == '']
return '\n'.join(filtered_lines)

# ignore comments will ignore lines that starts with # after being stripped
def assertExpectedInline(self, actual, expect, skip=0, ignore_comments=False, ignore_empty_lines=False):
actual = actual if isinstance(actual, str) else str(actual)
actual = self._remove_ansi_escape(actual)
expect = self._remove_ansi_escape(expect)
if ignore_comments:
actual = self.remove_comment_lines(actual)
expect = self.remove_comment_lines(expect)

if ignore_empty_lines:
actual = self.remove_empty_lines(actual)
expect = self.remove_empty_lines(expect)

return super().assertExpectedInline(actual if isinstance(actual, str) else str(actual), expect, skip + 1)

# Munges exceptions that internally contain stack traces, using munge_exc
Expand Down

0 comments on commit 7b6b104

Please sign in to comment.