| 1 |
#!/usr/bin/env python |
| 2 |
""" |
| 3 |
Functional tests for irclog2html.py |
| 4 |
|
| 5 |
You must run this script in a directory that contains irclog2html.py, |
| 6 |
irclog2html.pl and testcases/*.log. Both scripts must be executable. |
| 7 |
""" |
| 8 |
import difflib |
| 9 |
import glob |
| 10 |
import os |
| 11 |
import shutil |
| 12 |
import tempfile |
| 13 |
|
| 14 |
from irclog2html import RELEASE, VERSION |
| 15 |
|
| 16 |
|
| 17 |
def replace(s, replacements): |
| 18 |
"""Replace a bunch of things in a string.""" |
| 19 |
replacements = [(-len(k), k, v) for k, v in replacements.iteritems()] |
| 20 |
replacements.sort() # longest first |
| 21 |
for sortkey, k, v in replacements: |
| 22 |
s = s.replace(k, v) |
| 23 |
return s |
| 24 |
|
| 25 |
|
| 26 |
def run_in_tempdir(inputfile, script, args): |
| 27 |
"""Copy inputfile into a temporary directory and run a given command. |
| 28 |
|
| 29 |
Arguments for the script are args with the name of the copied file |
| 30 |
appended (e.g. run('/var/log/irc.log', 'irclog2html.py', '-s table') |
| 31 |
will run irclog2html.py -s table /tmp/tempdirname/irc.log) |
| 32 |
|
| 33 |
Performs no shell escaping whatsoever. |
| 34 |
|
| 35 |
Returns the contents of the output file (inputfile + '.html' in the |
| 36 |
temporary directory). |
| 37 |
""" |
| 38 |
dir = tempfile.mkdtemp() |
| 39 |
try: |
| 40 |
newinputfile = os.path.join(dir, os.path.basename(inputfile)) |
| 41 |
shutil.copyfile(inputfile, newinputfile) |
| 42 |
cmdline = '%s %s %s' % (script, args, newinputfile) |
| 43 |
pipe = os.popen('%s 2>&1' % cmdline, 'r') |
| 44 |
output = pipe.read() |
| 45 |
status = pipe.close() |
| 46 |
if status: |
| 47 |
raise AssertionError('%s returned status code %s\n%s' |
| 48 |
% (cmdline, status, output)) |
| 49 |
if output: |
| 50 |
raise AssertionError('%s said\n%s' |
| 51 |
% (cmdline, output)) |
| 52 |
outfilename = newinputfile + '.html' |
| 53 |
try: |
| 54 |
return open(outfilename).read().replace(dir, '/tmpdir') |
| 55 |
except IOError as e: |
| 56 |
raise AssertionError('%s did not create the output file\n%s' % |
| 57 |
(cmdline, e)) |
| 58 |
finally: |
| 59 |
shutil.rmtree(dir) |
| 60 |
|
| 61 |
|
| 62 |
def run_and_compare(inputfile, args=""): |
| 63 |
"""Run irclog2html.pl and irclog2html.py on inputfile and compare outputs. |
| 64 |
|
| 65 |
args specify additional command line arguments. |
| 66 |
""" |
| 67 |
output1 = run_in_tempdir(inputfile, './irclog2html.py', args) |
| 68 |
output1 = replace(output1, {'irclog2html.py': 'SCRIPT', |
| 69 |
'Marius Gedminas': 'AUTHOR', |
| 70 |
VERSION: 'VERSION', |
| 71 |
RELEASE: 'REVISION', |
| 72 |
'[email protected]': 'EMAIL', |
| 73 |
'https://mg.pov.lt/irclog2html/': 'URL', |
| 74 |
'mg.pov.lt': 'WEBSITE'}) |
| 75 |
output2 = run_in_tempdir(inputfile, './irclog2html.pl', args) |
| 76 |
output2 = replace(output2, {'irclog2html.pl': 'SCRIPT', |
| 77 |
'Jeff Waugh': 'AUTHOR', |
| 78 |
'2.1mg': 'VERSION', |
| 79 |
'27th July, 2001': 'REVISION', |
| 80 |
'[email protected]': 'EMAIL', |
| 81 |
'http://freshmeat.net/projects/irclog2html.pl/': |
| 82 |
'URL', |
| 83 |
'freshmeat.net': 'WEBSITE'}) |
| 84 |
if output1 != output2: |
| 85 |
raise AssertionError('files differ (- irclog2html.py,' |
| 86 |
' + irclog2html.pl):\n' + |
| 87 |
''.join(difflib.ndiff(output1.splitlines(True), |
| 88 |
output2.splitlines(True)))) |
| 89 |
|
| 90 |
|
| 91 |
DEFAULT_ARGS = ('-s table', '-s simplett', '-s tt', '-s simpletable', |
| 92 |
'-s table --colour-part="#deadbe"', |
| 93 |
'-s table --color-action=#cafeba') |
| 94 |
|
| 95 |
|
| 96 |
def testcase(inputfile, args_to_try=DEFAULT_ARGS): |
| 97 |
"""Run both scripts on inputfile with various arguments.""" |
| 98 |
print(inputfile), |
| 99 |
try: |
| 100 |
for args in args_to_try: |
| 101 |
print(".", end='') |
| 102 |
run_and_compare(inputfile, args) |
| 103 |
except AssertionError as e: |
| 104 |
print("FAILED") |
| 105 |
print() |
| 106 |
print(e) |
| 107 |
print() |
| 108 |
else: |
| 109 |
print("ok") |
| 110 |
|
| 111 |
|
| 112 |
def main(): |
| 113 |
os.chdir(os.path.dirname(__file__)) |
| 114 |
# the Perl script takes ages to process dircproxy-example.log; ignore it |
| 115 |
testcases = glob.glob('testcases/test*.log') |
| 116 |
print("Comparing outputs produced by the Perl version and the Python port") |
| 117 |
print("There are %d test cases" % len(testcases)) |
| 118 |
n = 0 |
| 119 |
for inputfile in testcases: |
| 120 |
n += 1 |
| 121 |
print(n, end='') |
| 122 |
testcase(inputfile) |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == '__main__': |
| 126 |
main() |