Allow Python cmdtest to be used as a library.

Added cmdtest_in_dir(path) to allow Python cmdtest to be used as part of
another Python script. Factored out most of the functionality from
main() to the common function test_files().
This commit is contained in:
Håkan Thörngren 2015-12-09 20:49:13 -08:00
parent f4754a10fd
commit f486a15f66

View File

@ -23,6 +23,7 @@
# This is a minimal Python version of "cmdtest". # This is a minimal Python version of "cmdtest".
import argparse import argparse
import contextlib
import copy import copy
import glob import glob
import hashlib import hashlib
@ -47,7 +48,7 @@ if sys.platform == 'win32':
class AssertFailed(Exception): class AssertFailed(Exception):
pass pass
ORIG_CWD = os.getcwd() ROOT_WD = os.getcwd()
class Statistics: class Statistics:
def __init__(self): def __init__(self):
@ -92,6 +93,16 @@ def mkdir_for(filepath):
if dirpath: if dirpath:
os.makedirs(dirpath, exist_ok=True) os.makedirs(dirpath, exist_ok=True)
@contextlib.contextmanager
def temp_chdir(path):
global ROOT_WD
starting_directory = os.getcwd()
try:
os.chdir(path)
ROOT_WD = os.getcwd()
yield
finally:
os.chdir(starting_directory)
def progress(*args): def progress(*args):
print("###", "-" * 50, *args) print("###", "-" * 50, *args)
@ -361,7 +372,7 @@ class TestCase:
pass pass
def prepend_path(self, dirpath): def prepend_path(self, dirpath):
os.environ['PATH'] = os.pathsep.join((os.path.join(ORIG_CWD, dirpath), os.environ['PATH'] = os.pathsep.join((os.path.join(ROOT_WD, dirpath),
os.environ['PATH'])) os.environ['PATH']))
def prepend_local_path(self, dirpath): def prepend_local_path(self, dirpath):
@ -370,7 +381,7 @@ class TestCase:
def import_file(self, src, tgt): def import_file(self, src, tgt):
mkdir_for(tgt) mkdir_for(tgt)
shutil.copy(os.path.join(ORIG_CWD, src), tgt) shutil.copy(os.path.join(ROOT_WD, src), tgt)
def create_file(self, fname, content, encoding='utf-8'): def create_file(self, fname, content, encoding='utf-8'):
mkdir_for(fname) mkdir_for(fname)
@ -608,6 +619,27 @@ class Tfile:
#---------------------------------------------------------------------- #----------------------------------------------------------------------
# Run cmdtest in given directory
def cmdtest_in_dir(path):
with temp_chdir(path):
py_files = glob.glob("CMDTEST_*.py")
return test_files(py_files)
def test_files(py_files, selected_methods = set()):
statistics = Statistics()
for py_file in py_files:
tfile = Tfile(py_file)
tmpdir = Tmpdir()
for tclass in tfile.tclasses():
statistics.classes += 1
progress(tclass.name())
for tmethod in tclass.tmethods():
if not selected_methods or tmethod.name() in selected_methods:
statistics.methods += 1
progress(tmethod.name())
tmethod.run(tmpdir, statistics)
return statistics
def parse_options(): def parse_options():
parser = argparse.ArgumentParser('cmdtest') parser = argparse.ArgumentParser('cmdtest')
parser.add_argument("-v", "--verbose", action="store_true", parser.add_argument("-v", "--verbose", action="store_true",
@ -632,18 +664,7 @@ def parse_options():
def main(): def main():
options, py_files, selected_methods = parse_options() options, py_files, selected_methods = parse_options()
statistics = Statistics() statistics = test_files(py_files, selected_methods)
for py_file in py_files:
tfile = Tfile(py_file)
tmpdir = Tmpdir()
for tclass in tfile.tclasses():
statistics.classes += 1
progress(tclass.name())
for tmethod in tclass.tmethods():
if not selected_methods or tmethod.name() in selected_methods:
statistics.methods += 1
progress(tmethod.name())
tmethod.run(tmpdir, statistics)
print() print()
print(statistics) print(statistics)
print() print()