From 80858ea67d85800089d7842fc66505fa83103aa0 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 16 Jun 2019 14:57:55 +0300 Subject: Change: make generated test program more convenient Also, the jt fable now works, against my locally modified version of jt (soon to be pushed to git.liw.fi). The generated program now has options --tempdir (default is something in /tmp). If --tempdir is used, it doesn't removed at the end. Each scenario gets a temporary directory created under the temporary directory. The generated program also has the options --log for specifying a log file, and --scenario to specify which scenarios to run (use once per scenario). --- fable.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 3 deletions(-) (limited to 'fable.py') diff --git a/fable.py b/fable.py index ec1d881..8e14357 100644 --- a/fable.py +++ b/fable.py @@ -1,7 +1,10 @@ # Note that the API of this module is guaranteed to change. +import argparse +import logging import os import sys +import shutil import tempfile @@ -20,30 +23,111 @@ class Context: self._vars[key] = value +class Settingses: + + def __init__(self): + self._scenarios = [] + + def add_scenario(self, name): + self._scenarios.append(name) + + def is_wanted_scenario(self, name): + return (not self._scenarios or + any(name.casefold() == w.casefold() for w in self._scenarios)) + + +class Datadir: + + def __init__(self): + self._dirname = None + self._ours = False + + def get(self): + if self._dirname is None: + self._dirname = tempfile.mkdtemp() + logging.info('Created temporary directory %s', self._dirname) + self._ours = True + return self._dirname + + def set(self, dirname): + self._dirname = os.path.abspath(dirname) + if not os.path.exists(self._dirname): + os.mkdir(self._dirname) + self._ours = False + + def cleanup(self): + if self._ours: + logging.debug('Removing %s', self._dirname) + shutil.rmtree(self._dirname) + + class Run: def __init__(self): self._name = None self._context = None self._srcdir = None - self._tmpdir = None + self._settingses = Settingses() + self._datadir = None + self._tempdir = None + self._parse_options() + + def _parse_options(self): + p = argparse.ArgumentParser() + p.add_argument('--log') + p.add_argument('--tempdir') + p.add_argument( + '--scenario', '-s', action='append', dest='scenarios', default=[]) + args = p.parse_args() + self._setup_logging(args.log) + self._tempdir = args.tempdir + for s in args.scenarios: + self._settingses.add_scenario(s) + + def _setup_logging(self, filename): + format = '%(asctime)s %(levelname)s %(message)s' + logging.basicConfig( + filename=filename, format=format, level=logging.DEBUG) + logging.info('test program starts =============') def get_context(self): return self._context def start(self, name): + if not self._settingses.is_wanted_scenario(name): + logging.info('Skipping unwanted scenario %s', name) + return False + + logging.info('Start scenario %s', name) self._context = Context() self._name = name - self._tmpdir = tempfile.mkdtemp() self._srcdir = os.getcwd() - os.chdir(self._tmpdir) + self._datadir = Datadir() + self._datadir.set(self._tempdir) + dirname = tempfile.mkdtemp(dir=self._datadir.get()) + os.chdir(dirname) + return True def end(self): + logging.info('OK %s', self._name) sys.stdout.write('OK: {}\n'.format(self._name)) os.chdir(self._srcdir) + self._datadir.cleanup() + + +def assertTrue(a): + if not a: + raise Exception( + 'expected {!r} to be true, but was disappointed'.format(a, b)) def assertEqual(a, b): if a != b: raise Exception( 'expected {!r} == {!r}, but was disappointed'.format(a, b)) + + +def assertNotEqual(a, b): + if a == b: + raise Exception( + 'expected {!r} != {!r}, but was disappointed'.format(a, b)) -- cgit v1.2.1