File: test.py

package info (click to toggle)
irclog2html 4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,144 kB
  • sloc: python: 2,594; perl: 216; makefile: 59
file content (126 lines) | stat: -rwxr-xr-x 4,528 bytes parent folder | download
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
#!/usr/bin/env python
"""
Functional tests for irclog2html.py

You must run this script in a directory that contains irclog2html.py,
irclog2html.pl and testcases/*.log.  Both scripts must be executable.
"""
import difflib
import glob
import os
import shutil
import tempfile

from irclog2html import RELEASE, VERSION


def replace(s, replacements):
    """Replace a bunch of things in a string."""
    replacements = [(-len(k), k, v) for k, v in replacements.iteritems()]
    replacements.sort() # longest first
    for sortkey, k, v in replacements:
        s = s.replace(k, v)
    return s


def run_in_tempdir(inputfile, script, args):
    """Copy inputfile into a temporary directory and run a given command.

    Arguments for the script are args with the name of the copied file
    appended (e.g. run('/var/log/irc.log', 'irclog2html.py', '-s table')
    will run irclog2html.py -s table /tmp/tempdirname/irc.log)

    Performs no shell escaping whatsoever.

    Returns the contents of the output file (inputfile + '.html' in the
    temporary directory).
    """
    dir = tempfile.mkdtemp()
    try:
        newinputfile = os.path.join(dir, os.path.basename(inputfile))
        shutil.copyfile(inputfile, newinputfile)
        cmdline = '%s %s %s' % (script, args, newinputfile)
        pipe = os.popen('%s 2>&1' % cmdline, 'r')
        output = pipe.read()
        status = pipe.close()
        if status:
            raise AssertionError('%s returned status code %s\n%s'
                                 % (cmdline, status, output))
        if output:
            raise AssertionError('%s said\n%s'
                                 % (cmdline, output))
        outfilename = newinputfile + '.html'
        try:
            return open(outfilename).read().replace(dir, '/tmpdir')
        except IOError as e:
            raise AssertionError('%s did not create the output file\n%s' %
                                 (cmdline, e))
    finally:
        shutil.rmtree(dir)


def run_and_compare(inputfile, args=""):
    """Run irclog2html.pl and irclog2html.py on inputfile and compare outputs.

    args specify additional command line arguments.
    """
    output1 = run_in_tempdir(inputfile, './irclog2html.py', args)
    output1 = replace(output1, {'irclog2html.py': 'SCRIPT',
                                'Marius Gedminas': 'AUTHOR',
                                VERSION: 'VERSION',
                                RELEASE: 'REVISION',
                                'marius@pov.lt': 'EMAIL',
                                'https://mg.pov.lt/irclog2html/': 'URL',
                                'mg.pov.lt': 'WEBSITE'})
    output2 = run_in_tempdir(inputfile, './irclog2html.pl', args)
    output2 = replace(output2, {'irclog2html.pl': 'SCRIPT',
                                'Jeff Waugh': 'AUTHOR',
                                '2.1mg': 'VERSION',
                                '27th July, 2001': 'REVISION',
                                'jdub@NOSPAMperkypants.org': 'EMAIL',
                                'http://freshmeat.net/projects/irclog2html.pl/':
                                    'URL',
                                'freshmeat.net': 'WEBSITE'})
    if output1 != output2:
        raise AssertionError('files differ (- irclog2html.py,'
                             ' + irclog2html.pl):\n' +
                             ''.join(difflib.ndiff(output1.splitlines(True),
                                                   output2.splitlines(True))))


DEFAULT_ARGS = ('-s table', '-s simplett', '-s tt', '-s simpletable',
                '-s table --colour-part="#deadbe"',
                '-s table --color-action=#cafeba')


def testcase(inputfile, args_to_try=DEFAULT_ARGS):
    """Run both scripts on inputfile with various arguments."""
    print(inputfile),
    try:
        for args in args_to_try:
            print(".", end='')
            run_and_compare(inputfile, args)
    except AssertionError as e:
        print("FAILED")
        print()
        print(e)
        print()
    else:
        print("ok")


def main():
    os.chdir(os.path.dirname(__file__))
    # the Perl script takes ages to process dircproxy-example.log; ignore it
    testcases = glob.glob('testcases/test*.log')
    print("Comparing outputs produced by the Perl version and the Python port")
    print("There are %d test cases" % len(testcases))
    n = 0
    for inputfile in testcases:
        n += 1
        print(n, end='')
        testcase(inputfile)


if __name__ == '__main__':
    main()