1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#
# txt2tags test-suite library (http://txt2tags.org)
# See also: run.py, */run.py
#
import difflib
import os
import platform
import re
import subprocess
import sys
import time
PYTHON = sys.executable
TEST_DIR = os.path.dirname(os.path.abspath(__file__))
print("Testing txt2tags on Python", platform.python_version())
# Path for txt2tags (change here if your txt2tags is in a different location)
TXT2TAGS = os.path.join(TEST_DIR, "..", "txt2tags.py")
CONFIG_FILE = "config"
CSS_FILE = "css"
DIR_OK = "ok"
DIR_ERROR = "error"
OK = FAILED = 0
ERROR_FILES = []
OVERRIDE = False
# force absolute path to avoid problems, set default options
TXT2TAGS = [os.path.abspath(TXT2TAGS), "-q", "--no-rc"]
def get_output(cmd):
return subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
#
# file tools
#
def ReadFile(filename):
with open(filename) as f:
return f.read()
def WriteFile(filename, content=""):
with open(filename, "w") as f:
f.write(content)
def MoveFile(orig, target):
if os.path.isfile(target):
os.remove(target)
os.link(orig, target)
os.remove(orig)
def initTest(name, infile, outfile, okfile=None):
if not okfile:
okfile = os.path.join(DIR_OK, outfile)
print(" %s" % name, end=" ")
if not os.path.isfile(okfile):
print("Skipping test (missing %s)" % okfile)
return False
return True
def getFileMtime(file):
ret = "-NO-MTIME-"
if os.path.isfile(file):
ret = time.strftime("%Y%m%d", time.localtime(os.path.getmtime(file)))
return ret
def getCurrentDate():
return time.strftime("%Y%m%d", time.localtime(time.time()))
def _convert(options):
cmdline = " ".join([PYTHON] + TXT2TAGS + options)
return subprocess.call(cmdline, shell=True)
def remove_version_and_dates(text):
version_re = r"\d+\.\d+(\.\d+)?"
for regex in [
r"txt2tags version {version_re}",
# Remove date from header.
r"\d{{2}}/\d{{2}}/\d{{4}}",
# lout escapes / with "/" in headers
r'\d{{2}}"/"\d{{2}}"/"\d{{4}}',
# Remove dates.
r"today is \d{{8}}",
r"which gives: \d{{2}}-\d{{2}}-\d{{4}}",
# man escapes - with \-
r"which gives: \d{{2}}\\-\d{{2}}\\-\d{{4}}",
]:
text = re.sub(regex.format(**locals()), "", text)
return text
def mark_ok(outfile):
global OK
print("OK")
OK += 1
os.remove(outfile)
def mark_failed(outfile, okfile=None):
global FAILED
print("FAILED")
FAILED += 1
if not os.path.isdir(DIR_ERROR):
os.mkdir(DIR_ERROR)
module = os.path.basename(os.getcwd())
errfile = os.path.join(TEST_DIR, module, DIR_ERROR, outfile)
MoveFile(outfile, errfile)
ERROR_FILES.append(errfile)
print("meld {okfile} {errfile}".format(**locals()))
def override(okfile, outfile):
global FAILED
print("OVERRIDE")
FAILED += 1
if os.path.exists(outfile):
MoveFile(outfile, okfile)
def _grep(okfile, outfile):
"""grep if the okfile snippet is contained in outfile"""
ok = ReadFile(okfile)
out = ReadFile(outfile)
if ok not in out:
if OVERRIDE:
override(okfile, outfile)
else:
mark_failed(outfile, okfile=okfile)
print("_grep: {okfile} contents not found in {outfile}".format(**locals()))
else:
mark_ok(outfile)
def _diff(outfile, okfile):
out = ReadFile(outfile)
out = remove_version_and_dates(out)
ok = ReadFile(okfile)
ok = remove_version_and_dates(ok)
if out != ok:
if OVERRIDE:
override(okfile, outfile)
else:
mark_failed(outfile, okfile=okfile)
for line in difflib.unified_diff(ok.splitlines(), out.splitlines()):
print(line)
else:
mark_ok(outfile)
def test(cmdline, outfile, okfile=None):
okfile = okfile or os.path.join(DIR_OK, outfile)
_convert(cmdline)
_diff(outfile, okfile)
|