test: add test to track additional bugs

Several related bugs that stem from a diff that
contains both deletions and additions. Specifically,
the line numbers aren't counted correctly, leading
to
- issue URL can't be inserted because it can't find
  the right line in the latest file
- generated issue references the wrong line number
- closed issue references the wrong line number

See GitHub issue #236

The last item might not have any actual impact as
(I think) it's just informational. But it'd still
be better if it reported the correct line number
of the deletion, which necessarily has to be
relative to the _old_ file's line number, not the
updated file's.

As there is no solution in place yet for these
bugs, the unittest is marked as an expected failure
This commit is contained in:
Robert Alonso 2024-11-11 20:55:14 +00:00
parent c643aecf02
commit f0ed571547
3 changed files with 73 additions and 0 deletions

15
tests/test_edit_py.diff Normal file
View File

@ -0,0 +1,15 @@
diff --git a/example_file.py b/example_file.py
index 6b0c6cf..b37e70a 100644
@@ -1,3 +1,9 @@
+def imaginative_string():
+ return 'figment of my imagination'
+
def hello_world():
- # TODO: Come up with a more imaginative greeting
- print('Hello world')
\ No newline at end of file
+ print(f'Hello {imaginative_string()}')
+
+ # TODO: Do more stuff
+ # This function should probably do something more interesting
+ # labels: help wanted

10
tests/test_new_py.diff Normal file
View File

@ -0,0 +1,10 @@
diff --git a/example_file.py b/example_file.py
new file mode 100644
index 0000000..525e25d
--- /dev/null
+++ b/example_file.py
@@ -0,0 +1,3 @@
+def hello_world():
+ # TODO: Come up with a more imaginative greeting
+ print('Hello world')
\ No newline at end of file

View File

@ -11,12 +11,17 @@ from main import process_diff
class IssueUrlInsertionTest(unittest.TestCase): class IssueUrlInsertionTest(unittest.TestCase):
_original_addSubTest = None
num_subtest_failures = 0
orig_cwd = None orig_cwd = None
tempdir = None tempdir = None
diff_file = None diff_file = None
parser = None parser = None
def _setUp(self, diff_files): def _setUp(self, diff_files):
# reset counter
self.num_subtest_failures = 0
# get current working directory # get current working directory
self.orig_cwd = os.getcwd() self.orig_cwd = os.getcwd()
@ -44,6 +49,8 @@ class IssueUrlInsertionTest(unittest.TestCase):
output = io.StringIO() output = io.StringIO()
# process the diffs # process the diffs
self.raw_issues = process_diff(diff=self.diff_file, insert_issue_urls=True, parser=self.parser, output=output) self.raw_issues = process_diff(diff=self.diff_file, insert_issue_urls=True, parser=self.parser, output=output)
# store the log for later processing
self.output_log = output.getvalue()
# make sure the number of issue URL comments inserted is as expected # make sure the number of issue URL comments inserted is as expected
self.assertEqual(output.getvalue().count('Issue URL successfully inserted'), self.assertEqual(output.getvalue().count('Issue URL successfully inserted'),
expected_count, expected_count,
@ -51,6 +58,19 @@ class IssueUrlInsertionTest(unittest.TestCase):
'\nProcessing log\n--------------\n'+output.getvalue() '\nProcessing log\n--------------\n'+output.getvalue()
if output_log_on_failure else None)) if output_log_on_failure else None))
def _addSubTest(self, test, subtest, outcome):
if outcome:
self.num_subtest_failures+=1
if self._original_addSubTest:
self._original_addSubTest(test, subtest, outcome)
def run(self, result=None):
if result and getattr(result, "addSubTest", None):
self._original_addSubTest = result.addSubTest
result.addSubTest = self._addSubTest
super().run(result)
# this test can take a while and, as far as TodoParser is concerned, # this test can take a while and, as far as TodoParser is concerned,
# redundant with the tests of test_todo_parser, so enable the means # redundant with the tests of test_todo_parser, so enable the means
# to skip it if desired # to skip it if desired
@ -60,6 +80,34 @@ class IssueUrlInsertionTest(unittest.TestCase):
self._setUp(['test_new.diff']) self._setUp(['test_new.diff'])
self._standardTest(80) self._standardTest(80)
# See GitHub issue #236
@unittest.expectedFailure
def test_line_numbering_with_deletions(self):
self._setUp(['test_new_py.diff', 'test_edit_py.diff'])
with self.subTest("Issue URL insertion"):
# was issue URL successfully inserted?
self._standardTest(1, False)
with self.subTest("Issue insertion line numbering"):
# make sure the log reports having inserted the issue based on the
# correct line numbering of the updated file
self.assertIn("Processing issue 1 of 2: 'Do more stuff' @ example_file.py:7",
self.output_log)
with self.subTest("Issue deletion line numbering"):
# make sure the log reports having closed the issue based on the
# correct line numbering of the old (not the updated!) file
self.assertIn("Processing issue 2 of 2: 'Come up with a more imaginative greeting' @ example_file.py:2",
self.output_log)
if self.num_subtest_failures > 0:
self.fail(
'\n'.join([
'',
'One or more subtests have failed',
'Processing log',
'--------------',
''])+
self.output_log)
# There is a known bug related to this issue, so until it's resolved # There is a known bug related to this issue, so until it's resolved
# this is an expected failure. # this is an expected failure.
# See #225 and #224 # See #225 and #224