Skip to content

Commit

Permalink
Pointers on debugging tests with run_script; re-instates parentheses-…
Browse files Browse the repository at this point in the history
…-used to avoid quick reading; removes all temp files for test beforehand in a safe manner
  • Loading branch information
Tom O'Hara committed Feb 14, 2024
1 parent c991019 commit 312a3f6
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions mezcla/unittest_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# - Overriding the temporary directory can be handy during debugging; however,
# you might need to specify different ones if you invoke helper scripts. See
# tests/test_extract_company_info.py for an example.
# - It can be confusing debugging tests that use run_script, because the trace level
# is raised by default. To disable this, set the SUB_DEBUG_LEVEL as follows:
# l=5; DEBUG_LEVEL=$l SUB_DEBUG_LEVEL=$l pytest -s tests/test_spell.py
# See glue_helper.py for implementation along with related ALLOW_SUBCOMMAND_TRACING.
# TODO:
# - * Clarify TEMP_BASE vs. TEMP_FILE usage.
# - Clarify that this can co-exist with pytest-based tests (see tests/test_main.py).
Expand Down Expand Up @@ -39,7 +43,6 @@
#-------------------------------------------------------------------------------
# TODO:
# - Add method to invoke unittest.main(), so clients don't need to import unittest.
# - Clarify how ALLOW_SUBCOMMAND_TRACING affects tests that invoke external scripts.
#

"""Unit test support class"""
Expand Down Expand Up @@ -190,7 +193,7 @@ def setUpClass(cls, filename=None, module=None):
cls.class_setup = True
debug.trace_object(7, cls, "TestWrapper class")
debug.assertion(cls.script_module != TODO_MODULE)
if cls.script_module is not None:
if (cls.script_module is not None):
# Try to pull up usage via python -m mezcla.xyz --help
help_usage = gh.run("python -m '{mod}' --help", mod=cls.script_module)
debug.assertion("No module named" not in help_usage,
Expand Down Expand Up @@ -298,10 +301,13 @@ def setUp(self):
else:
default_temp_file = self.temp_base + "-test-"
default_temp_file += str(TestWrapper.test_num)

# Get new temp file and delete existing file and variants based on temp_file_count,
# such as /tmp/test-2, /tmp/test-2-1, and /tmp/test-2-2 (but not /tmp/test-[13]*).
self.temp_file = system.getenv_text("TEMP_FILE", default_temp_file)
gh.delete_existing_file(f"{self.temp_file}")
for i in range(10):
gh.delete_existing_file(f"{self.temp_file}-{i}")
for f in gh.get_matching_files(f"{self.temp_file}-[0-9]*"):
gh.delete_existing_file(f)
TestWrapper.test_num += 1

debug.trace_object(6, self, "TestWrapper instance")
Expand Down Expand Up @@ -514,4 +520,4 @@ def tearDownClass(cls):

if __name__ == '__main__':
debug.trace_current_context(level=TL.QUITE_DETAILED)
debug.trace(TL.USUAL, "Warning: not intended for command-line use\n")
debug.trace(TL.USUAL, "Warning: not intended for command-line use\n")

0 comments on commit 312a3f6

Please sign in to comment.