Move main logic to main()

This avoids:
1) the main function running when sourcing the file
2) Creating many globally-scoped variables in the main logic

Also begin refactor of test output file logic

Signed-off-by: Will Beason <willbeason@gmail.com>
This commit is contained in:
Will Beason
2025-05-27 11:10:42 -05:00
parent 6d133575c7
commit 3d0bf89938
2 changed files with 128 additions and 117 deletions

View File

@@ -22,9 +22,6 @@ tracemalloc.start()
class Test_Wikipedia(unittest.TestCase):
def setUp(self):
if not os.path.exists("test_output"):
os.mkdir("test_output")
self.wiki = 'ikwiki-20180301-pages-meta-history'
self.wikiq_out_name = self.wiki + ".tsv"
self.test_output_dir = os.path.join(".", "test_output")
@@ -109,9 +106,6 @@ class Test_Wikipedia(unittest.TestCase):
class Test_Basic(unittest.TestCase):
def setUp(self):
if not os.path.exists("test_output"):
os.mkdir("test_output")
self.wiki = 'sailormoon'
self.wikiq_out_name = self.wiki + ".tsv"
self.test_output_dir = os.path.join(".", "test_output")
@@ -318,14 +312,14 @@ class Test_Regex(unittest.TestCase):
self.test_output_dir = os.path.join(".", "test_output")
self.call_output = os.path.join(self.test_output_dir, self.wikiq_out_name)
# we have two base calls, one for checking inputs and the other for checking outputs
# we have two base calls, one for checking arguments and the other for checking outputs
self.base_call = "../wikiq {0}"
self.base_call_outs = "../wikiq {0} -o {1}"
self.baseline_output_dir = "baseline_output"
# sample inputs for checking that bad inputs get terminated / test_regex_inputs
self.bad_inputs_list = [
# sample arguments for checking that bad arguments get terminated / test_regex_arguments
self.bad_arguments_list = [
# label is missing
"-RP '\\b\\d+\\b'",
# number of reg and number of labels do not match
@@ -337,33 +331,33 @@ class Test_Regex(unittest.TestCase):
"-RP '\\b\\w{3}\\b' -RPl threeletters -CP '\\b\\w{3}\\b'"
]
# sample inputs for checking the outcomes of good inputs / test_basic_regex
self.good_inputs_list = [
# sample arguments for checking the outcomes of good arguments / test_basic_regex
self.good_arguments_list = [
"-RP '\\b\\d{3}\\b' -RPl threedigits",
"-RP 'TestCase' -RP 'page' -RPl testcases -RPl page_word",
"-CP 'Chevalier' -CPl chev_com -RP 'welcome to Wikipedia' -RPl wiki_welcome -CP 'Warning' -CPl warning",
"-CP 'WP:EVADE' -CPl wp_evade"
]
self.cap_inputs_list = [
self.cap_arguments_list = [
"-RP 'Li Chevalier' -RPl li_cheval -CP '(?P<letter>\\b[a-zA-Z]{3}\\b)|(?P<number>\\b\\d+\\b)|(?P<cat>\\bcat\\b)' -CPl three",
"-CP '(?P<a>\\bTestCaseA\\b)|(?P<b>\\bTestCaseB\\b)|(?P<c>\\bTestCaseC\\b)|(?P<d>\\bTestCaseD\\b)' -CPl testcase -RP '(?P<npov>npov|NPOV)|(?P<neutral>neutral point of view)' -RPl npov"
]
def test_regex_inputs(self):
for input in self.bad_inputs_list:
def test_regex_arguments(self):
for arguments in self.bad_arguments_list:
call = self.base_call.format(self.input_file)
call = call + " --stdout " + input
call = call + " --stdout " + arguments
print(call)
with subprocess.Popen(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) as proc:
stdout, stderr = proc.communicate()
# we want to check that the bad inputs were caught and sys.exit is stopping the code
# we want to check that the bad arguments were caught and sys.exit is stopping the code
print(stderr.decode("utf-8"))
self.assertNotEqual(proc.returncode, 0)
def test_basic_regex(self):
for i, input in enumerate(self.good_inputs_list):
for i, arguments in enumerate(self.good_arguments_list):
test_filename = "basic_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))
# print(test_filename)
@@ -372,7 +366,7 @@ class Test_Regex(unittest.TestCase):
os.remove(test_file)
call = self.base_call_outs.format(self.input_file, self.test_output_dir)
call = call + " " + input
call = call + " " + arguments
print(call)
with subprocess.Popen(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) as proc:
proc.wait()
@@ -388,7 +382,7 @@ class Test_Regex(unittest.TestCase):
print(i)
def test_capturegroup_regex(self):
for i, input in enumerate(self.cap_inputs_list):
for i, arguments in enumerate(self.cap_arguments_list):
test_filename = "capturegroup_{0}_{1}.tsv".format(self.wikiq_out_name[:-4], str(i))
print(test_filename)
test_file = os.path.join(self.test_output_dir, test_filename)
@@ -396,10 +390,9 @@ class Test_Regex(unittest.TestCase):
os.remove(test_file)
call = self.base_call_outs.format(self.input_file, self.test_output_dir)
call = call + " " + input
call = call + " " + arguments
print(call)
print(call)
with subprocess.Popen(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) as proc:
proc.wait()
assert (proc.returncode == 0)
@@ -414,4 +407,14 @@ class Test_Regex(unittest.TestCase):
if __name__ == '__main__':
# Perform directory check and reset here as this is a one-time setup step as opposed to per-test setup.
if not os.path.exists("test_output"):
os.mkdir("test_output")
else:
# Avoid subsequent calls to tests interfering with each other.
# Otherwise, a test may erroneously pass if the program has no output
# but a previous run output what was expected.
for f in os.listdir("test_output"):
os.remove(os.path.join("test_output", f))
unittest.main()