refactor: split unit test class to have a common base class

This commit is contained in:
Robert Alonso
2024-11-09 12:23:22 +00:00
parent d57788aa0b
commit 61cb584226

View File

@@ -9,13 +9,13 @@ from TodoParser import TodoParser
from main import process_diff from main import process_diff
class IssueUrlInsertionTest(unittest.TestCase): class IssueUrlInsertionTestBase(unittest.TestCase):
orig_cwd = None orig_cwd = None
tempdir = None tempdir = None
diff_file = None diff_file = None
parser = None parser = None
def setUp(self): def _setUp(self, diff_file):
# get current working directory # get current working directory
self.orig_cwd = os.getcwd() self.orig_cwd = os.getcwd()
@@ -24,12 +24,12 @@ class IssueUrlInsertionTest(unittest.TestCase):
# run patch against the diff file to generate the simulated filesystem # run patch against the diff file to generate the simulated filesystem
subprocess.run(['patch', '-d', self.tempdir.name, subprocess.run(['patch', '-d', self.tempdir.name,
'-i', f'{os.getcwd()}/tests/test_new.diff'], '-i', f'{os.getcwd()}/tests/{diff_file}'],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
check=True) check=True)
self.diff_file = open('tests/test_new.diff', 'r') self.diff_file = open(f'tests/{diff_file}', 'r')
self.parser = TodoParser() self.parser = TodoParser()
with open('syntax.json', 'r') as syntax_json: with open('syntax.json', 'r') as syntax_json:
self.parser.syntax_dict = json.load(syntax_json) self.parser.syntax_dict = json.load(syntax_json)
@@ -37,6 +37,19 @@ class IssueUrlInsertionTest(unittest.TestCase):
# change to the simulated filesystem directory # change to the simulated filesystem directory
os.chdir(self.tempdir.name) os.chdir(self.tempdir.name)
def _tearDown(self):
# return to original working directory to ensure we don't mess up other tests
os.chdir(self.orig_cwd)
# explicitly cleanup to avoid warning being printed about implicit cleanup
self.tempdir.cleanup()
self.tempdir = None
class IssueUrlInsertionTest(IssueUrlInsertionTestBase):
def setUp(self):
return super()._setUp('test_new.diff')
# 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
@@ -51,9 +64,4 @@ class IssueUrlInsertionTest(unittest.TestCase):
self.assertEqual(output.getvalue().count('Issue URL successfully inserted'), 80) self.assertEqual(output.getvalue().count('Issue URL successfully inserted'), 80)
def tearDown(self): def tearDown(self):
# return to original working directory to ensure we don't mess up other tests return super()._tearDown()
os.chdir(self.orig_cwd)
# explicitly cleanup to avoid warning being printed about implicit cleanup
self.tempdir.cleanup()
self.tempdir = None